-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Users in our system can be associated with the three types of organization in the PDC (changemakers, funders, and data providers). These associations will allow them access to perform various actions in the context of those organizations. For instance, reading data, writing data, or managing other user associations. This list of abilities may change in future. We explored the concept of `user_roles` with foreign keys to different organization types (similar to the sources table) but decided to have three separate tables because they are slightly distinct concepts. For instance, there will probably be certain access types that only apply to certain types of organization in future. Another design decision was to have the permissions in terms of granted access type rather than higher level role. For instance, instead of roles like "administrator" and "editor" we have action oriented roles like "read" and "manage". This provides more granularity and is also more explicit about what a given role / access type actually allows. Issue #1250 Support associations between users and organizational entities
- Loading branch information
Showing
26 changed files
with
642 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
SELECT drop_function('changemaker_role_to_json'); | ||
|
||
CREATE FUNCTION changemaker_role_to_json(changemaker_role changemaker_roles) | ||
RETURNS JSONB AS $$ | ||
BEGIN | ||
RETURN jsonb_build_object( | ||
'userKeycloakUserId', changemaker_role.user_keycloak_user_id, | ||
'changemakerId', changemaker_role.changemaker_id, | ||
'accessType', changemaker_role.access_type, | ||
'createdBy', changemaker_role.created_by, | ||
'createdAt', changemaker_role.created_at | ||
); | ||
END; | ||
$$ LANGUAGE plpgsql; |
14 changes: 14 additions & 0 deletions
14
src/database/initialization/data_provider_role_to_json.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
SELECT drop_function('data_provider_role_to_json'); | ||
|
||
CREATE FUNCTION data_provider_role_to_json(data_provider_role data_provider_roles) | ||
RETURNS JSONB AS $$ | ||
BEGIN | ||
RETURN jsonb_build_object( | ||
'userKeycloakUserId', data_provider_role.user_keycloak_user_id, | ||
'dataProviderShortCode', data_provider_role.data_provider_short_code, | ||
'accessType', data_provider_role.access_type, | ||
'createdBy', data_provider_role.created_by, | ||
'createdAt', data_provider_role.created_at | ||
); | ||
END; | ||
$$ LANGUAGE plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
SELECT drop_function('funder_role_to_json'); | ||
|
||
CREATE FUNCTION funder_role_to_json(funder_role funder_roles) | ||
RETURNS JSONB AS $$ | ||
BEGIN | ||
RETURN jsonb_build_object( | ||
'userKeycloakUserId', funder_role.user_keycloak_user_id, | ||
'funderShortCode', funder_role.funder_short_code, | ||
'accessType', funder_role.access_type, | ||
'createdBy', funder_role.created_by, | ||
'createdAt', funder_role.created_at | ||
); | ||
END; | ||
$$ LANGUAGE plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
CREATE TYPE access_type_t AS ENUM ( | ||
'manage', | ||
'edit', | ||
'view' | ||
); | ||
|
||
CREATE TABLE changemaker_roles ( | ||
user_keycloak_user_id UUID NOT NULL, | ||
changemaker_id INT NOT NULL, | ||
access_type access_type_t NOT NULL, | ||
created_by UUID NOT NULL, | ||
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), | ||
PRIMARY KEY (user_keycloak_user_id, changemaker_id, access_type), | ||
FOREIGN KEY (created_by) REFERENCES users(keycloak_user_id) ON DELETE CASCADE, | ||
FOREIGN KEY (user_keycloak_user_id) REFERENCES users(keycloak_user_id) ON DELETE CASCADE, | ||
FOREIGN KEY (changemaker_id) REFERENCES changemakers(id) ON DELETE CASCADE | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
CREATE TABLE funder_roles ( | ||
user_keycloak_user_id UUID NOT NULL, | ||
funder_short_code short_code_t NOT NULL, | ||
access_type access_type_t NOT NULL, | ||
created_by UUID NOT NULL, | ||
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), | ||
PRIMARY KEY (user_keycloak_user_id, funder_short_code, access_type), | ||
FOREIGN KEY (created_by) REFERENCES users(keycloak_user_id) ON DELETE CASCADE, | ||
FOREIGN KEY (user_keycloak_user_id) REFERENCES users(keycloak_user_id) ON DELETE CASCADE, | ||
FOREIGN KEY (funder_short_code) REFERENCES funders(short_code) ON DELETE CASCADE | ||
); |
11 changes: 11 additions & 0 deletions
11
src/database/migrations/0042-create-data_provider_roles.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
CREATE TABLE data_provider_roles ( | ||
user_keycloak_user_id UUID NOT NULL, | ||
data_provider_short_code short_code_t NOT NULL, | ||
access_type access_type_t NOT NULL, | ||
created_by UUID NOT NULL, | ||
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), | ||
PRIMARY KEY (user_keycloak_user_id, data_provider_short_code, access_type), | ||
FOREIGN KEY (created_by) REFERENCES users(keycloak_user_id) ON DELETE CASCADE, | ||
FOREIGN KEY (user_keycloak_user_id) REFERENCES users(keycloak_user_id) ON DELETE CASCADE, | ||
FOREIGN KEY (data_provider_short_code) REFERENCES data_providers(short_code) ON DELETE CASCADE | ||
); |
32 changes: 32 additions & 0 deletions
32
src/database/operations/changemakerRoles/createOrUpdateChangemakerRole.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { db } from '../../db'; | ||
import type { | ||
DataProviderRole, | ||
InternallyWritableChangemakerRole, | ||
JsonResultSet, | ||
} from '../../../types'; | ||
|
||
const createOrUpdateChangemakerRole = async ( | ||
createValues: InternallyWritableChangemakerRole, | ||
): Promise<DataProviderRole> => { | ||
const { userKeycloakUserId, changemakerId, accessType, createdBy } = | ||
createValues; | ||
const result = await db.sql<JsonResultSet<DataProviderRole>>( | ||
'changemakerRoles.insertOrUpdateOne', | ||
{ | ||
changemakerId, | ||
userKeycloakUserId, | ||
accessType, | ||
createdBy, | ||
}, | ||
); | ||
|
||
const { object } = result.rows[0] ?? {}; | ||
if (object === undefined) { | ||
throw new Error( | ||
'The entity creation did not appear to fail, but no data was returned by the operation.', | ||
); | ||
} | ||
return object; | ||
}; | ||
|
||
export { createOrUpdateChangemakerRole }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './createOrUpdateChangemakerRole'; |
32 changes: 32 additions & 0 deletions
32
src/database/operations/dataProviderRoles/createOrUpdateDataProviderRole.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { db } from '../../db'; | ||
import type { | ||
DataProviderRole, | ||
InternallyWritableDataProviderRole, | ||
JsonResultSet, | ||
} from '../../../types'; | ||
|
||
const createOrUpdateDataProviderRole = async ( | ||
createValues: InternallyWritableDataProviderRole, | ||
): Promise<DataProviderRole> => { | ||
const { userKeycloakUserId, dataProviderShortCode, accessType, createdBy } = | ||
createValues; | ||
const result = await db.sql<JsonResultSet<DataProviderRole>>( | ||
'dataProviderRoles.insertOrUpdateOne', | ||
{ | ||
dataProviderShortCode, | ||
userKeycloakUserId, | ||
accessType, | ||
createdBy, | ||
}, | ||
); | ||
|
||
const { object } = result.rows[0] ?? {}; | ||
if (object === undefined) { | ||
throw new Error( | ||
'The entity creation did not appear to fail, but no data was returned by the operation.', | ||
); | ||
} | ||
return object; | ||
}; | ||
|
||
export { createOrUpdateDataProviderRole }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './createOrUpdateDataProviderRole'; |
Oops, something went wrong.