-
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.
- Loading branch information
1 parent
2cdd1c9
commit 22c8d64
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
-- Could not auto-generate a down migration. | ||
-- Please write an appropriate down migration for the SQL below: | ||
-- 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; |
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)'; |