mirror of
https://github.com/bitwarden/server.git
synced 2025-07-03 09:02:48 -05:00
[AC-1683] Fix DB migrations for new Manage permission (#3307)
* [AC-1683] Update migration script and introduce V2 procedures and types * [AC-1683] Update repository calls to use new V2 procedures / types * [AC-1684] Update bulk add collection migration script to use new V2 type * [AC-1683] Undo Manage changes to more original procedures * [AC-1683] Restore whitespace changes * [AC-1683] Clarify comments regarding explicit column lists * [AC-1683] Update migration script dates * [AC-1683] Split the migration script for readability * [AC-1683] Re-name SelectReadOnlyArray_V2 to CollectionAccessSelectionType
This commit is contained in:
@ -142,7 +142,7 @@ public class GroupRepository : Repository<Group, Guid>, IGroupRepository
|
|||||||
using (var connection = new SqlConnection(ConnectionString))
|
using (var connection = new SqlConnection(ConnectionString))
|
||||||
{
|
{
|
||||||
var results = await connection.ExecuteAsync(
|
var results = await connection.ExecuteAsync(
|
||||||
$"[{Schema}].[Group_CreateWithCollections]",
|
$"[{Schema}].[Group_CreateWithCollections_V2]",
|
||||||
objWithCollections,
|
objWithCollections,
|
||||||
commandType: CommandType.StoredProcedure);
|
commandType: CommandType.StoredProcedure);
|
||||||
}
|
}
|
||||||
@ -156,7 +156,7 @@ public class GroupRepository : Repository<Group, Guid>, IGroupRepository
|
|||||||
using (var connection = new SqlConnection(ConnectionString))
|
using (var connection = new SqlConnection(ConnectionString))
|
||||||
{
|
{
|
||||||
var results = await connection.ExecuteAsync(
|
var results = await connection.ExecuteAsync(
|
||||||
$"[{Schema}].[Group_UpdateWithCollections]",
|
$"[{Schema}].[Group_UpdateWithCollections_V2]",
|
||||||
objWithCollections,
|
objWithCollections,
|
||||||
commandType: CommandType.StoredProcedure);
|
commandType: CommandType.StoredProcedure);
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ public static class DapperHelpers
|
|||||||
public static DataTable ToArrayTVP(this IEnumerable<CollectionAccessSelection> values)
|
public static DataTable ToArrayTVP(this IEnumerable<CollectionAccessSelection> values)
|
||||||
{
|
{
|
||||||
var table = new DataTable();
|
var table = new DataTable();
|
||||||
table.SetTypeName("[dbo].[SelectionReadOnlyArray]");
|
table.SetTypeName("[dbo].[CollectionAccessSelectionType]");
|
||||||
|
|
||||||
var idColumn = new DataColumn("Id", typeof(Guid));
|
var idColumn = new DataColumn("Id", typeof(Guid));
|
||||||
table.Columns.Add(idColumn);
|
table.Columns.Add(idColumn);
|
||||||
|
@ -221,7 +221,7 @@ public class CollectionRepository : Repository<Collection, Guid>, ICollectionRep
|
|||||||
using (var connection = new SqlConnection(ConnectionString))
|
using (var connection = new SqlConnection(ConnectionString))
|
||||||
{
|
{
|
||||||
var results = await connection.ExecuteAsync(
|
var results = await connection.ExecuteAsync(
|
||||||
$"[{Schema}].[Collection_CreateWithGroupsAndUsers]",
|
$"[{Schema}].[Collection_CreateWithGroupsAndUsers_V2]",
|
||||||
objWithGroupsAndUsers,
|
objWithGroupsAndUsers,
|
||||||
commandType: CommandType.StoredProcedure);
|
commandType: CommandType.StoredProcedure);
|
||||||
}
|
}
|
||||||
@ -237,7 +237,7 @@ public class CollectionRepository : Repository<Collection, Guid>, ICollectionRep
|
|||||||
using (var connection = new SqlConnection(ConnectionString))
|
using (var connection = new SqlConnection(ConnectionString))
|
||||||
{
|
{
|
||||||
var results = await connection.ExecuteAsync(
|
var results = await connection.ExecuteAsync(
|
||||||
$"[{Schema}].[Collection_UpdateWithGroupsAndUsers]",
|
$"[{Schema}].[Collection_UpdateWithGroupsAndUsers_V2]",
|
||||||
objWithGroupsAndUsers,
|
objWithGroupsAndUsers,
|
||||||
commandType: CommandType.StoredProcedure);
|
commandType: CommandType.StoredProcedure);
|
||||||
}
|
}
|
||||||
@ -294,7 +294,7 @@ public class CollectionRepository : Repository<Collection, Guid>, ICollectionRep
|
|||||||
using (var connection = new SqlConnection(ConnectionString))
|
using (var connection = new SqlConnection(ConnectionString))
|
||||||
{
|
{
|
||||||
var results = await connection.ExecuteAsync(
|
var results = await connection.ExecuteAsync(
|
||||||
$"[{Schema}].[CollectionUser_UpdateUsers]",
|
$"[{Schema}].[CollectionUser_UpdateUsers_V2]",
|
||||||
new { CollectionId = id, Users = users.ToArrayTVP() },
|
new { CollectionId = id, Users = users.ToArrayTVP() },
|
||||||
commandType: CommandType.StoredProcedure);
|
commandType: CommandType.StoredProcedure);
|
||||||
}
|
}
|
||||||
|
@ -326,7 +326,7 @@ public class OrganizationUserRepository : Repository<OrganizationUser, Guid>, IO
|
|||||||
using (var connection = new SqlConnection(ConnectionString))
|
using (var connection = new SqlConnection(ConnectionString))
|
||||||
{
|
{
|
||||||
var results = await connection.ExecuteAsync(
|
var results = await connection.ExecuteAsync(
|
||||||
$"[{Schema}].[OrganizationUser_CreateWithCollections]",
|
$"[{Schema}].[OrganizationUser_CreateWithCollections_V2]",
|
||||||
objWithCollections,
|
objWithCollections,
|
||||||
commandType: CommandType.StoredProcedure);
|
commandType: CommandType.StoredProcedure);
|
||||||
}
|
}
|
||||||
@ -343,7 +343,7 @@ public class OrganizationUserRepository : Repository<OrganizationUser, Guid>, IO
|
|||||||
using (var connection = new SqlConnection(ConnectionString))
|
using (var connection = new SqlConnection(ConnectionString))
|
||||||
{
|
{
|
||||||
var results = await connection.ExecuteAsync(
|
var results = await connection.ExecuteAsync(
|
||||||
$"[{Schema}].[OrganizationUser_UpdateWithCollections]",
|
$"[{Schema}].[OrganizationUser_UpdateWithCollections_V2]",
|
||||||
objWithCollections,
|
objWithCollections,
|
||||||
commandType: CommandType.StoredProcedure);
|
commandType: CommandType.StoredProcedure);
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,7 @@ BEGIN
|
|||||||
[Target]
|
[Target]
|
||||||
SET
|
SET
|
||||||
[Target].[ReadOnly] = [Source].[ReadOnly],
|
[Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
[Target].[HidePasswords] = [Source].[HidePasswords],
|
[Target].[HidePasswords] = [Source].[HidePasswords]
|
||||||
[Target].[Manage] = [Source].[Manage]
|
|
||||||
FROM
|
FROM
|
||||||
[dbo].[CollectionUser] [Target]
|
[dbo].[CollectionUser] [Target]
|
||||||
INNER JOIN
|
INNER JOIN
|
||||||
@ -30,18 +29,21 @@ BEGIN
|
|||||||
AND (
|
AND (
|
||||||
[Target].[ReadOnly] != [Source].[ReadOnly]
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
OR [Target].[Manage] != [Source].[Manage]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
-- Insert
|
-- Insert (with column list because a value for Manage is not being provided)
|
||||||
INSERT INTO
|
INSERT INTO [dbo].[CollectionUser]
|
||||||
[dbo].[CollectionUser]
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords]
|
||||||
|
)
|
||||||
SELECT
|
SELECT
|
||||||
@CollectionId,
|
@CollectionId,
|
||||||
[Source].[Id],
|
[Source].[Id],
|
||||||
[Source].[ReadOnly],
|
[Source].[ReadOnly],
|
||||||
[Source].[HidePasswords],
|
[Source].[HidePasswords]
|
||||||
[Source].[Manage]
|
|
||||||
FROM
|
FROM
|
||||||
@Users [Source]
|
@Users [Source]
|
||||||
INNER JOIN
|
INNER JOIN
|
||||||
|
@ -0,0 +1,83 @@
|
|||||||
|
CREATE PROCEDURE [dbo].[CollectionUser_UpdateUsers_V2]
|
||||||
|
@CollectionId UNIQUEIDENTIFIER,
|
||||||
|
@Users AS [dbo].[CollectionAccessSelectionType] READONLY
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
DECLARE @OrgId UNIQUEIDENTIFIER = (
|
||||||
|
SELECT TOP 1
|
||||||
|
[OrganizationId]
|
||||||
|
FROM
|
||||||
|
[dbo].[Collection]
|
||||||
|
WHERE
|
||||||
|
[Id] = @CollectionId
|
||||||
|
)
|
||||||
|
|
||||||
|
-- Update
|
||||||
|
UPDATE
|
||||||
|
[Target]
|
||||||
|
SET
|
||||||
|
[Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
|
[Target].[HidePasswords] = [Source].[HidePasswords],
|
||||||
|
[Target].[Manage] = [Source].[Manage]
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser] [Target]
|
||||||
|
INNER JOIN
|
||||||
|
@Users [Source] ON [Source].[Id] = [Target].[OrganizationUserId]
|
||||||
|
WHERE
|
||||||
|
[Target].[CollectionId] = @CollectionId
|
||||||
|
AND (
|
||||||
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
|
OR [Target].[Manage] != [Source].[Manage]
|
||||||
|
)
|
||||||
|
|
||||||
|
-- Insert
|
||||||
|
INSERT INTO [dbo].[CollectionUser]
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
@CollectionId,
|
||||||
|
[Source].[Id],
|
||||||
|
[Source].[ReadOnly],
|
||||||
|
[Source].[HidePasswords],
|
||||||
|
[Source].[Manage]
|
||||||
|
FROM
|
||||||
|
@Users [Source]
|
||||||
|
INNER JOIN
|
||||||
|
[dbo].[OrganizationUser] OU ON [Source].[Id] = OU.[Id] AND OU.[OrganizationId] = @OrgId
|
||||||
|
WHERE
|
||||||
|
NOT EXISTS (
|
||||||
|
SELECT
|
||||||
|
1
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser]
|
||||||
|
WHERE
|
||||||
|
[CollectionId] = @CollectionId
|
||||||
|
AND [OrganizationUserId] = [Source].[Id]
|
||||||
|
)
|
||||||
|
|
||||||
|
-- Delete
|
||||||
|
DELETE
|
||||||
|
CU
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser] CU
|
||||||
|
WHERE
|
||||||
|
CU.[CollectionId] = @CollectionId
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT
|
||||||
|
1
|
||||||
|
FROM
|
||||||
|
@Users
|
||||||
|
WHERE
|
||||||
|
[Id] = CU.[OrganizationUserId]
|
||||||
|
)
|
||||||
|
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByCollectionId] @CollectionId, @OrgId
|
||||||
|
END
|
@ -1,8 +1,8 @@
|
|||||||
CREATE PROCEDURE [dbo].[Collection_CreateOrUpdateAccessForMany]
|
CREATE PROCEDURE [dbo].[Collection_CreateOrUpdateAccessForMany]
|
||||||
@OrganizationId UNIQUEIDENTIFIER,
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
@CollectionIds AS [dbo].[GuidIdArray] READONLY,
|
@CollectionIds AS [dbo].[GuidIdArray] READONLY,
|
||||||
@Groups AS [dbo].[SelectionReadOnlyArray] READONLY,
|
@Groups AS [dbo].[CollectionAccessSelectionType] READONLY,
|
||||||
@Users AS [dbo].[SelectionReadOnlyArray] READONLY
|
@Users AS [dbo].[CollectionAccessSelectionType] READONLY
|
||||||
AS
|
AS
|
||||||
BEGIN
|
BEGIN
|
||||||
SET NOCOUNT ON
|
SET NOCOUNT ON
|
||||||
@ -41,7 +41,15 @@ BEGIN
|
|||||||
[Target].[HidePasswords] = [Source].[HidePasswords],
|
[Target].[HidePasswords] = [Source].[HidePasswords],
|
||||||
[Target].[Manage] = [Source].[Manage]
|
[Target].[Manage] = [Source].[Manage]
|
||||||
WHEN NOT MATCHED BY TARGET
|
WHEN NOT MATCHED BY TARGET
|
||||||
THEN INSERT VALUES
|
THEN INSERT
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[GroupId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
(
|
(
|
||||||
[Source].[CollectionId],
|
[Source].[CollectionId],
|
||||||
[Source].[GroupId],
|
[Source].[GroupId],
|
||||||
@ -84,7 +92,15 @@ BEGIN
|
|||||||
[Target].[HidePasswords] = [Source].[HidePasswords],
|
[Target].[HidePasswords] = [Source].[HidePasswords],
|
||||||
[Target].[Manage] = [Source].[Manage]
|
[Target].[Manage] = [Source].[Manage]
|
||||||
WHEN NOT MATCHED BY TARGET
|
WHEN NOT MATCHED BY TARGET
|
||||||
THEN INSERT VALUES
|
THEN INSERT
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
(
|
(
|
||||||
[Source].[CollectionId],
|
[Source].[CollectionId],
|
||||||
[Source].[OrganizationUserId],
|
[Source].[OrganizationUserId],
|
||||||
|
@ -27,15 +27,13 @@ BEGIN
|
|||||||
[CollectionId],
|
[CollectionId],
|
||||||
[GroupId],
|
[GroupId],
|
||||||
[ReadOnly],
|
[ReadOnly],
|
||||||
[HidePasswords],
|
[HidePasswords]
|
||||||
[Manage]
|
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
@Id,
|
@Id,
|
||||||
[Id],
|
[Id],
|
||||||
[ReadOnly],
|
[ReadOnly],
|
||||||
[HidePasswords],
|
[HidePasswords]
|
||||||
[Manage]
|
|
||||||
FROM
|
FROM
|
||||||
@Groups
|
@Groups
|
||||||
WHERE
|
WHERE
|
||||||
@ -55,15 +53,13 @@ BEGIN
|
|||||||
[CollectionId],
|
[CollectionId],
|
||||||
[OrganizationUserId],
|
[OrganizationUserId],
|
||||||
[ReadOnly],
|
[ReadOnly],
|
||||||
[HidePasswords],
|
[HidePasswords]
|
||||||
[Manage]
|
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
@Id,
|
@Id,
|
||||||
[Id],
|
[Id],
|
||||||
[ReadOnly],
|
[ReadOnly],
|
||||||
[HidePasswords],
|
[HidePasswords]
|
||||||
[Manage]
|
|
||||||
FROM
|
FROM
|
||||||
@Users
|
@Users
|
||||||
WHERE
|
WHERE
|
||||||
|
@ -0,0 +1,73 @@
|
|||||||
|
CREATE PROCEDURE [dbo].[Collection_CreateWithGroupsAndUsers_V2]
|
||||||
|
@Id UNIQUEIDENTIFIER,
|
||||||
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
|
@Name VARCHAR(MAX),
|
||||||
|
@ExternalId NVARCHAR(300),
|
||||||
|
@CreationDate DATETIME2(7),
|
||||||
|
@RevisionDate DATETIME2(7),
|
||||||
|
@Groups AS [dbo].[CollectionAccessSelectionType] READONLY,
|
||||||
|
@Users AS [dbo].[CollectionAccessSelectionType] READONLY
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
EXEC [dbo].[Collection_Create] @Id, @OrganizationId, @Name, @ExternalId, @CreationDate, @RevisionDate
|
||||||
|
|
||||||
|
-- Groups
|
||||||
|
;WITH [AvailableGroupsCTE] AS(
|
||||||
|
SELECT
|
||||||
|
[Id]
|
||||||
|
FROM
|
||||||
|
[dbo].[Group]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @OrganizationId
|
||||||
|
)
|
||||||
|
INSERT INTO [dbo].[CollectionGroup]
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[GroupId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
@Id,
|
||||||
|
[Id],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
FROM
|
||||||
|
@Groups
|
||||||
|
WHERE
|
||||||
|
[Id] IN (SELECT [Id] FROM [AvailableGroupsCTE])
|
||||||
|
|
||||||
|
-- Users
|
||||||
|
;WITH [AvailableUsersCTE] AS(
|
||||||
|
SELECT
|
||||||
|
[Id]
|
||||||
|
FROM
|
||||||
|
[dbo].[OrganizationUser]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @OrganizationId
|
||||||
|
)
|
||||||
|
INSERT INTO [dbo].[CollectionUser]
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
@Id,
|
||||||
|
[Id],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
FROM
|
||||||
|
@Users
|
||||||
|
WHERE
|
||||||
|
[Id] IN (SELECT [Id] FROM [AvailableUsersCTE])
|
||||||
|
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
|
||||||
|
END
|
@ -31,22 +31,26 @@ BEGIN
|
|||||||
AND [Target].[GroupId] = [Source].[Id]
|
AND [Target].[GroupId] = [Source].[Id]
|
||||||
WHEN NOT MATCHED BY TARGET
|
WHEN NOT MATCHED BY TARGET
|
||||||
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableGroupsCTE]) THEN
|
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableGroupsCTE]) THEN
|
||||||
INSERT VALUES
|
INSERT -- With column list because a value for Manage is not being provided
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[GroupId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
(
|
(
|
||||||
@Id,
|
@Id,
|
||||||
[Source].[Id],
|
[Source].[Id],
|
||||||
[Source].[ReadOnly],
|
[Source].[ReadOnly],
|
||||||
[Source].[HidePasswords],
|
[Source].[HidePasswords]
|
||||||
[Source].[Manage]
|
|
||||||
)
|
)
|
||||||
WHEN MATCHED AND (
|
WHEN MATCHED AND (
|
||||||
[Target].[ReadOnly] != [Source].[ReadOnly]
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
OR [Target].[Manage] != [Source].[Manage]
|
|
||||||
) THEN
|
) THEN
|
||||||
UPDATE SET [Target].[ReadOnly] = [Source].[ReadOnly],
|
UPDATE SET [Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
[Target].[HidePasswords] = [Source].[HidePasswords],
|
[Target].[HidePasswords] = [Source].[HidePasswords]
|
||||||
[Target].[Manage] = [Source].[Manage]
|
|
||||||
WHEN NOT MATCHED BY SOURCE
|
WHEN NOT MATCHED BY SOURCE
|
||||||
AND [Target].[CollectionId] = @Id THEN
|
AND [Target].[CollectionId] = @Id THEN
|
||||||
DELETE
|
DELETE
|
||||||
@ -70,22 +74,26 @@ BEGIN
|
|||||||
AND [Target].[OrganizationUserId] = [Source].[Id]
|
AND [Target].[OrganizationUserId] = [Source].[Id]
|
||||||
WHEN NOT MATCHED BY TARGET
|
WHEN NOT MATCHED BY TARGET
|
||||||
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableGroupsCTE]) THEN
|
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableGroupsCTE]) THEN
|
||||||
INSERT VALUES
|
INSERT -- With column list because a value for Manage is not being provided
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
(
|
(
|
||||||
@Id,
|
@Id,
|
||||||
[Source].[Id],
|
[Source].[Id],
|
||||||
[Source].[ReadOnly],
|
[Source].[ReadOnly],
|
||||||
[Source].[HidePasswords],
|
[Source].[HidePasswords]
|
||||||
[Source].[Manage]
|
|
||||||
)
|
)
|
||||||
WHEN MATCHED AND (
|
WHEN MATCHED AND (
|
||||||
[Target].[ReadOnly] != [Source].[ReadOnly]
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
OR [Target].[Manage] != [Source].[Manage]
|
|
||||||
) THEN
|
) THEN
|
||||||
UPDATE SET [Target].[ReadOnly] = [Source].[ReadOnly],
|
UPDATE SET [Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
[Target].[HidePasswords] = [Source].[HidePasswords],
|
[Target].[HidePasswords] = [Source].[HidePasswords]
|
||||||
[Target].[Manage] = [Source].[Manage]
|
|
||||||
WHEN NOT MATCHED BY SOURCE
|
WHEN NOT MATCHED BY SOURCE
|
||||||
AND [Target].[CollectionId] = @Id THEN
|
AND [Target].[CollectionId] = @Id THEN
|
||||||
DELETE
|
DELETE
|
||||||
|
@ -0,0 +1,111 @@
|
|||||||
|
CREATE PROCEDURE [dbo].[Collection_UpdateWithGroupsAndUsers_V2]
|
||||||
|
@Id UNIQUEIDENTIFIER,
|
||||||
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
|
@Name VARCHAR(MAX),
|
||||||
|
@ExternalId NVARCHAR(300),
|
||||||
|
@CreationDate DATETIME2(7),
|
||||||
|
@RevisionDate DATETIME2(7),
|
||||||
|
@Groups AS [dbo].[CollectionAccessSelectionType] READONLY,
|
||||||
|
@Users AS [dbo].[CollectionAccessSelectionType] READONLY
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
EXEC [dbo].[Collection_Update] @Id, @OrganizationId, @Name, @ExternalId, @CreationDate, @RevisionDate
|
||||||
|
|
||||||
|
-- Groups
|
||||||
|
;WITH [AvailableGroupsCTE] AS(
|
||||||
|
SELECT
|
||||||
|
Id
|
||||||
|
FROM
|
||||||
|
[dbo].[Group]
|
||||||
|
WHERE
|
||||||
|
OrganizationId = @OrganizationId
|
||||||
|
)
|
||||||
|
MERGE
|
||||||
|
[dbo].[CollectionGroup] AS [Target]
|
||||||
|
USING
|
||||||
|
@Groups AS [Source]
|
||||||
|
ON
|
||||||
|
[Target].[CollectionId] = @Id
|
||||||
|
AND [Target].[GroupId] = [Source].[Id]
|
||||||
|
WHEN NOT MATCHED BY TARGET
|
||||||
|
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableGroupsCTE]) THEN
|
||||||
|
INSERT -- Add explicit column list
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[GroupId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
(
|
||||||
|
@Id,
|
||||||
|
[Source].[Id],
|
||||||
|
[Source].[ReadOnly],
|
||||||
|
[Source].[HidePasswords],
|
||||||
|
[Source].[Manage]
|
||||||
|
)
|
||||||
|
WHEN MATCHED AND (
|
||||||
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
|
OR [Target].[Manage] != [Source].[Manage]
|
||||||
|
) THEN
|
||||||
|
UPDATE SET [Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
|
[Target].[HidePasswords] = [Source].[HidePasswords],
|
||||||
|
[Target].[Manage] = [Source].[Manage]
|
||||||
|
WHEN NOT MATCHED BY SOURCE
|
||||||
|
AND [Target].[CollectionId] = @Id THEN
|
||||||
|
DELETE
|
||||||
|
;
|
||||||
|
|
||||||
|
-- Users
|
||||||
|
;WITH [AvailableGroupsCTE] AS(
|
||||||
|
SELECT
|
||||||
|
Id
|
||||||
|
FROM
|
||||||
|
[dbo].[OrganizationUser]
|
||||||
|
WHERE
|
||||||
|
OrganizationId = @OrganizationId
|
||||||
|
)
|
||||||
|
MERGE
|
||||||
|
[dbo].[CollectionUser] AS [Target]
|
||||||
|
USING
|
||||||
|
@Users AS [Source]
|
||||||
|
ON
|
||||||
|
[Target].[CollectionId] = @Id
|
||||||
|
AND [Target].[OrganizationUserId] = [Source].[Id]
|
||||||
|
WHEN NOT MATCHED BY TARGET
|
||||||
|
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableGroupsCTE]) THEN
|
||||||
|
INSERT
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
(
|
||||||
|
@Id,
|
||||||
|
[Source].[Id],
|
||||||
|
[Source].[ReadOnly],
|
||||||
|
[Source].[HidePasswords],
|
||||||
|
[Source].[Manage]
|
||||||
|
)
|
||||||
|
WHEN MATCHED AND (
|
||||||
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
|
OR [Target].[Manage] != [Source].[Manage]
|
||||||
|
) THEN
|
||||||
|
UPDATE SET [Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
|
[Target].[HidePasswords] = [Source].[HidePasswords],
|
||||||
|
[Target].[Manage] = [Source].[Manage]
|
||||||
|
WHEN NOT MATCHED BY SOURCE
|
||||||
|
AND [Target].[CollectionId] = @Id THEN
|
||||||
|
DELETE
|
||||||
|
;
|
||||||
|
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByCollectionId] @Id, @OrganizationId
|
||||||
|
END
|
@ -26,15 +26,13 @@ BEGIN
|
|||||||
[CollectionId],
|
[CollectionId],
|
||||||
[GroupId],
|
[GroupId],
|
||||||
[ReadOnly],
|
[ReadOnly],
|
||||||
[HidePasswords],
|
[HidePasswords]
|
||||||
[Manage]
|
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
[Id],
|
[Id],
|
||||||
@Id,
|
@Id,
|
||||||
[ReadOnly],
|
[ReadOnly],
|
||||||
[HidePasswords],
|
[HidePasswords]
|
||||||
[Manage]
|
|
||||||
FROM
|
FROM
|
||||||
@Collections
|
@Collections
|
||||||
WHERE
|
WHERE
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
CREATE PROCEDURE [dbo].[Group_CreateWithCollections_V2]
|
||||||
|
@Id UNIQUEIDENTIFIER,
|
||||||
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
|
@Name NVARCHAR(100),
|
||||||
|
@AccessAll BIT,
|
||||||
|
@ExternalId NVARCHAR(300),
|
||||||
|
@CreationDate DATETIME2(7),
|
||||||
|
@RevisionDate DATETIME2(7),
|
||||||
|
@Collections AS [dbo].[CollectionAccessSelectionType] READONLY
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
EXEC [dbo].[Group_Create] @Id, @OrganizationId, @Name, @AccessAll, @ExternalId, @CreationDate, @RevisionDate
|
||||||
|
|
||||||
|
;WITH [AvailableCollectionsCTE] AS(
|
||||||
|
SELECT
|
||||||
|
[Id]
|
||||||
|
FROM
|
||||||
|
[dbo].[Collection]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @OrganizationId
|
||||||
|
)
|
||||||
|
INSERT INTO [dbo].[CollectionGroup]
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[GroupId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
[Id],
|
||||||
|
@Id,
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
FROM
|
||||||
|
@Collections
|
||||||
|
WHERE
|
||||||
|
[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE])
|
||||||
|
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
|
||||||
|
END
|
@ -30,22 +30,26 @@ BEGIN
|
|||||||
AND [Target].[GroupId] = @Id
|
AND [Target].[GroupId] = @Id
|
||||||
WHEN NOT MATCHED BY TARGET
|
WHEN NOT MATCHED BY TARGET
|
||||||
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE]) THEN
|
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE]) THEN
|
||||||
INSERT VALUES
|
INSERT -- With column list because a value for Manage is not being provided
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[GroupId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
(
|
(
|
||||||
[Source].[Id],
|
[Source].[Id],
|
||||||
@Id,
|
@Id,
|
||||||
[Source].[ReadOnly],
|
[Source].[ReadOnly],
|
||||||
[Source].[HidePasswords],
|
[Source].[HidePasswords]
|
||||||
[Source].[Manage]
|
|
||||||
)
|
)
|
||||||
WHEN MATCHED AND (
|
WHEN MATCHED AND (
|
||||||
[Target].[ReadOnly] != [Source].[ReadOnly]
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
OR [Target].[Manage] != [Source].[Manage]
|
|
||||||
) THEN
|
) THEN
|
||||||
UPDATE SET [Target].[ReadOnly] = [Source].[ReadOnly],
|
UPDATE SET [Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
[Target].[HidePasswords] = [Source].[HidePasswords],
|
[Target].[HidePasswords] = [Source].[HidePasswords]
|
||||||
[Target].[Manage] = [Source].[Manage]
|
|
||||||
WHEN NOT MATCHED BY SOURCE
|
WHEN NOT MATCHED BY SOURCE
|
||||||
AND [Target].[GroupId] = @Id THEN
|
AND [Target].[GroupId] = @Id THEN
|
||||||
DELETE
|
DELETE
|
||||||
|
@ -0,0 +1,63 @@
|
|||||||
|
CREATE PROCEDURE [dbo].[Group_UpdateWithCollections_V2]
|
||||||
|
@Id UNIQUEIDENTIFIER,
|
||||||
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
|
@Name NVARCHAR(100),
|
||||||
|
@AccessAll BIT,
|
||||||
|
@ExternalId NVARCHAR(300),
|
||||||
|
@CreationDate DATETIME2(7),
|
||||||
|
@RevisionDate DATETIME2(7),
|
||||||
|
@Collections AS [dbo].[CollectionAccessSelectionType] READONLY
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
EXEC [dbo].[Group_Update] @Id, @OrganizationId, @Name, @AccessAll, @ExternalId, @CreationDate, @RevisionDate
|
||||||
|
|
||||||
|
;WITH [AvailableCollectionsCTE] AS(
|
||||||
|
SELECT
|
||||||
|
Id
|
||||||
|
FROM
|
||||||
|
[dbo].[Collection]
|
||||||
|
WHERE
|
||||||
|
OrganizationId = @OrganizationId
|
||||||
|
)
|
||||||
|
MERGE
|
||||||
|
[dbo].[CollectionGroup] AS [Target]
|
||||||
|
USING
|
||||||
|
@Collections AS [Source]
|
||||||
|
ON
|
||||||
|
[Target].[CollectionId] = [Source].[Id]
|
||||||
|
AND [Target].[GroupId] = @Id
|
||||||
|
WHEN NOT MATCHED BY TARGET
|
||||||
|
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE]) THEN
|
||||||
|
INSERT
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[GroupId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
(
|
||||||
|
[Source].[Id],
|
||||||
|
@Id,
|
||||||
|
[Source].[ReadOnly],
|
||||||
|
[Source].[HidePasswords],
|
||||||
|
[Source].[Manage]
|
||||||
|
)
|
||||||
|
WHEN MATCHED AND (
|
||||||
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
|
OR [Target].[Manage] != [Source].[Manage]
|
||||||
|
) THEN
|
||||||
|
UPDATE SET [Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
|
[Target].[HidePasswords] = [Source].[HidePasswords],
|
||||||
|
[Target].[Manage] = [Source].[Manage]
|
||||||
|
WHEN NOT MATCHED BY SOURCE
|
||||||
|
AND [Target].[GroupId] = @Id THEN
|
||||||
|
DELETE
|
||||||
|
;
|
||||||
|
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
|
||||||
|
END
|
@ -33,15 +33,13 @@ BEGIN
|
|||||||
[CollectionId],
|
[CollectionId],
|
||||||
[OrganizationUserId],
|
[OrganizationUserId],
|
||||||
[ReadOnly],
|
[ReadOnly],
|
||||||
[HidePasswords],
|
[HidePasswords]
|
||||||
[Manage]
|
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
[Id],
|
[Id],
|
||||||
@Id,
|
@Id,
|
||||||
[ReadOnly],
|
[ReadOnly],
|
||||||
[HidePasswords],
|
[HidePasswords]
|
||||||
[Manage]
|
|
||||||
FROM
|
FROM
|
||||||
@Collections
|
@Collections
|
||||||
WHERE
|
WHERE
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
CREATE PROCEDURE [dbo].[OrganizationUser_CreateWithCollections_V2]
|
||||||
|
@Id UNIQUEIDENTIFIER,
|
||||||
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
|
@UserId UNIQUEIDENTIFIER,
|
||||||
|
@Email NVARCHAR(256),
|
||||||
|
@Key VARCHAR(MAX),
|
||||||
|
@Status SMALLINT,
|
||||||
|
@Type TINYINT,
|
||||||
|
@AccessAll BIT,
|
||||||
|
@ExternalId NVARCHAR(300),
|
||||||
|
@CreationDate DATETIME2(7),
|
||||||
|
@RevisionDate DATETIME2(7),
|
||||||
|
@Permissions NVARCHAR(MAX),
|
||||||
|
@ResetPasswordKey VARCHAR(MAX),
|
||||||
|
@Collections AS [dbo].[CollectionAccessSelectionType] READONLY,
|
||||||
|
@AccessSecretsManager BIT = 0
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
EXEC [dbo].[OrganizationUser_Create] @Id, @OrganizationId, @UserId, @Email, @Key, @Status, @Type, @AccessAll, @ExternalId, @CreationDate, @RevisionDate, @Permissions, @ResetPasswordKey, @AccessSecretsManager
|
||||||
|
|
||||||
|
;WITH [AvailableCollectionsCTE] AS(
|
||||||
|
SELECT
|
||||||
|
[Id]
|
||||||
|
FROM
|
||||||
|
[dbo].[Collection]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @OrganizationId
|
||||||
|
)
|
||||||
|
INSERT INTO [dbo].[CollectionUser]
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
[Id],
|
||||||
|
@Id,
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
FROM
|
||||||
|
@Collections
|
||||||
|
WHERE
|
||||||
|
[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE])
|
||||||
|
END
|
@ -24,8 +24,7 @@ BEGIN
|
|||||||
[Target]
|
[Target]
|
||||||
SET
|
SET
|
||||||
[Target].[ReadOnly] = [Source].[ReadOnly],
|
[Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
[Target].[HidePasswords] = [Source].[HidePasswords],
|
[Target].[HidePasswords] = [Source].[HidePasswords]
|
||||||
[Target].[Manage] = [Source].[Manage]
|
|
||||||
FROM
|
FROM
|
||||||
[dbo].[CollectionUser] AS [Target]
|
[dbo].[CollectionUser] AS [Target]
|
||||||
INNER JOIN
|
INNER JOIN
|
||||||
@ -35,18 +34,21 @@ BEGIN
|
|||||||
AND (
|
AND (
|
||||||
[Target].[ReadOnly] != [Source].[ReadOnly]
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
OR [Target].[Manage] != [Source].[Manage]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
-- Insert
|
-- Insert (with column list because a value for Manage is not being provided)
|
||||||
INSERT INTO
|
INSERT INTO [dbo].[CollectionUser]
|
||||||
[dbo].[CollectionUser]
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords]
|
||||||
|
)
|
||||||
SELECT
|
SELECT
|
||||||
[Source].[Id],
|
[Source].[Id],
|
||||||
@Id,
|
@Id,
|
||||||
[Source].[ReadOnly],
|
[Source].[ReadOnly],
|
||||||
[Source].[HidePasswords],
|
[Source].[HidePasswords]
|
||||||
[Source].[Manage]
|
|
||||||
FROM
|
FROM
|
||||||
@Collections AS [Source]
|
@Collections AS [Source]
|
||||||
INNER JOIN
|
INNER JOIN
|
||||||
|
@ -0,0 +1,86 @@
|
|||||||
|
CREATE PROCEDURE [dbo].[OrganizationUser_UpdateWithCollections_V2]
|
||||||
|
@Id UNIQUEIDENTIFIER,
|
||||||
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
|
@UserId UNIQUEIDENTIFIER,
|
||||||
|
@Email NVARCHAR(256),
|
||||||
|
@Key VARCHAR(MAX),
|
||||||
|
@Status SMALLINT,
|
||||||
|
@Type TINYINT,
|
||||||
|
@AccessAll BIT,
|
||||||
|
@ExternalId NVARCHAR(300),
|
||||||
|
@CreationDate DATETIME2(7),
|
||||||
|
@RevisionDate DATETIME2(7),
|
||||||
|
@Permissions NVARCHAR(MAX),
|
||||||
|
@ResetPasswordKey VARCHAR(MAX),
|
||||||
|
@Collections AS [dbo].[CollectionAccessSelectionType] READONLY,
|
||||||
|
@AccessSecretsManager BIT = 0
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
EXEC [dbo].[OrganizationUser_Update] @Id, @OrganizationId, @UserId, @Email, @Key, @Status, @Type, @AccessAll, @ExternalId, @CreationDate, @RevisionDate, @Permissions, @ResetPasswordKey, @AccessSecretsManager
|
||||||
|
-- Update
|
||||||
|
UPDATE
|
||||||
|
[Target]
|
||||||
|
SET
|
||||||
|
[Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
|
[Target].[HidePasswords] = [Source].[HidePasswords],
|
||||||
|
[Target].[Manage] = [Source].[Manage]
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser] AS [Target]
|
||||||
|
INNER JOIN
|
||||||
|
@Collections AS [Source] ON [Source].[Id] = [Target].[CollectionId]
|
||||||
|
WHERE
|
||||||
|
[Target].[OrganizationUserId] = @Id
|
||||||
|
AND (
|
||||||
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
|
OR [Target].[Manage] != [Source].[Manage]
|
||||||
|
)
|
||||||
|
|
||||||
|
-- Insert
|
||||||
|
INSERT INTO [dbo].[CollectionUser]
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
[Source].[Id],
|
||||||
|
@Id,
|
||||||
|
[Source].[ReadOnly],
|
||||||
|
[Source].[HidePasswords],
|
||||||
|
[Source].[Manage]
|
||||||
|
FROM
|
||||||
|
@Collections AS [Source]
|
||||||
|
INNER JOIN
|
||||||
|
[dbo].[Collection] C ON C.[Id] = [Source].[Id] AND C.[OrganizationId] = @OrganizationId
|
||||||
|
WHERE
|
||||||
|
NOT EXISTS (
|
||||||
|
SELECT
|
||||||
|
1
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser]
|
||||||
|
WHERE
|
||||||
|
[CollectionId] = [Source].[Id]
|
||||||
|
AND [OrganizationUserId] = @Id
|
||||||
|
)
|
||||||
|
|
||||||
|
-- Delete
|
||||||
|
DELETE
|
||||||
|
CU
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser] CU
|
||||||
|
WHERE
|
||||||
|
CU.[OrganizationUserId] = @Id
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT
|
||||||
|
1
|
||||||
|
FROM
|
||||||
|
@Collections
|
||||||
|
WHERE
|
||||||
|
[Id] = CU.[CollectionId]
|
||||||
|
)
|
||||||
|
END
|
@ -0,0 +1,6 @@
|
|||||||
|
CREATE TYPE [dbo].[CollectionAccessSelectionType] AS TABLE (
|
||||||
|
[Id] UNIQUEIDENTIFIER NOT NULL,
|
||||||
|
[ReadOnly] BIT NOT NULL,
|
||||||
|
[HidePasswords] BIT NOT NULL,
|
||||||
|
[Manage] BIT NOT NULL);
|
||||||
|
|
@ -1,6 +1,5 @@
|
|||||||
CREATE TYPE [dbo].[SelectionReadOnlyArray] AS TABLE (
|
CREATE TYPE [dbo].[SelectionReadOnlyArray] AS TABLE (
|
||||||
[Id] UNIQUEIDENTIFIER NOT NULL,
|
[Id] UNIQUEIDENTIFIER NOT NULL,
|
||||||
[ReadOnly] BIT NOT NULL,
|
[ReadOnly] BIT NOT NULL,
|
||||||
[HidePasswords] BIT NOT NULL,
|
[HidePasswords] BIT NOT NULL);
|
||||||
[Manage] BIT NOT NULL);
|
|
||||||
|
|
||||||
|
@ -0,0 +1,340 @@
|
|||||||
|
/*
|
||||||
|
* Update existing write procedures to safely ignore any newly added columns to the CollectionUser and
|
||||||
|
* CollectionGroup tables (e.g. preparation for [Manage] in the next migration script). This is accomplished by
|
||||||
|
* explicitly listing the columns in the INSERT and UPDATE statements.
|
||||||
|
*/
|
||||||
|
|
||||||
|
-- Update INSERT statement to include explicit column list
|
||||||
|
CREATE OR ALTER PROCEDURE [dbo].[CollectionUser_UpdateUsers]
|
||||||
|
@CollectionId UNIQUEIDENTIFIER,
|
||||||
|
@Users AS [dbo].[SelectionReadOnlyArray] READONLY
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
DECLARE @OrgId UNIQUEIDENTIFIER = (
|
||||||
|
SELECT TOP 1
|
||||||
|
[OrganizationId]
|
||||||
|
FROM
|
||||||
|
[dbo].[Collection]
|
||||||
|
WHERE
|
||||||
|
[Id] = @CollectionId
|
||||||
|
)
|
||||||
|
|
||||||
|
-- Update
|
||||||
|
UPDATE
|
||||||
|
[Target]
|
||||||
|
SET
|
||||||
|
[Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
|
[Target].[HidePasswords] = [Source].[HidePasswords]
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser] [Target]
|
||||||
|
INNER JOIN
|
||||||
|
@Users [Source] ON [Source].[Id] = [Target].[OrganizationUserId]
|
||||||
|
WHERE
|
||||||
|
[Target].[CollectionId] = @CollectionId
|
||||||
|
AND (
|
||||||
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
|
)
|
||||||
|
|
||||||
|
-- Insert
|
||||||
|
INSERT INTO [dbo].[CollectionUser]
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords]
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
@CollectionId,
|
||||||
|
[Source].[Id],
|
||||||
|
[Source].[ReadOnly],
|
||||||
|
[Source].[HidePasswords]
|
||||||
|
FROM
|
||||||
|
@Users [Source]
|
||||||
|
INNER JOIN
|
||||||
|
[dbo].[OrganizationUser] OU ON [Source].[Id] = OU.[Id] AND OU.[OrganizationId] = @OrgId
|
||||||
|
WHERE
|
||||||
|
NOT EXISTS (
|
||||||
|
SELECT
|
||||||
|
1
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser]
|
||||||
|
WHERE
|
||||||
|
[CollectionId] = @CollectionId
|
||||||
|
AND [OrganizationUserId] = [Source].[Id]
|
||||||
|
)
|
||||||
|
|
||||||
|
-- Delete
|
||||||
|
DELETE
|
||||||
|
CU
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser] CU
|
||||||
|
WHERE
|
||||||
|
CU.[CollectionId] = @CollectionId
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT
|
||||||
|
1
|
||||||
|
FROM
|
||||||
|
@Users
|
||||||
|
WHERE
|
||||||
|
[Id] = CU.[OrganizationUserId]
|
||||||
|
)
|
||||||
|
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByCollectionId] @CollectionId, @OrgId
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
-- Update INSERT statement to include explicit column list
|
||||||
|
CREATE OR ALTER PROCEDURE [dbo].[Group_UpdateWithCollections]
|
||||||
|
@Id UNIQUEIDENTIFIER,
|
||||||
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
|
@Name NVARCHAR(100),
|
||||||
|
@AccessAll BIT,
|
||||||
|
@ExternalId NVARCHAR(300),
|
||||||
|
@CreationDate DATETIME2(7),
|
||||||
|
@RevisionDate DATETIME2(7),
|
||||||
|
@Collections AS [dbo].[SelectionReadOnlyArray] READONLY
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
EXEC [dbo].[Group_Update] @Id, @OrganizationId, @Name, @AccessAll, @ExternalId, @CreationDate, @RevisionDate
|
||||||
|
|
||||||
|
;WITH [AvailableCollectionsCTE] AS(
|
||||||
|
SELECT
|
||||||
|
Id
|
||||||
|
FROM
|
||||||
|
[dbo].[Collection]
|
||||||
|
WHERE
|
||||||
|
OrganizationId = @OrganizationId
|
||||||
|
)
|
||||||
|
MERGE
|
||||||
|
[dbo].[CollectionGroup] AS [Target]
|
||||||
|
USING
|
||||||
|
@Collections AS [Source]
|
||||||
|
ON
|
||||||
|
[Target].[CollectionId] = [Source].[Id]
|
||||||
|
AND [Target].[GroupId] = @Id
|
||||||
|
WHEN NOT MATCHED BY TARGET
|
||||||
|
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE]) THEN
|
||||||
|
INSERT
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[GroupId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
(
|
||||||
|
[Source].[Id],
|
||||||
|
@Id,
|
||||||
|
[Source].[ReadOnly],
|
||||||
|
[Source].[HidePasswords]
|
||||||
|
)
|
||||||
|
WHEN MATCHED AND (
|
||||||
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
|
) THEN
|
||||||
|
UPDATE SET [Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
|
[Target].[HidePasswords] = [Source].[HidePasswords]
|
||||||
|
WHEN NOT MATCHED BY SOURCE
|
||||||
|
AND [Target].[GroupId] = @Id THEN
|
||||||
|
DELETE
|
||||||
|
;
|
||||||
|
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
-- Update INSERT statements to include explicit column list
|
||||||
|
CREATE OR ALTER PROCEDURE [dbo].[Collection_UpdateWithGroupsAndUsers]
|
||||||
|
@Id UNIQUEIDENTIFIER,
|
||||||
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
|
@Name VARCHAR(MAX),
|
||||||
|
@ExternalId NVARCHAR(300),
|
||||||
|
@CreationDate DATETIME2(7),
|
||||||
|
@RevisionDate DATETIME2(7),
|
||||||
|
@Groups AS [dbo].[SelectionReadOnlyArray] READONLY,
|
||||||
|
@Users AS [dbo].[SelectionReadOnlyArray] READONLY
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
EXEC [dbo].[Collection_Update] @Id, @OrganizationId, @Name, @ExternalId, @CreationDate, @RevisionDate
|
||||||
|
|
||||||
|
-- Groups
|
||||||
|
;WITH [AvailableGroupsCTE] AS(
|
||||||
|
SELECT
|
||||||
|
Id
|
||||||
|
FROM
|
||||||
|
[dbo].[Group]
|
||||||
|
WHERE
|
||||||
|
OrganizationId = @OrganizationId
|
||||||
|
)
|
||||||
|
MERGE
|
||||||
|
[dbo].[CollectionGroup] AS [Target]
|
||||||
|
USING
|
||||||
|
@Groups AS [Source]
|
||||||
|
ON
|
||||||
|
[Target].[CollectionId] = @Id
|
||||||
|
AND [Target].[GroupId] = [Source].[Id]
|
||||||
|
WHEN NOT MATCHED BY TARGET
|
||||||
|
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableGroupsCTE]) THEN
|
||||||
|
INSERT -- With column list because a value for Manage is not being provided
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[GroupId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
(
|
||||||
|
@Id,
|
||||||
|
[Source].[Id],
|
||||||
|
[Source].[ReadOnly],
|
||||||
|
[Source].[HidePasswords]
|
||||||
|
)
|
||||||
|
WHEN MATCHED AND (
|
||||||
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
|
) THEN
|
||||||
|
UPDATE SET [Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
|
[Target].[HidePasswords] = [Source].[HidePasswords]
|
||||||
|
WHEN NOT MATCHED BY SOURCE
|
||||||
|
AND [Target].[CollectionId] = @Id THEN
|
||||||
|
DELETE
|
||||||
|
;
|
||||||
|
|
||||||
|
-- Users
|
||||||
|
;WITH [AvailableGroupsCTE] AS(
|
||||||
|
SELECT
|
||||||
|
Id
|
||||||
|
FROM
|
||||||
|
[dbo].[OrganizationUser]
|
||||||
|
WHERE
|
||||||
|
OrganizationId = @OrganizationId
|
||||||
|
)
|
||||||
|
MERGE
|
||||||
|
[dbo].[CollectionUser] AS [Target]
|
||||||
|
USING
|
||||||
|
@Users AS [Source]
|
||||||
|
ON
|
||||||
|
[Target].[CollectionId] = @Id
|
||||||
|
AND [Target].[OrganizationUserId] = [Source].[Id]
|
||||||
|
WHEN NOT MATCHED BY TARGET
|
||||||
|
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableGroupsCTE]) THEN
|
||||||
|
INSERT
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
(
|
||||||
|
@Id,
|
||||||
|
[Source].[Id],
|
||||||
|
[Source].[ReadOnly],
|
||||||
|
[Source].[HidePasswords]
|
||||||
|
)
|
||||||
|
WHEN MATCHED AND (
|
||||||
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
|
) THEN
|
||||||
|
UPDATE SET [Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
|
[Target].[HidePasswords] = [Source].[HidePasswords]
|
||||||
|
WHEN NOT MATCHED BY SOURCE
|
||||||
|
AND [Target].[CollectionId] = @Id THEN
|
||||||
|
DELETE
|
||||||
|
;
|
||||||
|
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByCollectionId] @Id, @OrganizationId
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
-- Update INSERT statement to include explicit column list
|
||||||
|
CREATE OR ALTER PROCEDURE [dbo].[OrganizationUser_UpdateWithCollections]
|
||||||
|
@Id UNIQUEIDENTIFIER,
|
||||||
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
|
@UserId UNIQUEIDENTIFIER,
|
||||||
|
@Email NVARCHAR(256),
|
||||||
|
@Key VARCHAR(MAX),
|
||||||
|
@Status SMALLINT,
|
||||||
|
@Type TINYINT,
|
||||||
|
@AccessAll BIT,
|
||||||
|
@ExternalId NVARCHAR(300),
|
||||||
|
@CreationDate DATETIME2(7),
|
||||||
|
@RevisionDate DATETIME2(7),
|
||||||
|
@Permissions NVARCHAR(MAX),
|
||||||
|
@ResetPasswordKey VARCHAR(MAX),
|
||||||
|
@Collections AS [dbo].[SelectionReadOnlyArray] READONLY,
|
||||||
|
@AccessSecretsManager BIT = 0
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
EXEC [dbo].[OrganizationUser_Update] @Id, @OrganizationId, @UserId, @Email, @Key, @Status, @Type, @AccessAll, @ExternalId, @CreationDate, @RevisionDate, @Permissions, @ResetPasswordKey, @AccessSecretsManager
|
||||||
|
-- Update
|
||||||
|
UPDATE
|
||||||
|
[Target]
|
||||||
|
SET
|
||||||
|
[Target].[ReadOnly] = [Source].[ReadOnly],
|
||||||
|
[Target].[HidePasswords] = [Source].[HidePasswords]
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser] AS [Target]
|
||||||
|
INNER JOIN
|
||||||
|
@Collections AS [Source] ON [Source].[Id] = [Target].[CollectionId]
|
||||||
|
WHERE
|
||||||
|
[Target].[OrganizationUserId] = @Id
|
||||||
|
AND (
|
||||||
|
[Target].[ReadOnly] != [Source].[ReadOnly]
|
||||||
|
OR [Target].[HidePasswords] != [Source].[HidePasswords]
|
||||||
|
)
|
||||||
|
|
||||||
|
-- Insert
|
||||||
|
INSERT INTO [dbo].[CollectionUser]
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords]
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
[Source].[Id],
|
||||||
|
@Id,
|
||||||
|
[Source].[ReadOnly],
|
||||||
|
[Source].[HidePasswords]
|
||||||
|
FROM
|
||||||
|
@Collections AS [Source]
|
||||||
|
INNER JOIN
|
||||||
|
[dbo].[Collection] C ON C.[Id] = [Source].[Id] AND C.[OrganizationId] = @OrganizationId
|
||||||
|
WHERE
|
||||||
|
NOT EXISTS (
|
||||||
|
SELECT
|
||||||
|
1
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser]
|
||||||
|
WHERE
|
||||||
|
[CollectionId] = [Source].[Id]
|
||||||
|
AND [OrganizationUserId] = @Id
|
||||||
|
)
|
||||||
|
|
||||||
|
-- Delete
|
||||||
|
DELETE
|
||||||
|
CU
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser] CU
|
||||||
|
WHERE
|
||||||
|
CU.[OrganizationUserId] = @Id
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT
|
||||||
|
1
|
||||||
|
FROM
|
||||||
|
@Collections
|
||||||
|
WHERE
|
||||||
|
[Id] = CU.[CollectionId]
|
||||||
|
)
|
||||||
|
END
|
||||||
|
GO
|
@ -1,65 +1,65 @@
|
|||||||
/*
|
/*
|
||||||
* Add Manage permission to collections
|
* Add Manage permission to collections and update associated stored procedures
|
||||||
*/
|
*/
|
||||||
|
|
||||||
-- Drop procedures that use the SelectionReadOnlyArray type
|
-- To allow the migration to be re-run, drop any of the V2 procedures as they depend on a new type
|
||||||
IF OBJECT_ID('[dbo].[Group_CreateWithCollections]') IS NOT NULL
|
|
||||||
|
IF OBJECT_ID('[dbo].[CollectionUser_UpdateUsers_V2]') IS NOT NULL
|
||||||
BEGIN
|
BEGIN
|
||||||
DROP PROCEDURE [dbo].[Group_CreateWithCollections]
|
DROP PROCEDURE [dbo].[CollectionUser_UpdateUsers_V2]
|
||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
IF OBJECT_ID('[dbo].[CollectionUser_UpdateUsers]') IS NOT NULL
|
IF OBJECT_ID('[dbo].[Group_UpdateWithCollections_V2]') IS NOT NULL
|
||||||
BEGIN
|
BEGIN
|
||||||
DROP PROCEDURE [dbo].[CollectionUser_UpdateUsers]
|
DROP PROCEDURE [dbo].[Group_UpdateWithCollections_V2]
|
||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
IF OBJECT_ID('[dbo].[Group_UpdateWithCollections]') IS NOT NULL
|
IF OBJECT_ID('[dbo].[Collection_UpdateWithGroupsAndUsers_V2]') IS NOT NULL
|
||||||
BEGIN
|
BEGIN
|
||||||
DROP PROCEDURE [dbo].[Group_UpdateWithCollections]
|
DROP PROCEDURE [dbo].[Collection_UpdateWithGroupsAndUsers_V2]
|
||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
IF OBJECT_ID('[dbo].[Collection_UpdateWithGroupsAndUsers]') IS NOT NULL
|
IF OBJECT_ID('[dbo].[OrganizationUser_UpdateWithCollections_V2]') IS NOT NULL
|
||||||
BEGIN
|
BEGIN
|
||||||
DROP PROCEDURE [dbo].[Collection_UpdateWithGroupsAndUsers]
|
DROP PROCEDURE [dbo].[OrganizationUser_UpdateWithCollections_V2]
|
||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
IF OBJECT_ID('[dbo].[OrganizationUser_CreateWithCollections]') IS NOT NULL
|
IF OBJECT_ID('[dbo].[Group_CreateWithCollections_V2]') IS NOT NULL
|
||||||
BEGIN
|
BEGIN
|
||||||
DROP PROCEDURE [dbo].[OrganizationUser_CreateWithCollections]
|
DROP PROCEDURE [dbo].[Group_CreateWithCollections_V2]
|
||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
IF OBJECT_ID('[dbo].[OrganizationUser_UpdateWithCollections]') IS NOT NULL
|
IF OBJECT_ID('[dbo].[OrganizationUser_CreateWithCollections_V2]') IS NOT NULL
|
||||||
BEGIN
|
BEGIN
|
||||||
DROP PROCEDURE [dbo].[OrganizationUser_UpdateWithCollections]
|
DROP PROCEDURE [dbo].[OrganizationUser_CreateWithCollections_V2]
|
||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
IF OBJECT_ID('[dbo].[Collection_CreateWithGroupsAndUsers]') IS NOT NULL
|
IF OBJECT_ID('[dbo].[Collection_CreateWithGroupsAndUsers_V2]') IS NOT NULL
|
||||||
BEGIN
|
BEGIN
|
||||||
DROP PROCEDURE [dbo].[Collection_CreateWithGroupsAndUsers]
|
DROP PROCEDURE [dbo].[Collection_CreateWithGroupsAndUsers_V2]
|
||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
IF TYPE_ID('[dbo].[SelectionReadOnlyArray]') IS NOT NULL
|
-- Create a new CollectionAccessSelectionType with a new [Manage] column
|
||||||
|
IF TYPE_ID('[dbo].[CollectionAccessSelectionType]') IS NOT NULL
|
||||||
BEGIN
|
BEGIN
|
||||||
DROP TYPE [dbo].[SelectionReadOnlyArray]
|
DROP TYPE [dbo].[CollectionAccessSelectionType]
|
||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
CREATE TYPE [dbo].[SelectionReadOnlyArray] AS TABLE (
|
CREATE TYPE [dbo].[CollectionAccessSelectionType] AS TABLE (
|
||||||
[Id] UNIQUEIDENTIFIER NOT NULL,
|
[Id] UNIQUEIDENTIFIER NOT NULL,
|
||||||
[ReadOnly] BIT NOT NULL,
|
[ReadOnly] BIT NOT NULL,
|
||||||
[HidePasswords] BIT NOT NULL,
|
[HidePasswords] BIT NOT NULL,
|
||||||
[Manage] BIT NOT NULL);
|
[Manage] BIT NOT NULL);
|
||||||
GO
|
GO
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- Add Manage Column
|
-- Add Manage Column
|
||||||
IF COL_LENGTH('[dbo].[CollectionUser]', 'Manage') IS NULL
|
IF COL_LENGTH('[dbo].[CollectionUser]', 'Manage') IS NULL
|
||||||
BEGIN
|
BEGIN
|
||||||
@ -74,6 +74,10 @@ IF COL_LENGTH('[dbo].[CollectionGroup]', 'Manage') IS NULL
|
|||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
|
-- BEGIN Update procedures that support backwards compatability in place
|
||||||
|
-- These procedures can be safely used by server in case of rollback and do not require V2 versions
|
||||||
|
|
||||||
|
-- Readonly query that adds [Manage] column to result, safely ignored by rolled back server
|
||||||
CREATE OR ALTER PROCEDURE [dbo].[CollectionUser_ReadByCollectionId]
|
CREATE OR ALTER PROCEDURE [dbo].[CollectionUser_ReadByCollectionId]
|
||||||
@CollectionId UNIQUEIDENTIFIER
|
@CollectionId UNIQUEIDENTIFIER
|
||||||
AS
|
AS
|
||||||
@ -92,6 +96,7 @@ BEGIN
|
|||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
|
-- Readonly query that adds [Manage] column to result, safely ignored by rolled back server
|
||||||
CREATE OR ALTER PROCEDURE [dbo].[CollectionGroup_ReadByCollectionId]
|
CREATE OR ALTER PROCEDURE [dbo].[CollectionGroup_ReadByCollectionId]
|
||||||
@CollectionId UNIQUEIDENTIFIER
|
@CollectionId UNIQUEIDENTIFIER
|
||||||
AS
|
AS
|
||||||
@ -110,55 +115,215 @@ BEGIN
|
|||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
CREATE PROCEDURE [dbo].[Group_CreateWithCollections]
|
-- Readonly query that adds [Manage] column to result, safely ignored by rolled back server
|
||||||
@Id UNIQUEIDENTIFIER,
|
CREATE OR ALTER PROCEDURE [dbo].[OrganizationUserUserDetails_ReadWithCollectionsById]
|
||||||
@OrganizationId UNIQUEIDENTIFIER,
|
@Id UNIQUEIDENTIFIER
|
||||||
@Name NVARCHAR(100),
|
|
||||||
@AccessAll BIT,
|
|
||||||
@ExternalId NVARCHAR(300),
|
|
||||||
@CreationDate DATETIME2(7),
|
|
||||||
@RevisionDate DATETIME2(7),
|
|
||||||
@Collections AS [dbo].[SelectionReadOnlyArray] READONLY
|
|
||||||
AS
|
AS
|
||||||
BEGIN
|
BEGIN
|
||||||
SET NOCOUNT ON
|
SET NOCOUNT ON
|
||||||
|
|
||||||
EXEC [dbo].[Group_Create] @Id, @OrganizationId, @Name, @AccessAll, @ExternalId, @CreationDate, @RevisionDate
|
EXEC [OrganizationUserUserDetails_ReadById] @Id
|
||||||
|
|
||||||
;WITH [AvailableCollectionsCTE] AS(
|
|
||||||
SELECT
|
SELECT
|
||||||
[Id]
|
CU.[CollectionId] Id,
|
||||||
|
CU.[ReadOnly],
|
||||||
|
CU.[HidePasswords],
|
||||||
|
CU.[Manage]
|
||||||
FROM
|
FROM
|
||||||
[dbo].[Collection]
|
[dbo].[OrganizationUser] OU
|
||||||
|
INNER JOIN
|
||||||
|
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[OrganizationUserId] = [OU].[Id]
|
||||||
WHERE
|
WHERE
|
||||||
[OrganizationId] = @OrganizationId
|
[OrganizationUserId] = @Id
|
||||||
)
|
|
||||||
INSERT INTO [dbo].[CollectionGroup]
|
|
||||||
(
|
|
||||||
[CollectionId],
|
|
||||||
[GroupId],
|
|
||||||
[ReadOnly],
|
|
||||||
[HidePasswords],
|
|
||||||
[Manage]
|
|
||||||
)
|
|
||||||
SELECT
|
|
||||||
[Id],
|
|
||||||
@Id,
|
|
||||||
[ReadOnly],
|
|
||||||
[HidePasswords],
|
|
||||||
[Manage]
|
|
||||||
FROM
|
|
||||||
@Collections
|
|
||||||
WHERE
|
|
||||||
[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE])
|
|
||||||
|
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
|
|
||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
CREATE PROCEDURE [dbo].[CollectionUser_UpdateUsers]
|
-- Readonly function that adds [Manage] column to result, safely ignored by rolled back server
|
||||||
|
CREATE OR ALTER FUNCTION [dbo].[UserCollectionDetails](@UserId UNIQUEIDENTIFIER)
|
||||||
|
RETURNS TABLE
|
||||||
|
AS RETURN
|
||||||
|
SELECT
|
||||||
|
C.*,
|
||||||
|
CASE
|
||||||
|
WHEN
|
||||||
|
OU.[AccessAll] = 1
|
||||||
|
OR G.[AccessAll] = 1
|
||||||
|
OR COALESCE(CU.[ReadOnly], CG.[ReadOnly], 0) = 0
|
||||||
|
THEN 0
|
||||||
|
ELSE 1
|
||||||
|
END [ReadOnly],
|
||||||
|
CASE
|
||||||
|
WHEN
|
||||||
|
OU.[AccessAll] = 1
|
||||||
|
OR G.[AccessAll] = 1
|
||||||
|
OR COALESCE(CU.[HidePasswords], CG.[HidePasswords], 0) = 0
|
||||||
|
THEN 0
|
||||||
|
ELSE 1
|
||||||
|
END [HidePasswords],
|
||||||
|
CASE
|
||||||
|
WHEN
|
||||||
|
OU.[AccessAll] = 1
|
||||||
|
OR G.[AccessAll] = 1
|
||||||
|
OR COALESCE(CU.[Manage], CG.[Manage], 0) = 0
|
||||||
|
THEN 0
|
||||||
|
ELSE 1
|
||||||
|
END [Manage]
|
||||||
|
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
|
||||||
|
)
|
||||||
|
GO
|
||||||
|
|
||||||
|
-- Readonly query that adds [Manage] column to result, safely ignored by rolled back server
|
||||||
|
CREATE OR ALTER PROCEDURE [dbo].[Collection_ReadByIdUserId]
|
||||||
|
@Id UNIQUEIDENTIFIER,
|
||||||
|
@UserId UNIQUEIDENTIFIER
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
SELECT
|
||||||
|
Id,
|
||||||
|
OrganizationId,
|
||||||
|
[Name],
|
||||||
|
CreationDate,
|
||||||
|
RevisionDate,
|
||||||
|
ExternalId,
|
||||||
|
MIN([ReadOnly]) AS [ReadOnly],
|
||||||
|
MIN([HidePasswords]) AS [HidePasswords],
|
||||||
|
MIN([Manage]) AS [Manage]
|
||||||
|
FROM
|
||||||
|
[dbo].[UserCollectionDetails](@UserId)
|
||||||
|
WHERE
|
||||||
|
[Id] = @Id
|
||||||
|
GROUP BY
|
||||||
|
Id,
|
||||||
|
OrganizationId,
|
||||||
|
[Name],
|
||||||
|
CreationDate,
|
||||||
|
RevisionDate,
|
||||||
|
ExternalId
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
-- Readonly query that adds [Manage] column to result, safely ignored by rolled back server
|
||||||
|
CREATE OR ALTER PROCEDURE [dbo].[Collection_ReadByUserId]
|
||||||
|
@UserId UNIQUEIDENTIFIER
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
Id,
|
||||||
|
OrganizationId,
|
||||||
|
[Name],
|
||||||
|
CreationDate,
|
||||||
|
RevisionDate,
|
||||||
|
ExternalId,
|
||||||
|
MIN([ReadOnly]) AS [ReadOnly],
|
||||||
|
MIN([HidePasswords]) AS [HidePasswords],
|
||||||
|
MIN([Manage]) AS [Manage]
|
||||||
|
FROM
|
||||||
|
[dbo].[UserCollectionDetails](@UserId)
|
||||||
|
GROUP BY
|
||||||
|
Id,
|
||||||
|
OrganizationId,
|
||||||
|
[Name],
|
||||||
|
CreationDate,
|
||||||
|
RevisionDate,
|
||||||
|
ExternalId
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
-- Readonly query that adds [Manage] column to result, safely ignored by rolled back server
|
||||||
|
CREATE OR ALTER PROCEDURE [dbo].[Collection_ReadWithGroupsAndUsersByUserId]
|
||||||
|
@UserId UNIQUEIDENTIFIER
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
DECLARE @TempUserCollections TABLE(
|
||||||
|
Id UNIQUEIDENTIFIER,
|
||||||
|
OrganizationId UNIQUEIDENTIFIER,
|
||||||
|
Name VARCHAR(MAX),
|
||||||
|
CreationDate DATETIME2(7),
|
||||||
|
RevisionDate DATETIME2(7),
|
||||||
|
ExternalId NVARCHAR(300),
|
||||||
|
ReadOnly BIT,
|
||||||
|
HidePasswords BIT,
|
||||||
|
Manage BIT)
|
||||||
|
|
||||||
|
INSERT INTO @TempUserCollections EXEC [dbo].[Collection_ReadByUserId] @UserId
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
@TempUserCollections C
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
CG.*
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionGroup] CG
|
||||||
|
INNER JOIN
|
||||||
|
@TempUserCollections C ON C.[Id] = CG.[CollectionId]
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
CU.*
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser] CU
|
||||||
|
INNER JOIN
|
||||||
|
@TempUserCollections C ON C.[Id] = CU.[CollectionId]
|
||||||
|
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
-- Readonly query that adds [Manage] column to result, safely ignored by rolled back server
|
||||||
|
CREATE OR ALTER PROCEDURE [dbo].[Group_ReadWithCollectionsById]
|
||||||
|
@Id UNIQUEIDENTIFIER
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
EXEC [dbo].[Group_ReadById] @Id
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
[CollectionId] [Id],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionGroup]
|
||||||
|
WHERE
|
||||||
|
[GroupId] = @Id
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
-- END Update procedures that support backwards compatability in place
|
||||||
|
|
||||||
|
-- BEGIN Create V2 of existing procedures to support new [Manage] column and new CollectionAccessSelectionType
|
||||||
|
|
||||||
|
CREATE OR ALTER PROCEDURE [dbo].[CollectionUser_UpdateUsers_V2]
|
||||||
@CollectionId UNIQUEIDENTIFIER,
|
@CollectionId UNIQUEIDENTIFIER,
|
||||||
@Users AS [dbo].[SelectionReadOnlyArray] READONLY
|
@Users AS [dbo].[CollectionAccessSelectionType] READONLY
|
||||||
AS
|
AS
|
||||||
BEGIN
|
BEGIN
|
||||||
SET NOCOUNT ON
|
SET NOCOUNT ON
|
||||||
@ -192,8 +357,14 @@ BEGIN
|
|||||||
)
|
)
|
||||||
|
|
||||||
-- Insert
|
-- Insert
|
||||||
INSERT INTO
|
INSERT INTO [dbo].[CollectionUser]
|
||||||
[dbo].[CollectionUser]
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
SELECT
|
SELECT
|
||||||
@CollectionId,
|
@CollectionId,
|
||||||
[Source].[Id],
|
[Source].[Id],
|
||||||
@ -235,7 +406,7 @@ BEGIN
|
|||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
CREATE PROCEDURE [dbo].[Group_UpdateWithCollections]
|
CREATE OR ALTER PROCEDURE [dbo].[Group_UpdateWithCollections_V2]
|
||||||
@Id UNIQUEIDENTIFIER,
|
@Id UNIQUEIDENTIFIER,
|
||||||
@OrganizationId UNIQUEIDENTIFIER,
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
@Name NVARCHAR(100),
|
@Name NVARCHAR(100),
|
||||||
@ -243,7 +414,7 @@ CREATE PROCEDURE [dbo].[Group_UpdateWithCollections]
|
|||||||
@ExternalId NVARCHAR(300),
|
@ExternalId NVARCHAR(300),
|
||||||
@CreationDate DATETIME2(7),
|
@CreationDate DATETIME2(7),
|
||||||
@RevisionDate DATETIME2(7),
|
@RevisionDate DATETIME2(7),
|
||||||
@Collections AS [dbo].[SelectionReadOnlyArray] READONLY
|
@Collections AS [dbo].[CollectionAccessSelectionType] READONLY
|
||||||
AS
|
AS
|
||||||
BEGIN
|
BEGIN
|
||||||
SET NOCOUNT ON
|
SET NOCOUNT ON
|
||||||
@ -267,7 +438,15 @@ BEGIN
|
|||||||
AND [Target].[GroupId] = @Id
|
AND [Target].[GroupId] = @Id
|
||||||
WHEN NOT MATCHED BY TARGET
|
WHEN NOT MATCHED BY TARGET
|
||||||
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE]) THEN
|
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE]) THEN
|
||||||
INSERT VALUES
|
INSERT
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[GroupId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
(
|
(
|
||||||
[Source].[Id],
|
[Source].[Id],
|
||||||
@Id,
|
@Id,
|
||||||
@ -292,15 +471,15 @@ BEGIN
|
|||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
CREATE PROCEDURE [dbo].[Collection_UpdateWithGroupsAndUsers]
|
CREATE OR ALTER PROCEDURE [dbo].[Collection_UpdateWithGroupsAndUsers_V2]
|
||||||
@Id UNIQUEIDENTIFIER,
|
@Id UNIQUEIDENTIFIER,
|
||||||
@OrganizationId UNIQUEIDENTIFIER,
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
@Name VARCHAR(MAX),
|
@Name VARCHAR(MAX),
|
||||||
@ExternalId NVARCHAR(300),
|
@ExternalId NVARCHAR(300),
|
||||||
@CreationDate DATETIME2(7),
|
@CreationDate DATETIME2(7),
|
||||||
@RevisionDate DATETIME2(7),
|
@RevisionDate DATETIME2(7),
|
||||||
@Groups AS [dbo].[SelectionReadOnlyArray] READONLY,
|
@Groups AS [dbo].[CollectionAccessSelectionType] READONLY,
|
||||||
@Users AS [dbo].[SelectionReadOnlyArray] READONLY
|
@Users AS [dbo].[CollectionAccessSelectionType] READONLY
|
||||||
AS
|
AS
|
||||||
BEGIN
|
BEGIN
|
||||||
SET NOCOUNT ON
|
SET NOCOUNT ON
|
||||||
@ -325,7 +504,15 @@ BEGIN
|
|||||||
AND [Target].[GroupId] = [Source].[Id]
|
AND [Target].[GroupId] = [Source].[Id]
|
||||||
WHEN NOT MATCHED BY TARGET
|
WHEN NOT MATCHED BY TARGET
|
||||||
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableGroupsCTE]) THEN
|
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableGroupsCTE]) THEN
|
||||||
INSERT VALUES
|
INSERT -- Add explicit column list
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[GroupId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
(
|
(
|
||||||
@Id,
|
@Id,
|
||||||
[Source].[Id],
|
[Source].[Id],
|
||||||
@ -364,7 +551,15 @@ BEGIN
|
|||||||
AND [Target].[OrganizationUserId] = [Source].[Id]
|
AND [Target].[OrganizationUserId] = [Source].[Id]
|
||||||
WHEN NOT MATCHED BY TARGET
|
WHEN NOT MATCHED BY TARGET
|
||||||
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableGroupsCTE]) THEN
|
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableGroupsCTE]) THEN
|
||||||
INSERT VALUES
|
INSERT
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
(
|
(
|
||||||
@Id,
|
@Id,
|
||||||
[Source].[Id],
|
[Source].[Id],
|
||||||
@ -389,7 +584,7 @@ BEGIN
|
|||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
CREATE PROCEDURE [dbo].[OrganizationUser_CreateWithCollections]
|
CREATE OR ALTER PROCEDURE [dbo].[OrganizationUser_UpdateWithCollections_V2]
|
||||||
@Id UNIQUEIDENTIFIER,
|
@Id UNIQUEIDENTIFIER,
|
||||||
@OrganizationId UNIQUEIDENTIFIER,
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
@UserId UNIQUEIDENTIFIER,
|
@UserId UNIQUEIDENTIFIER,
|
||||||
@ -403,58 +598,7 @@ CREATE PROCEDURE [dbo].[OrganizationUser_CreateWithCollections]
|
|||||||
@RevisionDate DATETIME2(7),
|
@RevisionDate DATETIME2(7),
|
||||||
@Permissions NVARCHAR(MAX),
|
@Permissions NVARCHAR(MAX),
|
||||||
@ResetPasswordKey VARCHAR(MAX),
|
@ResetPasswordKey VARCHAR(MAX),
|
||||||
@Collections AS [dbo].[SelectionReadOnlyArray] READONLY,
|
@Collections AS [dbo].[CollectionAccessSelectionType] READONLY,
|
||||||
@AccessSecretsManager BIT = 0
|
|
||||||
AS
|
|
||||||
BEGIN
|
|
||||||
SET NOCOUNT ON
|
|
||||||
|
|
||||||
EXEC [dbo].[OrganizationUser_Create] @Id, @OrganizationId, @UserId, @Email, @Key, @Status, @Type, @AccessAll, @ExternalId, @CreationDate, @RevisionDate, @Permissions, @ResetPasswordKey, @AccessSecretsManager
|
|
||||||
|
|
||||||
;WITH [AvailableCollectionsCTE] AS(
|
|
||||||
SELECT
|
|
||||||
[Id]
|
|
||||||
FROM
|
|
||||||
[dbo].[Collection]
|
|
||||||
WHERE
|
|
||||||
[OrganizationId] = @OrganizationId
|
|
||||||
)
|
|
||||||
INSERT INTO [dbo].[CollectionUser]
|
|
||||||
(
|
|
||||||
[CollectionId],
|
|
||||||
[OrganizationUserId],
|
|
||||||
[ReadOnly],
|
|
||||||
[HidePasswords],
|
|
||||||
[Manage]
|
|
||||||
)
|
|
||||||
SELECT
|
|
||||||
[Id],
|
|
||||||
@Id,
|
|
||||||
[ReadOnly],
|
|
||||||
[HidePasswords],
|
|
||||||
[Manage]
|
|
||||||
FROM
|
|
||||||
@Collections
|
|
||||||
WHERE
|
|
||||||
[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE])
|
|
||||||
END
|
|
||||||
GO
|
|
||||||
|
|
||||||
CREATE PROCEDURE [dbo].[OrganizationUser_UpdateWithCollections]
|
|
||||||
@Id UNIQUEIDENTIFIER,
|
|
||||||
@OrganizationId UNIQUEIDENTIFIER,
|
|
||||||
@UserId UNIQUEIDENTIFIER,
|
|
||||||
@Email NVARCHAR(256),
|
|
||||||
@Key VARCHAR(MAX),
|
|
||||||
@Status SMALLINT,
|
|
||||||
@Type TINYINT,
|
|
||||||
@AccessAll BIT,
|
|
||||||
@ExternalId NVARCHAR(300),
|
|
||||||
@CreationDate DATETIME2(7),
|
|
||||||
@RevisionDate DATETIME2(7),
|
|
||||||
@Permissions NVARCHAR(MAX),
|
|
||||||
@ResetPasswordKey VARCHAR(MAX),
|
|
||||||
@Collections AS [dbo].[SelectionReadOnlyArray] READONLY,
|
|
||||||
@AccessSecretsManager BIT = 0
|
@AccessSecretsManager BIT = 0
|
||||||
AS
|
AS
|
||||||
BEGIN
|
BEGIN
|
||||||
@ -481,8 +625,14 @@ BEGIN
|
|||||||
)
|
)
|
||||||
|
|
||||||
-- Insert
|
-- Insert
|
||||||
INSERT INTO
|
INSERT INTO [dbo].[CollectionUser]
|
||||||
[dbo].[CollectionUser]
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
SELECT
|
SELECT
|
||||||
[Source].[Id],
|
[Source].[Id],
|
||||||
@Id,
|
@Id,
|
||||||
@ -522,15 +672,112 @@ BEGIN
|
|||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
CREATE PROCEDURE [dbo].[Collection_CreateWithGroupsAndUsers]
|
CREATE OR ALTER PROCEDURE [dbo].[Group_CreateWithCollections_V2]
|
||||||
|
@Id UNIQUEIDENTIFIER,
|
||||||
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
|
@Name NVARCHAR(100),
|
||||||
|
@AccessAll BIT,
|
||||||
|
@ExternalId NVARCHAR(300),
|
||||||
|
@CreationDate DATETIME2(7),
|
||||||
|
@RevisionDate DATETIME2(7),
|
||||||
|
@Collections AS [dbo].[CollectionAccessSelectionType] READONLY
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
EXEC [dbo].[Group_Create] @Id, @OrganizationId, @Name, @AccessAll, @ExternalId, @CreationDate, @RevisionDate
|
||||||
|
|
||||||
|
;WITH [AvailableCollectionsCTE] AS(
|
||||||
|
SELECT
|
||||||
|
[Id]
|
||||||
|
FROM
|
||||||
|
[dbo].[Collection]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @OrganizationId
|
||||||
|
)
|
||||||
|
INSERT INTO [dbo].[CollectionGroup]
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[GroupId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
[Id],
|
||||||
|
@Id,
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
FROM
|
||||||
|
@Collections
|
||||||
|
WHERE
|
||||||
|
[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE])
|
||||||
|
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
CREATE OR ALTER PROCEDURE [dbo].[OrganizationUser_CreateWithCollections_V2]
|
||||||
|
@Id UNIQUEIDENTIFIER,
|
||||||
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
|
@UserId UNIQUEIDENTIFIER,
|
||||||
|
@Email NVARCHAR(256),
|
||||||
|
@Key VARCHAR(MAX),
|
||||||
|
@Status SMALLINT,
|
||||||
|
@Type TINYINT,
|
||||||
|
@AccessAll BIT,
|
||||||
|
@ExternalId NVARCHAR(300),
|
||||||
|
@CreationDate DATETIME2(7),
|
||||||
|
@RevisionDate DATETIME2(7),
|
||||||
|
@Permissions NVARCHAR(MAX),
|
||||||
|
@ResetPasswordKey VARCHAR(MAX),
|
||||||
|
@Collections AS [dbo].[CollectionAccessSelectionType] READONLY,
|
||||||
|
@AccessSecretsManager BIT = 0
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
EXEC [dbo].[OrganizationUser_Create] @Id, @OrganizationId, @UserId, @Email, @Key, @Status, @Type, @AccessAll, @ExternalId, @CreationDate, @RevisionDate, @Permissions, @ResetPasswordKey, @AccessSecretsManager
|
||||||
|
|
||||||
|
;WITH [AvailableCollectionsCTE] AS(
|
||||||
|
SELECT
|
||||||
|
[Id]
|
||||||
|
FROM
|
||||||
|
[dbo].[Collection]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @OrganizationId
|
||||||
|
)
|
||||||
|
INSERT INTO [dbo].[CollectionUser]
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
[Id],
|
||||||
|
@Id,
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
FROM
|
||||||
|
@Collections
|
||||||
|
WHERE
|
||||||
|
[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE])
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
CREATE OR ALTER PROCEDURE [dbo].[Collection_CreateWithGroupsAndUsers_V2]
|
||||||
@Id UNIQUEIDENTIFIER,
|
@Id UNIQUEIDENTIFIER,
|
||||||
@OrganizationId UNIQUEIDENTIFIER,
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
@Name VARCHAR(MAX),
|
@Name VARCHAR(MAX),
|
||||||
@ExternalId NVARCHAR(300),
|
@ExternalId NVARCHAR(300),
|
||||||
@CreationDate DATETIME2(7),
|
@CreationDate DATETIME2(7),
|
||||||
@RevisionDate DATETIME2(7),
|
@RevisionDate DATETIME2(7),
|
||||||
@Groups AS [dbo].[SelectionReadOnlyArray] READONLY,
|
@Groups AS [dbo].[CollectionAccessSelectionType] READONLY,
|
||||||
@Users AS [dbo].[SelectionReadOnlyArray] READONLY
|
@Users AS [dbo].[CollectionAccessSelectionType] READONLY
|
||||||
AS
|
AS
|
||||||
BEGIN
|
BEGIN
|
||||||
SET NOCOUNT ON
|
SET NOCOUNT ON
|
||||||
@ -596,199 +843,3 @@ BEGIN
|
|||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
|
||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
|
||||||
CREATE OR ALTER PROCEDURE [dbo].[OrganizationUserUserDetails_ReadWithCollectionsById]
|
|
||||||
@Id UNIQUEIDENTIFIER
|
|
||||||
AS
|
|
||||||
BEGIN
|
|
||||||
SET NOCOUNT ON
|
|
||||||
|
|
||||||
EXEC [OrganizationUserUserDetails_ReadById] @Id
|
|
||||||
|
|
||||||
SELECT
|
|
||||||
CU.[CollectionId] Id,
|
|
||||||
CU.[ReadOnly],
|
|
||||||
CU.[HidePasswords],
|
|
||||||
CU.[Manage]
|
|
||||||
FROM
|
|
||||||
[dbo].[OrganizationUser] OU
|
|
||||||
INNER JOIN
|
|
||||||
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[OrganizationUserId] = [OU].[Id]
|
|
||||||
WHERE
|
|
||||||
[OrganizationUserId] = @Id
|
|
||||||
END
|
|
||||||
GO
|
|
||||||
|
|
||||||
CREATE OR ALTER FUNCTION [dbo].[UserCollectionDetails](@UserId UNIQUEIDENTIFIER)
|
|
||||||
RETURNS TABLE
|
|
||||||
AS RETURN
|
|
||||||
SELECT
|
|
||||||
C.*,
|
|
||||||
CASE
|
|
||||||
WHEN
|
|
||||||
OU.[AccessAll] = 1
|
|
||||||
OR G.[AccessAll] = 1
|
|
||||||
OR COALESCE(CU.[ReadOnly], CG.[ReadOnly], 0) = 0
|
|
||||||
THEN 0
|
|
||||||
ELSE 1
|
|
||||||
END [ReadOnly],
|
|
||||||
CASE
|
|
||||||
WHEN
|
|
||||||
OU.[AccessAll] = 1
|
|
||||||
OR G.[AccessAll] = 1
|
|
||||||
OR COALESCE(CU.[HidePasswords], CG.[HidePasswords], 0) = 0
|
|
||||||
THEN 0
|
|
||||||
ELSE 1
|
|
||||||
END [HidePasswords],
|
|
||||||
CASE
|
|
||||||
WHEN
|
|
||||||
OU.[AccessAll] = 1
|
|
||||||
OR G.[AccessAll] = 1
|
|
||||||
OR COALESCE(CU.[Manage], CG.[Manage], 0) = 0
|
|
||||||
THEN 0
|
|
||||||
ELSE 1
|
|
||||||
END [Manage]
|
|
||||||
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
|
|
||||||
)
|
|
||||||
GO
|
|
||||||
|
|
||||||
CREATE OR ALTER PROCEDURE [dbo].[Collection_ReadByIdUserId]
|
|
||||||
@Id UNIQUEIDENTIFIER,
|
|
||||||
@UserId UNIQUEIDENTIFIER
|
|
||||||
AS
|
|
||||||
BEGIN
|
|
||||||
SET NOCOUNT ON
|
|
||||||
SELECT
|
|
||||||
Id,
|
|
||||||
OrganizationId,
|
|
||||||
[Name],
|
|
||||||
CreationDate,
|
|
||||||
RevisionDate,
|
|
||||||
ExternalId,
|
|
||||||
MIN([ReadOnly]) AS [ReadOnly],
|
|
||||||
MIN([HidePasswords]) AS [HidePasswords],
|
|
||||||
MIN([Manage]) AS [Manage]
|
|
||||||
FROM
|
|
||||||
[dbo].[UserCollectionDetails](@UserId)
|
|
||||||
WHERE
|
|
||||||
[Id] = @Id
|
|
||||||
GROUP BY
|
|
||||||
Id,
|
|
||||||
OrganizationId,
|
|
||||||
[Name],
|
|
||||||
CreationDate,
|
|
||||||
RevisionDate,
|
|
||||||
ExternalId
|
|
||||||
END
|
|
||||||
GO
|
|
||||||
|
|
||||||
CREATE OR ALTER PROCEDURE [dbo].[Collection_ReadByUserId]
|
|
||||||
@UserId UNIQUEIDENTIFIER
|
|
||||||
AS
|
|
||||||
BEGIN
|
|
||||||
SET NOCOUNT ON
|
|
||||||
|
|
||||||
SELECT
|
|
||||||
Id,
|
|
||||||
OrganizationId,
|
|
||||||
[Name],
|
|
||||||
CreationDate,
|
|
||||||
RevisionDate,
|
|
||||||
ExternalId,
|
|
||||||
MIN([ReadOnly]) AS [ReadOnly],
|
|
||||||
MIN([HidePasswords]) AS [HidePasswords],
|
|
||||||
MIN([Manage]) AS [Manage]
|
|
||||||
FROM
|
|
||||||
[dbo].[UserCollectionDetails](@UserId)
|
|
||||||
GROUP BY
|
|
||||||
Id,
|
|
||||||
OrganizationId,
|
|
||||||
[Name],
|
|
||||||
CreationDate,
|
|
||||||
RevisionDate,
|
|
||||||
ExternalId
|
|
||||||
END
|
|
||||||
GO
|
|
||||||
|
|
||||||
CREATE OR ALTER PROCEDURE [dbo].[Collection_ReadWithGroupsAndUsersByUserId]
|
|
||||||
@UserId UNIQUEIDENTIFIER
|
|
||||||
AS
|
|
||||||
BEGIN
|
|
||||||
SET NOCOUNT ON
|
|
||||||
|
|
||||||
DECLARE @TempUserCollections TABLE(
|
|
||||||
Id UNIQUEIDENTIFIER,
|
|
||||||
OrganizationId UNIQUEIDENTIFIER,
|
|
||||||
Name VARCHAR(MAX),
|
|
||||||
CreationDate DATETIME2(7),
|
|
||||||
RevisionDate DATETIME2(7),
|
|
||||||
ExternalId NVARCHAR(300),
|
|
||||||
ReadOnly BIT,
|
|
||||||
HidePasswords BIT,
|
|
||||||
Manage BIT)
|
|
||||||
|
|
||||||
INSERT INTO @TempUserCollections EXEC [dbo].[Collection_ReadByUserId] @UserId
|
|
||||||
|
|
||||||
SELECT
|
|
||||||
*
|
|
||||||
FROM
|
|
||||||
@TempUserCollections C
|
|
||||||
|
|
||||||
SELECT
|
|
||||||
CG.*
|
|
||||||
FROM
|
|
||||||
[dbo].[CollectionGroup] CG
|
|
||||||
INNER JOIN
|
|
||||||
@TempUserCollections C ON C.[Id] = CG.[CollectionId]
|
|
||||||
|
|
||||||
SELECT
|
|
||||||
CU.*
|
|
||||||
FROM
|
|
||||||
[dbo].[CollectionUser] CU
|
|
||||||
INNER JOIN
|
|
||||||
@TempUserCollections C ON C.[Id] = CU.[CollectionId]
|
|
||||||
|
|
||||||
END
|
|
||||||
GO
|
|
||||||
|
|
||||||
CREATE OR ALTER PROCEDURE [dbo].[Group_ReadWithCollectionsById]
|
|
||||||
@Id UNIQUEIDENTIFIER
|
|
||||||
AS
|
|
||||||
BEGIN
|
|
||||||
SET NOCOUNT ON
|
|
||||||
|
|
||||||
EXEC [dbo].[Group_ReadById] @Id
|
|
||||||
|
|
||||||
SELECT
|
|
||||||
[CollectionId] [Id],
|
|
||||||
[ReadOnly],
|
|
||||||
[HidePasswords],
|
|
||||||
[Manage]
|
|
||||||
FROM
|
|
||||||
[dbo].[CollectionGroup]
|
|
||||||
WHERE
|
|
||||||
[GroupId] = @Id
|
|
||||||
END
|
|
||||||
GO
|
|
@ -38,8 +38,8 @@ GO
|
|||||||
CREATE OR ALTER PROCEDURE [dbo].[Collection_CreateOrUpdateAccessForMany]
|
CREATE OR ALTER PROCEDURE [dbo].[Collection_CreateOrUpdateAccessForMany]
|
||||||
@OrganizationId UNIQUEIDENTIFIER,
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
@CollectionIds AS [dbo].[GuidIdArray] READONLY,
|
@CollectionIds AS [dbo].[GuidIdArray] READONLY,
|
||||||
@Groups AS [dbo].[SelectionReadOnlyArray] READONLY,
|
@Groups AS [dbo].[CollectionAccessSelectionType] READONLY,
|
||||||
@Users AS [dbo].[SelectionReadOnlyArray] READONLY
|
@Users AS [dbo].[CollectionAccessSelectionType] READONLY
|
||||||
AS
|
AS
|
||||||
BEGIN
|
BEGIN
|
||||||
SET NOCOUNT ON
|
SET NOCOUNT ON
|
||||||
@ -78,7 +78,15 @@ BEGIN
|
|||||||
[Target].[HidePasswords] = [Source].[HidePasswords],
|
[Target].[HidePasswords] = [Source].[HidePasswords],
|
||||||
[Target].[Manage] = [Source].[Manage]
|
[Target].[Manage] = [Source].[Manage]
|
||||||
WHEN NOT MATCHED BY TARGET
|
WHEN NOT MATCHED BY TARGET
|
||||||
THEN INSERT VALUES
|
THEN INSERT
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[GroupId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
(
|
(
|
||||||
[Source].[CollectionId],
|
[Source].[CollectionId],
|
||||||
[Source].[GroupId],
|
[Source].[GroupId],
|
||||||
@ -121,7 +129,15 @@ BEGIN
|
|||||||
[Target].[HidePasswords] = [Source].[HidePasswords],
|
[Target].[HidePasswords] = [Source].[HidePasswords],
|
||||||
[Target].[Manage] = [Source].[Manage]
|
[Target].[Manage] = [Source].[Manage]
|
||||||
WHEN NOT MATCHED BY TARGET
|
WHEN NOT MATCHED BY TARGET
|
||||||
THEN INSERT VALUES
|
THEN INSERT
|
||||||
|
(
|
||||||
|
[CollectionId],
|
||||||
|
[OrganizationUserId],
|
||||||
|
[ReadOnly],
|
||||||
|
[HidePasswords],
|
||||||
|
[Manage]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
(
|
(
|
||||||
[Source].[CollectionId],
|
[Source].[CollectionId],
|
||||||
[Source].[OrganizationUserId],
|
[Source].[OrganizationUserId],
|
@ -0,0 +1,50 @@
|
|||||||
|
-- Remove old stored procedures and SelectionReadOnlyArray for Flexible Collections
|
||||||
|
-- They have been superseded via their respective _V2 variants and the CollectionAccessSelectionType
|
||||||
|
|
||||||
|
IF OBJECT_ID('[dbo].[CollectionUser_UpdateUsers]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[CollectionUser_UpdateUsers]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
IF OBJECT_ID('[dbo].[Group_UpdateWithCollections]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[Group_UpdateWithCollections]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
IF OBJECT_ID('[dbo].[Collection_UpdateWithGroupsAndUsers]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[Collection_UpdateWithGroupsAndUsers]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
IF OBJECT_ID('[dbo].[OrganizationUser_UpdateWithCollections]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[OrganizationUser_UpdateWithCollections]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
IF OBJECT_ID('[dbo].[Group_CreateWithCollections]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[Group_CreateWithCollections]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
IF OBJECT_ID('[dbo].[OrganizationUser_CreateWithCollections]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[OrganizationUser_CreateWithCollections]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
IF OBJECT_ID('[dbo].[Collection_CreateWithGroupsAndUsers]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[Collection_CreateWithGroupsAndUsers]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
IF TYPE_ID('[dbo].[SelectionReadOnlyArray]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP TYPE [dbo].[SelectionReadOnlyArray]
|
||||||
|
END
|
||||||
|
GO
|
Reference in New Issue
Block a user