コード例 #1
0
        public async Task <IActionResult> Get([FromQuery] UserFilteringParams filter, [FromQuery] UserSortingParameters sorting)
        {
            try
            {
                var currentUser = new UserIdentityParams
                {
                    Id        = HttpContext.User.Claims.FirstOrDefault(x => x.Type == Consts.UserIdPropertyName)?.Value,
                    Role      = HttpContext.User.Claims.FirstOrDefault(x => x.Type == Consts.RoleClaimType)?.Value,
                    CompanyId = HttpContext.User.Claims.FirstOrDefault(x => x.Type == Consts.CompanyIdClaimType)?.Value
                };
                var users = await _userService.GetAllUsersAsync(currentUser, filter, sorting);

                _logger.LogInformation("Get all users requested by user with object id {id}, users: {users}", currentUser.Id, string.Join(",", users.Result.Select(user => $"{user.GivenName ?? ""} {user.Surname ?? ""}")));
                return(Ok(users));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (AccessException)
            {
                return(new ContentResult {
                    StatusCode = 403
                });
            }
        }
コード例 #2
0
        private static List <AzureUser> GetFiltered(this List <AzureUser> users, UserFilteringParams filters)
        {
            UserFilters condition = null;

            if (!string.IsNullOrEmpty(filters.Status))
            {
                condition += (AzureUser user) => FilterUserByStatus(user, filters.Status);
            }

            if (filters.Role != null && filters.Role.Any())
            {
                condition += (AzureUser user) => filters.Role.Select(role => role.ToLower()).Contains(user.Role?.ToLower());
            }

            if (filters.State != null && filters.State.Any())
            {
                condition += (AzureUser user) => filters.State.Select(state => state.ToLower()).Contains(user.State?.ToLower());
            }

            if (filters.City != null && filters.City.Any())
            {
                condition += (AzureUser user) => filters.City.Select(city => city.ToLower()).Contains(user.City?.ToLower());
            }

            if (filters.UserName != null)
            {
                condition += (AzureUser user) => user.DisplayName.ToLower().Contains(filters.UserName.ToLower());
            }

            if (filters.UsersId != null && filters.UsersId.Any())
            {
                condition += (AzureUser user) => filters.UsersId.Contains(user.ObjectId);
            }

            if (filters.CompanyId != null && filters.CompanyId.Any())
            {
                condition += (AzureUser user) => filters.CompanyId.Contains(user.CompanyId?.ToLower());
            }

            if (filters.BranchesId != null && filters.BranchesId.Any())
            {
                condition += (AzureUser user) => user.BranchId.HasValue ? filters.BranchesId.Contains(user.BranchId.Value) : false;
            }

            if (condition != null)
            {
                return(users.Where(user => AllTrue(condition, user)).ToList());
            }
            return(users);
        }
コード例 #3
0
        public static List <AzureUser> GetByParameters(this List <AzureUser> users, UserFilteringParams filters = null, UserSortingParameters sorting = null)
        {
            if (filters != null)
            {
                users = users.GetFiltered(filters);
            }

            if (sorting != null)
            {
                users = users.GetSorted(sorting);
            }

            return(users.Skip(filters?.Offset ?? 0)
                   .Take(filters?.Limit ?? users.Count).ToList());
        }
コード例 #4
0
        private async Task <LazyLoadedResult <Profile> > GetUsers(UserFilteringParams filter = null, UserSortingParameters sorting = null)
        {
            var statuses = await _requestStatusRepository.GetAsync();

            var users = _cache.Get <IEnumerable <AzureUser> >(Consts.Cache.UsersKey)
                        .Join(statuses, user => user.StatusId, status => status.Id, (user, status) =>
            {
                user.RequestStatus = status;
                return(user);
            }).ToList();

            var searchedUsers = users.GetByParameters(filter, sorting);

            searchedUsers.ForEach(x => x.AvatarUrl = FormUrlForDownloadUserAvatar(x.ObjectId));

            return(await Task.FromResult(new LazyLoadedResult <Profile>
            {
                Result = searchedUsers?.Adapt <IEnumerable <Profile> >(),
                Limit = filter?.Limit,
                Offset = filter?.Offset,
                Total = searchedUsers?.Count
            }));
        }
コード例 #5
0
        public async Task <IActionResult> GetFilteredUsersForTrustedService(string token, [FromBody] UserFilteringParams filters)
        {
            try
            {
                if (token != Consts.Security.StaticToken)
                {
                    return(new ContentResult {
                        StatusCode = 403
                    });
                }

                var currentUser = new UserIdentityParams
                {
                    Role = Consts.Roles.OperationsAdmin
                };
                var users = await _userService.GetAllUsersAsync(currentUser, filters);

                return(Ok(users));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(new ValidationProblemDetails(new Dictionary <string, string[]>(new List <KeyValuePair <string, string[]> >
                {
                    new KeyValuePair <string, string[]>(ex.ParamName ?? "Unknown", new string[] { ex.Message })
                }))));
            }
            catch (AccessException)
            {
                return(new ContentResult {
                    StatusCode = 403
                });
            }
        }
コード例 #6
0
        /// <inheritdoc />
        public async Task <LazyLoadedResult <Profile> > GetAllUsersAsync(UserIdentityParams user, UserFilteringParams filter = null, UserSortingParameters sorting = null)
        {
            await _permissionService.CheckPermissionExpiration();

            if (_permissionService.CheckPermission(user.Role, new string[] { Consts.UsersReadPermission.ReadAll }))
            {
                return(await GetUsers(filter, sorting));
            }

            if (_permissionService.CheckPermission(user.Role, new string[] { Consts.UsersReadPermission.ReadInCompany }))
            {
                filter.CompanyId = new List <string> {
                    user.CompanyId
                };

                return(await GetUsers(filter, sorting));
            }

            if (_permissionService.CheckPermission(user.Role, new string[] { Consts.UsersReadPermission.ReadOnlyRequester }))
            {
                var salesRep = await GetUserByIdAsync(user.Id.ToString(), user);

                salesRep.AvatarUrl = FormUrlForDownloadUserAvatar(salesRep.ObjectId);

                return(new LazyLoadedResult <Profile>()
                {
                    Result = new List <Profile> {
                        salesRep.Adapt <Profile>()
                    },
                    Limit = 1,
                    Offset = 0,
                    Total = 1
                });
            }

            throw new ArgumentException("Can not check you permission");
        }