Skip to content

Commit

Permalink
feat: Update trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Oct 30, 2024
1 parent 2cdd1c9 commit 22c8d64
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
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 hasura.planx.uk/migrations/1730277860890_run_sql_migration/up.sql
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)';

0 comments on commit 22c8d64

Please sign in to comment.