1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-15 18:18:12 -05:00

return collection readonly details

This commit is contained in:
Kyle Spearrin 2018-06-11 14:25:53 -04:00
parent 9cf036227e
commit 74874a1c38
10 changed files with 108 additions and 27 deletions

View File

@ -46,7 +46,7 @@ namespace Bit.Api.Controllers
} }
[HttpGet("{id}/details")] [HttpGet("{id}/details")]
public async Task<CollectionDetailsResponseModel> GetDetails(string orgId, string id) public async Task<CollectionGroupDetailsResponseModel> GetDetails(string orgId, string id)
{ {
var collectionDetails = await _collectionRepository.GetByIdWithGroupsAsync(new Guid(id)); var collectionDetails = await _collectionRepository.GetByIdWithGroupsAsync(new Guid(id));
if(collectionDetails?.Item1 == null || !_currentContext.OrganizationAdmin(collectionDetails.Item1.OrganizationId)) if(collectionDetails?.Item1 == null || !_currentContext.OrganizationAdmin(collectionDetails.Item1.OrganizationId))
@ -54,7 +54,7 @@ namespace Bit.Api.Controllers
throw new NotFoundException(); throw new NotFoundException();
} }
return new CollectionDetailsResponseModel(collectionDetails.Item1, collectionDetails.Item2); return new CollectionGroupDetailsResponseModel(collectionDetails.Item1, collectionDetails.Item2);
} }
[HttpGet("")] [HttpGet("")]
@ -72,12 +72,19 @@ namespace Bit.Api.Controllers
} }
[HttpGet("~/collections")] [HttpGet("~/collections")]
public async Task<ListResponseModel<CollectionResponseModel>> GetUser([FromQuery]bool writeOnly = false) public async Task<ListResponseModel<CollectionDetailsResponseModel>> GetUser([FromQuery]bool writeOnly = false)
{ {
var collections = await _collectionRepository.GetManyByUserIdAsync( var collections = await _collectionRepository.GetManyByUserIdAsync(
_userService.GetProperUserId(User).Value, writeOnly); _userService.GetProperUserId(User).Value);
var responses = collections.Select(c => new CollectionResponseModel(c));
return new ListResponseModel<CollectionResponseModel>(responses); // TODO: Deprecated. writeOnly flag can be removed after v1.21.0
if(writeOnly)
{
collections = collections.Where(c => !c.ReadOnly).ToList();
}
var responses = collections.Select(c => new CollectionDetailsResponseModel(c));
return new ListResponseModel<CollectionDetailsResponseModel>(responses);
} }
[HttpGet("{id}/users")] [HttpGet("{id}/users")]

View File

@ -11,6 +11,7 @@ using Bit.Core.Exceptions;
using System.Linq; using System.Linq;
using Bit.Core.Models.Table; using Bit.Core.Models.Table;
using System.Collections.Generic; using System.Collections.Generic;
using Bit.Core.Models.Data;
namespace Bit.Api.Controllers namespace Bit.Api.Controllers
{ {
@ -59,11 +60,11 @@ namespace Bit.Api.Controllers
var folders = await _folderRepository.GetManyByUserIdAsync(user.Id); var folders = await _folderRepository.GetManyByUserIdAsync(user.Id);
var ciphers = await _cipherRepository.GetManyByUserIdAsync(user.Id, hasEnabledOrgs); var ciphers = await _cipherRepository.GetManyByUserIdAsync(user.Id, hasEnabledOrgs);
IEnumerable<Collection> collections = null; IEnumerable<CollectionDetails> collections = null;
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphersGroupDict = null; IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphersGroupDict = null;
if(hasEnabledOrgs) if(hasEnabledOrgs)
{ {
collections = await _collectionRepository.GetManyByUserIdAsync(user.Id, false); collections = await _collectionRepository.GetManyByUserIdAsync(user.Id);
var collectionCiphers = await _collectionCipherRepository.GetManyByUserIdAsync(user.Id); var collectionCiphers = await _collectionCipherRepository.GetManyByUserIdAsync(user.Id);
collectionCiphersGroupDict = collectionCiphers.GroupBy(c => c.CipherId).ToDictionary(s => s.Key); collectionCiphersGroupDict = collectionCiphers.GroupBy(c => c.CipherId).ToDictionary(s => s.Key);
} }

View File

@ -28,8 +28,19 @@ namespace Bit.Core.Models.Api
public class CollectionDetailsResponseModel : CollectionResponseModel public class CollectionDetailsResponseModel : CollectionResponseModel
{ {
public CollectionDetailsResponseModel(Collection collection, IEnumerable<SelectionReadOnly> groups) public CollectionDetailsResponseModel(CollectionDetails collectionDetails)
: base(collection, "collectionDetails") : base(collectionDetails, "collectionDetails")
{
ReadOnly = collectionDetails.ReadOnly;
}
public bool ReadOnly { get; set; }
}
public class CollectionGroupDetailsResponseModel : CollectionResponseModel
{
public CollectionGroupDetailsResponseModel(Collection collection, IEnumerable<SelectionReadOnly> groups)
: base(collection, "collectionGroupDetails")
{ {
Groups = groups.Select(g => new SelectionReadOnlyResponseModel(g)); Groups = groups.Select(g => new SelectionReadOnlyResponseModel(g));
} }

View File

@ -14,7 +14,7 @@ namespace Bit.Core.Models.Api
User user, User user,
IEnumerable<OrganizationUserOrganizationDetails> organizationUserDetails, IEnumerable<OrganizationUserOrganizationDetails> organizationUserDetails,
IEnumerable<Folder> folders, IEnumerable<Folder> folders,
IEnumerable<Collection> collections, IEnumerable<CollectionDetails> collections,
IEnumerable<CipherDetails> ciphers, IEnumerable<CipherDetails> ciphers,
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphersDict) IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphersDict)
: base("sync") : base("sync")
@ -22,13 +22,14 @@ namespace Bit.Core.Models.Api
Profile = new ProfileResponseModel(user, organizationUserDetails); Profile = new ProfileResponseModel(user, organizationUserDetails);
Folders = folders.Select(f => new FolderResponseModel(f)); Folders = folders.Select(f => new FolderResponseModel(f));
Ciphers = ciphers.Select(c => new CipherDetailsResponseModel(c, globalSettings, collectionCiphersDict)); Ciphers = ciphers.Select(c => new CipherDetailsResponseModel(c, globalSettings, collectionCiphersDict));
Collections = collections?.Select(c => new CollectionResponseModel(c)) ?? new List<CollectionResponseModel>(); Collections = collections?.Select(
c => new CollectionDetailsResponseModel(c)) ?? new List<CollectionDetailsResponseModel>();
Domains = new DomainsResponseModel(user, false); Domains = new DomainsResponseModel(user, false);
} }
public ProfileResponseModel Profile { get; set; } public ProfileResponseModel Profile { get; set; }
public IEnumerable<FolderResponseModel> Folders { get; set; } public IEnumerable<FolderResponseModel> Folders { get; set; }
public IEnumerable<CollectionResponseModel> Collections { get; set; } public IEnumerable<CollectionDetailsResponseModel> Collections { get; set; }
public IEnumerable<CipherDetailsResponseModel> Ciphers { get; set; } public IEnumerable<CipherDetailsResponseModel> Ciphers { get; set; }
public DomainsResponseModel Domains { get; set; } public DomainsResponseModel Domains { get; set; }
} }

View File

@ -0,0 +1,9 @@
using Bit.Core.Models.Table;
namespace Bit.Core.Models.Data
{
public class CollectionDetails : Collection
{
public bool ReadOnly { get; set; }
}
}

View File

@ -11,7 +11,7 @@ namespace Bit.Core.Repositories
Task<int> GetCountByOrganizationIdAsync(Guid organizationId); Task<int> GetCountByOrganizationIdAsync(Guid organizationId);
Task<Tuple<Collection, ICollection<SelectionReadOnly>>> GetByIdWithGroupsAsync(Guid id); Task<Tuple<Collection, ICollection<SelectionReadOnly>>> GetByIdWithGroupsAsync(Guid id);
Task<ICollection<Collection>> GetManyByOrganizationIdAsync(Guid organizationId); Task<ICollection<Collection>> GetManyByOrganizationIdAsync(Guid organizationId);
Task<ICollection<Collection>> GetManyByUserIdAsync(Guid userId, bool writeOnly); Task<ICollection<CollectionDetails>> GetManyByUserIdAsync(Guid userId);
Task<ICollection<CollectionUserDetails>> GetManyUserDetailsByIdAsync(Guid organizationId, Guid collectionId); Task<ICollection<CollectionUserDetails>> GetManyUserDetailsByIdAsync(Guid organizationId, Guid collectionId);
Task CreateAsync(Collection obj, IEnumerable<SelectionReadOnly> groups); Task CreateAsync(Collection obj, IEnumerable<SelectionReadOnly> groups);
Task ReplaceAsync(Collection obj, IEnumerable<SelectionReadOnly> groups); Task ReplaceAsync(Collection obj, IEnumerable<SelectionReadOnly> groups);

View File

@ -64,13 +64,13 @@ namespace Bit.Core.Repositories.SqlServer
} }
} }
public async Task<ICollection<Collection>> GetManyByUserIdAsync(Guid userId, bool writeOnly) public async Task<ICollection<CollectionDetails>> GetManyByUserIdAsync(Guid userId)
{ {
using(var connection = new SqlConnection(ConnectionString)) using(var connection = new SqlConnection(ConnectionString))
{ {
var results = await connection.QueryAsync<Collection>( var results = await connection.QueryAsync<CollectionDetails>(
$"[{Schema}].[Collection_ReadByUserId]", $"[{Schema}].[Collection_ReadByUserId]",
new { UserId = userId, WriteOnly = writeOnly }, new { UserId = userId },
commandType: CommandType.StoredProcedure); commandType: CommandType.StoredProcedure);
// Return distinct Id results. // Return distinct Id results.

View File

@ -1,12 +1,20 @@
CREATE PROCEDURE [dbo].[Collection_ReadByUserId] CREATE PROCEDURE [dbo].[Collection_ReadByUserId]
@UserId UNIQUEIDENTIFIER, @UserId UNIQUEIDENTIFIER
@WriteOnly BIT
AS AS
BEGIN BEGIN
SET NOCOUNT ON SET NOCOUNT ON
SELECT SELECT
C.* C.*,
CASE
WHEN
OU.[AccessAll] = 1
OR G.[AccessAll] = 1
OR CU.[ReadOnly] = 0
OR CG.[ReadOnly] = 0
THEN 1
ELSE 0
END [ReadOnly]
FROM FROM
[dbo].[CollectionView] C [dbo].[CollectionView] C
INNER JOIN INNER JOIN
@ -31,11 +39,4 @@ BEGIN
OR G.[AccessAll] = 1 OR G.[AccessAll] = 1
OR CG.[CollectionId] IS NOT NULL OR CG.[CollectionId] IS NOT NULL
) )
AND (
@WriteOnly = 0
OR OU.[AccessAll] = 1
OR G.[AccessAll] = 1
OR CU.[ReadOnly] = 0
OR CG.[ReadOnly] = 0
)
END END

View File

@ -0,0 +1,49 @@
IF OBJECT_ID('[dbo].[Collection_ReadByUserId]') IS NOT NULL
BEGIN
DROP PROCEDURE [dbo].[Collection_ReadByUserId]
END
GO
CREATE PROCEDURE [dbo].[Collection_ReadByUserId]
@UserId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
C.*,
CASE
WHEN
OU.[AccessAll] = 1
OR G.[AccessAll] = 1
OR CU.[ReadOnly] = 0
OR CG.[ReadOnly] = 0
THEN 1
ELSE 0
END [ReadOnly]
FROM
[dbo].[CollectionView] C
INNER JOIN
[dbo].[OrganizationUser] OU ON C.[OrganizationId] = OU.[OrganizationId]
INNER JOIN
[dbo].[Organization] O ON O.[Id] = C.[OrganizationId]
LEFT JOIN
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[CollectionId] = C.[Id] AND CU.[OrganizationUserId] = [OU].[Id]
LEFT JOIN
[dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND OU.[AccessAll] = 0 AND GU.[OrganizationUserId] = OU.[Id]
LEFT JOIN
[dbo].[Group] G ON G.[Id] = GU.[GroupId]
LEFT JOIN
[dbo].[CollectionGroup] CG ON G.[AccessAll] = 0 AND CG.[CollectionId] = C.[Id] AND CG.[GroupId] = GU.[GroupId]
WHERE
OU.[UserId] = @UserId
AND OU.[Status] = 2 -- 2 = Confirmed
AND O.[Enabled] = 1
AND (
OU.[AccessAll] = 1
OR CU.[CollectionId] IS NOT NULL
OR G.[AccessAll] = 1
OR CG.[CollectionId] IS NOT NULL
)
END
GO

View File

@ -10,9 +10,11 @@
<ItemGroup> <ItemGroup>
<None Remove="DbScripts\2018-04-02_00_Org2fa.sql" /> <None Remove="DbScripts\2018-04-02_00_Org2fa.sql" />
<None Remove="DbScripts\2018-04-24_00_CipherQueryTuning.sql" /> <None Remove="DbScripts\2018-04-24_00_CipherQueryTuning.sql" />
<None Remove="DbScripts\2018-06-11_00_WebVaultUpdates.sql" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="DbScripts\2018-06-11_00_WebVaultUpdates.sql" />
<EmbeddedResource Include="DbScripts\2018-04-24_00_CipherQueryTuning.sql" /> <EmbeddedResource Include="DbScripts\2018-04-24_00_CipherQueryTuning.sql" />
<EmbeddedResource Include="DbScripts\2018-04-02_00_Org2fa.sql" /> <EmbeddedResource Include="DbScripts\2018-04-02_00_Org2fa.sql" />
<EmbeddedResource Include="DbScripts\2018-03-21_00_AdminPortal.sql" /> <EmbeddedResource Include="DbScripts\2018-03-21_00_AdminPortal.sql" />