feat: 实现用户登录认证与权限控制系统

- 新增用户登录页面与API端点(/api/auth/login、/api/auth/logout、/api/auth/me)
- 引入基于密码哈希的认证机制,支持默认管理员账号登录
- 创建访问控制模块(lib/access-control.ts),实现基于角色的权限验证
- 在现有API端点中集成权限检查,限制非管理员用户只能访问其所属项目数据
- 更新应用布局,要求用户登录后才能访问主界面
- 扩展用户表结构,增加用户名和密码哈希字段
- 调整项目、KR、目标、会议记录等资源的查询逻辑,根据用户权限过滤数据
- 改进管理页面(指标、模板)的删除操作,使用确认对话框替代直接确认
- 优化“我的KR”页面,为管理员提供全员KR概览视图
- 修复.gitignore文件中的拼写错误(.trae → .travis)
- 添加数据库迁移脚本,支持任务直接关联项目、禁用RLS、添加求助字段等
- 配置Next.js以支持独立输出和子路径部署
This commit is contained in:
2026-01-28 12:57:36 +08:00
parent e3e5e89df6
commit 7948b1afc7
50 changed files with 9506 additions and 150 deletions

View File

@@ -0,0 +1,68 @@
-- Create tasks table
CREATE TABLE IF NOT EXISTS tasks (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
kr_id UUID NOT NULL REFERENCES krs(id) ON DELETE CASCADE,
title TEXT NOT NULL,
description TEXT, -- 思路
status TEXT NOT NULL DEFAULT 'todo', -- todo, in_progress, expected, done
priority INTEGER DEFAULT 50, -- 重要级 0-100
urgency INTEGER DEFAULT 50, -- 紧急程度 0-100
owner_user_id UUID REFERENCES users(id), -- 负责人
executor_ids UUID[] DEFAULT '{}', -- 执行人列表
start_date DATE,
end_date DATE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- Create task_steps table
CREATE TABLE IF NOT EXISTS task_steps (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
task_id UUID NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
content TEXT NOT NULL,
note TEXT,
executor_id UUID REFERENCES users(id),
start_date DATE,
end_date DATE,
is_completed BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- Add indexes
CREATE INDEX IF NOT EXISTS idx_tasks_kr_id ON tasks(kr_id);
CREATE INDEX IF NOT EXISTS idx_task_steps_task_id ON task_steps(task_id);
-- Enable RLS
ALTER TABLE tasks ENABLE ROW LEVEL SECURITY;
ALTER TABLE task_steps ENABLE ROW LEVEL SECURITY;
-- RLS Policies for tasks
CREATE POLICY "Users can view tasks of visible KRs" ON tasks
FOR SELECT USING (
EXISTS (
SELECT 1 FROM krs
WHERE krs.id = tasks.kr_id
AND (
-- KR visibility logic (simplified for now: if you can see the project, you can see the KR)
EXISTS (
SELECT 1 FROM projects WHERE projects.id = krs.project_id
)
)
)
);
CREATE POLICY "Users can insert tasks" ON tasks
FOR INSERT WITH CHECK (true); -- Allow all authenticated users to create tasks for now
CREATE POLICY "Users can update tasks" ON tasks
FOR UPDATE USING (true);
CREATE POLICY "Users can delete tasks" ON tasks
FOR DELETE USING (true);
-- RLS Policies for task_steps
CREATE POLICY "Users can view steps" ON task_steps FOR SELECT USING (true);
CREATE POLICY "Users can insert steps" ON task_steps FOR INSERT WITH CHECK (true);
CREATE POLICY "Users can update steps" ON task_steps FOR UPDATE USING (true);
CREATE POLICY "Users can delete steps" ON task_steps FOR DELETE USING (true);

View File

@@ -0,0 +1,10 @@
-- Add project_id column to tasks table and make kr_id nullable
ALTER TABLE tasks
ADD COLUMN project_id UUID REFERENCES projects(id) ON DELETE CASCADE;
-- Make kr_id nullable since tasks can now belong directly to a project
ALTER TABLE tasks
ALTER COLUMN kr_id DROP NOT NULL;
-- Create index for project_id for better query performance
CREATE INDEX idx_tasks_project_id ON tasks(project_id);

View File

@@ -0,0 +1,56 @@
alter table public.tasks
add column if not exists help_requested_to_user_id uuid null,
add column if not exists help_requested_by_user_id uuid null,
add column if not exists help_requested_at timestamptz null;
alter table public.task_steps
add column if not exists help_requested_to_user_id uuid null,
add column if not exists help_requested_by_user_id uuid null,
add column if not exists help_requested_at timestamptz null;
do $$
begin
if not exists (
select 1 from pg_constraint where conname = 'tasks_help_requested_to_user_id_fkey'
) then
alter table public.tasks
add constraint tasks_help_requested_to_user_id_fkey
foreign key (help_requested_to_user_id) references public.users(id);
end if;
if not exists (
select 1 from pg_constraint where conname = 'tasks_help_requested_by_user_id_fkey'
) then
alter table public.tasks
add constraint tasks_help_requested_by_user_id_fkey
foreign key (help_requested_by_user_id) references public.users(id);
end if;
if not exists (
select 1 from pg_constraint where conname = 'task_steps_help_requested_to_user_id_fkey'
) then
alter table public.task_steps
add constraint task_steps_help_requested_to_user_id_fkey
foreign key (help_requested_to_user_id) references public.users(id);
end if;
if not exists (
select 1 from pg_constraint where conname = 'task_steps_help_requested_by_user_id_fkey'
) then
alter table public.task_steps
add constraint task_steps_help_requested_by_user_id_fkey
foreign key (help_requested_by_user_id) references public.users(id);
end if;
end $$;
create index if not exists tasks_help_requested_to_user_id_idx
on public.tasks(help_requested_to_user_id);
create index if not exists tasks_help_requested_by_user_id_idx
on public.tasks(help_requested_by_user_id);
create index if not exists task_steps_help_requested_to_user_id_idx
on public.task_steps(help_requested_to_user_id);
create index if not exists task_steps_help_requested_by_user_id_idx
on public.task_steps(help_requested_by_user_id);

View File

@@ -0,0 +1,15 @@
alter table public.users
add column if not exists username text null,
add column if not exists password_hash text null;
do $$
begin
if not exists (
select 1
from pg_indexes
where schemaname = 'public'
and indexname = 'users_username_unique'
) then
create unique index users_username_unique on public.users (username) where username is not null;
end if;
end $$;

View File

@@ -0,0 +1,2 @@
alter table public.tasks disable row level security;
alter table public.task_steps disable row level security;