Esempio n. 1
0
        public async Task <IActionResult> Index()
        {
            var hasPermission = await _accessManager.HasPermission(User.Identity.Name, _adminSubsystemName);

            if (hasPermission)
            {
                var users = _userDataService.GetAllUsers();
                var roles = _userDataService.GetAllRoles();

                var userViewModel = users.Select(u => new UserViewModel()
                {
                    Id       = u.Id,
                    Email    = u.Email,
                    Name     = $"{u.LastName} {u.Name} {u.Patronymic}",
                    Roles    = string.Join(", ", _userDataService.GetUserRoles(u.Id).Result),
                    Accesses = string.Join(", ", _accessManager.GetAccessibleSubsystems(u.Id).Result)
                });

                var roleViewModel = roles.Select(r => new RoleViewModel()
                {
                    Name = r,
                    AccessibleSubsystems = string.Join(", ", _accessManager.GetAccessibleSubsystemsByRole(r).Result)
                });

                var adminViewModel = new AdminViewModel()
                {
                    RoleViewModels = roleViewModel,
                    UserViewModels = userViewModel
                };

                return(View(adminViewModel));
            }

            return(Forbid(CookieAuthenticationDefaults.AuthenticationScheme));
        }
        public async Task <IActionResult> Index(string userSearch = "", string roleSearch = "", string positionSearch = "")
        {
            var hasPermission = await _accessManager.HasPermission(User.Identity.Name, _adminSubsystemName);

            if (hasPermission)
            {
                var users     = _userDataService.GetAllUsers();
                var roles     = _userDataService.GetAllRoles();
                var positions = _positionService.GetAll();
                userSearch ??= "";
                roleSearch ??= "";
                positionSearch ??= "";

                var userViewModel = users
                                    .Select(u => new UserViewModel()
                {
                    Id       = u.Id,
                    Email    = u.Email,
                    Name     = $"{u.LastName} {u.Name} {u.Patronymic}",
                    Roles    = string.Join(", ", _userDataService.GetUserRoles(u.Id).Result),
                    Accesses = string.Join(", ", _accessManager.GetAccessibleSubsystems(u.Id).Result)
                })
                                    .Where(u =>
                                           u.Name.Contains(userSearch, StringComparison.OrdinalIgnoreCase) ||
                                           u.Email.Contains(userSearch, StringComparison.OrdinalIgnoreCase));

                var roleViewModel = roles
                                    .Select(r => new RoleViewModel()
                {
                    Name = r,
                    AccessibleSubsystems = string.Join(", ", _accessManager.GetAccessibleSubsystemsByRole(r).Result)
                })
                                    .Where(r => r.Name.Contains(roleSearch, StringComparison.OrdinalIgnoreCase));

                var positionViewModel = positions
                                        .Select(p => new PositionViewModel()
                {
                    Id   = p.Id,
                    Name = p.Name
                })
                                        .Where(p => p.Name.Contains(positionSearch, StringComparison.OrdinalIgnoreCase));

                var adminViewModel = new AdminViewModel()
                {
                    RoleViewModels     = roleViewModel,
                    UserViewModels     = userViewModel,
                    PositionViewModels = positionViewModel,
                    UserSearch         = userSearch,
                    PositionSearch     = positionSearch,
                    RoleSearch         = roleSearch
                };

                return(View(adminViewModel));
            }

            return(Forbid(CookieAuthenticationDefaults.AuthenticationScheme));
        }