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());
        }
Esempio n. 2
0
        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();
        }
Esempio n. 3
0
        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 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> 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());
        }