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

Group access & sproc/model refactoring.

This commit is contained in:
Kyle Spearrin 2017-05-11 10:32:25 -04:00
parent 76cebdd886
commit f0d7dc8023
26 changed files with 238 additions and 183 deletions

View File

@ -74,12 +74,11 @@ namespace Bit.Api.Controllers
}
[HttpGet("~/collections")]
public async Task<ListResponseModel<CollectionUserDetailsResponseModel>> GetUser()
public async Task<ListResponseModel<CollectionResponseModel>> GetUser()
{
var collections = await _collectionUserRepository.GetManyDetailsByUserIdAsync(
_userService.GetProperUserId(User).Value);
var responses = collections.Select(c => new CollectionUserDetailsResponseModel(c));
return new ListResponseModel<CollectionUserDetailsResponseModel>(responses);
var collections = await _collectionRepository.GetManyByUserIdAsync(_userService.GetProperUserId(User).Value);
var responses = collections.Select(c => new CollectionResponseModel(c));
return new ListResponseModel<CollectionResponseModel>(responses);
}
[HttpPost("")]

View File

@ -45,7 +45,7 @@ namespace Bit.Api.Controllers
[HttpGet("{id}")]
public async Task<OrganizationUserDetailsResponseModel> Get(string orgId, string id)
{
var organizationUser = await _organizationUserRepository.GetDetailsByIdAsync(new Guid(id));
var organizationUser = await _organizationUserRepository.GetByIdWithCollectionsAsync(new Guid(id));
if(organizationUser == null || !_currentContext.OrganizationAdmin(organizationUser.Item1.OrganizationId))
{
throw new NotFoundException();
@ -55,7 +55,7 @@ namespace Bit.Api.Controllers
}
[HttpGet("")]
public async Task<ListResponseModel<OrganizationUserResponseModel>> Get(string orgId)
public async Task<ListResponseModel<OrganizationUserUserDetailsResponseModel>> Get(string orgId)
{
var orgGuidId = new Guid(orgId);
if(!_currentContext.OrganizationAdmin(orgGuidId))
@ -64,8 +64,8 @@ namespace Bit.Api.Controllers
}
var organizationUsers = await _organizationUserRepository.GetManyDetailsByOrganizationAsync(orgGuidId);
var responses = organizationUsers.Select(o => new OrganizationUserResponseModel(o));
return new ListResponseModel<OrganizationUserResponseModel>(responses);
var responses = organizationUsers.Select(o => new OrganizationUserUserDetailsResponseModel(o));
return new ListResponseModel<OrganizationUserUserDetailsResponseModel>(responses);
}
[HttpGet("{id}/groups")]

View File

@ -1,6 +1,5 @@
using System;
using Bit.Core.Models.Table;
using Bit.Core.Models.Data;
using System.Collections.Generic;
namespace Bit.Core.Models.Api
@ -20,19 +19,6 @@ namespace Bit.Core.Models.Api
Name = collection.Name;
}
public CollectionResponseModel(CollectionUserCollectionDetails collection, string obj = "collection")
: base(obj)
{
if(collection == null)
{
throw new ArgumentNullException(nameof(collection));
}
Id = collection.Id.ToString();
OrganizationId = collection.OrganizationId.ToString();
Name = collection.Name;
}
public string Id { get; set; }
public string OrganizationId { get; set; }
public string Name { get; set; }
@ -48,15 +34,4 @@ namespace Bit.Core.Models.Api
public IEnumerable<Guid> GroupIds { get; set; }
}
public class CollectionUserDetailsResponseModel : CollectionResponseModel
{
public CollectionUserDetailsResponseModel(CollectionUserCollectionDetails collection)
: base(collection, "collectionUserDetails")
{
ReadOnly = collection.ReadOnly;
}
public bool ReadOnly { get; set; }
}
}

View File

@ -1,26 +0,0 @@
using System;
using Bit.Core.Models.Data;
namespace Bit.Core.Models.Api
{
public class OrganizationUserCollectionResponseModel : ResponseModel
{
public OrganizationUserCollectionResponseModel(CollectionUserCollectionDetails details,
string obj = "organizationUserCollection")
: base(obj)
{
if(details == null)
{
throw new ArgumentNullException(nameof(details));
}
Id = details.Id.ToString();
Name = details.Name;
ReadOnly = details.ReadOnly;
}
public string Id { get; set; }
public string Name { get; set; }
public bool ReadOnly { get; set; }
}
}

View File

@ -3,11 +3,27 @@ using Bit.Core.Enums;
using Bit.Core.Models.Data;
using System.Collections.Generic;
using System.Linq;
using Bit.Core.Models.Table;
namespace Bit.Core.Models.Api
{
public class OrganizationUserResponseModel : ResponseModel
{
public OrganizationUserResponseModel(OrganizationUser organizationUser, string obj = "organizationUser")
: base(obj)
{
if(organizationUser == null)
{
throw new ArgumentNullException(nameof(organizationUser));
}
Id = organizationUser.Id.ToString();
UserId = organizationUser.UserId?.ToString();
Type = organizationUser.Type;
Status = organizationUser.Status;
AccessAll = organizationUser.AccessAll;
}
public OrganizationUserResponseModel(OrganizationUserUserDetails organizationUser, string obj = "organizationUser")
: base(obj)
{
@ -18,8 +34,6 @@ namespace Bit.Core.Models.Api
Id = organizationUser.Id.ToString();
UserId = organizationUser.UserId?.ToString();
Name = organizationUser.Name;
Email = organizationUser.Email;
Type = organizationUser.Type;
Status = organizationUser.Status;
AccessAll = organizationUser.AccessAll;
@ -27,8 +41,6 @@ namespace Bit.Core.Models.Api
public string Id { get; set; }
public string UserId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public OrganizationUserType Type { get; set; }
public OrganizationUserStatusType Status { get; set; }
public bool AccessAll { get; set; }
@ -36,14 +48,48 @@ namespace Bit.Core.Models.Api
public class OrganizationUserDetailsResponseModel : OrganizationUserResponseModel
{
public OrganizationUserDetailsResponseModel(OrganizationUserUserDetails organizationUser,
IEnumerable<CollectionUserCollectionDetails> collections)
public OrganizationUserDetailsResponseModel(OrganizationUser organizationUser,
IEnumerable<SelectionReadOnly> collections)
: base(organizationUser, "organizationUserDetails")
{
Collections = new ListResponseModel<OrganizationUserCollectionResponseModel>(
collections.Select(c => new OrganizationUserCollectionResponseModel(c)));
Collections = collections.Select(c => new CollectionSelection(c));
}
public ListResponseModel<OrganizationUserCollectionResponseModel> Collections { get; set; }
public IEnumerable<CollectionSelection> Collections { get; set; }
public class CollectionSelection
{
public CollectionSelection(Data.SelectionReadOnly selection)
{
if(selection == null)
{
throw new ArgumentNullException(nameof(selection));
}
Id = selection.Id.ToString();
ReadOnly = selection.ReadOnly;
}
public string Id { get; set; }
public bool ReadOnly { get; set; }
}
}
public class OrganizationUserUserDetailsResponseModel : OrganizationUserResponseModel
{
public OrganizationUserUserDetailsResponseModel(OrganizationUserUserDetails organizationUser,
string obj = "organizationUserUserDetails")
: base(organizationUser, obj)
{
if(organizationUser == null)
{
throw new ArgumentNullException(nameof(organizationUser));
}
Name = organizationUser.Name;
Email = organizationUser.Email;
}
public string Name { get; set; }
public string Email { get; set; }
}
}

View File

@ -2,11 +2,9 @@
namespace Bit.Core.Models.Data
{
public class CollectionUserCollectionDetails
public class SelectionReadOnly
{
public Guid Id { get; set; }
public Guid OrganizationId { get; set; }
public string Name { get; set; }
public bool ReadOnly { get; set; }
}
}

View File

@ -9,7 +9,6 @@ namespace Bit.Core.Repositories
public interface ICollectionUserRepository : IRepository<CollectionUser, Guid>
{
Task<ICollection<CollectionUser>> GetManyByOrganizationUserIdAsync(Guid orgUserId);
Task<ICollection<CollectionUserCollectionDetails>> GetManyDetailsByUserIdAsync(Guid userId);
Task<ICollection<CollectionUserUserDetails>> GetManyDetailsByCollectionIdAsync(Guid organizationId, Guid collectionId);
}
}

View File

@ -16,7 +16,7 @@ namespace Bit.Core.Repositories
Task<ICollection<OrganizationUser>> GetManyByOrganizationAsync(Guid organizationId, OrganizationUserType? type);
Task<OrganizationUser> GetByOrganizationAsync(Guid organizationId, string email);
Task<OrganizationUser> GetByOrganizationAsync(Guid organizationId, Guid userId);
Task<Tuple<OrganizationUserUserDetails, ICollection<CollectionUserCollectionDetails>>> GetDetailsByIdAsync(Guid id);
Task<Tuple<OrganizationUser, ICollection<SelectionReadOnly>>> GetByIdWithCollectionsAsync(Guid id);
Task<ICollection<OrganizationUserUserDetails>> GetManyDetailsByOrganizationAsync(Guid organizationId);
Task<ICollection<OrganizationUserOrganizationDetails>> GetManyDetailsByUserAsync(Guid userId,
OrganizationUserStatusType? status = null);

View File

@ -68,11 +68,15 @@ namespace Bit.Core.Repositories.SqlServer
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<Collection>(
$"[{Schema}].[{Table}_ReadByUserId]",
$"[{Schema}].[Collection_ReadByUserId]",
new { UserId = userId },
commandType: CommandType.StoredProcedure);
return results.ToList();
// Return distinct Id results.
return results
.GroupBy(c => c.Id)
.Select(c => c.First())
.ToList();
}
}

View File

@ -33,19 +33,6 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task<ICollection<CollectionUserCollectionDetails>> GetManyDetailsByUserIdAsync(Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<CollectionUserCollectionDetails>(
$"[{Schema}].[CollectionUserCollectionDetails_ReadByUserId]",
new { UserId = userId },
commandType: CommandType.StoredProcedure);
return results.ToList();
}
}
public async Task<ICollection<CollectionUserUserDetails>> GetManyDetailsByCollectionIdAsync(Guid organizationId,
Guid collectionId)
{
@ -56,7 +43,11 @@ namespace Bit.Core.Repositories.SqlServer
new { OrganizationId = organizationId, CollectionId = collectionId },
commandType: CommandType.StoredProcedure);
return results.ToList();
// Return distinct Id results. If at least one of the grouped results is not ReadOnly, that we return it.
return results
.GroupBy(c => c.Id)
.Select(g => g.OrderBy(og => og.ReadOnly).First())
.ToList();
}
}
}

View File

@ -114,19 +114,18 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task<Tuple<OrganizationUserUserDetails, ICollection<CollectionUserCollectionDetails>>>
GetDetailsByIdAsync(Guid id)
public async Task<Tuple<OrganizationUser, ICollection<SelectionReadOnly>>> GetByIdWithCollectionsAsync(Guid id)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryMultipleAsync(
"[dbo].[OrganizationUserUserDetails_ReadById]",
"[dbo].[OrganizationUser_ReadWithCollectionsById]",
new { Id = id },
commandType: CommandType.StoredProcedure);
var user = (await results.ReadAsync<OrganizationUserUserDetails>()).SingleOrDefault();
var collections = (await results.ReadAsync<CollectionUserCollectionDetails>()).ToList();
return new Tuple<OrganizationUserUserDetails, ICollection<CollectionUserCollectionDetails>>(user, collections);
var user = (await results.ReadAsync<OrganizationUser>()).SingleOrDefault();
var collections = (await results.ReadAsync<SelectionReadOnly>()).ToList();
return new Tuple<OrganizationUser, ICollection<SelectionReadOnly>>(user, collections);
}
}

View File

@ -82,7 +82,6 @@
<Build Include="dbo\Tables\OrganizationUser.sql" />
<Build Include="dbo\Views\GrantView.sql" />
<Build Include="dbo\Views\UserView.sql" />
<Build Include="dbo\Views\CollectionUserCollectionDetailsView.sql" />
<Build Include="dbo\Views\CollectionUserUserDetailsView.sql" />
<Build Include="dbo\Views\CollectionUserView.sql" />
<Build Include="dbo\Views\CollectionView.sql" />
@ -119,7 +118,6 @@
<Build Include="dbo\Stored Procedures\Collection_DeleteById.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadById.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadCountByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\Collection_Update.sql" />
<Build Include="dbo\Stored Procedures\CollectionCipher_Create.sql" />
@ -137,7 +135,7 @@
<Build Include="dbo\Stored Procedures\Cipher_DeleteById.sql" />
<Build Include="dbo\Stored Procedures\Cipher_ReadById.sql" />
<Build Include="dbo\Stored Procedures\CollectionUser_Update.sql" />
<Build Include="dbo\Stored Procedures\CollectionUserCollectionDetails_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\CollectionUserUserDetails_ReadByCollectionId.sql" />
<Build Include="dbo\Stored Procedures\Cipher_Update.sql" />
<Build Include="dbo\Stored Procedures\Device_Create.sql" />
@ -174,7 +172,7 @@
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadCountByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_Update.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUserOrganizationDetails_ReadByUserIdStatus.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUserUserDetails_ReadById.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadWithCollectionsById.sql" />
<Build Include="dbo\User Defined Types\GuidIdArray.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadCountByOrganizationOwnerUser.sql" />
<Build Include="dbo\Stored Procedures\Cipher_ReadCanEditByIdUserId.sql" />

View File

@ -8,7 +8,7 @@ BEGIN
SELECT TOP 1
C.*,
CASE
WHEN C.[UserId] IS NOT NULL OR OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0 THEN 1
WHEN C.[UserId] IS NOT NULL OR OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0 OR G.[AccessAll] = 1 OR CG.[ReadOnly] = 0 THEN 1
ELSE 0
END [Edit]
FROM
@ -21,6 +21,12 @@ BEGIN
[dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAll] = 0 AND CC.[CipherId] = C.[Id]
LEFT JOIN
[dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] AND CU.[OrganizationUserId] = OU.[Id]
LEFT JOIN
[dbo].[GroupUser] GU ON C.[UserId] IS NULL AND 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] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
WHERE
C.Id = @Id
AND (
@ -29,7 +35,12 @@ BEGIN
C.[UserId] IS NULL
AND OU.[Status] = 2 -- 2 = Confirmed
AND O.[Enabled] = 1
AND (OU.[AccessAll] = 1 OR CU.[CollectionId] IS NOT NULL)
AND (
OU.[AccessAll] = 1
OR CU.[CollectionId] IS NOT NULL
OR G.[AccessAll] = 1
OR CG.[CollectionId] IS NOT NULL
)
)
)
ORDER BY

View File

@ -8,7 +8,7 @@ BEGIN
SELECT
C.*,
CASE
WHEN C.[UserId] IS NOT NULL OR OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0 THEN 1
WHEN C.[UserId] IS NOT NULL OR OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0 OR G.[AccessAll] = 1 OR CG.[ReadOnly] = 0 THEN 1
ELSE 0
END [Edit]
FROM
@ -21,6 +21,12 @@ BEGIN
[dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAll] = 0 AND CC.[CipherId] = C.[Id]
LEFT JOIN
[dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] AND CU.[OrganizationUserId] = OU.[Id]
LEFT JOIN
[dbo].[GroupUser] GU ON C.[UserId] IS NULL AND 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] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
WHERE
C.[Type] = @Type
AND (
@ -29,7 +35,12 @@ BEGIN
C.[UserId] IS NULL
AND OU.[Status] = 2 -- 2 = Confirmed
AND O.[Enabled] = 1
AND (OU.[AccessAll] = 1 OR CU.[CollectionId] IS NOT NULL)
AND (
OU.[AccessAll] = 1
OR CU.[CollectionId] IS NOT NULL
OR G.[AccessAll] = 1
OR CG.[CollectionId] IS NOT NULL
)
)
)
END

View File

@ -7,7 +7,7 @@ BEGIN
SELECT
C.*,
CASE
WHEN C.[UserId] IS NOT NULL OR OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0 THEN 1
WHEN C.[UserId] IS NOT NULL OR OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0 OR G.[AccessAll] = 1 OR CG.[ReadOnly] = 0 THEN 1
ELSE 0
END [Edit]
FROM
@ -20,12 +20,23 @@ BEGIN
[dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAll] = 0 AND CC.[CipherId] = C.[Id]
LEFT JOIN
[dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] AND CU.[OrganizationUserId] = OU.[Id]
LEFT JOIN
[dbo].[GroupUser] GU ON C.[UserId] IS NULL AND 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] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
WHERE
C.[UserId] = @UserId
OR (
C.[UserId] IS NULL
AND OU.[Status] = 2 -- 2 = Confirmed
AND O.[Enabled] = 1
AND (OU.[AccessAll] = 1 OR CU.[CollectionId] IS NOT NULL)
AND (
OU.[AccessAll] = 1
OR CU.[CollectionId] IS NOT NULL
OR G.[AccessAll] = 1
OR CG.[CollectionId] IS NOT NULL
)
)
END

View File

@ -7,7 +7,7 @@ BEGIN
SELECT
C.*,
CASE
WHEN OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0 THEN 1
WHEN C.[UserId] IS NOT NULL OR OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0 OR G.[AccessAll] = 1 OR CG.[ReadOnly] = 0 THEN 1
ELSE 0
END [Edit]
FROM
@ -20,8 +20,19 @@ BEGIN
[dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAll] = 0 AND CC.[CipherId] = C.[Id]
LEFT JOIN
[dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] AND CU.[OrganizationUserId] = OU.[Id]
LEFT JOIN
[dbo].[GroupUser] GU ON C.[UserId] IS NULL AND 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] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
WHERE
OU.[Status] = 2 -- 2 = Confirmed
AND O.[Enabled] = 1
AND (OU.[AccessAll] = 1 OR CU.[CollectionId] IS NOT NULL)
AND (
OU.[AccessAll] = 1
OR CU.[CollectionId] IS NOT NULL
OR G.[AccessAll] = 1
OR CG.[CollectionId] IS NOT NULL
)
END

View File

@ -35,11 +35,22 @@ BEGIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
LEFT JOIN
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[CollectionId] = S.[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.[GroupId] = GU.[GroupId]
WHERE
O.[Id] = @OrganizationId
AND O.[Enabled] = 1
AND OU.[Status] = 2 -- Confirmed
AND (OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0)
AND (
OU.[AccessAll] = 1
OR CU.[ReadOnly] = 0
OR G.[AccessAll] = 1
OR CG.[ReadOnly] = 0
)
)
INSERT INTO [dbo].[CollectionCipher]
(

View File

@ -5,16 +5,27 @@ BEGIN
SET NOCOUNT ON
SELECT
SC.*
CC.*
FROM
[dbo].[CollectionCipher] SC
[dbo].[CollectionCipher] CC
INNER JOIN
[dbo].[Collection] S ON S.[Id] = SC.[CollectionId]
[dbo].[Collection] S ON S.[Id] = CC.[CollectionId]
INNER JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = S.[OrganizationId] AND OU.[UserId] = @UserId
LEFT JOIN
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[CollectionId] = S.[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] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
WHERE
OU.[Status] = 2 -- Confirmed
AND (OU.[AccessAll] = 1 OR CU.[CollectionId] IS NOT NULL)
AND (
OU.[AccessAll] = 1
OR CU.[CollectionId] IS NOT NULL
OR G.[AccessAll] = 1
OR CG.[CollectionId] IS NOT NULL
)
END

View File

@ -6,17 +6,28 @@ BEGIN
SET NOCOUNT ON
SELECT
SC.*
CC.*
FROM
[dbo].[CollectionCipher] SC
[dbo].[CollectionCipher] CC
INNER JOIN
[dbo].[Collection] S ON S.[Id] = SC.[CollectionId]
[dbo].[Collection] S ON S.[Id] = CC.[CollectionId]
INNER JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = S.[OrganizationId] AND OU.[UserId] = @UserId
LEFT JOIN
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[CollectionId] = S.[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] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
WHERE
SC.[CipherId] = @CipherId
CC.[CipherId] = @CipherId
AND OU.[Status] = 2 -- Confirmed
AND (OU.[AccessAll] = 1 OR CU.[CollectionId] IS NOT NULL)
AND (
OU.[AccessAll] = 1
OR CU.[CollectionId] IS NOT NULL
OR G.[AccessAll] = 1
OR CG.[CollectionId] IS NOT NULL
)
END

View File

@ -17,20 +17,31 @@ BEGIN
;WITH [AvailableCollectionsCTE] AS(
SELECT
S.[Id]
C.[Id]
FROM
[dbo].[Collection] S
[dbo].[Collection] C
INNER JOIN
[Organization] O ON O.[Id] = S.[OrganizationId]
[Organization] O ON O.[Id] = C.[OrganizationId]
INNER JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
LEFT JOIN
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[CollectionId] = S.[Id] AND CU.[OrganizationUserId] = OU.[Id]
[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
O.[Id] = @OrgId
AND O.[Enabled] = 1
AND OU.[Status] = 2 -- Confirmed
AND (OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0)
AND (
OU.[AccessAll] = 1
OR CU.[CollectionId] IS NOT NULL
OR G.[AccessAll] = 1
OR CG.[CollectionId] IS NOT NULL
)
)
MERGE
[dbo].[CollectionCipher] AS [Target]

View File

@ -1,13 +0,0 @@
CREATE PROCEDURE [dbo].[CollectionUserCollectionDetails_ReadByUserId]
@UserId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
*
FROM
[dbo].[CollectionUserCollectionDetailsView]
WHERE
[UserId] = @UserId
END

View File

@ -9,13 +9,21 @@ BEGIN
FROM
[dbo].[CollectionView] C
INNER JOIN
[Organization] O ON O.[Id] = C.[OrganizationId]
INNER JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
[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]
[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.[Status] = 2 -- Confirmed
AND O.[Enabled] = 1
AND (OU.[AccessAll] = 1 OR CU.[CollectionId] IS NOT NULL)
OU.[UserId] = @UserId
AND (
OU.[AccessAll] = 1
OR CU.[Id] IS NOT NULL
OR G.[AccessAll] = 1
OR CG.[CollectionId] IS NOT NULL
)
END

View File

@ -1,20 +0,0 @@
CREATE PROCEDURE [dbo].[OrganizationUserUserDetails_ReadById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
*
FROM
[dbo].[OrganizationUserUserDetailsView]
WHERE
[Id] = @Id
SELECT
*
FROM
[dbo].[CollectionUserCollectionDetailsView]
WHERE
[OrganizationUserId] = @Id
END

View File

@ -0,0 +1,18 @@
CREATE PROCEDURE [dbo].[OrganizationUser_ReadWithCollectionsById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
EXEC [OrganizationUser_ReadById] @Id
SELECT
CU.[CollectionId] Id,
CU.[ReadOnly]
FROM
[dbo].[OrganizationUser] OU
INNER JOIN
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[OrganizationUserId] = [OU].[Id]
WHERE
[OrganizationUserId] = @Id
END

View File

@ -1,18 +0,0 @@
CREATE VIEW [dbo].[CollectionUserCollectionDetailsView]
AS
SELECT
C.[Id] Id,
C.[OrganizationId],
C.[Name],
OU.[UserId],
OU.[Id] AS [OrganizationUserId],
CASE WHEN OU.[AccessAll] = 0 AND CU.[ReadOnly] = 1 THEN 1 ELSE 0 END [ReadOnly]
FROM
[dbo].[Collection] C
INNER JOIN
[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

View File

@ -10,10 +10,19 @@ SELECT
ISNULL(U.[Email], OU.[Email]) Email,
OU.[Status],
OU.[Type],
CASE WHEN OU.[AccessAll] = 0 AND CU.[ReadOnly] = 1 THEN 1 ELSE 0 END [ReadOnly]
CASE
WHEN OU.[AccessAll] = 0 AND CU.[ReadOnly] = 1 AND G.[AccessAll] = 0 AND CG.[ReadOnly] = 1 THEN 1
ELSE 0
END [ReadOnly]
FROM
[dbo].[OrganizationUser] OU
LEFT JOIN
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[OrganizationUserId] = OU.[Id]
LEFT JOIN
[dbo].[User] U ON U.[Id] = OU.[UserId]
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.[GroupId] = GU.[GroupId]