mirror of
https://github.com/bitwarden/server.git
synced 2025-02-01 23:31:41 +01:00
org.UseGroups check on all group saves
This commit is contained in:
parent
a67b2b75a1
commit
71e9e82ea1
@ -16,16 +16,16 @@ namespace Bit.Api.Controllers
|
||||
public class GroupsController : Controller
|
||||
{
|
||||
private readonly IGroupRepository _groupRepository;
|
||||
private readonly IUserService _userService;
|
||||
private readonly IGroupService _groupService;
|
||||
private readonly CurrentContext _currentContext;
|
||||
|
||||
public GroupsController(
|
||||
IGroupRepository groupRepository,
|
||||
IUserService userService,
|
||||
IGroupService groupService,
|
||||
CurrentContext currentContext)
|
||||
{
|
||||
_groupRepository = groupRepository;
|
||||
_userService = userService;
|
||||
_groupService = groupService;
|
||||
_currentContext = currentContext;
|
||||
}
|
||||
|
||||
@ -77,15 +77,7 @@ namespace Bit.Api.Controllers
|
||||
}
|
||||
|
||||
var group = model.ToGroup(orgIdGuid);
|
||||
if(model.CollectionIds == null)
|
||||
{
|
||||
await _groupRepository.CreateAsync(group);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _groupRepository.CreateAsync(group, model.CollectionIds.Select(c => new Guid(c)));
|
||||
}
|
||||
|
||||
await _groupService.SaveAsync(group, model.CollectionIds?.Select(c => new Guid(c)));
|
||||
return new GroupResponseModel(group);
|
||||
}
|
||||
|
||||
@ -99,15 +91,7 @@ namespace Bit.Api.Controllers
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
if(model.CollectionIds == null)
|
||||
{
|
||||
await _groupRepository.ReplaceAsync(model.ToGroup(group));
|
||||
}
|
||||
else
|
||||
{
|
||||
await _groupRepository.ReplaceAsync(model.ToGroup(group), model.CollectionIds.Select(c => new Guid(c)));
|
||||
}
|
||||
|
||||
await _groupService.SaveAsync(model.ToGroup(group), model.CollectionIds?.Select(c => new Guid(c)));
|
||||
return new GroupResponseModel(group);
|
||||
}
|
||||
|
||||
|
12
src/Core/Services/IGroupService.cs
Normal file
12
src/Core/Services/IGroupService.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Models.Table;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
public interface IGroupService
|
||||
{
|
||||
Task SaveAsync(Group group, IEnumerable<Guid> collectionIds = null);
|
||||
}
|
||||
}
|
60
src/Core/Services/Implementations/GroupService.cs
Normal file
60
src/Core/Services/Implementations/GroupService.cs
Normal file
@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Models.Table;
|
||||
using Bit.Core.Repositories;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
public class GroupService : IGroupService
|
||||
{
|
||||
private readonly IOrganizationRepository _organizationRepository;
|
||||
private readonly IGroupRepository _groupRepository;
|
||||
|
||||
public GroupService(
|
||||
IOrganizationRepository organizationRepository,
|
||||
IGroupRepository groupRepository)
|
||||
{
|
||||
_organizationRepository = organizationRepository;
|
||||
_groupRepository = groupRepository;
|
||||
}
|
||||
|
||||
public async Task SaveAsync(Group group, IEnumerable<Guid> collectionIds = null)
|
||||
{
|
||||
var org = await _organizationRepository.GetByIdAsync(group.OrganizationId);
|
||||
if(org == null)
|
||||
{
|
||||
throw new BadRequestException("Organization not found");
|
||||
}
|
||||
|
||||
if(!org.UseGroups)
|
||||
{
|
||||
throw new BadRequestException("This organization cannot use groups.");
|
||||
}
|
||||
|
||||
if(group.Id == default(Guid))
|
||||
{
|
||||
if(collectionIds == null)
|
||||
{
|
||||
await _groupRepository.CreateAsync(group);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _groupRepository.CreateAsync(group, collectionIds);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(collectionIds == null)
|
||||
{
|
||||
await _groupRepository.ReplaceAsync(group);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _groupRepository.ReplaceAsync(group, collectionIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -45,6 +45,7 @@ namespace Bit.Core.Utilities
|
||||
services.AddSingleton<IDeviceService, DeviceService>();
|
||||
services.AddSingleton<IOrganizationService, OrganizationService>();
|
||||
services.AddSingleton<ICollectionService, CollectionService>();
|
||||
services.AddSingleton<IGroupService, GroupService>();
|
||||
}
|
||||
|
||||
public static void AddDefaultServices(this IServiceCollection services)
|
||||
|
Loading…
Reference in New Issue
Block a user