コード例 #1
0
        public Result Add(string roleId, AddRoleAssignmentRequest addRoleAssignment)
        {
            ValidationResult validationResult = _addRoleAssignmentValidator.Validate(addRoleAssignment);

            if (!validationResult.IsValid)
            {
                _logger.LogWarning($"Invalid AddRoleAssignmentRequest model");
                return(Result.Fail(validationResult.Errors));
            }

            if (roleId == addRoleAssignment.RoleId)
            {
                _logger.LogError($"Can not assign Role to itself. RoleId {roleId}");
                return(Result.Fail("can_not_assigne_to_itself", "Can not assign Role to itself"));
            }

            Result roleExist = ValidateRole(roleId);

            if (roleExist.Failure)
            {
                return(Result.Fail(roleExist.Errors));
            }

            Result canAssigneRoleExist = ValidateRole(addRoleAssignment.RoleId);

            if (canAssigneRoleExist.Failure)
            {
                return(Result.Fail(canAssigneRoleExist.Errors));
            }

            BaseSpecification <RoleAssignmentEntity> roleAlreadyAssignedSpecification = new BaseSpecification <RoleAssignmentEntity>();

            roleAlreadyAssignedSpecification.AddFilter(x => x.RoleId == roleId);
            roleAlreadyAssignedSpecification.AddFilter(x => x.CanAssigneRoleId == addRoleAssignment.RoleId);

            bool roleAlreadyAssigned = _groupRoleAssignmentRepository.Exist(roleAlreadyAssignedSpecification);

            if (roleAlreadyAssigned)
            {
                _logger.LogError($"Role is already assigned. RoleId {roleId}, CanAssigneRoleId {addRoleAssignment.RoleId}");
                return(Result.Fail("role_is_already_assigned", "Role is already assigned"));
            }

            //TODO: check for recursion loop if we are going to use role assignment recursion

            RoleAssignmentEntity roleAssignment = new RoleAssignmentEntity(
                roleId: roleId,
                canAssigneRoleId: addRoleAssignment.RoleId);

            bool addResult = _groupRoleAssignmentRepository.Add(roleAssignment);

            if (!addResult)
            {
                _logger.LogError($"Failed to add GroupRoleAssignment, RoleId {roleId}, CanAssignedRoleId {addRoleAssignment.RoleId}");
                return(Result.Fail("failed_to_add_role_assignment", "Failed to add RoleAssignment"));
            }

            return(Result.Ok());
        }
コード例 #2
0
        public static void Patch(this RoleAssignmentEntity source, RoleAssignmentEntity target)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            var patchInjection = new PatchInjection <RoleAssignmentEntity>(x => x.RoleId, x => x.AccountId);

            target.InjectFrom(patchInjection, source);
        }
コード例 #3
0
        private Result Remove(BaseSpecification <RoleAssignmentEntity> baseSpecification)
        {
            RoleAssignmentEntity roleAssignment = _groupRoleAssignmentRepository.SingleOrDefault(baseSpecification);

            if (roleAssignment == null)
            {
                _logger.LogError($"No RoleAssignment");
                return(Result.Fail("no_role_assignment", "No RoleAssignment"));
            }

            bool removeResult = _groupRoleAssignmentRepository.Remove(roleAssignment);

            if (!removeResult)
            {
                _logger.LogError($"Failed to remove RoleAssignemt");
                return(Result.Fail("failed_to_remove_role_assignment", "Failed to remove RoleAssignment"));
            }

            return(Result.Ok());
        }
コード例 #4
0
        public void SeedRoleAssigments(List <RoleSeedModel> roleSeedModels)
        {
            List <RoleEntity> roles = _context.Roles
                                      .Include(x => x.CanAssigne)
                                      .ThenInclude(x => x.Role)
                                      .ToList();

            List <RoleAssignmentEntity> roleAssignmentEntities = new List <RoleAssignmentEntity>();

            foreach (RoleSeedModel role in roleSeedModels)
            {
                if (!role.RoleAssignments.Any())
                {
                    continue;
                }

                RoleEntity roleEntity = roles
                                        .Where(x => x.NormalizedName == role.Name.ToUpper())
                                        .SingleOrDefault();

                if (roleEntity == null)
                {
                    _logger.LogCritical($"No role Role. Name {roleEntity.Name}");
                    throw new Exception($"No Role.");
                }

                foreach (string assigment in role.RoleAssignments)
                {
                    bool exists = roles
                                  .Where(x => x.CanAssigne.Any(c => c.CanAssigneRole.NormalizedName == assigment.ToUpper()))
                                  .Any();
                    if (exists)
                    {
                        continue;
                    }

                    RoleEntity assigneRoleEntity = roles
                                                   .Where(x => x.NormalizedName == assigment.ToUpper())
                                                   .SingleOrDefault();

                    if (assigneRoleEntity == null)
                    {
                        _logger.LogCritical($"No assign role. Name {assigneRoleEntity.Name}");
                        throw new Exception($"No assign Role.");
                    }

                    if (assigneRoleEntity.NormalizedName == roleEntity.NormalizedName)
                    {
                        _logger.LogCritical($"Role can not assign self. Role name {roleEntity.Name}");
                        throw new Exception($"Role can not assign self");
                    }

                    if (assigneRoleEntity.Type != RoleTypes.Group)
                    {
                        _logger.LogCritical($"Wrong role type for role assignment. Role name {roleEntity.Name}");
                        throw new Exception($"Wrong role type for role assignment");
                    }

                    RoleAssignmentEntity roleAssignmentEntity = new RoleAssignmentEntity(
                        roleEntity.Id,
                        assigneRoleEntity.Id);

                    roleAssignmentEntities.Add(roleAssignmentEntity);
                }
            }

            _context.RoleAssignments.AddRange(roleAssignmentEntities);
            int roleAssignmentsChanges = _context.SaveChanges();

            if (roleAssignmentsChanges != roleAssignmentEntities.Count())
            {
                _logger.LogCritical($"Failed to seed role assignments");
                throw new Exception("Failed to seed role assignments");
            }
        }