public IActionResult Edit([FromBody] EditUserGroupModel model)
        {
            if (!_currentUser.HasRole(Permission.UserAccountAdmin))
            {
                return(Forbidden());
            }

            _logger.LogInformation("Updating user group: {0}", model.Name);

            Result <string> name = ((Maybe <string>)model.Name)
                                   .ToResult("User group name is not specified")
                                   .OnSuccess(d => d.Trim())
                                   .Ensure(d => d.Length >= 5, "User group name is too short")
                                   .Ensure(d => d.Length <= 50, "User group name is too long")
                                   .Ensure(d => new Regex(@"^([a-zA-Z0-9 ])*$", RegexOptions.CultureInvariant, TimeSpan.FromSeconds(2)).IsMatch(d)
                                           , "User group contains invalid characters");

            Result <string> description = ((Maybe <string>)model.Description)
                                          .ToResult("User group description is not specified")
                                          .OnSuccess(d => d.Trim())
                                          .Ensure(d => d.Length <= 250, "User group name is too long");

            Result result = Result.Combine(name, description);

            if (result.IsFailure)
            {
                return(Error(result.Error));
            }

            Maybe <AppGroup> userGroup = _groupRepo.GetById(model.Id);

            if (userGroup.HasNoValue)
            {
                return(Error($"No matching user group found: {model.Id}"));
            }

            userGroup.Value.Update(name.Value, description.Value);

            if (!userGroup.Value.IsNameSimilarTo(name.Value)) // if there is a group name change
            {
                if (_groupRepo.HasGroup(name.Value))
                {
                    return(Error("Specified user group already exist"));
                }
            }

            model.SelectedPermissions ??= new string[] { };

            Result <List <AppPermission> > selectedPermissions = GetMatchingPermissions(_permissionRepo, model.SelectedPermissions);

            if (selectedPermissions.IsFailure)
            {
                return(Error(selectedPermissions.Error));
            }

            userGroup.Value.UpdatePermissions(selectedPermissions.Value);
            _groupRepo.Update(userGroup.Value);

            return(Ok("User group updated"));
        }
Ejemplo n.º 2
0
 public void Update(AppGroup appGroup)
 {
     if (_appGroupRepository.CheckContains(x => x.Name == appGroup.Name && x.Id != appGroup.Id))
     {
         throw new NameDuplicatedException("Tên không được trùng");
     }
     _appGroupRepository.Update(appGroup);
 }
Ejemplo n.º 3
0
 public override AppGroup Update(AppGroup appGroup)
 {
     if (_appGroupRepo.CheckContains(x => x.Name == appGroup.Name && x.Id != appGroup.Id))
     {
         throw new NameDuplicatedException("Tên không được trùng");
     }
     return(_appGroupRepo.Update(appGroup));
 }