private void Validate(IPersistenceUnitOfWork uow, Entities.Security.Role roleToUpdate, UpdateRoleCommand command) { if (roleToUpdate.IsSystemRole && command.IncludeFuctions) { if (!string.Equals((roleToUpdate.Name ?? "").Trim(), (command.Name ?? "").Trim(), StringComparison.OrdinalIgnoreCase)) { throw new ValidationException("A system role permissions cannot be changed."); } } }
private void Validate(IPersistenceUnitOfWork uow, Entities.Security.Role roleToDelete) { if (roleToDelete.IsSystemRole) { throw new ValidationException("A system role cannot be deleted."); } long roleId = roleToDelete.Id; bool hasUsers = uow.Context .Query <Entities.Security.User>() .Any(u => u.Roles.Any(r => r.Id == roleId)); if (hasUsers) { throw new ValidationException("Role is assigned to users and cannot be deleted."); } }
protected override void OnHandle(AddRoleCommand command) { var roleType = RoleTypeCodes.User; if (!Enum.TryParse(command.RoleType, out roleType)) { throw new ArgumentException($"{command.RoleType} role type is not in the range"); } using (var uow = UowFactory.Create()) { Validate(command, uow); var newRole = new Entities.Security.Role { Name = command.Name, Description = command.Description, IsSystemRole = false, RoleType = roleType, }; // Get system role for specified role type var systemRole = uow.Context .Query <Entities.Security.Role>() .SingleOrDefault(r => r.IsSystemRole && r.RoleType == roleType); if (systemRole == null) { throw new EntityNotFoundException($"No system role fond for RoleType: {roleType}"); } // Copy app functions from the relating system role foreach (var appFunction in systemRole.GetFunctions()) { newRole.AppFunctions.Add(appFunction); } uow.Context.Add(newRole); uow.Complete(); } }