Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ The bot's socket.io client authenticates with its service token in `auth.token`

The IdP notifies `POST /internal/events` when access changes; the backend also polls the snapshot every 30 s. The last good signing keys and snapshot are stored in `common_idp_state`, so a restart during an IdP outage resumes from them. Permission checks deny once the snapshot is more than an hour old. Configuration is in `.env.example`.

## People

The `people` router is the admin dashboard's register of Group Admins, Capo Admins and team members. Each person
is one record in `people_persons`; their roles are rows in `people_assignments`, with an append-only history in
`people_activity`.

- **A record linked to a login account** (`account_sub`): PoliNetwork Auth is the source of truth. People reads
who holds the roles bound to People from `GET /api/internal/people-roles` (15 s cache, the last answer during an
outage of up to an hour) and creates linked records for people given a bound role by hand in Auth. Changes go to
`POST /api/internal/people-roles` through the `people_role_commands` outbox: each command has an ID Auth applies at
most once, so the cron sends it again until Auth answers. The assignment shows `sent` until Auth confirms it, and
Auth's reason when it refuses.
- **A record that isn't linked yet**: its assignments stay `pending` in the backend, and are sent when the record is
linked, acting as whoever links it by hand or, for an invite, as whoever made each assignment.

The backend checks every role change with the same rules as Auth (`src/people/rules.ts`, shaped like Auth's
`src/auth/people-binding.ts`): no self-grants, the right permission per kind, and bounded delegation against the
role's permissions. A concept with no role bound in Auth, or with more than one, can't be assigned. Writes are
serialized by one transaction lock. Grant `idp:people:roles` to the backend's OAuth client to let it change roles.

## E-signing with the CIE

The `esign` router and `/esign/agent/*` endpoints let dashboard users sign PDFs with the Italian identity card
Expand Down
105 changes: 105 additions & 0 deletions drizzle/0031_people_records.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
CREATE TABLE "people_activity" (
"id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "people_activity_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
"person_id" text NOT NULL,
"actor" text NOT NULL,
"text" text NOT NULL,
"at" timestamp with time zone NOT NULL
);
--> statement-breakpoint
CREATE TABLE "people_assignments" (
"id" uuid PRIMARY KEY NOT NULL,
"person_id" text NOT NULL,
"kind" text NOT NULL,
"team" text,
"team_role" text,
"capo_scope" text,
"capo_of_reference_id" text,
"fields" jsonb DEFAULT '{}'::jsonb NOT NULL,
"since" date NOT NULL,
"ended_on" date,
"end_reason" text,
"assigned_by" text NOT NULL,
"change" text NOT NULL,
"status" text NOT NULL,
"refusal" text,
"command_id" uuid,
CONSTRAINT "people_assignments_kind_check" CHECK ("people_assignments"."kind" in ('admin', 'capo', 'team')),
CONSTRAINT "people_assignments_team_check" CHECK (("people_assignments"."kind" = 'team') = ("people_assignments"."team" is not null and "people_assignments"."team_role" in ('member', 'lead'))),
CONSTRAINT "people_assignments_capo_ref_check" CHECK ("people_assignments"."kind" = 'admin' or "people_assignments"."capo_of_reference_id" is null),
CONSTRAINT "people_assignments_change_check" CHECK ("people_assignments"."change" in ('start', 'end')),
CONSTRAINT "people_assignments_status_check" CHECK ("people_assignments"."status" in ('pending', 'sent', 'applied', 'refused')),
CONSTRAINT "people_assignments_sent_check" CHECK (("people_assignments"."status" = 'sent') = ("people_assignments"."command_id" is not null))
);
--> statement-breakpoint
CREATE TABLE "people_counters" (
"key" text PRIMARY KEY NOT NULL,
"value" bigint NOT NULL
);
--> statement-breakpoint
CREATE TABLE "people_invites" (
"id" uuid PRIMARY KEY NOT NULL,
"person_id" text NOT NULL,
"token_hash" text NOT NULL,
"email" text NOT NULL,
"sent_by" text NOT NULL,
"sent_at" timestamp with time zone NOT NULL,
"expires_at" timestamp with time zone NOT NULL,
"revoked_at" timestamp with time zone,
"accepted_at" timestamp with time zone,
"accepted_by" text,
CONSTRAINT "people_invites_token_hash_unique" UNIQUE("token_hash")
);
--> statement-breakpoint
CREATE TABLE "people_mail" (
"id" text PRIMARY KEY NOT NULL,
"person_id" text NOT NULL,
"to" text NOT NULL,
"subject" text NOT NULL,
"text" text NOT NULL,
"status" text NOT NULL,
"attempted_at" timestamp with time zone,
"error" text
);
--> statement-breakpoint
CREATE TABLE "people_persons" (
"id" text PRIMARY KEY NOT NULL,
"first_name" text NOT NULL,
"last_name" text NOT NULL,
"contact_email" text,
"phone" text,
"telegram_username" text,
"notes" text,
"account_sub" text,
"created_by" text NOT NULL,
"created_at" timestamp with time zone NOT NULL,
"updated_by" text NOT NULL,
"updated_at" timestamp with time zone NOT NULL,
CONSTRAINT "people_persons_account_sub_unique" UNIQUE("account_sub")
);
--> statement-breakpoint
CREATE TABLE "people_role_commands" (
"id" uuid PRIMARY KEY NOT NULL,
"person_id" text NOT NULL,
"request" jsonb NOT NULL,
"status" text NOT NULL,
"attempts" integer DEFAULT 0 NOT NULL,
"error" text,
"created_at" timestamp with time zone NOT NULL,
"answered_at" timestamp with time zone,
CONSTRAINT "people_role_commands_status_check" CHECK ("people_role_commands"."status" in ('queued', 'applied', 'refused'))
);
--> statement-breakpoint
DROP TABLE "people_identities" CASCADE;--> statement-breakpoint
DROP TABLE "people_records" CASCADE;--> statement-breakpoint
ALTER TABLE "people_activity" ADD CONSTRAINT "people_activity_person_id_people_persons_id_fk" FOREIGN KEY ("person_id") REFERENCES "public"."people_persons"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "people_assignments" ADD CONSTRAINT "people_assignments_person_id_people_persons_id_fk" FOREIGN KEY ("person_id") REFERENCES "public"."people_persons"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "people_assignments" ADD CONSTRAINT "people_assignments_capo_of_reference_id_people_persons_id_fk" FOREIGN KEY ("capo_of_reference_id") REFERENCES "public"."people_persons"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "people_assignments" ADD CONSTRAINT "people_assignments_command_id_people_role_commands_id_fk" FOREIGN KEY ("command_id") REFERENCES "public"."people_role_commands"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "people_invites" ADD CONSTRAINT "people_invites_person_id_people_persons_id_fk" FOREIGN KEY ("person_id") REFERENCES "public"."people_persons"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "people_mail" ADD CONSTRAINT "people_mail_person_id_people_persons_id_fk" FOREIGN KEY ("person_id") REFERENCES "public"."people_persons"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "people_role_commands" ADD CONSTRAINT "people_role_commands_person_id_people_persons_id_fk" FOREIGN KEY ("person_id") REFERENCES "public"."people_persons"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "people_activity_person_idx" ON "people_activity" USING btree ("person_id","id");--> statement-breakpoint
CREATE INDEX "people_assignments_person_idx" ON "people_assignments" USING btree ("person_id");--> statement-breakpoint
CREATE UNIQUE INDEX "people_assignments_current_idx" ON "people_assignments" USING btree ("person_id","kind",coalesce("team", '')) WHERE "people_assignments"."ended_on" is null;--> statement-breakpoint
CREATE INDEX "people_invites_person_idx" ON "people_invites" USING btree ("person_id");--> statement-breakpoint
CREATE INDEX "people_role_commands_queued_idx" ON "people_role_commands" USING btree ("created_at") WHERE "people_role_commands"."status" = 'queued';
Loading
Loading