public UpdateGroupResult Edit(int id, string title, GroupType type, string shortDescription, string description, string image, bool?hot) { var result = new UpdateGroupResult(); try { var group = _context.Groups.Include(i => i.Owner).FirstOrDefault(i => i.Id == id); var user = _userManager.GetUserAsync(_http.HttpContext.User).Result; if (!_http.HttpContext.User.Identity.IsAuthenticated) { return(new UpdateGroupResult(401, "You must be logged in to do this.")); } if (group == null) { return(new UpdateGroupResult(404, "Company you're editing is not found.")); } if (group.Owner != user && user.Role != Role.Admin) { return(new UpdateGroupResult(403, "You don't have permissions to do this.")); } if (!String.IsNullOrEmpty(title)) { group.Title = title; result.Title = "OK"; } if (type != group.Type) { group.Type = type; result.Type = "OK"; } if (!String.IsNullOrEmpty(shortDescription)) { group.ShortDescription = shortDescription; result.ShortDescription = "OK"; } if (!String.IsNullOrEmpty(description)) { group.Description = description; result.Description = "OK"; } if (!String.IsNullOrEmpty(image)) { group.Image = image; result.Image = "OK"; } if (hot != null) { if (user.Role == Role.Admin) { group.Hot = true; } else { return(new UpdateGroupResult(403, "You don't have required permissions to promote a group.")); } } _context.SaveChanges(true); return(result); } catch (Exception ex) { LogException(ex); return(new UpdateGroupResult(400, ex.Message)); } }
public async Task SyncGroupAsync(Guid groupId, Guid scimAppSettingsId) { ScimGroupSyncState?syncState = await _authDbContext .ScimGroupSyncStates .SingleOrDefaultAsync(s => s.SCIMAppSettings.Id == scimAppSettingsId && s.UserGroup.Id == groupId); List <ScimUserSyncState> userSyncStates = await _authDbContext .ScimUserSyncStates .Where(s => s.SCIMAppSettings.Id == scimAppSettingsId && s.User.Groups.Any(g => g.Id == groupId)) .ToListAsync(); List <Gatekeeper.SCIM.Client.Schema.Core20.Group.GroupMembership> groupMemberships = new List <Gatekeeper.SCIM.Client.Schema.Core20.Group.GroupMembership>(); foreach (ScimUserSyncState userSyncState in userSyncStates) { groupMemberships.Add(new Gatekeeper.SCIM.Client.Schema.Core20.Group.GroupMembership { Value = userSyncState.ServiceId, }); } UserGroup group = await _authDbContext .UserGroup .SingleAsync(u => u.Id == groupId); Gatekeeper.SCIM.Client.Schema.Core20.Group scimGroup = new Gatekeeper.SCIM.Client.Schema.Core20.Group { ExternalId = group.Id.ToString(), DisplayName = group.Name, Members = groupMemberships, }; Gatekeeper.SCIM.Client.Client scimClient = await GetScimClient(scimAppSettingsId); if (syncState == null) { CreateAction <Gatekeeper.SCIM.Client.Schema.Core20.Group> createGroupAction = new CreateAction <Gatekeeper.SCIM.Client.Schema.Core20.Group>(scimGroup); CreateResult <Gatekeeper.SCIM.Client.Schema.Core20.Group> createUserResult = await scimClient.PerformAction <CreateResult <Gatekeeper.SCIM.Client.Schema.Core20.Group> >(createGroupAction); if (createUserResult.ResultStatus == StateEnum.Success && createUserResult.Resource != null && createUserResult.Resource.Id != null ) { syncState = new ScimGroupSyncState { UserGroup = group, SCIMAppSettingsId = scimAppSettingsId, ServiceId = createUserResult.Resource.Id, }; _authDbContext.Add(syncState); await _authDbContext.SaveChangesAsync(); } else { throw new Exception("SCIM initial sync failed"); } } else { scimGroup.Id = syncState.ServiceId; UpdateGroupAction updateGroup = new UpdateGroupAction(scimGroup); UpdateGroupResult updateGroupResult = await scimClient.PerformAction <UpdateGroupResult>(updateGroup); if (updateGroupResult.ResultStatus != StateEnum.Success) { throw new Exception("SCIM update failed"); } } }