public async Task <IQueryable <Organization> > EditAsync(OrganizationEditRequest organizationEditRequest, Guid userId) { bool hasRight = await Organizations .Where(org => org.Id == organizationEditRequest.Id) .SelectMany(org => org.Users) .Where(u => u.UserId == userId && u.OrganizationId == organizationEditRequest.Id) .AnyAsync(userorgright => userorgright.UserOrganizationRight.RightName == Configure.OrganizationRights.CanEditOrganizationInformation.ToString()); if (!hasRight) { throw new MethodAccessException(); } Organization orgToEdit = await Organizations .FirstOrDefaultAsync(org => org.Id == organizationEditRequest.Id); if (orgToEdit == null) { throw new ArgumentNullException(); } if (orgToEdit.Name != organizationEditRequest.Name && await Organizations .Where(org => org.Name == organizationEditRequest.Name) .AnyAsync()) { throw new ArgumentException(); } mapper.Map(organizationEditRequest, orgToEdit); dbContext.Organizations.Update(orgToEdit); await dbContext.SaveChangesAsync(); return(Organizations .Where(org => org.Id == organizationEditRequest.Id)); }
public async Task <ActionResult <OrganizationView> > EditOrganizationAsync([FromBody] OrganizationEditRequest organizationEditRequest) { var currentUserId = GetCurrentUserId(); try { var editedOrg = await organizationManager.EditAsync(organizationEditRequest, currentUserId); return(Ok(await editedOrg .ProjectTo <OrganizationView>(mapper.ConfigurationProvider) .SingleAsync())); } catch (ArgumentNullException ane) { logger.LogDebug(ane.Message + "\n" + ane.StackTrace); return(NotFound($"Can't find organization {organizationEditRequest.Id}")); } catch (ArgumentException ae) { logger.LogDebug(ae.Message + "\n" + ae.StackTrace); return(BadRequest($"Organization with name '{organizationEditRequest.Name}' already exists")); } catch (MethodAccessException mae) { logger.LogDebug(mae.Message + "\n" + mae.StackTrace); logger.LogDebug($"User {currentUserId} has no rights to edit organization {organizationEditRequest.Id}"); return(Forbid(JwtBearerDefaults.AuthenticationScheme, CookieAuthenticationDefaults.AuthenticationScheme)); } catch (Exception ex) { logger.LogDebug(ex.Message + "\n" + ex.StackTrace); return(StatusCode(500)); } }
public Organization CreateEntityFromRequest(OrganizationEditRequest request, Organization entity) { entity.UpdatedBy = request.CurrentUserId; entity.Name = request.Name; entity.Description = request.Description; return(entity); }
public async Task <OrganizationEditResponse> EditOrganization(OrganizationEditRequest request) { var response = new OrganizationEditResponse(); var currentUser = _cacheManager.GetCachedCurrentUser(request.CurrentUserId); if (!currentUser.IsAdmin) { response.SetInvalid(); return(response); } if (await _organizationRepository.Any(x => x.Id == currentUser.OrganizationId && !x.IsActive)) { response.SetInvalid(); response.ErrorMessages.Add("organization_not_found"); return(response); } var entity = _cacheManager.GetCachedOrganization(currentUser.OrganizationUid); if (entity.Id != currentUser.OrganizationId) { response.SetInvalid(); return(response); } if (await _organizationRepository.Any(x => x.Name == request.Name && x.Id != currentUser.OrganizationId)) { response.ErrorMessages.Add("organization_name_already_exist"); response.Status = ResponseStatus.Invalid; } var updatedEntity = _organizationFactory.CreateEntityFromRequest(request, entity); var result = await _organizationRepository.Update(request.CurrentUserId, updatedEntity); if (result) { _cacheManager.UpsertOrganizationCache(updatedEntity, _organizationFactory.MapCurrentOrganization(updatedEntity)); response.Item = _organizationFactory.CreateDtoFromEntity(updatedEntity); response.Status = ResponseStatus.Success; return(response); } response.SetFailed(); return(response); }
public async Task <IActionResult> Edit(OrganizationEditModel model) { if (model.IsNotValid()) { model.SetInputModelValues(); return(View(model)); } var request = new OrganizationEditRequest(CurrentUser.Id, model.OrganizationUid, model.Name, model.Description); var response = await OrganizationService.EditOrganization(request); if (response.Status.IsNotSuccess) { model.MapMessages(response); model.SetInputModelValues(); return(View(model)); } CurrentUser.IsActionSucceed = true; return(Redirect($"/Organization/Detail/{model.OrganizationUid }")); }
public static OrganizationEditRequest GetOrganizationEditRequest() { var request = new OrganizationEditRequest(CurrentUserId, UidOne, StringOne, StringOne); return(request); }