public async Task Delete(Group model) { var group = await _authorizationDbContext.Groups .Include(g => g.GroupRoles) .Include(g => g.GroupUsers) .SingleOrDefaultAsync(g => g.GroupId == Guid.Parse(model.Id)); if (group == null) { throw new NotFoundException <Group>($"Could not find {typeof(Group).Name} entity with ID {model.Name}"); } group.IsDeleted = true; foreach (var groupRole in group.GroupRoles) { if (!groupRole.IsDeleted) { groupRole.IsDeleted = true; } } foreach (var groupUser in group.GroupUsers) { if (!groupUser.IsDeleted) { groupUser.IsDeleted = true; } } await _authorizationDbContext.SaveChangesAsync(); }
public async Task <Group> RemoveChildGroups(Group group, IEnumerable <Group> childGroups) { var childGroupList = childGroups.ToList(); var childGroupIds = childGroupList.Select(g => g.Id); var childGroupEntities = AuthorizationDbContext.ChildGroups.Where(cg => !cg.IsDeleted && cg.ParentGroupId == group.Id && childGroupIds.Contains(cg.ChildGroupId)); foreach (var childGroup in childGroupList) { var childToRemove = group.Children.FirstOrDefault(c => c.Name == childGroup.Name); if (childToRemove == null) { continue; } group.Children.Remove(childToRemove); var childToRemoveEntity = childGroupEntities.FirstOrDefault(cg => cg.ParentGroupId == group.Id && cg.ChildGroupId == childGroup.Id); if (childToRemoveEntity != null) { childToRemoveEntity.IsDeleted = true; } } await AuthorizationDbContext.SaveChangesAsync(); await EventService.RaiseEventAsync(new EntityAuditEvent <Group>(EventTypes.ChildEntityDeletedEvent, group.Id.ToString(), group)); return(group); }
public async Task Delete(Group group) { var groupEntity = await AuthorizationDbContext.Groups .Include(g => g.GroupRoles) .Include(g => g.GroupUsers) .SingleOrDefaultAsync(g => g.GroupId == Guid.Parse(group.Id)); if (groupEntity == null) { throw new NotFoundException <Group>($"Could not find {typeof(Group).Name} entity with ID {group.Name}"); } groupEntity.IsDeleted = true; foreach (var groupRole in groupEntity.GroupRoles) { if (!groupRole.IsDeleted) { groupRole.IsDeleted = true; } } foreach (var groupUser in groupEntity.GroupUsers) { if (!groupUser.IsDeleted) { groupUser.IsDeleted = true; } } await AuthorizationDbContext.SaveChangesAsync(); await EventService.RaiseEventAsync(new EntityAuditEvent <Group>(EventTypes.EntityDeletedEvent, group.Id, groupEntity.ToModel())); }
public async Task <Group> AddUsersToGroup(Group group, IEnumerable <User> usersToAdd) { var groupEntity = await _authorizationDbContext.Groups.SingleOrDefaultAsync(g => g.Name.Equals(group.Name, StringComparison.OrdinalIgnoreCase) && !g.IsDeleted); if (groupEntity == null) { throw new NotFoundException <Group>($"Could not find {typeof(Group).Name} entity with ID {group.Name}"); } foreach (var user in usersToAdd) { group.Users.Add(user); _authorizationDbContext.GroupUsers.Add(new GroupUser { GroupId = groupEntity.GroupId, IdentityProvider = user.IdentityProvider, SubjectId = user.SubjectId }); } await _authorizationDbContext.SaveChangesAsync(); return(group); }
public async Task <Group> AddUsersToGroup(Group group, IEnumerable <User> usersToAdd) { var groupEntity = await AuthorizationDbContext.Groups.SingleOrDefaultAsync(g => g.Name == group.Name && g.TenantId == group.TenantId && g.IdentityProvider == group.IdentityProvider && !g.IsDeleted); if (groupEntity == null) { throw new NotFoundException <Group>($"Could not find {typeof(Group).Name} entity with ID {group.Name}"); } var groupUsers = new List <GroupUser>(); foreach (var user in usersToAdd) { group.Users.Add(user); var groupUser = new GroupUser { GroupId = groupEntity.GroupId, IdentityProvider = user.IdentityProvider, SubjectId = user.SubjectId }; AuthorizationDbContext.GroupUsers.Add(groupUser); groupUsers.Add(groupUser); } await AuthorizationDbContext.SaveChangesAsync(); await EventService.RaiseEventAsync(new EntityAuditEvent <Group>(EventTypes.ChildEntityCreatedEvent, group.Id.ToString(), group)); return(group); }
public async Task <Group> AddRolesToGroup(Group group, IEnumerable <Role> rolesToAdd) { var groupEntity = await AuthorizationDbContext.Groups.SingleOrDefaultAsync(g => g.Name == group.Name && g.TenantId == group.TenantId && g.IdentityProvider == group.IdentityProvider && !g.IsDeleted); if (groupEntity == null) { throw new NotFoundException <Group>($"Could not find {typeof(Group).Name} entity with ID {group.Name}"); } foreach (var role in rolesToAdd) { group.Roles.Add(role); AuthorizationDbContext.GroupRoles.Add(new GroupRole { GroupId = groupEntity.GroupId, RoleId = role.Id }); } await AuthorizationDbContext.SaveChangesAsync(); await EventService.RaiseEventAsync(new EntityAuditEvent <Group>(EventTypes.ChildEntityCreatedEvent, group.Id.ToString(), group)); return(group); }
public async Task <Group> Add(Group group) { var groupNameMatches = AuthorizationDbContext.Groups .Where(g => !g.IsDeleted && g.Name == group.GroupIdentifier.GroupName).Select(e => e.ToModel()).ToList(); if (groupNameMatches.Any()) { if (group.SourceEquals(GroupConstants.CustomSource)) { throw new AlreadyExistsException <Group>( $"Could not create group name {group.Name}. A group with the same name exists as a Custom group or a Directory group."); } if (groupNameMatches.Any(g => new GroupIdentifierComparer().Equals(g.GroupIdentifier, group.GroupIdentifier))) { throw new AlreadyExistsException <Group>( $"Group {group.GroupIdentifier} already exists. Please use a different GroupName, IdentityProvider, or TenantId."); } } group.Id = Guid.NewGuid(); var groupEntity = group.ToEntity(); AuthorizationDbContext.Groups.Add(groupEntity); await AuthorizationDbContext.SaveChangesAsync(); await EventService.RaiseEventAsync(new EntityAuditEvent <Group>(EventTypes.EntityCreatedEvent, group.Id.ToString(), groupEntity.ToModel())); return(groupEntity.ToModel()); }
public async Task <Group> AddUserToGroup(Group group, User user) { var groupUser = new GroupUser { GroupId = Guid.Parse(group.Id), SubjectId = user.SubjectId, IdentityProvider = user.IdentityProvider }; _authorizationDbContext.GroupUsers.Add(groupUser); await _authorizationDbContext.SaveChangesAsync(); return(group); }
public async Task <Group> AddUserToGroup(Group group, User user) { var groupUser = new GroupUser { GroupId = Guid.Parse(group.Id), SubjectId = user.SubjectId, IdentityProvider = user.IdentityProvider }; AuthorizationDbContext.GroupUsers.Add(groupUser); await AuthorizationDbContext.SaveChangesAsync(); await EventService.RaiseEventAsync(new EntityAuditEvent <Group>(EventTypes.ChildEntityCreatedEvent, group.Id, group)); return(group); }
public async Task <Group> DeleteUserFromGroup(Group group, User user) { var userInGroup = await _authorizationDbContext.GroupUsers .SingleOrDefaultAsync(gu => gu.SubjectId.Equals(user.SubjectId, StringComparison.OrdinalIgnoreCase) && gu.IdentityProvider.Equals(user.IdentityProvider, StringComparison.OrdinalIgnoreCase) && gu.GroupId == Guid.Parse(group.Id)); if (userInGroup != null) { userInGroup.IsDeleted = true; _authorizationDbContext.GroupUsers.Update(userInGroup); await _authorizationDbContext.SaveChangesAsync(); } return(group); }
public async Task <Group> Add(Group model) { var alreadyExists = await Exists(model.Name); if (alreadyExists) { throw new AlreadyExistsException <Group>($"Group {model.Name} already exists. Please use a different GroupName."); } model.Id = Guid.NewGuid().ToString(); var groupEntity = model.ToEntity(); _authorizationDbContext.Groups.Add(groupEntity); await _authorizationDbContext.SaveChangesAsync(); return(groupEntity.ToModel()); }
public async Task Update(Group model) { var group = await _authorizationDbContext.Groups .Include(g => g.GroupRoles) .Include(g => g.GroupUsers) .SingleOrDefaultAsync(g => g.GroupId == Guid.Parse(model.Id)); if (group == null) { throw new NotFoundException <Group>($"Could not find {typeof(Group).Name} entity with ID {model.Name}"); } model.ToEntity(group); _authorizationDbContext.Groups.Update(group); await _authorizationDbContext.SaveChangesAsync(); }
public async Task <Group> DeleteRolesFromGroup(Group group, IEnumerable <Guid> roleIdsToDelete) { var groupRolesToRemove = AuthorizationDbContext.GroupRoles .Where(gr => roleIdsToDelete.Contains(gr.RoleId) && gr.GroupId == group.Id && !gr.IsDeleted).ToList(); if (groupRolesToRemove.Count == 0) { throw new NotFoundException <Role>( $"No role mappings found for group {group.Name} with the supplied role IDs"); } var missingRoleMappings = new List <Guid>(); foreach (var groupRole in groupRolesToRemove) { // remove the role from the domain model var roleToRemove = group.Roles.FirstOrDefault(r => r.Id == groupRole.RoleId); if (roleToRemove == null) { missingRoleMappings.Add(groupRole.RoleId); } group.Roles.Remove(roleToRemove); // mark the many-to-many DB entity as deleted groupRole.IsDeleted = true; AuthorizationDbContext.GroupRoles.Update(groupRole); } if (missingRoleMappings.Any()) { throw new NotFoundException <Role>( $"No role mapping(s) found for group {group.Name} with the following role IDs: {missingRoleMappings.ToString(", ")}"); } await AuthorizationDbContext.SaveChangesAsync(); await EventService.RaiseEventAsync(new EntityAuditEvent <Group>(EventTypes.ChildEntityDeletedEvent, group.Id.ToString(), group)); return(group); }
public async Task Update(Group group) { var groupEntity = await AuthorizationDbContext.Groups .Include(g => g.GroupRoles) .Include(g => g.GroupUsers) .SingleOrDefaultAsync(g => g.GroupId == group.Id); if (groupEntity == null) { throw new NotFoundException <Group>($"Could not find {typeof(Group).Name} entity with ID {group.Name}"); } group.ToEntity(groupEntity); AuthorizationDbContext.Groups.Update(groupEntity); await AuthorizationDbContext.SaveChangesAsync(); await EventService.RaiseEventAsync(new EntityAuditEvent <Group>(EventTypes.EntityUpdatedEvent, group.Id.ToString(), groupEntity.ToModel())); }
public async Task <Group> AddChildGroups(Group group, IEnumerable <Group> childGroups) { foreach (var childGroup in childGroups) { var newChildGroup = new ChildGroup { ParentGroupId = group.Id, ChildGroupId = childGroup.Id }; AuthorizationDbContext.ChildGroups.Add(newChildGroup); group.Children.Add(childGroup); } await AuthorizationDbContext.SaveChangesAsync(); await EventService.RaiseEventAsync(new EntityAuditEvent <Group>(EventTypes.ChildEntityCreatedEvent, group.Id.ToString(), group)); return(group); }
public async Task <Group> Add(Group group) { var alreadyExists = await Exists(group.Name); if (alreadyExists) { throw new AlreadyExistsException <Group>( $"Group {group.Name} already exists. Please use a different GroupName."); } group.Id = Guid.NewGuid().ToString(); var groupEntity = group.ToEntity(); AuthorizationDbContext.Groups.Add(groupEntity); await AuthorizationDbContext.SaveChangesAsync(); await EventService.RaiseEventAsync(new EntityAuditEvent <Group>(EventTypes.EntityCreatedEvent, group.Id, groupEntity.ToModel())); return(groupEntity.ToModel()); }
public async Task <Group> DeleteUserFromGroup(Group group, User user) { var groupUser = await AuthorizationDbContext.GroupUsers .SingleOrDefaultAsync(gu => gu.SubjectId == user.SubjectId && gu.IdentityProvider == user.IdentityProvider && gu.GroupId == group.Id && !gu.IsDeleted); if (groupUser == null) { return(group); } groupUser.IsDeleted = true; AuthorizationDbContext.GroupUsers.Update(groupUser); await AuthorizationDbContext.SaveChangesAsync(); await EventService.RaiseEventAsync(new EntityAuditEvent <Group>(EventTypes.ChildEntityDeletedEvent, groupUser.Id.ToString(), group)); return(group); }
public async Task <Group> AddRolesToGroup(Group group, IEnumerable <Role> rolesToAdd) { var groupEntity = await _authorizationDbContext.Groups.SingleOrDefaultAsync(g => g.Name.Equals(group.Name, StringComparison.OrdinalIgnoreCase) && !g.IsDeleted); if (groupEntity == null) { throw new NotFoundException <Group>($"Could not find {typeof(Group).Name} entity with ID {group.Name}"); } foreach (var role in rolesToAdd) { group.Roles.Add(role); _authorizationDbContext.GroupRoles.Add(new GroupRole { GroupId = groupEntity.GroupId, RoleId = role.Id }); } await _authorizationDbContext.SaveChangesAsync(); return(group); }