Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 3b734676

Přidáno uživatelem Milan Hotovec před téměř 4 roky(ů)

User filtration for Goal Definitions

Zobrazit rozdíly:

src/Core/Domain/Leuze.Core.Domain/Repositories/IDomainUserRepository.cs
78 78
        /// </summary>
79 79
        /// <param name="id"></param>
80 80
        /// <returns></returns>
81
        Task<List<UserShortDto>> GetAllMAUsersFromTL(Guid id);
81
        Task<List<UserShortDto>> GetAllDirectUsersForTL(Guid id);
82

  
83
        /// <summary>
84
        /// 
85
        /// </summary>
86
        /// <param name="id"></param>
87
        /// <returns></returns>
88
        Task<List<UserShortDto>> GetAllUsersForTL(Guid id);
89

  
90
        /// <summary>
91
        /// 
92
        /// </summary>
93
        /// <param name="id"></param>
94
        /// <returns></returns>
95
        Task<UserShortDto> GetDtoByIdAsync(Guid id);
82 96

  
83 97
    }
84 98
}
src/Core/Infrastructure/Leuze.Core.Infrastructure.Persistence/Repository/DomainUserRepository.cs
93 93
                .Select(o => new UserShortDto(o.UserId, o.Name.FullName, o.SeniorUserId, o.SeniorUser != null ? o.SeniorUser.Name.FullName : "", o.User.Roles.First().Id, o.User.Roles.First().Name)).ToListAsync();
94 94

  
95 95
        /// <inheritdoc/>
96
        public async Task<List<UserShortDto>> GetAllMAUsersFromTL(Guid id)
96
        public async Task<List<UserShortDto>> GetAllDirectUsersForTL(Guid id)
97 97
            => await dbSet.Include(o => o.SeniorUser).Include(o => o.User).ThenInclude(o => o.Roles)
98
                .Where(o => o.SeniorUserId.HasValue && o.SeniorUserId.Value == id && o.User.Roles.Any(a => a.NormalizedName == "MA"))
98
                .Where(o => o.SeniorUserId.HasValue && o.SeniorUserId.Value == id && o.User.Roles.Any(a => a.NormalizedName == "MA" || a.NormalizedName == "TL"))
99 99
                .Select(o => new UserShortDto(o.UserId, o.Name.FullName, o.SeniorUserId, o.SeniorUser != null ? o.SeniorUser.Name.FullName : "", o.User.Roles.First().Id, o.User.Roles.First().Name)).ToListAsync();
100

  
101
        /// <inheritdoc/>
102
        public async Task<List<UserShortDto>> GetAllUsersForTL(Guid id)
103
        {
104
            HashSet<UserShortDto> result = new();
105
            Stack<UserShortDto> todoList = new();
106

  
107
            var user = await GetDtoByIdAsync(id);
108
            if (user != null)
109
            {
110
                todoList.Push(user);
111

  
112
                var current = todoList.Pop();
113
                bool stackNotEmpty = true;
114
                while (stackNotEmpty)
115
                {
116
                    if (!result.Contains(current))
117
                    {
118
                        result.Add(current);
119
                        var tempList = await GetAllDirectUsersForTL(current.Id);
120
                        foreach (var item in tempList)
121
                        {
122
                            if (item.RoleName == "MA")
123
                            {
124
                                result.Add(item);
125
                            }
126
                            else if (item.RoleName == "TL")
127
                            {
128
                                todoList.Push(item);
129
                            }
130
                        }
131
                    }
132
                    stackNotEmpty = todoList.TryPop(out current);
133
                }
134
            }
135
            return result.ToList();
136
        }
137

  
138
        /// <inheritdoc/>
139
        public async Task<UserShortDto> GetDtoByIdAsync(Guid id)
140
        {
141
            return await dbSet.Where(o => o.UserId == id)
142
                .Include(o => o.SeniorUser).Include(o => o.User).ThenInclude(o => o.Roles)
143
                .Select(o => new UserShortDto(o.UserId, o.Name.FullName, o.SeniorUserId, o.SeniorUser != null ? o.SeniorUser.Name.FullName : "", o.User.Roles.First().Id, o.User.Roles.First().Name))
144
                .FirstOrDefaultAsync();
145
            
146
        }
100 147
    }
101 148
}
src/Core/Infrastructure/Leuze.Core.Infrastructure.Persistence/Seeds/ApplicationSeed.cs
29 29
            var role2 = CreateRole("TL", false, roleManager, permissions);
30 30
            var role3 = CreateRole("MA", true, roleManager, permissions);
31 31
            CreateUser("Testovací admin", "admin@test.cz", role1, context, userManager);
32
            CreateUser("Testovací TL", "tl@test.cz", role2, context, userManager);
33
            CreateUser("Testovací MA", "ma@test.cz", role3, context, userManager);
32
            var silentUser = CreateUser("Pan Silent", "silent@test.cz", role2, context, userManager);
33
            var uzuUser = CreateUser("Jan Hošek", "uzuyami@test.cz", role2, context, userManager, silentUser);
34
            var chloubicUser = CreateUser("Dominik Chlouba", "chloubic@test.cz", role2, context, userManager, silentUser);
35
            CreateUser("Tomáš Orlovský", "orlicek@test.cz", role3, context, userManager, uzuUser);
36
            CreateUser("Dominik Frolík", "dimitros@test.cz", role3, context, userManager, chloubicUser);
34 37
        }
35 38

  
36
        private static void CreateUser(string name, string email, ApplicationRole role, DbContext context, UserManager<ApplicationUser> userManager)
39
        private static DomainUser CreateUser(string name, string email, ApplicationRole role, DbContext context, UserManager<ApplicationUser> userManager, DomainUser? senior = null)
37 40
        {
38 41
            ApplicationUser user = new(email, email, false);
39 42
            userManager.CreateAsync(user).Wait();
40 43
            userManager.AddPasswordAsync(user, "Test123*").Wait();
41 44
            userManager.AddToRoleAsync(user, role.Name).Wait();
42 45
            user = context.Set<ApplicationUser>().Single(o => o.Id == user.Id);
43
            DomainUser domainUser = new(user, new(name), new(email), null!);
46
            DomainUser domainUser = new(user, new(name), new(email), senior);
44 47
            context.Add(domainUser);
45 48
            context.SaveChanges();
49
            return domainUser;
46 50
        }
47 51

  
48 52
        private static ApplicationRole CreateRole(string name, bool isDefault, RoleManager<ApplicationRole> roleManager, List<ApplicationPermission> permissions)
src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/GoalDefinitions/Queries/GetAllForArea.cs
23 23
        public class Handler : IBaseRequestHandler<Query, Response>
24 24
        {
25 25
            private readonly IGoalDefinitionRepository _goalDefinitionRepo;
26
            private readonly AuthenticationStateProvider _authenticationStateProvider;
27
            private readonly IDomainUserRepository _domainUserRepo;
26 28

  
27
            public Handler(IGoalDefinitionRepository goalDefinitionRepo)
29
            public Handler(IDomainUserRepository domainUserRepo, AuthenticationStateProvider authStateProvider, IGoalDefinitionRepository goalDefinitionRepo)
28 30
            {
29 31
                _goalDefinitionRepo = goalDefinitionRepo;
32
                _domainUserRepo = domainUserRepo;
33
                _authenticationStateProvider = authStateProvider;
30 34
            }
31 35

  
32 36
            public async Task<RequestResponse<Response>> Handle(Query request, CancellationToken cancellationToken)
33 37
            {
38
                var state = await _authenticationStateProvider.GetAuthenticationStateAsync();
39
                var id = Guid.Parse(state.User!.FindFirst(ClaimTypes.NameIdentifier)!.Value);
40
                var userShortDto = await _domainUserRepo.GetDtoByIdAsync(id);
41

  
42
                if(userShortDto == null)
43
                {
44
                    return RequestResponse<Response>.CreateErrorResponse($"Nelze načíst definice pro uživatele: {id}!");
45
                }
46

  
34 47
                var goalDefs = await _goalDefinitionRepo.GetAllForAreaAsync(request.areaId);
35 48

  
36 49
                if (goalDefs == null)
......
38 51
                    return RequestResponse<Response>.CreateErrorResponse($"Nelze načíst definice pro oblast: {request.areaId}!");
39 52
                }
40 53

  
54
                if(userShortDto.RoleName != "Admin")
55
                {
56
                    var allMaUsers = await _domainUserRepo.GetAllUsersForTL(id);
57

  
58
                    goalDefs = goalDefs.Where(o => allMaUsers.Exists(item => item.Id == o.OwnerId)).ToList();
59
                }
60

  
41 61
                return RequestResponse<Response>.CreateSuccessResponse(new(goalDefs));
42 62
            }
43 63
        }
src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/GoalDefinitions/Queries/GetAllUnassignedInArea.cs
38 38
            {
39 39
                var state = await _authenticationStateProvider.GetAuthenticationStateAsync();
40 40
                var id = Guid.Parse(state.User!.FindFirst(ClaimTypes.NameIdentifier)!.Value);
41
                var user = await _domainUserRepo.GetByIdAsync(id);
41
                var userShortDto = await _domainUserRepo.GetDtoByIdAsync(id);
42 42

  
43
                //var goalDefs = await _goalDefinitionRepo.GetAllForAreaAsync(request.areaId);
44
                var allUsers = await _domainUserRepo.GetAllNonAdminUsers();
45
                //allUsers = allUsers.Where(o => o.) //Remove admins
43
                if (userShortDto == null)
44
                {
45
                    return RequestResponse<Response>.CreateErrorResponse($"Nelze načíst definice pro uživatele: {id}!");
46
                }
47

  
48
                List<UserShortDto> allUsers = null;
49
                if (userShortDto.RoleName != "Admin")
50
                {
51
                    allUsers = await _domainUserRepo.GetAllUsersForTL(id);
52
                }
53
                else
54
                {
55
                    allUsers = await _domainUserRepo.GetAllNonAdminUsers();
56
                }
46 57
                var areaDefs = await _goalDefinitionRepo.GetAllForAreaAsync(request.areaId);
47 58
                var assignedList = areaDefs.Select(o => new UserShortDto(o.Owner.UserId, o.Owner.Name.FullName, o.Owner.SeniorUserId, o.Owner.SeniorUser?.Name.FullName ?? "", o.Owner.User.Roles.First().Id, o.Owner.User.Roles.First().Name)).ToList();
48 59

  
src/Modules/Goal/Infrastructure/Leuze.Modules.Goal.Infrastructure.Persistence/DependencyInjection.cs
56 56
                var appSettings = serviceScope.ServiceProvider.GetRequiredService<IOptions<AppSettings>>().Value;
57 57
                
58 58
                //TODO: This is debug delet this
59
                /*
59 60
                var user = context.Set<DomainUser>().Where(item => item.EmailAddress.Email == "ma@test.cz").FirstOrDefault();
60 61
                GlobalDefinitionArea area = new GlobalDefinitionArea(user, DateTime.Now, DateTime.Now.AddDays(10));
61 62
                context.Add(area);
62 63
                GoalDefinition gd = new GoalDefinition(user, user, area, "Test Goal definition");
63 64
                context.Add(gd);
64
                context.SaveChanges();
65
                context.SaveChanges();*/
65 66
            }
66 67
        }
67 68
    }

Také k dispozici: Unified diff