Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate triggers to apply permissions in sqlite #1071

Draft
wants to merge 5 commits into
base: garry/vax-1425-create-permissions-protobuf-definitions-and-write-validation
Choose a base branch
from

Conversation

magnetised
Copy link
Contributor

@magnetised magnetised commented Mar 14, 2024

Example trigger sql:

-- example perms check for insert with project scoped table
CREATE TRIGGER "__electric_permissions__comments_INSERT"
    BEFORE INSERT ON "comments"
    FOR EACH ROW
    WHEN NOT (
        SELECT CASE
            WHEN (
                WITH __scope__ AS (
                    SELECT "projects"."id" AS pk0
                        FROM "projects"
                        LEFT JOIN "issues" ON
                            "issues"."project_id" = "projects"."id"
                        WHERE
                            "issues"."id" = NEW."issue_id"
                        LIMIT 1
                ) SELECT CASE
                    WHEN (
                        WITH __tomb__ AS (
                            SELECT row_id FROM __electric_local_roles_tombstone
                                WHERE assign_id IS 'assign-1'
                        )
                        SELECT (
                            (
                                ('p1') = (SELECT "pk0" FROM __scope__)
                                AND ('["pm1"]' NOT IN (SELECT row_id FROM __tomb__))
                            )
                        )
                    ) THEN TRUE
                    WHEN (
                        SELECT 1 FROM "__electric_local_roles"
                            WHERE (scope = 'projects')
                            AND (scope_id = (SELECT json_array("pk0") FROM __scope__))
                            AND (role = 'manager')
                    ) THEN TRUE
                    ELSE FALSE
                END
            ) THEN TRUE
            ELSE FALSE
        END
    )
BEGIN
    SELECT RAISE(ROLLBACK, 'does not have matching INSERT permissions on "comments"');
END;

-- validate scope moves
CREATE TRIGGER "__electric_permissions__comments_UPDATE_scope_move"
    BEFORE UPDATE OF "issue_id" ON "comments"
    FOR EACH ROW
    WHEN NOT (
        WITH
             __scope__ AS (
                SELECT "projects"."id" AS pk0
                    FROM "projects"
                    LEFT JOIN "issues" ON
                        "issues"."project_id" = "projects"."id"
                    WHERE
                        "issues"."id" = NEW."issue_id"
                    LIMIT 1
            ),
            "__tomb__assign-1" AS (
                SELECT row_id FROM __electric_local_roles_tombstone WHERE assign_id IS 'assign-1'
            )
        SELECT CASE
            WHEN (
                (('p1') = (SELECT pk0 FROM __scope__))
                AND ('["pm1"]' NOT IN (SELECT row_id FROM "__tomb__assign-1"))
            ) THEN TRUE
            WHEN (
                SELECT 1 FROM "__electric_local_roles"
                    WHERE (scope = 'projects')
                    AND (scope_id = (SELECT json_array("pk0") FROM __scope__))
                    AND (role = 'manager')
            ) THEN TRUE
            ELSE FALSE
        END
    )
BEGIN
    SELECT RAISE(ROLLBACK, 'does not have matching UPDATE permissions in new scope on "comments"');
END;

-- reject updates to table pk

CREATE TRIGGER "__electric_permissions__comments_UPDATE_protect_pk"
    BEFORE UPDATE OF "id" ON "comments"
    FOR EACH ROW
BEGIN
    SELECT RAISE(ROLLBACK, 'invalid update of primary key on "comments"');
END;

-- local role creation based on assigns

CREATE TRIGGER "__electric_permissions__project_memberships_INSERT_assign_assign-1_0"
    BEFORE INSERT ON "project_memberships"
    FOR EACH ROW WHEN (
        (NEW."user_id" IS '92bafe18-a818-4a3f-874f-590324140478')
    )
BEGIN
    INSERT INTO "__electric_local_roles"
        (assign_id, row_id, scope, scope_id, role)
    VALUES (
        'assign-1',
        json_array(NEW."id"),
        'projects',
        json_array(NEW."project_id"),
        NEW."role"
    );
END;

Copy link

linear bot commented Mar 14, 2024

@magnetised magnetised force-pushed the garry/vax-1713-experiment-with-implementing-permissions-as-sqlite-triggers branch 9 times, most recently from 968faf0 to 6ff7a53 Compare March 20, 2024 12:18
@magnetised magnetised marked this pull request as ready for review March 20, 2024 13:19
@magnetised magnetised force-pushed the garry/vax-1713-experiment-with-implementing-permissions-as-sqlite-triggers branch 2 times, most recently from 0dc42fe to c76781c Compare March 20, 2024 13:39
@magnetised magnetised force-pushed the garry/vax-1645-merge-grant-statements-into-a-global-state branch from bd5a92f to 1f3dec2 Compare March 20, 2024 15:46
@magnetised magnetised force-pushed the garry/vax-1713-experiment-with-implementing-permissions-as-sqlite-triggers branch from c76781c to ae8b532 Compare March 20, 2024 15:46
@magnetised magnetised force-pushed the garry/vax-1645-merge-grant-statements-into-a-global-state branch from 1f3dec2 to 950b2e9 Compare March 20, 2024 16:14
@magnetised magnetised force-pushed the garry/vax-1713-experiment-with-implementing-permissions-as-sqlite-triggers branch 2 times, most recently from 8d355ca to 8ecbacc Compare March 20, 2024 16:16
@magnetised magnetised force-pushed the garry/vax-1645-merge-grant-statements-into-a-global-state branch from 950b2e9 to 6813f2d Compare March 21, 2024 10:48
@magnetised magnetised force-pushed the garry/vax-1713-experiment-with-implementing-permissions-as-sqlite-triggers branch 2 times, most recently from ef2fae8 to 4892645 Compare March 21, 2024 10:55
@magnetised magnetised force-pushed the garry/vax-1645-merge-grant-statements-into-a-global-state branch from 6813f2d to b042b8f Compare March 21, 2024 11:01
@magnetised magnetised force-pushed the garry/vax-1713-experiment-with-implementing-permissions-as-sqlite-triggers branch 2 times, most recently from c084362 to 6a39868 Compare March 21, 2024 11:18
@magnetised magnetised force-pushed the garry/vax-1645-merge-grant-statements-into-a-global-state branch from b042b8f to b601f09 Compare March 21, 2024 11:33
@magnetised magnetised force-pushed the garry/vax-1713-experiment-with-implementing-permissions-as-sqlite-triggers branch from 6a39868 to ff48c3c Compare March 21, 2024 11:34
@magnetised magnetised force-pushed the garry/vax-1645-merge-grant-statements-into-a-global-state branch from b601f09 to 682f294 Compare April 16, 2024 11:14
@magnetised magnetised force-pushed the garry/vax-1713-experiment-with-implementing-permissions-as-sqlite-triggers branch from ff48c3c to 3451e6b Compare April 16, 2024 11:14
@magnetised magnetised force-pushed the garry/vax-1645-merge-grant-statements-into-a-global-state branch from 682f294 to bca436c Compare April 16, 2024 12:35
@magnetised magnetised force-pushed the garry/vax-1713-experiment-with-implementing-permissions-as-sqlite-triggers branch from 3451e6b to 973f4c2 Compare April 16, 2024 12:35
@magnetised magnetised force-pushed the garry/vax-1645-merge-grant-statements-into-a-global-state branch from bca436c to db30dca Compare April 16, 2024 12:45
@magnetised magnetised force-pushed the garry/vax-1713-experiment-with-implementing-permissions-as-sqlite-triggers branch from 973f4c2 to 0185d1e Compare April 16, 2024 12:46
@magnetised magnetised force-pushed the garry/vax-1645-merge-grant-statements-into-a-global-state branch from db30dca to eb73260 Compare April 16, 2024 13:26
@magnetised magnetised force-pushed the garry/vax-1713-experiment-with-implementing-permissions-as-sqlite-triggers branch from 0185d1e to 7a9aa4a Compare April 16, 2024 13:26
@magnetised magnetised force-pushed the garry/vax-1645-merge-grant-statements-into-a-global-state branch from eb73260 to bd8efe6 Compare April 16, 2024 15:15
@magnetised magnetised force-pushed the garry/vax-1713-experiment-with-implementing-permissions-as-sqlite-triggers branch from 7a9aa4a to 3b44825 Compare April 16, 2024 15:15
@magnetised magnetised changed the base branch from garry/vax-1645-merge-grant-statements-into-a-global-state to garry/vax-1439-where-clauses April 17, 2024 07:51
@magnetised magnetised force-pushed the garry/vax-1713-experiment-with-implementing-permissions-as-sqlite-triggers branch from 3b44825 to 3028553 Compare April 17, 2024 07:51
Copy link
Member

@alco alco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯

Base automatically changed from garry/vax-1439-where-clauses to garry/vax-1645-merge-grant-statements-into-a-global-state April 23, 2024 09:03
Base automatically changed from garry/vax-1645-merge-grant-statements-into-a-global-state to garry/vax-1644-serialise-grant-statements-to-protobuf April 23, 2024 09:04
Base automatically changed from garry/vax-1644-serialise-grant-statements-to-protobuf to garry/vax-1425-create-permissions-protobuf-definitions-and-write-validation April 23, 2024 09:05
@magnetised magnetised marked this pull request as draft April 23, 2024 09:05
@magnetised magnetised force-pushed the garry/vax-1425-create-permissions-protobuf-definitions-and-write-validation branch from 40308b1 to c8d6cc8 Compare April 23, 2024 09:31
@magnetised magnetised force-pushed the garry/vax-1425-create-permissions-protobuf-definitions-and-write-validation branch from 3f6681e to 919a4d6 Compare May 2, 2024 15:11
@magnetised magnetised force-pushed the garry/vax-1425-create-permissions-protobuf-definitions-and-write-validation branch 5 times, most recently from cefbf6e to 8395e84 Compare May 15, 2024 09:50
@magnetised magnetised force-pushed the garry/vax-1425-create-permissions-protobuf-definitions-and-write-validation branch from 93d48b0 to e4b22fc Compare May 20, 2024 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants