public async Task <OutResult> AddPermissionToGroup(int groupId, int permissionId)
        {
            var group = await GetById(groupId);

            if (!group.Any())
            {
                return(OutResult.Failed("Group not found"));
            }
            var permission = _permissionManager.Roles.Where(x => x.Id == permissionId);

            if (!permission.Any())
            {
                return(OutResult.Failed("Permission not found"));
            }

            var groupToAdd      = group.First();
            var permissionToAdd = permission.First();

            var groupPermission = new GroupPermission()
            {
                Group        = groupToAdd,
                Permission   = permissionToAdd,
                GroupId      = groupId,
                PermissionId = permissionId
            };

            if (groupToAdd.GroupPermissions.Contains(groupPermission))
            {
                return(OutResult.Failed("Group Already have Permission"));
            }


            var permissionResult = await _groupPermissionRepository.Insert(groupPermission);

            var groupUsers = groupToAdd.UsersInGroup.Select(x => x.User);

            foreach (var user in groupUsers)
            {
                if (!await _applicationUserManager.IsInRoleAsync(user, permissionToAdd.Name))
                {
                    await _applicationUserManager.AddToRoleAsync(user, permissionToAdd.Name);
                }
            }
            return(OutResult.Success_Updated());
        }
Beispiel #2
0
        public async Task <OutResult> RemovePermission(string name)
        {
            var permissionExists = await _permissionManager.FindByNameAsync(name);

            if (permissionExists == null)
            {
                return(OutResult.Failed("Permission not Exists"));
            }

            var groupWithPermissions = permissionExists.GroupPermissions.Select(x => x.Group);

            foreach (var group in groupWithPermissions)
            {
                await _groupService.RemovePermissionFromGroup(group.Id, permissionExists.Id);
            }

            return(OutResult.Success_Deleted());
        }
Beispiel #3
0
        public async Task <OutResult> AddPermission(string name, string description)
        {
            var permissionExists = await _permissionManager.FindByNameAsync(name);

            if (permissionExists != null)
            {
                return(OutResult.Error_AlreadyExists());
            }
            var newPermission = new AppPermission()
            {
                Name        = name,
                Description = description,
                CreatedAt   = DateTime.Now,
                UpdatedAt   = DateTime.Now
            };
            var result = await _permissionManager.CreateAsync(newPermission);

            return(result.Succeeded ? OutResult.Success_Created() : OutResult.Failed("Failed to add Permission"));
        }