-
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.
feat: Do not automatically assign
demoUser
s the teamEditor
role w…
…ithin the Templates team (#3878)
- Loading branch information
1 parent
e4900c9
commit a318850
Showing
5 changed files
with
128 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,48 @@ | ||
import { TEST_EMAIL } from "../../../ui-driven/src/helpers/globalHelpers"; | ||
import { $admin } from "../client"; | ||
import { safely } from "../globalHelpers"; | ||
import gql from "graphql-tag"; | ||
|
||
export const cleanup = async () => { | ||
await $admin.user._destroyAll(); | ||
await $admin.team._destroyAll(); | ||
}; | ||
|
||
export async function createDemoUser(demoTeamId: number) { | ||
const variables = { | ||
firstName: "Test", | ||
lastName: "Test", | ||
email: TEST_EMAIL, | ||
teamId: demoTeamId, | ||
role: "demoUser", | ||
}; | ||
|
||
const response = await safely(() => | ||
$admin.client.request<{ insertUsersOne: { id: number } }>( | ||
gql` | ||
mutation CreateAndAddUserToTeam( | ||
$email: String! | ||
$firstName: String! | ||
$lastName: String! | ||
$teamId: Int! | ||
$role: user_roles_enum! | ||
) { | ||
insertUsersOne: insert_users_one( | ||
object: { | ||
email: $email | ||
first_name: $firstName | ||
last_name: $lastName | ||
teams: { data: { role: $role, team_id: $teamId } } | ||
} | ||
) { | ||
id | ||
} | ||
} | ||
`, | ||
variables, | ||
), | ||
); | ||
|
||
const userId = response.insertUsersOne.id; | ||
return userId; | ||
} |
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
22 changes: 22 additions & 0 deletions
22
hasura.planx.uk/migrations/1730277860890_run_sql_migration/down.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,22 @@ | ||
-- Previous definition from 1696884217237_grant_new_user_template_team_access/up.sql | ||
|
||
CREATE OR REPLACE FUNCTION grant_new_user_template_team_access() RETURNS trigger AS $$ | ||
DECLARE | ||
templates_team_id INT; | ||
BEGIN | ||
SELECT id INTO templates_team_id FROM teams WHERE slug = 'templates'; | ||
IF templates_team_id IS NOT NULL THEN | ||
INSERT INTO team_members (user_id, team_id, role) VALUES (NEW.id, templates_team_id, 'teamEditor'); | ||
END IF; | ||
|
||
RETURN NULL; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER grant_new_user_template_team_access ON users; | ||
|
||
CREATE TRIGGER grant_new_user_template_team_access AFTER INSERT ON users | ||
FOR EACH ROW EXECUTE PROCEDURE grant_new_user_template_team_access(); | ||
|
||
COMMENT ON TRIGGER grant_new_user_template_team_access ON users | ||
IS 'Automatically grant all new users teamEditor access to the shared Templates team'; |
36 changes: 36 additions & 0 deletions
36
hasura.planx.uk/migrations/1730277860890_run_sql_migration/up.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,36 @@ | ||
CREATE OR REPLACE FUNCTION grant_new_user_template_access() RETURNS trigger AS $$ | ||
DECLARE | ||
templates_team_id INT; | ||
is_demo_user BOOLEAN; | ||
BEGIN | ||
SELECT EXISTS ( | ||
SELECT 1 | ||
FROM team_members | ||
WHERE user_id = NEW.id | ||
AND role = 'demoUser' | ||
) INTO is_demo_user; | ||
|
||
-- Demo user should not get access as a teamEditor for the templates team... | ||
IF is_demo_user THEN | ||
RETURN NULL; | ||
END IF; | ||
|
||
-- ...but all other users should | ||
SELECT id INTO templates_team_id FROM teams WHERE slug = 'templates'; | ||
IF templates_team_id IS NOT NULL THEN | ||
INSERT INTO team_members (user_id, team_id, role) | ||
VALUES (NEW.id, templates_team_id, 'teamEditor'); | ||
END IF; | ||
|
||
RETURN NULL; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
DROP TRIGGER IF EXISTS grant_new_user_template_team_access ON users; | ||
|
||
CREATE CONSTRAINT TRIGGER grant_new_user_template_team_access | ||
AFTER INSERT ON users | ||
DEFERRABLE INITIALLY DEFERRED | ||
FOR EACH ROW EXECUTE FUNCTION grant_new_user_template_access(); | ||
|
||
COMMENT ON TRIGGER grant_new_user_template_team_access ON users IS 'Automatically grant all new users teamEditor access to the shared Templates team (apart from users with the demoUser role)'; |