Esempio n. 1
0
        private async Task AssignUsersToRole(RoleMiniViewModel roleViewModel)
        {
            var usersInModelIds = _mapper.Map <IEnumerable <ApplicationUserViewModel>, string[]>(roleViewModel.Users);
            var usersToAdd      = await _applicationUserRepository.Get(u => u.Roles.Count(r => r.RoleId.Contains(roleViewModel.Id)) == 0 && usersInModelIds.Contains(u.Id)).ToListAsync();

            foreach (var user in usersToAdd)
            {
                var state = await _userManager.AddToRoleAsync(user.Id, roleViewModel.Name);

                if (!state.Succeeded)
                {
                    throw new SystemException(state.Errors.Aggregate(new StringBuilder(), (sb, a) => sb.AppendLine(string.Join(", ", a)), sb => sb.ToString()));
                }
            }

            var usersToRemove = await _applicationUserRepository.Get(u => u.Roles.Count(r => r.RoleId.Contains(roleViewModel.Id)) == 1 && !usersInModelIds.Contains(u.Id)).ToListAsync();

            foreach (var user in usersToRemove)
            {
                var state = await _userManager.RemoveFromRoleAsync(user.Id, roleViewModel.Name);

                if (!state.Succeeded)
                {
                    throw new SystemException(state.Errors.Aggregate(new StringBuilder(), (sb, a) => sb.AppendLine(string.Join(", ", a)), sb => sb.ToString()));
                }
            }
        }
Esempio n. 2
0
        private async Task AssignPermissionsToRoleAsync(RoleMiniViewModel roleViewModel, ApplicationRole role)
        {
            var filteredPermissions = GetFilter(roleViewModel.Permissions.ToList());

            role.Permissions = await _permissionRepository.Get(filteredPermissions).ToListAsync();

            _roleRepository.Update(role);
            await _unitOfWork.SaveAsync();
        }
Esempio n. 3
0
        public HttpResponseMessage Put([FromBody] RoleMiniViewModel roleViewModel)
        {
            if (_roleRepository.Get(r => r.Name == roleViewModel.Name && r.Id != roleViewModel.Id).Any())
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, Resources.Models.Role.Role.RoleNameExistsError));
            }

            var role = _roleRepository.Get(r => r.Id == roleViewModel.Id, includeProperties: "Permissions").FirstOrDefault();

            _mapper.Map(roleViewModel, role);

            AssignPermissionsToARole(roleViewModel, role);
            AssignUsersToRole(roleViewModel);
            _permissionsCache.Clear();

            return(Request.CreateResponse(HttpStatusCode.OK, role.Id));
        }
Esempio n. 4
0
        public HttpResponseMessage Post([FromBody] RoleMiniViewModel roleViewModel)
        {
            if (_roleRepository.Get(r => r.Name == roleViewModel.Name).Any())
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, Resources.Models.Role.Role.RoleNameExistsError));
            }

            roleViewModel.Id = Guid.NewGuid().ToString();

            var role = _mapper.Map <RoleMiniViewModel, ApplicationRole>(roleViewModel);

            role.CreatedTime    = DateTime.UtcNow;
            role.OrganizationId = GetUserAndOrganization().OrganizationId;

            RoleManager.Create(role);
            AssignPermissionsToARole(roleViewModel, role);
            AssignUsersToRole(roleViewModel);
            _permissionsCache.Clear();

            return(Request.CreateResponse(HttpStatusCode.OK, role.Id));
        }
Esempio n. 5
0
 private void AssignPermissionsToARole(RoleMiniViewModel roleViewModel, ApplicationRole role)
 {
     role.Permissions = _permissionRepository.Get(GetFilter(roleViewModel.Permissions)).ToList();
     _roleRepository.Update(role);
     _unitOfWork.Save();
 }