Пример #1
0
        public IEnumerable <Users> GetUsers(SubjectsExpr subjects, long?projectId)
        {
            var users = new List <Users>();

            var hasSpecificRoles = (subjects.Roles != null) && (subjects.Roles.Length != 0);

            if (subjects.IsAll || hasSpecificRoles)
            {
                var roles = _roleRepository.GetByProject(projectId);

                if (hasSpecificRoles)
                {
                    var selectedRoles = subjects.Roles.Select(x => x.RoleName).ToArray();
                    roles = roles.Where(x => selectedRoles.Any(y => y == x.Alias));
                }

                var usersFromRoles = new List <long>();

                foreach (var role in roles)
                {
                    var roleUsers = _userRoleProvider.GetUsersByRole(role).Select(_ => _.Id);

                    usersFromRoles.AddRange(roleUsers);
                }

                var distinctUsers = usersFromRoles.Distinct().ToArray();
                var actualUsers   = _userRepository.Query().Where(x => distinctUsers.Any(y => y == x.Id)).ToArray();
                if (hasSpecificRoles)
                {
                    var excludedPersons =
                        subjects.Roles.Where(x => (x.ExcludedPersons != null) && (x.ExcludedPersons.Length != 0))
                        .SelectMany(x => x.ExcludedPersons).Distinct().ToArray();
                    actualUsers = actualUsers.Where(x => excludedPersons.All(y => y != x.Login)).ToArray();
                }

                users.AddRange(actualUsers);
            }

            if ((subjects.Persons == null) || (subjects.Persons.Length == 0))
            {
                return(users.Distinct().ToArray());
            }

            var persons = _userRepository.Query().Where(x => subjects.Persons.Any(y => y == x.Login)).ToArray();

            users.AddRange(persons);

            return(users.Distinct().ToArray());
        }
Пример #2
0
        public UserDto[] Execute(GetUsersByProjectQuery dataQuery)
        {
            if (dataQuery == null)
            {
                throw new ArgumentNullException(nameof(dataQuery));
            }

            var canViewProjectUsers = _userAuthorityValidator.HasUserAuthorities(
                _userPrincipal.Info.Id,
                new[] { Authorities.UI.Project.ProjectsList.View },
                dataQuery.ProjectId);

            if (!canViewProjectUsers)
            {
                throw new UnauthorizedAccessException();
            }

            var roles = _roleProvider.Get(dataQuery.ProjectId);

            var users = new List <Users>();

            // ReSharper disable once LoopCanBePartlyConvertedToQuery
            foreach (var role in roles)
            {
                var roleUsers = _userRoleProvider.GetUsersByRole(role);

                users.AddRange(roleUsers);
            }

            var userDtos = users
                           .Select(_ => new UserDto
            {
                CurrentCulture = null,
                DisplayName    = _.DisplayName,
                Email          = _.Email,
                Id             = _.Id,
                Login          = _.Login,
                Sid            = _.Sid
            })
                           .ToArray();

            return(userDtos);
        }