Esempio n. 1
0
        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();
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        public async Task <Group> DeleteRoleFromGroup(Group group, Role role)
        {
            var groupRoleToRemove = await _authorizationDbContext.GroupRoles
                                    .SingleOrDefaultAsync(gr => gr.RoleId.Equals(role.Id) &&
                                                          gr.GroupId == Guid.Parse(group.Id));

            if (groupRoleToRemove != null)
            {
                groupRoleToRemove.IsDeleted = true;
                _authorizationDbContext.GroupRoles.Update(groupRoleToRemove);
                await _authorizationDbContext.SaveChangesAsync();
            }

            return(group);
        }
Esempio n. 4
0
        public async Task <Group> AddRoleToGroup(Group group, Role role)
        {
            var groupRole = new GroupRole
            {
                GroupId = Guid.Parse(group.Id),
                RoleId  = role.Id
            };

            group.Roles.Add(role);

            _authorizationDbContext.GroupRoles.Add(groupRole);
            await _authorizationDbContext.SaveChangesAsync();

            return(group);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
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. 7
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.");
            }

            /*if (!string.IsNullOrEmpty(model.Id))
             * {
             *  throw new BadRequestException<Group>("Id is generated internally and cannot be supplied by the caller.");
             * }*/

            model.Id = Guid.NewGuid().ToString();
            var groupEntity = model.ToEntity();

            _authorizationDbContext.Groups.Add(groupEntity);
            await _authorizationDbContext.SaveChangesAsync();

            return(groupEntity.ToModel());
        }