1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-26 12:55:17 +01:00

support access all for collection user details

This commit is contained in:
Kyle Spearrin 2017-05-08 11:27:21 -04:00
parent 2c604d61b2
commit 9936f69481
9 changed files with 56 additions and 25 deletions

View File

@ -16,17 +16,20 @@ namespace Bit.Api.Controllers
public class CollectionsController : Controller public class CollectionsController : Controller
{ {
private readonly ICollectionRepository _collectionRepository; private readonly ICollectionRepository _collectionRepository;
private readonly ICollectionUserRepository _collectionUserRepository;
private readonly ICollectionService _collectionService; private readonly ICollectionService _collectionService;
private readonly IUserService _userService; private readonly IUserService _userService;
private readonly CurrentContext _currentContext; private readonly CurrentContext _currentContext;
public CollectionsController( public CollectionsController(
ICollectionRepository collectionRepository, ICollectionRepository collectionRepository,
ICollectionUserRepository collectionUserRepository,
ICollectionService collectionService, ICollectionService collectionService,
IUserService userService, IUserService userService,
CurrentContext currentContext) CurrentContext currentContext)
{ {
_collectionRepository = collectionRepository; _collectionRepository = collectionRepository;
_collectionUserRepository = collectionUserRepository;
_collectionService = collectionService; _collectionService = collectionService;
_userService = userService; _userService = userService;
_currentContext = currentContext; _currentContext = currentContext;
@ -59,11 +62,12 @@ namespace Bit.Api.Controllers
} }
[HttpGet("~/collections")] [HttpGet("~/collections")]
public async Task<ListResponseModel<CollectionResponseModel>> GetUser() public async Task<ListResponseModel<CollectionUserDetailsResponseModel>> GetUser()
{ {
var collections = await _collectionRepository.GetManyByUserIdAsync(_userService.GetProperUserId(User).Value); var collections = await _collectionUserRepository.GetManyDetailsByUserIdAsync(
var responses = collections.Select(c => new CollectionResponseModel(c)); _userService.GetProperUserId(User).Value);
return new ListResponseModel<CollectionResponseModel>(responses); var responses = collections.Select(c => new CollectionUserDetailsResponseModel(c));
return new ListResponseModel<CollectionUserDetailsResponseModel>(responses);
} }
[HttpPost("")] [HttpPost("")]

View File

@ -1,12 +1,26 @@
using System; using System;
using Bit.Core.Models.Table; using Bit.Core.Models.Table;
using Bit.Core.Models.Data;
namespace Bit.Core.Models.Api namespace Bit.Core.Models.Api
{ {
public class CollectionResponseModel : ResponseModel public class CollectionResponseModel : ResponseModel
{ {
public CollectionResponseModel(Collection collection) public CollectionResponseModel(Collection collection, string obj = "collection")
: base("collection") : base(obj)
{
if(collection == null)
{
throw new ArgumentNullException(nameof(collection));
}
Id = collection.Id.ToString();
OrganizationId = collection.OrganizationId.ToString();
Name = collection.Name;
}
public CollectionResponseModel(CollectionUserCollectionDetails collection, string obj = "collection")
: base(obj)
{ {
if(collection == null) if(collection == null)
{ {
@ -22,4 +36,15 @@ namespace Bit.Core.Models.Api
public string OrganizationId { get; set; } public string OrganizationId { get; set; }
public string Name { get; set; } public string Name { get; set; }
} }
public class CollectionUserDetailsResponseModel : CollectionResponseModel
{
public CollectionUserDetailsResponseModel(CollectionUserCollectionDetails collection)
: base(collection, "collectionUserDetails")
{
ReadOnly = collection.ReadOnly;
}
public bool ReadOnly { get; set; }
}
} }

View File

@ -16,13 +16,11 @@ namespace Bit.Core.Models.Api
Id = details.Id.ToString(); Id = details.Id.ToString();
Name = details.Name; Name = details.Name;
CollectionId = details.CollectionId.ToString();
ReadOnly = details.ReadOnly; ReadOnly = details.ReadOnly;
} }
public string Id { get; set; } public string Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string CollectionId { get; set; }
public bool ReadOnly { get; set; } public bool ReadOnly { get; set; }
} }
} }

View File

@ -5,9 +5,8 @@ namespace Bit.Core.Models.Data
public class CollectionUserCollectionDetails public class CollectionUserCollectionDetails
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
public Guid OrganizationUserId { get; set; } public Guid OrganizationId { get; set; }
public string Name { get; set; } public string Name { get; set; }
public Guid CollectionId { get; set; }
public bool ReadOnly { get; set; } public bool ReadOnly { get; set; }
} }
} }

View File

@ -41,7 +41,7 @@ namespace Bit.Core.Repositories.SqlServer
{ {
var result = await connection.QueryFirstOrDefaultAsync<bool>( var result = await connection.QueryFirstOrDefaultAsync<bool>(
$"[{Schema}].[Cipher_ReadCanEditByIdUserId]", $"[{Schema}].[Cipher_ReadCanEditByIdUserId]",
new { UserId = userId, CipherId = cipherId }, new { UserId = userId, Id = cipherId },
commandType: CommandType.StoredProcedure); commandType: CommandType.StoredProcedure);
return result; return result;

View File

@ -113,7 +113,8 @@ namespace Bit.Core.Repositories.SqlServer
} }
} }
public async Task<Tuple<OrganizationUserUserDetails, ICollection<CollectionUserCollectionDetails>>> GetDetailsByIdAsync(Guid id) public async Task<Tuple<OrganizationUserUserDetails, ICollection<CollectionUserCollectionDetails>>>
GetDetailsByIdAsync(Guid id)
{ {
using(var connection = new SqlConnection(ConnectionString)) using(var connection = new SqlConnection(ConnectionString))
{ {

View File

@ -1,4 +1,4 @@
CREATE PROCEDURE [dbo].[Cipher_CanEditByIdUserId] CREATE PROCEDURE [dbo].[Cipher_ReadCanEditByIdUserId]
@Id UNIQUEIDENTIFIER, @Id UNIQUEIDENTIFIER,
@UserId UNIQUEIDENTIFIER @UserId UNIQUEIDENTIFIER
AS AS

View File

@ -5,11 +5,9 @@ BEGIN
SET NOCOUNT ON SET NOCOUNT ON
SELECT SELECT
CU.* *
FROM FROM
[dbo].[CollectionUserCollectionDetailsView] CU [dbo].[CollectionUserCollectionDetailsView]
INNER JOIN
[OrganizationUser] OU ON CU.[OrganizationUserId] = OU.[Id]
WHERE WHERE
OU.[UserId] = @UserId [UserId] = @UserId
END END

View File

@ -1,12 +1,18 @@
CREATE VIEW [dbo].[CollectionUserCollectionDetailsView] CREATE VIEW [dbo].[CollectionUserCollectionDetailsView]
AS AS
SELECT SELECT
CU.[Id], C.[Id] Id,
CU.[OrganizationUserId], C.[OrganizationId],
S.[Name], C.[Name],
S.[Id] CollectionId, OU.[UserId],
CU.[ReadOnly] OU.[Id] AS [OrganizationUserId],
CASE WHEN OU.[AccessAll] = 0 AND CU.[ReadOnly] = 1 THEN 1 ELSE 0 END [ReadOnly]
FROM FROM
[dbo].[CollectionUser] CU [dbo].[Collection] C
INNER JOIN INNER JOIN
[dbo].[Collection] S ON S.[Id] = CU.[CollectionId] [dbo].[OrganizationUser] OU ON C.[OrganizationId] = OU.[OrganizationId]
LEFT JOIN
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[CollectionId] = C.[Id] AND CU.[OrganizationUserId] = [OU].[Id]
WHERE
OU.[AccessAll] = 1
OR CU.[Id] IS NOT NULL