feat: remove user from groups on org membership delete (#14701)

* feat: remove user from groups on org membership delete

Groups inherently provide authz access to certain resources. If a
user is removed from an organization, they should be removed
from all their groups in said organization.
This commit is contained in:
Steven Masley
2024-09-17 19:41:34 -05:00
committed by GitHub
parent c145f113fe
commit 1e5438eadb
6 changed files with 196 additions and 1 deletions
+21
View File
@@ -286,6 +286,25 @@ BEGIN
END;
$$;
CREATE FUNCTION delete_group_members_on_org_member_delete() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
BEGIN
-- Remove the user from all groups associated with the same
-- organization as the organization_member being deleted.
DELETE FROM group_members
WHERE
user_id = OLD.user_id
AND group_id IN (
SELECT id
FROM groups
WHERE organization_id = OLD.organization_id
);
RETURN OLD;
END;
$$;
CREATE FUNCTION inhibit_enqueue_if_disabled() RETURNS trigger
LANGUAGE plpgsql
AS $$
@@ -2041,6 +2060,8 @@ CREATE TRIGGER tailnet_notify_peer_change AFTER INSERT OR DELETE OR UPDATE ON ta
CREATE TRIGGER tailnet_notify_tunnel_change AFTER INSERT OR DELETE OR UPDATE ON tailnet_tunnels FOR EACH ROW EXECUTE FUNCTION tailnet_notify_tunnel_change();
CREATE TRIGGER trigger_delete_group_members_on_org_member_delete BEFORE DELETE ON organization_members FOR EACH ROW EXECUTE FUNCTION delete_group_members_on_org_member_delete();
CREATE TRIGGER trigger_delete_oauth2_provider_app_token AFTER DELETE ON oauth2_provider_app_tokens FOR EACH ROW EXECUTE FUNCTION delete_deleted_oauth2_provider_app_token_api_key();
CREATE TRIGGER trigger_insert_apikeys BEFORE INSERT ON api_keys FOR EACH ROW EXECUTE FUNCTION insert_apikey_fail_if_user_deleted();