188 lines
6.3 KiB
PL/PgSQL
188 lines
6.3 KiB
PL/PgSQL
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
|
|
|
CREATE TABLE IF NOT EXISTS public.users (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
email text NOT NULL UNIQUE,
|
|
password_hash text NOT NULL,
|
|
first_name text NOT NULL,
|
|
second_name text NOT NULL,
|
|
birth_date date,
|
|
sex text NOT NULL,
|
|
is_admin boolean DEFAULT false
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS public.oauth_clients (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
client_id text NOT NULL UNIQUE,
|
|
client_secret text NOT NULL,
|
|
name text NOT NULL,
|
|
redirect_uris JSONB NOT NULL DEFAULT '[]'::jsonb,
|
|
grant_types JSONB NOT NULL DEFAULT '[]'::jsonb,
|
|
scopes JSONB DEFAULT '[]'::jsonb,
|
|
is_active boolean DEFAULT TRUE,
|
|
trusted boolean DEFAULT FALSE,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
updated_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS public.authorization_codes (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
code_hash text NOT NULL UNIQUE,
|
|
user_id uuid NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
|
|
client_id uuid NOT NULL REFERENCES public.oauth_clients(id) ON DELETE CASCADE,
|
|
redirect_uri text NOT NULL,
|
|
scopes jsonb NOT NULL DEFAULT '[]'::jsonb,
|
|
code_challenge text NOT NULL,
|
|
code_challenge_method text NOT NULL DEFAULT 'S256',
|
|
expires_at timestamptz NOT NULL,
|
|
consumed_at timestamptz,
|
|
created_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_authorization_codes_code_hash
|
|
ON public.authorization_codes (code_hash);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_authorization_codes_expires_at
|
|
ON public.authorization_codes (expires_at);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_authorization_codes_consumed_at
|
|
ON public.authorization_codes (consumed_at);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_authorization_codes_user_id
|
|
ON public.authorization_codes(user_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_authorization_codes_client_id
|
|
ON public.authorization_codes(client_id);
|
|
|
|
CREATE TABLE IF NOT EXISTS public.refresh_tokens (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
token_hash text NOT NULL UNIQUE,
|
|
user_id uuid NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
|
|
client_id uuid NOT NULL REFERENCES public.oauth_clients(id) ON DELETE CASCADE,
|
|
scopes jsonb NOT NULL DEFAULT '[]'::jsonb,
|
|
expires_at timestamptz NOT NULL,
|
|
created_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_token_hash
|
|
ON public.refresh_tokens (token_hash);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_expires_at
|
|
ON public.refresh_tokens (expires_at);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_user_id
|
|
ON public.refresh_tokens(user_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_client_id
|
|
ON public.refresh_tokens(client_id);
|
|
|
|
CREATE TABLE IF NOT EXISTS public.user_consents (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
user_id uuid NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
|
|
client_id uuid NOT NULL REFERENCES public.oauth_clients(id) ON DELETE CASCADE,
|
|
scopes jsonb NOT NULL,
|
|
authorized_at timestamptz NOT NULL DEFAULT now(),
|
|
last_used_at timestamptz,
|
|
expires_at timestamptz,
|
|
revoked_at timestamptz,
|
|
revoked_by uuid REFERENCES public.users(id)
|
|
);
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS unique_active_consent
|
|
ON public.user_consents (user_id, client_id)
|
|
WHERE revoked_at IS NULL;
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_user_consents_user_id
|
|
ON public.user_consents(user_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_user_consents_client_id
|
|
ON public.user_consents(client_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_user_consents_revoked
|
|
ON public.user_consents(revoked_at)
|
|
WHERE revoked_at IS NOT NULL;
|
|
|
|
CREATE TABLE IF NOT EXISTS public.positions (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
code text NOT NULL UNIQUE,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
updated_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS public.permissions (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
code text NOT NULL UNIQUE,
|
|
description text,
|
|
created_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
|
|
INSERT INTO public.permissions (code, description) VALUES
|
|
('positions.manage', 'Create and edit positions'),
|
|
('positions.assign', 'Assign positions to users')
|
|
ON CONFLICT (code) DO NOTHING;
|
|
|
|
CREATE TABLE IF NOT EXISTS public.position_permissions (
|
|
position_id uuid NOT NULL REFERENCES public.positions(id) ON DELETE CASCADE,
|
|
permission_id uuid NOT NULL REFERENCES public.permissions(id) ON DELETE CASCADE,
|
|
PRIMARY KEY (position_id, permission_id)
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_position_permissions_position_id
|
|
ON public.position_permissions (position_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_position_permissions_permission_id
|
|
ON public.position_permissions (permission_id);
|
|
|
|
CREATE TABLE IF NOT EXISTS public.user_positions (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
user_id uuid NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
|
|
position_id uuid NOT NULL REFERENCES public.positions(id) ON DELETE CASCADE,
|
|
valid_from timestamptz NOT NULL DEFAULT now(),
|
|
valid_to timestamptz,
|
|
assigned_by uuid REFERENCES public.users(id) ON DELETE SET NULL
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_user_positions_user_id
|
|
ON public.user_positions(user_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_user_positions_position_id
|
|
ON public.user_positions(position_id);
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_user_positions_unique_active
|
|
ON public.user_positions (user_id, position_id)
|
|
WHERE valid_to IS NULL;
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_permissions_code
|
|
ON public.permissions (code);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_users_is_admin
|
|
ON public.users (is_admin)
|
|
WHERE is_admin = TRUE;
|
|
|
|
CREATE OR REPLACE FUNCTION public.set_updated_at()
|
|
RETURNS trigger AS $$
|
|
BEGIN
|
|
NEW.updated_at = now();
|
|
RETURN NEW;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
DROP TRIGGER IF EXISTS users_updated_at ON public.users;
|
|
CREATE TRIGGER users_updated_at
|
|
BEFORE UPDATE ON public.users
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION public.set_updated_at();
|
|
|
|
DROP TRIGGER IF EXISTS oauth_clients_updated_at ON public.oauth_clients;
|
|
CREATE TRIGGER oauth_clients_updated_at
|
|
BEFORE UPDATE ON public.oauth_clients
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION public.set_updated_at();
|
|
|
|
DROP TRIGGER IF EXISTS positions_updated_at ON public.positions;
|
|
CREATE TRIGGER positions_updated_at
|
|
BEFORE UPDATE ON public.positions
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION public.set_updated_at(); |