mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
Fix dashboard
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"template": "vite"
|
||||
}
|
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
# Create support requests table
|
||||
|
||||
1. New Tables
|
||||
- `support_requests`
|
||||
- `id` (uuid, primary key)
|
||||
- `user_id` (text)
|
||||
- `department` (text)
|
||||
- `request_type` (text) - for storing request type
|
||||
- `priority` (text)
|
||||
- `description` (text)
|
||||
- `status` (text)
|
||||
- `created_at` (timestamptz)
|
||||
|
||||
2. Security
|
||||
- Enable RLS
|
||||
- Add policies for users and admins
|
||||
*/
|
||||
|
||||
-- Create support requests table
|
||||
CREATE TABLE IF NOT EXISTS support_requests (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id text NOT NULL,
|
||||
department text NOT NULL,
|
||||
request_type text NOT NULL CHECK (request_type IN ('hardware', 'software', 'network', 'access', 'other')),
|
||||
priority text NOT NULL CHECK (priority IN ('low', 'medium', 'high', 'critical')),
|
||||
description text,
|
||||
status text DEFAULT 'new' CHECK (status IN ('new', 'in_progress', 'resolved', 'closed')),
|
||||
created_at timestamptz DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable Row Level Security
|
||||
ALTER TABLE support_requests ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policies
|
||||
CREATE POLICY "Users can create their own requests"
|
||||
ON support_requests
|
||||
FOR INSERT
|
||||
TO authenticated
|
||||
WITH CHECK (auth.uid()::text = user_id);
|
||||
|
||||
CREATE POLICY "Users can view their own requests"
|
||||
ON support_requests
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (auth.uid()::text = user_id OR auth.role() = 'admin');
|
||||
|
||||
CREATE POLICY "Admins can update requests"
|
||||
ON support_requests
|
||||
FOR UPDATE
|
||||
TO authenticated
|
||||
USING (auth.role() = 'admin')
|
||||
WITH CHECK (auth.role() = 'admin');
|
@@ -1,80 +0,0 @@
|
||||
/*
|
||||
# Initial schema setup
|
||||
|
||||
1. New Tables
|
||||
- `users`
|
||||
- `id` (uuid, primary key)
|
||||
- `first_name` (text)
|
||||
- `last_name` (text)
|
||||
- `department` (text)
|
||||
- `password` (text)
|
||||
- `created_at` (timestamp)
|
||||
|
||||
- `requests`
|
||||
- `id` (uuid, primary key)
|
||||
- `user_id` (uuid, foreign key)
|
||||
- `first_name` (text)
|
||||
- `last_name` (text)
|
||||
- `department` (text)
|
||||
- `urgency` (text)
|
||||
- `description` (text)
|
||||
- `status` (text)
|
||||
- `created_at` (timestamp)
|
||||
|
||||
2. Security
|
||||
- Enable RLS on both tables
|
||||
- Add policies for authenticated users
|
||||
*/
|
||||
|
||||
-- Create users table
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
first_name text NOT NULL,
|
||||
last_name text NOT NULL,
|
||||
department text NOT NULL,
|
||||
password text NOT NULL,
|
||||
created_at timestamptz DEFAULT now()
|
||||
);
|
||||
|
||||
-- Create requests table
|
||||
CREATE TABLE IF NOT EXISTS requests (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
first_name text NOT NULL,
|
||||
last_name text NOT NULL,
|
||||
department text NOT NULL,
|
||||
urgency text NOT NULL,
|
||||
description text,
|
||||
status text NOT NULL DEFAULT 'new',
|
||||
created_at timestamptz DEFAULT now(),
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE requests ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policies
|
||||
CREATE POLICY "Users can read own data"
|
||||
ON users
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (auth.uid() = id);
|
||||
|
||||
CREATE POLICY "Users can read all requests"
|
||||
ON requests
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (true);
|
||||
|
||||
CREATE POLICY "Users can create requests"
|
||||
ON requests
|
||||
FOR INSERT
|
||||
TO authenticated
|
||||
WITH CHECK (auth.uid() = user_id);
|
||||
|
||||
CREATE POLICY "Users can update own requests"
|
||||
ON requests
|
||||
FOR UPDATE
|
||||
TO authenticated
|
||||
USING (auth.uid() = user_id);
|
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
# Support System Database Schema
|
||||
|
||||
1. New Tables
|
||||
- `support_requests`
|
||||
- `id` (uuid, primary key)
|
||||
- `user_id` (text)
|
||||
- `department` (text)
|
||||
- `request_type` (text)
|
||||
- `priority` (text)
|
||||
- `description` (text)
|
||||
- `status` (text)
|
||||
- `created_at` (timestamp)
|
||||
|
||||
2. Security
|
||||
- Enable RLS
|
||||
- Add policies for users and admins
|
||||
*/
|
||||
|
||||
-- Drop existing table if it exists
|
||||
DROP TABLE IF EXISTS support_requests CASCADE;
|
||||
|
||||
-- Create support requests table
|
||||
CREATE TABLE support_requests (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id text NOT NULL,
|
||||
department text NOT NULL,
|
||||
request_type text NOT NULL CHECK (request_type IN ('hardware', 'software', 'network', 'access', 'other')),
|
||||
priority text NOT NULL CHECK (priority IN ('low', 'medium', 'high', 'critical')),
|
||||
description text,
|
||||
status text DEFAULT 'new' CHECK (status IN ('new', 'in_progress', 'resolved', 'closed')),
|
||||
created_at timestamptz DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE support_requests ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policies
|
||||
CREATE POLICY "Users can create their own requests"
|
||||
ON support_requests
|
||||
FOR INSERT
|
||||
TO authenticated
|
||||
WITH CHECK (auth.uid()::text = user_id);
|
||||
|
||||
CREATE POLICY "Users can view their own requests"
|
||||
ON support_requests
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (auth.uid()::text = user_id OR auth.role() = 'admin');
|
||||
|
||||
CREATE POLICY "Admins can update requests"
|
||||
ON support_requests
|
||||
FOR UPDATE
|
||||
TO authenticated
|
||||
USING (auth.role() = 'admin')
|
||||
WITH CHECK (auth.role() = 'admin');
|
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
# Support Requests Table
|
||||
|
||||
1. New Table
|
||||
- `support_requests`
|
||||
- `id` (uuid, primary key) - Unique identifier
|
||||
- `user_id` (text) - ID of the user who created the request
|
||||
- `department` (text) - Department the request is from
|
||||
- `request_type` (text) - Type of request (hardware/software/etc)
|
||||
- `priority` (text) - Request priority level
|
||||
- `description` (text) - Detailed description of the request
|
||||
- `status` (text) - Current status of the request
|
||||
- `created_at` (timestamp) - When the request was created
|
||||
|
||||
2. Security
|
||||
- Enable RLS
|
||||
- Add policies for users and admins
|
||||
*/
|
||||
|
||||
-- Create support requests table
|
||||
CREATE TABLE IF NOT EXISTS support_requests (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id text NOT NULL,
|
||||
department text NOT NULL,
|
||||
request_type text NOT NULL CHECK (request_type IN ('hardware', 'software', 'network', 'access', 'other')),
|
||||
priority text NOT NULL CHECK (priority IN ('low', 'medium', 'high', 'critical')),
|
||||
description text,
|
||||
status text DEFAULT 'new' CHECK (status IN ('new', 'in_progress', 'resolved', 'closed')),
|
||||
created_at timestamptz DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable Row Level Security
|
||||
ALTER TABLE support_requests ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policies
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM pg_policies
|
||||
WHERE schemaname = 'public'
|
||||
AND tablename = 'support_requests'
|
||||
) THEN
|
||||
DROP POLICY IF EXISTS "Users can create their own requests" ON support_requests;
|
||||
DROP POLICY IF EXISTS "Users can view their own requests" ON support_requests;
|
||||
DROP POLICY IF EXISTS "Admins can update requests" ON support_requests;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
CREATE POLICY "Users can create their own requests"
|
||||
ON support_requests
|
||||
FOR INSERT
|
||||
TO authenticated
|
||||
WITH CHECK (auth.uid()::text = user_id);
|
||||
|
||||
CREATE POLICY "Users can view their own requests"
|
||||
ON support_requests
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (auth.uid()::text = user_id OR auth.role() = 'admin');
|
||||
|
||||
CREATE POLICY "Admins can update requests"
|
||||
ON support_requests
|
||||
FOR UPDATE
|
||||
TO authenticated
|
||||
USING (auth.role() = 'admin')
|
||||
WITH CHECK (auth.role() = 'admin');
|
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
# Fix Authentication Table Structure
|
||||
|
||||
1. Changes
|
||||
- Recreate employees table with correct structure
|
||||
- Add proper indexes
|
||||
- Insert admin user
|
||||
|
||||
2. Security
|
||||
- Enable RLS
|
||||
- Add proper policies
|
||||
*/
|
||||
|
||||
-- Recreate the employees table with correct structure
|
||||
CREATE TABLE IF NOT EXISTS employees (
|
||||
username text PRIMARY KEY,
|
||||
last_name text NOT NULL,
|
||||
department text NOT NULL,
|
||||
last_login_timestamp timestamptz,
|
||||
created_at timestamptz DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE employees ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policies
|
||||
CREATE POLICY "Users can read own data"
|
||||
ON employees
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (auth.uid()::text = username);
|
||||
|
||||
CREATE POLICY "Users can update their own data"
|
||||
ON employees
|
||||
FOR UPDATE
|
||||
TO authenticated
|
||||
USING (auth.uid()::text = username)
|
||||
WITH CHECK (auth.uid()::text = username);
|
||||
|
||||
-- Add indexes
|
||||
CREATE INDEX IF NOT EXISTS idx_employees_username
|
||||
ON employees(username);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_employees_last_login
|
||||
ON employees(last_login_timestamp);
|
||||
|
||||
-- Insert admin user (if not exists)
|
||||
INSERT INTO employees (username, last_name, department)
|
||||
VALUES (
|
||||
'admin',
|
||||
'Administrator',
|
||||
'IT'
|
||||
) ON CONFLICT (username) DO NOTHING;
|
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
# Employee Management System Schema
|
||||
|
||||
1. Table Structure
|
||||
- Creates `employees` table with:
|
||||
- `username` (text, primary key)
|
||||
- `last_name` (text, not null)
|
||||
- `department` (text, not null)
|
||||
- `last_login_timestamp` (timestamptz)
|
||||
- `created_at` (timestamptz with default)
|
||||
|
||||
2. Security
|
||||
- Enables Row Level Security (RLS)
|
||||
- Adds policies for:
|
||||
- Reading own data
|
||||
- Updating own data
|
||||
|
||||
3. Performance
|
||||
- Adds indexes on frequently queried columns
|
||||
*/
|
||||
|
||||
-- Drop existing table and dependencies if they exist
|
||||
DROP TABLE IF EXISTS employees CASCADE;
|
||||
|
||||
-- Create employees table
|
||||
CREATE TABLE employees (
|
||||
username text PRIMARY KEY,
|
||||
last_name text NOT NULL,
|
||||
department text NOT NULL,
|
||||
last_login_timestamp timestamptz,
|
||||
created_at timestamptz DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE employees ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policies
|
||||
CREATE POLICY "Users can read own data"
|
||||
ON employees
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (auth.uid()::text = username);
|
||||
|
||||
CREATE POLICY "Users can update their own data"
|
||||
ON employees
|
||||
FOR UPDATE
|
||||
TO authenticated
|
||||
USING (auth.uid()::text = username)
|
||||
WITH CHECK (auth.uid()::text = username);
|
||||
|
||||
-- Add performance indexes
|
||||
CREATE INDEX idx_employees_username ON employees(username);
|
||||
CREATE INDEX idx_employees_last_login ON employees(last_login_timestamp);
|
||||
|
||||
-- Insert default admin user
|
||||
INSERT INTO employees (username, last_name, department)
|
||||
VALUES (
|
||||
'admin',
|
||||
'Administrator',
|
||||
'IT'
|
||||
) ON CONFLICT (username) DO UPDATE SET
|
||||
last_name = EXCLUDED.last_name,
|
||||
department = EXCLUDED.department;
|
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
# Authentication System Setup
|
||||
|
||||
1. Changes
|
||||
- Create employees table with all required fields
|
||||
- Add password field with NOT NULL constraint
|
||||
- Add timestamp fields for auditing
|
||||
|
||||
2. Security
|
||||
- Enable RLS
|
||||
- Set up read and update policies
|
||||
- Add performance indexes
|
||||
*/
|
||||
|
||||
-- Clean up and recreate employees table
|
||||
CREATE TABLE IF NOT EXISTS employees (
|
||||
username text PRIMARY KEY,
|
||||
password text NOT NULL,
|
||||
last_name text NOT NULL,
|
||||
department text NOT NULL,
|
||||
last_login_timestamp timestamptz,
|
||||
created_at timestamptz DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE employees ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Safely handle existing policies
|
||||
DO $$
|
||||
BEGIN
|
||||
DROP POLICY IF EXISTS "Users can read own data" ON employees;
|
||||
DROP POLICY IF EXISTS "Users can update their own data" ON employees;
|
||||
END $$;
|
||||
|
||||
-- Create policies
|
||||
CREATE POLICY "Users can read own data"
|
||||
ON employees
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (auth.uid()::text = username);
|
||||
|
||||
CREATE POLICY "Users can update their own data"
|
||||
ON employees
|
||||
FOR UPDATE
|
||||
TO authenticated
|
||||
USING (auth.uid()::text = username)
|
||||
WITH CHECK (auth.uid()::text = username);
|
||||
|
||||
-- Safely handle existing indexes
|
||||
DO $$
|
||||
BEGIN
|
||||
DROP INDEX IF EXISTS idx_employees_username;
|
||||
DROP INDEX IF EXISTS idx_employees_last_login;
|
||||
END $$;
|
||||
|
||||
-- Add indexes
|
||||
CREATE INDEX idx_employees_username ON employees(username);
|
||||
CREATE INDEX idx_employees_last_login ON employees(last_login_timestamp);
|
||||
|
||||
-- Insert admin user with hashed password for 'admin66'
|
||||
INSERT INTO employees (username, password, last_name, department)
|
||||
VALUES (
|
||||
'admin',
|
||||
'$2a$10$xJ7Yt1UqZKhVkk2mFXgQe.UuB3YH3QQMkj8AfzF8fxMjGlZZYf.Hy',
|
||||
'Administrator',
|
||||
'IT'
|
||||
) ON CONFLICT (username) DO NOTHING;
|
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
# Update employees table and policies
|
||||
|
||||
1. Changes
|
||||
- Ensures employees table exists with required fields
|
||||
- Safely handles existing RLS policy
|
||||
- Updates admin user with correct password hash
|
||||
*/
|
||||
|
||||
-- Create table if it doesn't exist
|
||||
CREATE TABLE IF NOT EXISTS employees (
|
||||
username text PRIMARY KEY,
|
||||
password text NOT NULL,
|
||||
last_name text NOT NULL,
|
||||
department text NOT NULL,
|
||||
created_at timestamptz DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable RLS (idempotent operation)
|
||||
ALTER TABLE employees ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Drop existing policy if it exists
|
||||
DO $$
|
||||
BEGIN
|
||||
DROP POLICY IF EXISTS "Users can read own data" ON employees;
|
||||
END $$;
|
||||
|
||||
-- Create policy
|
||||
CREATE POLICY "Users can read own data"
|
||||
ON employees
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (auth.uid()::text = username);
|
||||
|
||||
-- Update or insert admin user
|
||||
INSERT INTO employees (username, password, last_name, department)
|
||||
VALUES (
|
||||
'admin',
|
||||
'$2a$10$X4kv7j5ZcG39WgkdqhzJXO2/ZZJHNNxt0Bz4Y8DzxfBqL0Q1erqJS', -- hashed 'admin'
|
||||
'Administrator',
|
||||
'IT'
|
||||
) ON CONFLICT (username)
|
||||
DO UPDATE SET
|
||||
password = EXCLUDED.password,
|
||||
last_name = EXCLUDED.last_name,
|
||||
department = EXCLUDED.department;
|
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
# Update employees table and policies
|
||||
|
||||
1. Changes
|
||||
- Ensures employees table exists with required fields
|
||||
- Safely handles existing RLS policy
|
||||
- Updates admin user with correct password hash for 'admin66'
|
||||
*/
|
||||
|
||||
-- Create table if it doesn't exist
|
||||
CREATE TABLE IF NOT EXISTS employees (
|
||||
username text PRIMARY KEY,
|
||||
password text NOT NULL,
|
||||
last_name text NOT NULL,
|
||||
department text NOT NULL,
|
||||
created_at timestamptz DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable RLS (idempotent operation)
|
||||
ALTER TABLE employees ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Safely handle existing policy
|
||||
DO $$
|
||||
BEGIN
|
||||
DROP POLICY IF EXISTS "Users can read own data" ON employees;
|
||||
END $$;
|
||||
|
||||
-- Create policy
|
||||
CREATE POLICY "Users can read own data"
|
||||
ON employees
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (auth.uid()::text = username);
|
||||
|
||||
-- Update or insert admin user with hashed password for 'admin66'
|
||||
INSERT INTO employees (username, password, last_name, department)
|
||||
VALUES (
|
||||
'admin',
|
||||
'$2a$10$xJ7Yt1UqZKhVkk2mFXgQe.UuB3YH3QQMkj8AfzF8fxMjGlZZYf.Hy', -- hashed 'admin66'
|
||||
'Administrator',
|
||||
'IT'
|
||||
) ON CONFLICT (username)
|
||||
DO UPDATE SET
|
||||
password = EXCLUDED.password,
|
||||
last_name = EXCLUDED.last_name,
|
||||
department = EXCLUDED.department;
|
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
# Create Users Table
|
||||
|
||||
1. New Tables
|
||||
- `users`
|
||||
- Basic user information and password storage
|
||||
- Includes RLS policies for security
|
||||
*/
|
||||
|
||||
-- Create extension for password hashing
|
||||
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
||||
|
||||
-- Create users table
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
first_name text NOT NULL,
|
||||
last_name text NOT NULL,
|
||||
department text NOT NULL,
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create basic security policies
|
||||
CREATE POLICY "Users can view own data"
|
||||
ON users
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (id = auth.uid());
|
||||
|
||||
CREATE POLICY "Admins can manage all users"
|
||||
ON users
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (auth.jwt() ->> 'email' = 'admin@example.com');
|
@@ -1,166 +0,0 @@
|
||||
/*
|
||||
# Support Request System Tables
|
||||
|
||||
1. New Tables
|
||||
- `support_requests`
|
||||
- `id` (uuid, primary key)
|
||||
- `employee_id` (uuid, references employees)
|
||||
- `department` (text)
|
||||
- `request_type` (enum)
|
||||
- `priority` (enum)
|
||||
- `status` (enum)
|
||||
- `description` (text)
|
||||
- `created_at` (timestamptz)
|
||||
- `last_status_change` (timestamptz)
|
||||
|
||||
- `status_history`
|
||||
- `id` (uuid, primary key)
|
||||
- `request_id` (uuid, references support_requests)
|
||||
- `old_status` (enum)
|
||||
- `new_status` (enum)
|
||||
- `changed_by` (uuid, references employees)
|
||||
- `changed_at` (timestamptz)
|
||||
|
||||
2. Security
|
||||
- Enable RLS on all tables
|
||||
- Add policies for employees and admins
|
||||
*/
|
||||
|
||||
-- Create enum types
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE request_type AS ENUM ('hardware', 'software', 'network', 'access', 'other');
|
||||
CREATE TYPE request_priority AS ENUM ('low', 'medium', 'high', 'critical');
|
||||
CREATE TYPE request_status AS ENUM ('new', 'in_progress', 'resolved', 'closed');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
|
||||
-- Create support requests table
|
||||
CREATE TABLE IF NOT EXISTS support_requests (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
employee_id uuid REFERENCES employees(id) NOT NULL,
|
||||
department text NOT NULL,
|
||||
request_type request_type NOT NULL,
|
||||
priority request_priority NOT NULL,
|
||||
status request_status NOT NULL DEFAULT 'new',
|
||||
description text,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
last_status_change timestamptz DEFAULT now()
|
||||
);
|
||||
|
||||
-- Create status history table
|
||||
CREATE TABLE IF NOT EXISTS status_history (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
request_id uuid REFERENCES support_requests(id) ON DELETE CASCADE,
|
||||
old_status request_status,
|
||||
new_status request_status NOT NULL,
|
||||
changed_by uuid REFERENCES employees(id) NOT NULL,
|
||||
changed_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE support_requests ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE status_history ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policies for support_requests
|
||||
DO $$ BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT FROM pg_policies
|
||||
WHERE tablename = 'support_requests'
|
||||
AND policyname = 'Employees can view their own requests'
|
||||
) THEN
|
||||
CREATE POLICY "Employees can view their own requests"
|
||||
ON support_requests
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (employee_id = auth.uid());
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT FROM pg_policies
|
||||
WHERE tablename = 'support_requests'
|
||||
AND policyname = 'Employees can create their own requests'
|
||||
) THEN
|
||||
CREATE POLICY "Employees can create their own requests"
|
||||
ON support_requests
|
||||
FOR INSERT
|
||||
TO authenticated
|
||||
WITH CHECK (employee_id = auth.uid());
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT FROM pg_policies
|
||||
WHERE tablename = 'support_requests'
|
||||
AND policyname = 'Admins can view all requests'
|
||||
) THEN
|
||||
CREATE POLICY "Admins can view all requests"
|
||||
ON support_requests
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (auth.jwt() ->> 'role' = 'admin');
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Create policies for status_history
|
||||
DO $$ BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT FROM pg_policies
|
||||
WHERE tablename = 'status_history'
|
||||
AND policyname = 'Employees can view status history of their requests'
|
||||
) THEN
|
||||
CREATE POLICY "Employees can view status history of their requests"
|
||||
ON status_history
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (
|
||||
EXISTS (
|
||||
SELECT 1 FROM support_requests
|
||||
WHERE id = status_history.request_id
|
||||
AND employee_id = auth.uid()
|
||||
)
|
||||
);
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT FROM pg_policies
|
||||
WHERE tablename = 'status_history'
|
||||
AND policyname = 'Admins can view all status history'
|
||||
) THEN
|
||||
CREATE POLICY "Admins can view all status history"
|
||||
ON status_history
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (auth.jwt() ->> 'role' = 'admin');
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Create status update trigger
|
||||
CREATE OR REPLACE FUNCTION update_request_status_history()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
IF (TG_OP = 'UPDATE' AND OLD.status IS DISTINCT FROM NEW.status) THEN
|
||||
INSERT INTO status_history (
|
||||
request_id,
|
||||
old_status,
|
||||
new_status,
|
||||
changed_by
|
||||
) VALUES (
|
||||
NEW.id,
|
||||
OLD.status,
|
||||
NEW.status,
|
||||
auth.uid()
|
||||
);
|
||||
|
||||
NEW.last_status_change = now();
|
||||
END IF;
|
||||
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql SECURITY DEFINER;
|
||||
|
||||
-- Create trigger
|
||||
DROP TRIGGER IF EXISTS track_request_status_changes ON support_requests;
|
||||
CREATE TRIGGER track_request_status_changes
|
||||
BEFORE UPDATE ON support_requests
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_request_status_history();
|
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
# Add employee details to support requests
|
||||
|
||||
1. Changes
|
||||
- Add employee_last_name and employee_department columns to support_requests
|
||||
- Add trigger to automatically populate employee details on insert
|
||||
- Update existing records with employee details
|
||||
|
||||
2. Security
|
||||
- Maintain existing RLS policies
|
||||
- No changes to security policies required as these are derived fields
|
||||
*/
|
||||
|
||||
-- Add new columns for employee details
|
||||
ALTER TABLE support_requests
|
||||
ADD COLUMN IF NOT EXISTS employee_last_name text,
|
||||
ADD COLUMN IF NOT EXISTS employee_department text;
|
||||
|
||||
-- Create function to populate employee details
|
||||
CREATE OR REPLACE FUNCTION populate_employee_details()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
SELECT
|
||||
last_name,
|
||||
department
|
||||
INTO
|
||||
NEW.employee_last_name,
|
||||
NEW.employee_department
|
||||
FROM employees
|
||||
WHERE id = NEW.employee_id;
|
||||
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
-- Drop trigger if exists to avoid conflicts
|
||||
DROP TRIGGER IF EXISTS set_employee_details ON support_requests;
|
||||
|
||||
-- Create trigger to automatically populate employee details
|
||||
CREATE TRIGGER set_employee_details
|
||||
BEFORE INSERT ON support_requests
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION populate_employee_details();
|
||||
|
||||
-- Update existing records with employee details
|
||||
UPDATE support_requests sr
|
||||
SET
|
||||
employee_last_name = e.last_name,
|
||||
employee_department = e.department
|
||||
FROM employees e
|
||||
WHERE sr.employee_id = e.id;
|
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
# Add create_user function
|
||||
|
||||
1. Changes
|
||||
- Add function to create new users in employees table
|
||||
- Function handles first name, last name, department and password
|
||||
- Returns the created employee record
|
||||
|
||||
2. Details
|
||||
- Creates a stored procedure for consistent user creation
|
||||
- Validates input parameters
|
||||
- Returns the full employee record after creation
|
||||
|
||||
3. Security
|
||||
- Function is SECURITY DEFINER to ensure proper access control
|
||||
- Input validation to prevent invalid data
|
||||
*/
|
||||
|
||||
-- Create function to handle user creation
|
||||
CREATE OR REPLACE FUNCTION create_user(
|
||||
p_first_name text,
|
||||
p_last_name text,
|
||||
p_department text,
|
||||
p_password text
|
||||
)
|
||||
RETURNS employees
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
AS $$
|
||||
DECLARE
|
||||
v_employee employees;
|
||||
BEGIN
|
||||
-- Validate inputs
|
||||
IF p_first_name IS NULL OR p_first_name = '' THEN
|
||||
RAISE EXCEPTION 'First name cannot be empty';
|
||||
END IF;
|
||||
|
||||
IF p_last_name IS NULL OR p_last_name = '' THEN
|
||||
RAISE EXCEPTION 'Last name cannot be empty';
|
||||
END IF;
|
||||
|
||||
IF p_department IS NULL OR p_department = '' THEN
|
||||
RAISE EXCEPTION 'Department cannot be empty';
|
||||
END IF;
|
||||
|
||||
IF p_password IS NULL OR p_password = '' THEN
|
||||
RAISE EXCEPTION 'Password cannot be empty';
|
||||
END IF;
|
||||
|
||||
-- Insert new employee
|
||||
INSERT INTO employees (
|
||||
first_name,
|
||||
last_name,
|
||||
department,
|
||||
email -- Generate email from name
|
||||
) VALUES (
|
||||
p_first_name,
|
||||
p_last_name,
|
||||
p_department,
|
||||
lower(p_last_name || '@example.com')
|
||||
)
|
||||
RETURNING * INTO v_employee;
|
||||
|
||||
RETURN v_employee;
|
||||
END;
|
||||
$$;
|
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
# Update Employee Table RLS Policies
|
||||
|
||||
1. Changes
|
||||
- Drop existing RLS policies
|
||||
- Create new policies for admin access
|
||||
- Add policy for employee self-access
|
||||
|
||||
2. Security
|
||||
- Enable RLS on employees table
|
||||
- Admin can manage all employees
|
||||
- Employees can view their own data
|
||||
*/
|
||||
|
||||
-- Drop existing policies if they exist
|
||||
DROP POLICY IF EXISTS "Admins can manage employees" ON employees;
|
||||
DROP POLICY IF EXISTS "Employees can view own data" ON employees;
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE employees ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create admin policy for full access
|
||||
CREATE POLICY "Admins can manage employees"
|
||||
ON employees
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (
|
||||
auth.jwt() ->> 'email' = 'admin@example.com'
|
||||
);
|
||||
|
||||
-- Create policy for employees to view their own data
|
||||
CREATE POLICY "Employees can view own data"
|
||||
ON employees
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (
|
||||
id = auth.uid()
|
||||
);
|
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
# Update support requests schema
|
||||
|
||||
1. Changes
|
||||
- Change foreign key reference from auth.users to employees table
|
||||
- Update RLS policies to use employee_id instead of user_id
|
||||
- Add indexes for better query performance
|
||||
|
||||
2. Security
|
||||
- Enable RLS
|
||||
- Add policies for employees to manage their requests
|
||||
*/
|
||||
|
||||
-- First drop the foreign key constraint
|
||||
ALTER TABLE support_requests
|
||||
DROP CONSTRAINT IF EXISTS support_requests_user_id_fkey;
|
||||
|
||||
-- Then rename the column
|
||||
ALTER TABLE support_requests
|
||||
RENAME COLUMN user_id TO employee_id;
|
||||
|
||||
-- Add new foreign key constraint
|
||||
ALTER TABLE support_requests
|
||||
ADD CONSTRAINT support_requests_employee_id_fkey
|
||||
FOREIGN KEY (employee_id) REFERENCES employees(id);
|
||||
|
||||
-- Create index for better performance
|
||||
CREATE INDEX IF NOT EXISTS idx_support_requests_employee_id
|
||||
ON support_requests(employee_id);
|
||||
|
||||
-- Update RLS policies
|
||||
DROP POLICY IF EXISTS "Users can create requests" ON support_requests;
|
||||
DROP POLICY IF EXISTS "Users can view their own requests" ON support_requests;
|
||||
DROP POLICY IF EXISTS "Users can update their own requests" ON support_requests;
|
||||
DROP POLICY IF EXISTS "IT department can manage all requests" ON support_requests;
|
||||
|
||||
CREATE POLICY "Employees can create requests"
|
||||
ON support_requests
|
||||
FOR INSERT
|
||||
TO authenticated
|
||||
WITH CHECK (
|
||||
EXISTS (
|
||||
SELECT 1 FROM employees
|
||||
WHERE id = support_requests.employee_id
|
||||
)
|
||||
);
|
||||
|
||||
CREATE POLICY "Employees can view their own requests"
|
||||
ON support_requests
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (
|
||||
employee_id IN (
|
||||
SELECT id FROM employees
|
||||
WHERE id = support_requests.employee_id
|
||||
)
|
||||
);
|
||||
|
||||
CREATE POLICY "Employees can update their own requests"
|
||||
ON support_requests
|
||||
FOR UPDATE
|
||||
TO authenticated
|
||||
USING (
|
||||
employee_id IN (
|
||||
SELECT id FROM employees
|
||||
WHERE id = support_requests.employee_id
|
||||
)
|
||||
);
|
@@ -1,112 +0,0 @@
|
||||
/*
|
||||
# Create employees and support requests tables
|
||||
|
||||
1. New Tables
|
||||
- `employees`
|
||||
- `id` (uuid, primary key)
|
||||
- `first_name` (text)
|
||||
- `last_name` (text)
|
||||
- `department` (text)
|
||||
- `created_at` (timestamptz)
|
||||
- `support_requests`
|
||||
- `id` (uuid, primary key)
|
||||
- `employee_id` (uuid, foreign key)
|
||||
- `department` (text)
|
||||
- `request_type` (enum)
|
||||
- `priority` (enum)
|
||||
- `status` (enum)
|
||||
- `description` (text)
|
||||
- `created_at` (timestamptz)
|
||||
- `last_status_change` (timestamptz)
|
||||
|
||||
2. Security
|
||||
- Enable RLS on both tables
|
||||
- Add appropriate policies for employees and admins
|
||||
*/
|
||||
|
||||
-- Create enum types if they don't exist
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE request_type AS ENUM ('hardware', 'software', 'network', 'access', 'other');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE request_priority AS ENUM ('low', 'medium', 'high', 'critical');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE request_status AS ENUM ('new', 'in_progress', 'resolved', 'closed');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
|
||||
-- Create employees table
|
||||
CREATE TABLE IF NOT EXISTS employees (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
first_name text NOT NULL,
|
||||
last_name text NOT NULL,
|
||||
department text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable RLS for employees
|
||||
ALTER TABLE employees ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policies for employees
|
||||
CREATE POLICY "Employees can view their own data"
|
||||
ON employees
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (id = auth.uid());
|
||||
|
||||
CREATE POLICY "Admins can manage all employees"
|
||||
ON employees
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (auth.jwt() ->> 'role' = 'admin');
|
||||
|
||||
-- Create support requests table
|
||||
CREATE TABLE IF NOT EXISTS support_requests (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
employee_id uuid REFERENCES employees(id) NOT NULL,
|
||||
department text NOT NULL,
|
||||
request_type request_type NOT NULL,
|
||||
priority request_priority NOT NULL,
|
||||
status request_status NOT NULL DEFAULT 'new',
|
||||
description text,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
last_status_change timestamptz DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable RLS for support requests
|
||||
ALTER TABLE support_requests ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policies for support requests
|
||||
CREATE POLICY "Employees can view their own requests"
|
||||
ON support_requests
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (employee_id = auth.uid());
|
||||
|
||||
CREATE POLICY "Employees can create their own requests"
|
||||
ON support_requests
|
||||
FOR INSERT
|
||||
TO authenticated
|
||||
WITH CHECK (employee_id = auth.uid());
|
||||
|
||||
CREATE POLICY "Admins can manage all requests"
|
||||
ON support_requests
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (auth.jwt() ->> 'role' = 'admin');
|
||||
|
||||
-- Add initial test data for employees
|
||||
INSERT INTO employees (first_name, last_name, department)
|
||||
VALUES
|
||||
('Иван', 'Иванов', 'aho'),
|
||||
('Петр', 'Петров', 'gkh'),
|
||||
('Сергей', 'Сергеев', 'general')
|
||||
ON CONFLICT (id) DO NOTHING;
|
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
# Add employee details to support requests
|
||||
|
||||
1. Changes
|
||||
- Add employee_last_name and employee_department columns to support_requests
|
||||
- Create trigger to automatically populate employee details on insert
|
||||
- Update existing records with employee details
|
||||
|
||||
2. Details
|
||||
- Adds columns to store employee information directly in support_requests
|
||||
- Creates trigger to automatically populate these fields on insert
|
||||
- Updates existing records with employee information
|
||||
- Uses user_id to link with employees table
|
||||
|
||||
3. Security
|
||||
- Maintains existing RLS policies
|
||||
- No additional security changes needed as these are derived fields
|
||||
*/
|
||||
|
||||
-- Add new columns for employee details
|
||||
ALTER TABLE support_requests
|
||||
ADD COLUMN IF NOT EXISTS employee_last_name text,
|
||||
ADD COLUMN IF NOT EXISTS employee_department text;
|
||||
|
||||
-- Create function to populate employee details
|
||||
CREATE OR REPLACE FUNCTION populate_employee_details()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
SELECT
|
||||
last_name,
|
||||
department
|
||||
INTO
|
||||
NEW.employee_last_name,
|
||||
NEW.employee_department
|
||||
FROM employees
|
||||
WHERE id = NEW.user_id;
|
||||
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
-- Drop trigger if exists to avoid conflicts
|
||||
DROP TRIGGER IF EXISTS set_employee_details ON support_requests;
|
||||
|
||||
-- Create trigger to automatically populate employee details
|
||||
CREATE TRIGGER set_employee_details
|
||||
BEFORE INSERT ON support_requests
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION populate_employee_details();
|
||||
|
||||
-- Update existing records with employee details
|
||||
UPDATE support_requests sr
|
||||
SET
|
||||
employee_last_name = e.last_name,
|
||||
employee_department = e.department
|
||||
FROM employees e
|
||||
WHERE sr.user_id = e.id;
|
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
# Update support requests policies
|
||||
|
||||
1. Changes
|
||||
- Add RLS policies for support requests table
|
||||
- Allow authenticated users to create and view their requests
|
||||
- Allow admins to manage all requests
|
||||
|
||||
2. Security
|
||||
- Enable RLS on support_requests table
|
||||
- Add policies for authenticated users
|
||||
- Add admin policies
|
||||
*/
|
||||
|
||||
-- Drop existing policies if they exist
|
||||
DROP POLICY IF EXISTS "Users can create their own requests" ON support_requests;
|
||||
DROP POLICY IF EXISTS "Users can view their own requests" ON support_requests;
|
||||
DROP POLICY IF EXISTS "Users can update their own requests" ON support_requests;
|
||||
DROP POLICY IF EXISTS "Admins can view all requests" ON support_requests;
|
||||
|
||||
-- Create new policies
|
||||
CREATE POLICY "Users can create requests"
|
||||
ON support_requests
|
||||
FOR INSERT
|
||||
TO authenticated
|
||||
WITH CHECK (true);
|
||||
|
||||
CREATE POLICY "Users can view their own requests"
|
||||
ON support_requests
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (
|
||||
auth.uid() = user_id OR
|
||||
EXISTS (
|
||||
SELECT 1 FROM users
|
||||
WHERE users.id = auth.uid() AND department = 'it'
|
||||
)
|
||||
);
|
||||
|
||||
CREATE POLICY "Users can update their own requests"
|
||||
ON support_requests
|
||||
FOR UPDATE
|
||||
TO authenticated
|
||||
USING (
|
||||
auth.uid() = user_id OR
|
||||
EXISTS (
|
||||
SELECT 1 FROM users
|
||||
WHERE users.id = auth.uid() AND department = 'it'
|
||||
)
|
||||
)
|
||||
WITH CHECK (
|
||||
auth.uid() = user_id OR
|
||||
EXISTS (
|
||||
SELECT 1 FROM users
|
||||
WHERE users.id = auth.uid() AND department = 'it'
|
||||
)
|
||||
);
|
||||
|
||||
CREATE POLICY "IT department can manage all requests"
|
||||
ON support_requests
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (
|
||||
EXISTS (
|
||||
SELECT 1 FROM users
|
||||
WHERE users.id = auth.uid() AND department = 'it'
|
||||
)
|
||||
);
|
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
# Add Employee Creation Function
|
||||
|
||||
1. Changes
|
||||
- Add secure function to create new employees with password hashing
|
||||
|
||||
2. Security
|
||||
- Uses hash_password function for secure password storage
|
||||
- SECURITY DEFINER to ensure proper access control
|
||||
- Returns employee data without password hash
|
||||
*/
|
||||
|
||||
-- Create function to create employee with password
|
||||
CREATE OR REPLACE FUNCTION create_employee(
|
||||
p_first_name text,
|
||||
p_last_name text,
|
||||
p_department text,
|
||||
p_password text
|
||||
)
|
||||
RETURNS employees
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
AS $$
|
||||
DECLARE
|
||||
v_employee employees;
|
||||
BEGIN
|
||||
-- Validate input
|
||||
IF p_password IS NULL OR length(p_password) < 6 THEN
|
||||
RAISE EXCEPTION 'Password must be at least 6 characters long';
|
||||
END IF;
|
||||
|
||||
-- Create employee with hashed password
|
||||
INSERT INTO employees (
|
||||
first_name,
|
||||
last_name,
|
||||
department,
|
||||
password_hash
|
||||
) VALUES (
|
||||
p_first_name,
|
||||
p_last_name,
|
||||
p_department,
|
||||
hash_password(p_password)
|
||||
)
|
||||
RETURNING * INTO v_employee;
|
||||
|
||||
-- Return employee data without password hash
|
||||
v_employee.password_hash := NULL;
|
||||
RETURN v_employee;
|
||||
END;
|
||||
$$;
|
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
# Fix employee creation process
|
||||
|
||||
1. Changes
|
||||
- Add trigger to create auth user and employee synchronously
|
||||
- Update create_employee function to handle auth user creation
|
||||
- Add proper error handling
|
||||
|
||||
2. Security
|
||||
- Maintain RLS policies
|
||||
- Ensure secure password handling
|
||||
*/
|
||||
|
||||
-- Function to create auth user and employee
|
||||
CREATE OR REPLACE FUNCTION create_employee(
|
||||
p_first_name text,
|
||||
p_last_name text,
|
||||
p_department text,
|
||||
p_password text
|
||||
)
|
||||
RETURNS employees
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
AS $$
|
||||
DECLARE
|
||||
v_employee employees;
|
||||
v_auth_user uuid;
|
||||
BEGIN
|
||||
-- Validate password
|
||||
PERFORM validate_password(p_password);
|
||||
|
||||
-- Create auth user first
|
||||
v_auth_user := auth.uid();
|
||||
|
||||
-- Create employee record
|
||||
INSERT INTO employees (
|
||||
id,
|
||||
first_name,
|
||||
last_name,
|
||||
department,
|
||||
password_hash
|
||||
) VALUES (
|
||||
v_auth_user,
|
||||
p_first_name,
|
||||
p_last_name,
|
||||
p_department,
|
||||
hash_password(p_password)
|
||||
)
|
||||
RETURNING * INTO v_employee;
|
||||
|
||||
RETURN v_employee;
|
||||
EXCEPTION
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Failed to create employee: %', SQLERRM;
|
||||
END;
|
||||
$$;
|
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
# Remove email dependency from employees table
|
||||
|
||||
1. Changes
|
||||
- Remove email column from employees table
|
||||
- Update create_employee function to work without email
|
||||
- Preserve existing data integrity
|
||||
|
||||
2. Security
|
||||
- Maintain existing RLS policies
|
||||
*/
|
||||
|
||||
-- Remove email column and its constraint
|
||||
ALTER TABLE employees
|
||||
DROP COLUMN IF EXISTS email;
|
||||
|
||||
-- Update create_employee function
|
||||
CREATE OR REPLACE FUNCTION create_employee(
|
||||
p_first_name text,
|
||||
p_last_name text,
|
||||
p_department text,
|
||||
p_password text
|
||||
)
|
||||
RETURNS employees
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
AS $$
|
||||
DECLARE
|
||||
v_employee employees;
|
||||
BEGIN
|
||||
-- Input validation
|
||||
IF p_first_name IS NULL OR p_first_name = '' THEN
|
||||
RAISE EXCEPTION 'First name is required';
|
||||
END IF;
|
||||
|
||||
IF p_last_name IS NULL OR p_last_name = '' THEN
|
||||
RAISE EXCEPTION 'Last name is required';
|
||||
END IF;
|
||||
|
||||
IF p_department IS NULL OR p_department = '' THEN
|
||||
RAISE EXCEPTION 'Department is required';
|
||||
END IF;
|
||||
|
||||
IF p_password IS NULL OR length(p_password) < 6 THEN
|
||||
RAISE EXCEPTION 'Password must be at least 6 characters long';
|
||||
END IF;
|
||||
|
||||
-- Create employee record
|
||||
INSERT INTO employees (
|
||||
first_name,
|
||||
last_name,
|
||||
department,
|
||||
password_hash
|
||||
) VALUES (
|
||||
p_first_name,
|
||||
p_last_name,
|
||||
p_department,
|
||||
hash_password(p_password)
|
||||
)
|
||||
RETURNING * INTO v_employee;
|
||||
|
||||
RETURN v_employee;
|
||||
EXCEPTION
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Failed to create employee: %', SQLERRM;
|
||||
END;
|
||||
$$;
|
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
# Create status history table
|
||||
|
||||
1. New Tables
|
||||
- `status_history`
|
||||
- `id` (uuid, primary key)
|
||||
- `request_id` (uuid, foreign key to support_requests.id)
|
||||
- `old_status` (request_status)
|
||||
- `new_status` (request_status)
|
||||
- `changed_by` (uuid, foreign key to employees.id)
|
||||
- `changed_at` (timestamptz)
|
||||
|
||||
2. Security
|
||||
- Enable RLS
|
||||
- Add policies for status history access
|
||||
*/
|
||||
|
||||
CREATE TABLE IF NOT EXISTS status_history (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
request_id uuid REFERENCES support_requests(id) ON DELETE CASCADE NOT NULL,
|
||||
old_status request_status,
|
||||
new_status request_status NOT NULL,
|
||||
changed_by uuid REFERENCES employees(id) ON DELETE CASCADE NOT NULL,
|
||||
changed_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE status_history ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policies
|
||||
CREATE POLICY "Users can view status history of their requests"
|
||||
ON status_history
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (
|
||||
EXISTS (
|
||||
SELECT 1 FROM support_requests sr
|
||||
WHERE sr.id = status_history.request_id
|
||||
AND sr.employee_id = auth.uid()
|
||||
)
|
||||
);
|
||||
|
||||
CREATE POLICY "Admins can view all status history"
|
||||
ON status_history
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (
|
||||
EXISTS (
|
||||
SELECT 1 FROM employees e
|
||||
WHERE e.id = auth.uid()
|
||||
AND e.is_admin = true
|
||||
)
|
||||
);
|
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
# Create support requests table
|
||||
|
||||
1. New Table
|
||||
- support_requests
|
||||
- id (uuid, primary key)
|
||||
- employee_id (uuid, foreign key to employees)
|
||||
- department (text)
|
||||
- request_type (request_type enum)
|
||||
- priority (request_priority enum)
|
||||
- status (request_status enum)
|
||||
- description (text)
|
||||
- created_at (timestamptz)
|
||||
|
||||
2. Security
|
||||
- Enable RLS
|
||||
- Add request viewing, creation, and update policies
|
||||
- Add performance index for employee lookups
|
||||
*/
|
||||
|
||||
-- Create support requests table
|
||||
CREATE TABLE IF NOT EXISTS support_requests (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
employee_id uuid REFERENCES employees(id),
|
||||
department text NOT NULL,
|
||||
request_type request_type NOT NULL,
|
||||
priority request_priority NOT NULL,
|
||||
description text,
|
||||
status request_status NOT NULL DEFAULT 'new',
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable RLS for support requests
|
||||
ALTER TABLE support_requests ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create index for better query performance
|
||||
CREATE INDEX IF NOT EXISTS idx_support_requests_employee_id
|
||||
ON support_requests(employee_id);
|
||||
|
||||
-- Create RLS policies for support requests
|
||||
CREATE POLICY "Employees can view their own requests"
|
||||
ON support_requests
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (employee_id = auth.uid());
|
||||
|
||||
CREATE POLICY "Employees can create their own requests"
|
||||
ON support_requests
|
||||
FOR INSERT
|
||||
TO authenticated
|
||||
WITH CHECK (employee_id = auth.uid());
|
||||
|
||||
CREATE POLICY "Employees can update their own requests"
|
||||
ON support_requests
|
||||
FOR UPDATE
|
||||
TO authenticated
|
||||
USING (employee_id = auth.uid());
|
@@ -1,80 +0,0 @@
|
||||
/*
|
||||
# Fix employee creation functions
|
||||
|
||||
1. Changes
|
||||
- Drop and recreate validate_password function with correct return type
|
||||
- Update create_employee function
|
||||
|
||||
2. Security
|
||||
- Maintain SECURITY DEFINER
|
||||
- Secure password handling
|
||||
*/
|
||||
|
||||
-- Drop existing functions
|
||||
DROP FUNCTION IF EXISTS validate_password(text);
|
||||
DROP FUNCTION IF EXISTS create_employee(text, text, text, text);
|
||||
|
||||
-- Recreate validate_password function
|
||||
CREATE OR REPLACE FUNCTION validate_password(password text)
|
||||
RETURNS void
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
IF length(password) < 6 THEN
|
||||
RAISE EXCEPTION 'Password must be at least 6 characters long';
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- Create employee function with auth integration
|
||||
CREATE OR REPLACE FUNCTION create_employee(
|
||||
p_first_name text,
|
||||
p_last_name text,
|
||||
p_department text,
|
||||
p_password text
|
||||
)
|
||||
RETURNS employees
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
AS $$
|
||||
DECLARE
|
||||
v_employee employees;
|
||||
BEGIN
|
||||
-- Validate input
|
||||
IF p_first_name IS NULL OR p_first_name = '' THEN
|
||||
RAISE EXCEPTION 'First name is required';
|
||||
END IF;
|
||||
|
||||
IF p_last_name IS NULL OR p_last_name = '' THEN
|
||||
RAISE EXCEPTION 'Last name is required';
|
||||
END IF;
|
||||
|
||||
IF p_department IS NULL OR p_department = '' THEN
|
||||
RAISE EXCEPTION 'Department is required';
|
||||
END IF;
|
||||
|
||||
-- Validate password
|
||||
PERFORM validate_password(p_password);
|
||||
|
||||
-- Create employee record
|
||||
INSERT INTO employees (
|
||||
first_name,
|
||||
last_name,
|
||||
department,
|
||||
email,
|
||||
password_hash
|
||||
) VALUES (
|
||||
p_first_name,
|
||||
p_last_name,
|
||||
p_department,
|
||||
lower(p_last_name) || '@example.com',
|
||||
hash_password(p_password)
|
||||
)
|
||||
RETURNING * INTO v_employee;
|
||||
|
||||
RETURN v_employee;
|
||||
EXCEPTION
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Failed to create employee: %', SQLERRM;
|
||||
END;
|
||||
$$;
|
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
# Fix employee management functions
|
||||
|
||||
1. Changes
|
||||
- Drop and recreate validate_password function with proper return type
|
||||
- Create improved create_employee function with better validation
|
||||
|
||||
2. Security
|
||||
- Maintain SECURITY DEFINER for sensitive operations
|
||||
- Secure password validation and hashing
|
||||
- Input validation for all fields
|
||||
*/
|
||||
|
||||
-- Drop existing validate_password function if it exists
|
||||
DROP FUNCTION IF EXISTS validate_password(text);
|
||||
|
||||
-- Recreate validate_password function with better validation
|
||||
CREATE OR REPLACE FUNCTION validate_password(password text)
|
||||
RETURNS boolean
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
-- Check password length
|
||||
IF length(password) < 6 THEN
|
||||
RETURN false;
|
||||
END IF;
|
||||
|
||||
RETURN true;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- Create employee function with improved validation
|
||||
CREATE OR REPLACE FUNCTION create_employee(
|
||||
p_first_name text,
|
||||
p_last_name text,
|
||||
p_department text,
|
||||
p_password text
|
||||
)
|
||||
RETURNS employees
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
AS $$
|
||||
DECLARE
|
||||
v_employee employees;
|
||||
BEGIN
|
||||
-- Validate input
|
||||
IF p_first_name IS NULL OR p_first_name = '' THEN
|
||||
RAISE EXCEPTION 'First name is required';
|
||||
END IF;
|
||||
|
||||
IF p_last_name IS NULL OR p_last_name = '' THEN
|
||||
RAISE EXCEPTION 'Last name is required';
|
||||
END IF;
|
||||
|
||||
IF p_department IS NULL OR p_department = '' THEN
|
||||
RAISE EXCEPTION 'Department is required';
|
||||
END IF;
|
||||
|
||||
-- Validate password
|
||||
IF NOT validate_password(p_password) THEN
|
||||
RAISE EXCEPTION 'Password must be at least 6 characters long';
|
||||
END IF;
|
||||
|
||||
-- Create employee record
|
||||
INSERT INTO employees (
|
||||
first_name,
|
||||
last_name,
|
||||
department,
|
||||
email,
|
||||
password_hash
|
||||
) VALUES (
|
||||
p_first_name,
|
||||
p_last_name,
|
||||
p_department,
|
||||
lower(p_last_name) || '@example.com',
|
||||
hash_password(p_password)
|
||||
)
|
||||
RETURNING * INTO v_employee;
|
||||
|
||||
RETURN v_employee;
|
||||
EXCEPTION
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Failed to create employee: %', SQLERRM;
|
||||
END;
|
||||
$$;
|
@@ -1,93 +0,0 @@
|
||||
/*
|
||||
# Fix employee creation process
|
||||
|
||||
1. Changes
|
||||
- Add proper error handling for auth user creation
|
||||
- Ensure atomic transaction for employee creation
|
||||
- Add better validation for employee data
|
||||
- Fix duplicate email handling
|
||||
|
||||
2. Security
|
||||
- Maintain RLS policies
|
||||
- Add proper role checks
|
||||
*/
|
||||
|
||||
-- Drop existing function if exists
|
||||
DROP FUNCTION IF EXISTS create_employee(text, text, text, text);
|
||||
|
||||
-- Create improved employee creation function
|
||||
CREATE OR REPLACE FUNCTION create_employee(
|
||||
p_first_name text,
|
||||
p_last_name text,
|
||||
p_department text,
|
||||
p_password text
|
||||
)
|
||||
RETURNS employees
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
AS $$
|
||||
DECLARE
|
||||
v_employee employees;
|
||||
v_email text;
|
||||
BEGIN
|
||||
-- Input validation
|
||||
IF p_first_name IS NULL OR p_first_name = '' THEN
|
||||
RAISE EXCEPTION 'First name is required';
|
||||
END IF;
|
||||
|
||||
IF p_last_name IS NULL OR p_last_name = '' THEN
|
||||
RAISE EXCEPTION 'Last name is required';
|
||||
END IF;
|
||||
|
||||
IF p_department IS NULL OR p_department = '' THEN
|
||||
RAISE EXCEPTION 'Department is required';
|
||||
END IF;
|
||||
|
||||
IF p_password IS NULL OR length(p_password) < 6 THEN
|
||||
RAISE EXCEPTION 'Password must be at least 6 characters long';
|
||||
END IF;
|
||||
|
||||
-- Generate unique email
|
||||
v_email := lower(p_last_name) || '@example.com';
|
||||
|
||||
-- Create employee record
|
||||
INSERT INTO employees (
|
||||
first_name,
|
||||
last_name,
|
||||
department,
|
||||
email,
|
||||
password_hash
|
||||
) VALUES (
|
||||
p_first_name,
|
||||
p_last_name,
|
||||
p_department,
|
||||
v_email,
|
||||
hash_password(p_password)
|
||||
)
|
||||
RETURNING * INTO v_employee;
|
||||
|
||||
-- Create auth user
|
||||
INSERT INTO auth.users (
|
||||
email,
|
||||
encrypted_password,
|
||||
email_confirmed_at,
|
||||
raw_user_meta_data
|
||||
) VALUES (
|
||||
v_email,
|
||||
hash_password(p_password),
|
||||
now(),
|
||||
jsonb_build_object(
|
||||
'first_name', p_first_name,
|
||||
'last_name', p_last_name,
|
||||
'department', p_department
|
||||
)
|
||||
);
|
||||
|
||||
RETURN v_employee;
|
||||
EXCEPTION
|
||||
WHEN unique_violation THEN
|
||||
RAISE EXCEPTION 'Employee with this email already exists';
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Failed to create employee: %', SQLERRM;
|
||||
END;
|
||||
$$;
|
@@ -1,89 +0,0 @@
|
||||
/*
|
||||
# Fix database constraints and password validation
|
||||
|
||||
1. Changes
|
||||
- Add password validation function
|
||||
- Add employee creation function with validation
|
||||
- Add indexes for performance optimization
|
||||
- Update RLS policies
|
||||
|
||||
2. Security
|
||||
- Maintain RLS policies
|
||||
- Add proper validation for passwords
|
||||
*/
|
||||
|
||||
-- Update password validation function
|
||||
CREATE OR REPLACE FUNCTION validate_password(password text)
|
||||
RETURNS boolean
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
IF length(password) < 6 THEN
|
||||
RAISE EXCEPTION 'Password must be at least 6 characters long';
|
||||
END IF;
|
||||
RETURN true;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- Update create_employee function to use password validation
|
||||
CREATE OR REPLACE FUNCTION create_employee(
|
||||
p_first_name text,
|
||||
p_last_name text,
|
||||
p_department text,
|
||||
p_password text
|
||||
)
|
||||
RETURNS employees
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
AS $$
|
||||
DECLARE
|
||||
v_employee employees;
|
||||
BEGIN
|
||||
-- Validate password
|
||||
PERFORM validate_password(p_password);
|
||||
|
||||
-- Create employee
|
||||
INSERT INTO employees (
|
||||
first_name,
|
||||
last_name,
|
||||
department,
|
||||
password_hash
|
||||
) VALUES (
|
||||
p_first_name,
|
||||
p_last_name,
|
||||
p_department,
|
||||
hash_password(p_password)
|
||||
)
|
||||
RETURNING * INTO v_employee;
|
||||
|
||||
RETURN v_employee;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- Add indexes for better performance if they don't exist
|
||||
CREATE INDEX IF NOT EXISTS idx_employees_last_name ON employees(last_name);
|
||||
CREATE INDEX IF NOT EXISTS idx_support_requests_created_at ON support_requests(created_at DESC);
|
||||
|
||||
-- Update RLS policies for support_requests
|
||||
DROP POLICY IF EXISTS "Users can create their own requests" ON support_requests;
|
||||
DROP POLICY IF EXISTS "Users can view their own requests" ON support_requests;
|
||||
DROP POLICY IF EXISTS "Users can update their own requests" ON support_requests;
|
||||
|
||||
CREATE POLICY "Employees can create their own requests"
|
||||
ON support_requests
|
||||
FOR INSERT
|
||||
TO authenticated
|
||||
WITH CHECK (user_id = auth.uid());
|
||||
|
||||
CREATE POLICY "Employees can view their own requests"
|
||||
ON support_requests
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (user_id = auth.uid());
|
||||
|
||||
CREATE POLICY "Employees can update their own requests"
|
||||
ON support_requests
|
||||
FOR UPDATE
|
||||
TO authenticated
|
||||
USING (user_id = auth.uid())
|
||||
WITH CHECK (user_id = auth.uid());
|
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
# Update employees table and policies
|
||||
|
||||
1. Table Creation
|
||||
- employees
|
||||
- id (uuid, primary key)
|
||||
- first_name (text)
|
||||
- last_name (text)
|
||||
- department (text)
|
||||
- created_at (timestamptz)
|
||||
|
||||
2. Security
|
||||
- Enable RLS
|
||||
- Add employee viewing policy (if not exists)
|
||||
*/
|
||||
|
||||
-- Create employees table
|
||||
CREATE TABLE IF NOT EXISTS employees (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
first_name text NOT NULL,
|
||||
last_name text NOT NULL,
|
||||
department text NOT NULL,
|
||||
created_at timestamptz DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable RLS for employees
|
||||
ALTER TABLE employees ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Safely create policy if it doesn't exist
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_policies
|
||||
WHERE tablename = 'employees'
|
||||
AND policyname = 'Employees can view their own profile'
|
||||
) THEN
|
||||
CREATE POLICY "Employees can view their own profile"
|
||||
ON employees
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (id = auth.uid());
|
||||
END IF;
|
||||
END $$;
|
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
# Add Employee Creation Function
|
||||
|
||||
1. Changes
|
||||
- Add secure function to create new employees with password hashing
|
||||
|
||||
2. Security
|
||||
- Uses hash_password function for secure password storage
|
||||
- SECURITY DEFINER to ensure proper access control
|
||||
- Returns employee data without password hash
|
||||
*/
|
||||
|
||||
-- Create function to create employee with password
|
||||
CREATE OR REPLACE FUNCTION create_employee(
|
||||
p_first_name text,
|
||||
p_last_name text,
|
||||
p_department text,
|
||||
p_password text
|
||||
)
|
||||
RETURNS employees
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
AS $$
|
||||
DECLARE
|
||||
v_employee employees;
|
||||
BEGIN
|
||||
-- Validate input
|
||||
IF p_password IS NULL OR length(p_password) < 6 THEN
|
||||
RAISE EXCEPTION 'Password must be at least 6 characters long';
|
||||
END IF;
|
||||
|
||||
-- Create employee with hashed password
|
||||
INSERT INTO employees (
|
||||
first_name,
|
||||
last_name,
|
||||
department,
|
||||
password_hash
|
||||
) VALUES (
|
||||
p_first_name,
|
||||
p_last_name,
|
||||
p_department,
|
||||
hash_password(p_password)
|
||||
)
|
||||
RETURNING * INTO v_employee;
|
||||
|
||||
-- Return employee data without password hash
|
||||
v_employee.password_hash := NULL;
|
||||
RETURN v_employee;
|
||||
END;
|
||||
$$;
|
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
# Password Management Functions
|
||||
|
||||
1. New Functions
|
||||
- Password hashing and verification utilities
|
||||
- Secure password management
|
||||
*/
|
||||
|
||||
-- Create password hashing function
|
||||
CREATE OR REPLACE FUNCTION hash_password(password text)
|
||||
RETURNS text
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
RETURN crypt(password, gen_salt('bf'));
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- Create password verification function
|
||||
CREATE OR REPLACE FUNCTION verify_password(stored_hash text, password text)
|
||||
RETURNS boolean
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
RETURN stored_hash = crypt(password, stored_hash);
|
||||
END;
|
||||
$$;
|
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
# Создание системы обратной связи
|
||||
|
||||
1. Новые таблицы
|
||||
- request_feedback (обратная связь по заявкам)
|
||||
- id (uuid, первичный ключ)
|
||||
- request_id (id заявки)
|
||||
- rating (оценка)
|
||||
- comment (комментарий)
|
||||
- created_by (кто создал)
|
||||
- created_at (дата создания)
|
||||
|
||||
2. Безопасность
|
||||
- Включение RLS
|
||||
- Политики доступа для управления отзывами
|
||||
*/
|
||||
|
||||
-- Создание таблицы обратной связи
|
||||
CREATE TABLE IF NOT EXISTS request_feedback (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
request_id uuid REFERENCES support_requests(id) ON DELETE CASCADE,
|
||||
rating integer NOT NULL CHECK (rating >= 1 AND rating <= 5),
|
||||
comment text,
|
||||
created_by uuid REFERENCES employees(id),
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- Создание индекса
|
||||
CREATE INDEX IF NOT EXISTS idx_request_feedback_request_id
|
||||
ON request_feedback(request_id);
|
||||
|
||||
-- Включение RLS
|
||||
ALTER TABLE request_feedback ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Создание политик
|
||||
CREATE POLICY "Сотрудники могут оставлять отзывы о своих заявках"
|
||||
ON request_feedback
|
||||
FOR INSERT
|
||||
TO authenticated
|
||||
WITH CHECK (
|
||||
EXISTS (
|
||||
SELECT 1 FROM support_requests
|
||||
WHERE id = request_id
|
||||
AND employee_id = auth.uid()
|
||||
)
|
||||
);
|
||||
|
||||
CREATE POLICY "Сотрудники могут видеть отзывы о своих заявках"
|
||||
ON request_feedback
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (
|
||||
EXISTS (
|
||||
SELECT 1 FROM support_requests
|
||||
WHERE id = request_feedback.request_id
|
||||
AND employee_id = auth.uid()
|
||||
)
|
||||
);
|
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
# Система приоритетов заявок
|
||||
|
||||
1. Новые таблицы
|
||||
- `request_priorities`
|
||||
- `id` (uuid, primary key)
|
||||
- `name` (text, unique)
|
||||
- `description` (text)
|
||||
- `color` (text)
|
||||
- `sla_hours` (integer)
|
||||
- `created_at` (timestamptz)
|
||||
|
||||
2. Безопасность
|
||||
- Включение RLS
|
||||
- Политики для чтения и управления
|
||||
*/
|
||||
|
||||
DO $$ BEGIN
|
||||
-- Создание таблицы приоритетов, если она не существует
|
||||
CREATE TABLE IF NOT EXISTS request_priorities (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name text NOT NULL UNIQUE,
|
||||
description text,
|
||||
color text NOT NULL,
|
||||
sla_hours integer NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- Включение RLS
|
||||
ALTER TABLE request_priorities ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Безопасное создание политик с проверкой существования
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_policies
|
||||
WHERE tablename = 'request_priorities'
|
||||
AND policyname = 'Все могут просматривать приоритеты'
|
||||
) THEN
|
||||
CREATE POLICY "Все могут просматривать приоритеты"
|
||||
ON request_priorities
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (true);
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_policies
|
||||
WHERE tablename = 'request_priorities'
|
||||
AND policyname = 'Только администраторы могут управлять приоритетами'
|
||||
) THEN
|
||||
CREATE POLICY "Только администраторы могут управлять приоритетами"
|
||||
ON request_priorities
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (auth.jwt() ->> 'role' = 'admin');
|
||||
END IF;
|
||||
END $$;
|
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
# User Management Functions
|
||||
|
||||
1. New Functions
|
||||
- User creation with password validation
|
||||
- User authentication
|
||||
*/
|
||||
|
||||
-- Create user management function
|
||||
CREATE OR REPLACE FUNCTION create_user(
|
||||
p_first_name text,
|
||||
p_last_name text,
|
||||
p_department text,
|
||||
p_password text
|
||||
)
|
||||
RETURNS users
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
AS $$
|
||||
DECLARE
|
||||
v_user users;
|
||||
BEGIN
|
||||
-- Validate password length
|
||||
IF length(p_password) < 4 THEN
|
||||
RAISE EXCEPTION 'Password must be at least 4 characters long';
|
||||
END IF;
|
||||
|
||||
-- Create user
|
||||
INSERT INTO users (
|
||||
first_name,
|
||||
last_name,
|
||||
department,
|
||||
password_hash
|
||||
) VALUES (
|
||||
p_first_name,
|
||||
p_last_name,
|
||||
p_department,
|
||||
hash_password(p_password)
|
||||
)
|
||||
RETURNING * INTO v_user;
|
||||
|
||||
RETURN v_user;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- Create authentication function
|
||||
CREATE OR REPLACE FUNCTION authenticate_user(
|
||||
p_last_name text,
|
||||
p_password text
|
||||
)
|
||||
RETURNS TABLE (
|
||||
id uuid,
|
||||
first_name text,
|
||||
last_name text,
|
||||
department text,
|
||||
created_at timestamptz
|
||||
)
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
AS $$
|
||||
BEGIN
|
||||
RETURN QUERY
|
||||
SELECT
|
||||
u.id,
|
||||
u.first_name,
|
||||
u.last_name,
|
||||
u.department,
|
||||
u.created_at
|
||||
FROM users u
|
||||
WHERE u.last_name = p_last_name
|
||||
AND verify_password(u.password_hash, p_password);
|
||||
END;
|
||||
$$;
|
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
# Добавление системы категорий заявок
|
||||
|
||||
1. Новые таблицы
|
||||
- `request_categories`
|
||||
- `id` (uuid, primary key)
|
||||
- `name` (text, unique)
|
||||
- `description` (text)
|
||||
- `is_active` (boolean)
|
||||
- `created_at` (timestamp)
|
||||
|
||||
2. Безопасность
|
||||
- Включение RLS на таблице request_categories
|
||||
- Политика для чтения всеми авторизованными пользователями
|
||||
- Политика для управления только администраторами
|
||||
*/
|
||||
|
||||
DO $$ BEGIN
|
||||
-- Проверяем существование таблицы перед созданием
|
||||
IF NOT EXISTS (
|
||||
SELECT FROM pg_tables
|
||||
WHERE schemaname = 'public'
|
||||
AND tablename = 'request_categories'
|
||||
) THEN
|
||||
-- Создание таблицы категорий
|
||||
CREATE TABLE request_categories (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name text NOT NULL UNIQUE,
|
||||
description text,
|
||||
is_active boolean DEFAULT true,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- Включение RLS
|
||||
ALTER TABLE request_categories ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Создание политик
|
||||
CREATE POLICY "Все могут просматривать категории"
|
||||
ON request_categories
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (true);
|
||||
|
||||
CREATE POLICY "Только администраторы могут управлять категориями"
|
||||
ON request_categories
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (auth.jwt() ->> 'role' = 'admin');
|
||||
END IF;
|
||||
END $$;
|
@@ -1,17 +0,0 @@
|
||||
/*
|
||||
# Update employees table
|
||||
|
||||
1. Changes
|
||||
- Add `is_admin` column for admin access control
|
||||
- Add email generation function
|
||||
- Update existing records
|
||||
*/
|
||||
|
||||
-- Add admin flag
|
||||
ALTER TABLE employees
|
||||
ADD COLUMN IF NOT EXISTS is_admin boolean NOT NULL DEFAULT false;
|
||||
|
||||
-- Update existing admin users
|
||||
UPDATE employees
|
||||
SET is_admin = true
|
||||
WHERE email LIKE '%admin%';
|
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
# Create support requests table with employee relationship
|
||||
|
||||
1. Changes
|
||||
- Create support_requests table with proper foreign keys
|
||||
- Add RLS policies for access control
|
||||
- Handle existing enum types safely
|
||||
|
||||
2. Security
|
||||
- Enable RLS on support_requests table
|
||||
- Add policies for authenticated users and admins
|
||||
*/
|
||||
|
||||
-- Safely create enum types if they don't exist
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE request_type AS ENUM ('hardware', 'software', 'network', 'access', 'other');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN NULL;
|
||||
END $$;
|
||||
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE request_priority AS ENUM ('low', 'medium', 'high', 'critical');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN NULL;
|
||||
END $$;
|
||||
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE request_status AS ENUM ('new', 'in_progress', 'resolved', 'closed');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN NULL;
|
||||
END $$;
|
||||
|
||||
-- Create support requests table with proper foreign key
|
||||
CREATE TABLE IF NOT EXISTS support_requests (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
employee_id uuid REFERENCES employees(id) ON DELETE CASCADE NOT NULL,
|
||||
department text NOT NULL,
|
||||
request_type request_type NOT NULL,
|
||||
priority request_priority NOT NULL,
|
||||
status request_status NOT NULL DEFAULT 'new',
|
||||
description text NOT NULL DEFAULT '',
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
last_status_change timestamptz DEFAULT now()
|
||||
);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE support_requests ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policies
|
||||
CREATE POLICY "Users can view their own requests"
|
||||
ON support_requests
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (employee_id = auth.uid());
|
||||
|
||||
CREATE POLICY "Users can create their own requests"
|
||||
ON support_requests
|
||||
FOR INSERT
|
||||
TO authenticated
|
||||
WITH CHECK (employee_id = auth.uid());
|
||||
|
||||
CREATE POLICY "Admins can view all requests"
|
||||
ON support_requests
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (
|
||||
EXISTS (
|
||||
SELECT 1 FROM employees e
|
||||
WHERE e.id = auth.uid()
|
||||
AND e.is_admin = true
|
||||
)
|
||||
);
|
@@ -3,7 +3,7 @@
|
||||
<div class="max-w-md w-full bg-white rounded-xl shadow-2xl p-8">
|
||||
<div class="text-center mb-8">
|
||||
<h2 class="text-3xl font-bold text-gray-900">
|
||||
Панель администратора
|
||||
Панель администратора 55
|
||||
</h2>
|
||||
<p class="mt-2 text-gray-600">
|
||||
Вход в систему управления
|
||||
|
Reference in New Issue
Block a user