public async Task <IActionResult> DetachFromOrganizationRightAsync([FromBody] AttachDetachRightRequest attachDetachRightRequest) { var currentUserId = GetCurrentUserId(); try { await organizationManager.DetachFromRightAsync(attachDetachRightRequest, currentUserId); return(Ok()); } catch (ArgumentNullException ane) { logger.LogDebug(ane.Message + "\n" + ane.StackTrace); logger.LogDebug($"Can't find organization {attachDetachRightRequest.OrganizationId} or user {attachDetachRightRequest.UserId}"); return(NotFound("Can't find organization, user or right")); } catch (ArgumentException ae) { logger.LogDebug(ae.Message + "\n" + ae.StackTrace); logger.LogDebug($"Can't detach user {attachDetachRightRequest.UserId} from right {attachDetachRightRequest.Right}"); return(BadRequest("Can't detach from this right")); } catch (MethodAccessException mae) { logger.LogDebug(mae.Message + "\n" + mae.StackTrace); logger.LogDebug($"User {currentUserId} has no rights to edit rights in organization {attachDetachRightRequest.OrganizationId}"); return(Forbid(JwtBearerDefaults.AuthenticationScheme, CookieAuthenticationDefaults.AuthenticationScheme)); } catch (MemberAccessException mae) { logger.LogDebug(mae.Message + "\n" + mae.StackTrace); logger.LogDebug($"User {attachDetachRightRequest.UserId} doesn't have right {attachDetachRightRequest.Right} in organization {attachDetachRightRequest.OrganizationId}"); return(BadRequest("User doesn't have this right in organization")); } catch (Exception ex) { logger.LogDebug(ex.Message + "\n" + ex.StackTrace); return(StatusCode(500)); } }
public async Task DetachFromRightAsync(AttachDetachRightRequest attachDetachRightRequest, Guid userId) { bool exist = await dbContext.OrganizationRights .AnyAsync(or => or.RightName == attachDetachRightRequest.Right); if (!exist) { logger.LogDebug($"Right {attachDetachRightRequest.Right} doesn't exist"); throw new ArgumentNullException(); } if (attachDetachRightRequest.Right == Configure.OrganizationRights.CanDeleteOrganization.ToString()) { logger.LogDebug($"Can't detach from right {attachDetachRightRequest.Right}"); throw new ArgumentException(); } exist = await Organizations .AnyAsync(org => org.Id == attachDetachRightRequest.OrganizationId); if (!exist) { logger.LogDebug($"Organization {attachDetachRightRequest.OrganizationId} doesn't exist"); throw new ArgumentNullException(); } exist = await userManager.Users .AnyAsync(u => u.Id == attachDetachRightRequest.UserId); if (!exist) { logger.LogDebug($"Current user {userId} doesn't exist"); throw new ArgumentNullException(); } bool hasRight = await Organizations .Where(org => org.Id == attachDetachRightRequest.OrganizationId) .SelectMany(org => org.Users) .Where(u => u.UserId == userId && u.OrganizationId == attachDetachRightRequest.OrganizationId) .AnyAsync(userorgright => userorgright.UserOrganizationRight.RightName == Configure.OrganizationRights.CanEditRights.ToString()); if (!hasRight) { logger.LogDebug($"Current user {userId} doesn't have rights to edit rights in organization {attachDetachRightRequest.OrganizationId}"); throw new MethodAccessException(); } exist = await Organizations .Where(org => org.Id == attachDetachRightRequest.OrganizationId) .SelectMany(org => org.Users) .Where(u => u.UserId == attachDetachRightRequest.UserId && u.OrganizationId == attachDetachRightRequest.OrganizationId) .AnyAsync(userorgright => userorgright.UserOrganizationRight.RightName == attachDetachRightRequest.Right); if (!exist) { logger.LogDebug($"User {attachDetachRightRequest.UserId} doesn't have right {attachDetachRightRequest.Right}"); throw new MemberAccessException(); } Organization organization = await Organizations .Where(org => org.Id == attachDetachRightRequest.OrganizationId) .Include(org => org.Users) .SingleAsync(); User user = await userManager.FindByIdAsync(attachDetachRightRequest.UserId.ToString()); OrganizationRight organizationRight = await dbContext.OrganizationRights.FirstOrDefaultAsync(or => or.RightName == attachDetachRightRequest.Right); UserRightsInOrganization userRightsInOrganization = organization.Users.FirstOrDefault(userorgright => userorgright.UserId == user.Id && userorgright.OrganizationId == organization.Id && userorgright.OrganizationRightId == organizationRight.Id); organization.Users.Remove(userRightsInOrganization); dbContext.Organizations.Update(organization); await dbContext.SaveChangesAsync(); logger.LogDebug($"Current user {userId} detached user {attachDetachRightRequest.UserId} from right {attachDetachRightRequest.Right} in organizaiton {attachDetachRightRequest.OrganizationId}"); }