Skip to content

Commit 3623b1d

Browse files
Add user identities and role read-permission migrations
- 0001: user_identities table for stable Microsoft (tid/oid) sign-in links - 0002: idempotent data migration granting the new read permissions (conversations.read, contacts.read, tasks.read, contacts.view) to existing roles - Refuse accepting invitations while impersonating a user
1 parent 0330391 commit 3623b1d

7 files changed

Lines changed: 10519 additions & 0 deletions

File tree

‎drizzle/0001_user_identities.sql‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE TABLE "user_identities" (
2+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
3+
"user_id" uuid NOT NULL,
4+
"provider" text NOT NULL,
5+
"subject" text NOT NULL,
6+
"email" text,
7+
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
8+
"last_used_at" timestamp with time zone DEFAULT now() NOT NULL
9+
);
10+
--> statement-breakpoint
11+
ALTER TABLE "user_identities" ADD CONSTRAINT "user_identities_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
12+
CREATE UNIQUE INDEX "user_identities_subject_idx" ON "user_identities" USING btree ("provider","subject");--> statement-breakpoint
13+
CREATE INDEX "user_identities_user_idx" ON "user_identities" USING btree ("user_id");
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- Read permissions introduced in v1.0.0 (API key scopes, shared address book visibility).
2+
-- Idempotent: appends only the permissions a role doesn't have yet, keeping array order.
3+
UPDATE "roles" SET "permissions" = "permissions" || ARRAY(
4+
SELECT p FROM unnest(ARRAY['conversations.read', 'contacts.read', 'tasks.read', 'contacts.view']) p WHERE p <> ALL("permissions")
5+
) WHERE "key" IN ('owner', 'admin', 'member');
6+
--> statement-breakpoint
7+
UPDATE "roles" SET "permissions" = "permissions" || ARRAY(
8+
SELECT p FROM unnest(ARRAY['conversations.read']) p WHERE p <> ALL("permissions")
9+
) WHERE "key" = 'guest';
10+
--> statement-breakpoint
11+
-- Custom roles keep what they could read; the full shared address book stays visible to roles that manage contacts or see all inboxes.
12+
UPDATE "roles" SET "permissions" = "permissions" || ARRAY(
13+
SELECT p FROM unnest(ARRAY['conversations.read', 'contacts.read', 'tasks.read']
14+
|| CASE WHEN "permissions" && ARRAY['contacts.manage', 'conversations.view_all'] THEN ARRAY['contacts.view'] ELSE ARRAY[]::text[] END) p
15+
WHERE p <> ALL("permissions")
16+
) WHERE "key" IS NULL OR "key" NOT IN ('owner', 'admin', 'member', 'guest');

0 commit comments

Comments
 (0)