301 lines
12 KiB
PL/PgSQL
301 lines
12 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.regions (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
code char(2) NOT NULL UNIQUE,
|
|
created_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS public.region_translations (
|
|
region_id uuid NOT NULL REFERENCES public.regions(id) ON DELETE CASCADE,
|
|
locale text NOT NULL,
|
|
name text NOT NULL,
|
|
PRIMARY KEY (region_id, locale)
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_region_translations_region_id
|
|
ON public.region_translations (region_id);
|
|
|
|
CREATE TABLE IF NOT EXISTS public.citizenship_applications (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
user_id uuid NOT NULL UNIQUE REFERENCES public.users(id) ON DELETE CASCADE,
|
|
status text NOT NULL DEFAULT 'pending',
|
|
region_id uuid REFERENCES public.regions(id),
|
|
reviewed_by uuid REFERENCES public.users(id),
|
|
reviewed_at timestamptz,
|
|
reject_reason text,
|
|
created_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS public.citizens (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
user_id uuid NOT NULL UNIQUE REFERENCES public.users(id),
|
|
application_id uuid NOT NULL REFERENCES public.citizenship_applications(id),
|
|
number text NOT NULL UNIQUE,
|
|
issued_at timestamptz NOT NULL DEFAULT now(),
|
|
region_id uuid REFERENCES public.regions(id)
|
|
);
|
|
|
|
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.position_translations (
|
|
position_id uuid NOT NULL REFERENCES public.positions(id) ON DELETE CASCADE,
|
|
locale text NOT NULL DEFAULT 'en',
|
|
name text NOT NULL,
|
|
description text,
|
|
PRIMARY KEY (position_id, locale)
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_position_translations_position_id
|
|
ON public.position_translations (position_id);
|
|
CREATE INDEX IF NOT EXISTS idx_position_translations_locale
|
|
ON public.position_translations (locale);
|
|
|
|
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()
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS public.permission_translations (
|
|
permission_id uuid NOT NULL REFERENCES public.permissions(id) ON DELETE CASCADE,
|
|
locale text NOT NULL DEFAULT 'en',
|
|
name text NOT NULL,
|
|
description text,
|
|
PRIMARY KEY (permission_id, locale)
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_permission_translations_permission_id
|
|
ON public.permission_translations (permission_id);
|
|
CREATE INDEX IF NOT EXISTS idx_permission_translations_locale
|
|
ON public.permission_translations (locale);
|
|
|
|
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();
|
|
|
|
INSERT INTO public.permissions (code, description) VALUES
|
|
('positions.manage', 'Create and edit positions'),
|
|
('positions.assign', 'Assign positions to users'),
|
|
('positions.revoke', 'Revoke positions from users')
|
|
ON CONFLICT (code) DO NOTHING;
|
|
|
|
INSERT INTO public.permission_translations (permission_id, locale, name, description)
|
|
SELECT id, 'en', code, description FROM public.permissions
|
|
ON CONFLICT (permission_id, locale) DO NOTHING;
|
|
|
|
INSERT INTO public.permission_translations (permission_id, locale, name, description)
|
|
SELECT id,
|
|
'ru',
|
|
CASE code
|
|
WHEN 'positions.manage' THEN 'Управление должностями'
|
|
WHEN 'positions.assign' THEN 'Назначение должностей'
|
|
WHEN 'positions.revoke' THEN 'Отзыв должностей'
|
|
ELSE code
|
|
END,
|
|
CASE code
|
|
WHEN 'positions.manage' THEN 'Создание и редактирование должностей'
|
|
WHEN 'positions.assign' THEN 'Назначение должностей пользователям'
|
|
WHEN 'positions.revoke' THEN 'Отзыв должностей у пользователей'
|
|
ELSE description
|
|
END
|
|
FROM public.permissions
|
|
ON CONFLICT (permission_id, locale) DO NOTHING;
|
|
|
|
INSERT INTO public.permission_translations (permission_id, locale, name, description)
|
|
SELECT id,
|
|
'it',
|
|
CASE code
|
|
WHEN 'positions.manage' THEN 'Gestione posizioni'
|
|
WHEN 'positions.assign' THEN 'Assegnazione posizioni'
|
|
WHEN 'positions.revoke' THEN 'Revoca posizioni'
|
|
ELSE code
|
|
END,
|
|
CASE code
|
|
WHEN 'positions.manage' THEN 'Creazione e modifica delle posizioni'
|
|
WHEN 'positions.assign' THEN 'Assegnazione delle posizioni agli utenti'
|
|
WHEN 'positions.revoke' THEN 'Revoca delle posizioni dagli utenti'
|
|
ELSE description
|
|
END
|
|
FROM public.permissions
|
|
ON CONFLICT (permission_id, locale) DO NOTHING;
|
|
|
|
INSERT INTO public.positions (code)
|
|
VALUES ('kotosoft_id_staff')
|
|
ON CONFLICT (code) DO NOTHING;
|
|
|
|
INSERT INTO public.position_translations (position_id, locale, name, description)
|
|
SELECT id, 'en', 'Kotosoft ID Staff', 'Default staff position for Kotosoft ID operators'
|
|
FROM public.positions WHERE code = 'kotosoft_id_staff'
|
|
ON CONFLICT (position_id, locale) DO NOTHING;
|
|
|
|
INSERT INTO public.position_translations (position_id, locale, name, description)
|
|
SELECT id, 'ru', 'Сотрудник Kotosoft ID', 'Базовая должность сотрудника Kotosoft ID'
|
|
FROM public.positions WHERE code = 'kotosoft_id_staff'
|
|
ON CONFLICT (position_id, locale) DO NOTHING;
|
|
|
|
INSERT INTO public.position_translations (position_id, locale, name, description)
|
|
SELECT id, 'it', 'Operatore Kotosoft ID', 'Posizione predefinita per operatori Kotosoft ID'
|
|
FROM public.positions WHERE code = 'kotosoft_id_staff'
|
|
ON CONFLICT (position_id, locale) DO NOTHING;
|
|
|
|
INSERT INTO public.position_permissions (position_id, permission_id)
|
|
SELECT p.id, perm.id
|
|
FROM public.positions p
|
|
JOIN public.permissions perm
|
|
ON perm.code IN ('positions.manage', 'positions.assign', 'positions.revoke')
|
|
WHERE p.code = 'kotosoft_id_staff'
|
|
ON CONFLICT (position_id, permission_id) DO NOTHING; |