Spaces:
Running

math-tutor / db /003_roster.sql
seongui2030
deploy 2026-12-03 20:26
b146445
Raw History Blame Contribute Delete
5.09 kB
-- =====================================================================
-- 003_roster.sql : ν—ˆμš© λͺ…단(allowlist) + 둜그인 μ‹œ μžλ™ μ—­ν• Β·ν•™κΈ‰ λ°°μ •
--
-- ν•˜λŠ” 일
-- 1) allowed_users : ν•™μƒΒ·κ΅μ‚¬Β·κ΄€λ¦¬μž λͺ…단. 이 ν‘œμ— μ—†λŠ” ꡬ글 계정은 둜그인(졜초 κ°€μž…)이 λ§‰νž™λ‹ˆλ‹€.
-- 2) handle_new_user() λ₯Ό λ°”κΏˆ : λͺ…단에 있으면 μ—­ν• (student/teacher/admin)·이름·학급을 μžλ™μœΌλ‘œ λ„£κ³ ,
-- λͺ…단에 μ—†μœΌλ©΄ 'not_allowed' 였λ₯˜λ‘œ κ°€μž…μ„ κ±°μ ˆν•©λ‹ˆλ‹€.
-- 3) sync_roster() : 이미 κ°€μž…ν•œ μ‚¬λžŒμ˜ 역할·학급을 λͺ…λ‹¨λŒ€λ‘œ λ‹€μ‹œ 맞좀 (λͺ…단을 고친 λ’€ μ‹€ν–‰)
--
-- μ‹€ν–‰ μˆœμ„œ (μ€‘μš”): 이 파일 μ‹€ν–‰ -> 08_import_roster.py 둜 λͺ…단 적재 -> κ·Έ λ‹€μŒμ— κ΅¬κΈ€λ‘œ 둜그인
-- (λͺ…단을 λ„£κΈ° 전에 λ‘œκ·ΈμΈν•˜λ©΄ κ΄€λ¦¬μžλ„ κ±°μ ˆλ©λ‹ˆλ‹€. κ±°μ ˆλΌλ„ λͺ…단 적재 ν›„ λ‹€μ‹œ λ‘œκ·ΈμΈν•˜λ©΄ λ©λ‹ˆλ‹€.)
-- μ—¬λŸ¬ 번 싀행해도 μ•ˆμ „ν•©λ‹ˆλ‹€.
-- =====================================================================
create table if not exists allowed_users (
email text primary key check (email = lower(email)), -- 항상 μ†Œλ¬Έμž
name text,
role user_role not null,
school_year smallint not null default 2026, -- 학년도 (3월에 μƒˆ 학년이 되면 μƒˆλ‘œ 적재)
grade smallint check (grade between 1 and 3),
class_no smallint check (class_no >= 1),
student_no smallint, -- 번호 (학생)
subject text, -- λ‹΄λ‹Ή κ³Όλͺ© (ꡐ사)
active boolean not null default true, -- false 둜 λ°”κΎΈλ©΄ μ‹ κ·œ κ°€μž… λΆˆκ°€
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
drop trigger if exists trg_allowed_users_updated on allowed_users;
create trigger trg_allowed_users_updated before update on allowed_users
for each row execute function set_updated_at();
-- λͺ…단은 κ°œμΈμ •λ³΄: μ•± μ„œλ²„(service_role)만 λ‹€λ£Ήλ‹ˆλ‹€. 일반 둜그인 μ‚¬μš©μžμ—κ²ŒλŠ” 정책을 μ£Όμ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
alter table allowed_users enable row level security;
grant select, insert, update, delete on allowed_users to service_role;
-- ν•œ μ‚¬λžŒμ˜ ν•™κΈ‰ μ†Œμ†μ„ λͺ…단 κΈ°μ€€μœΌλ‘œ 맞좀: 학생은 ν•™κΈ‰ κ΅¬μ„±μ›μœΌλ‘œ, κ΅μ‚¬λŠ” λ‹΄μž„μœΌλ‘œ
create or replace function apply_roster_row(uid uuid, r allowed_users)
returns void language plpgsql security definer set search_path = public as $$
declare cid uuid;
begin
if r.grade is null or r.class_no is null then
return;
end if;
insert into classes (school_year, grade, class_no)
values (r.school_year, r.grade, r.class_no)
on conflict (school_year, grade, class_no) do nothing;
select id into cid from classes
where school_year = r.school_year and grade = r.grade and class_no = r.class_no;
if r.role = 'student' then
insert into class_members (class_id, student_id, student_no)
values (cid, uid, r.student_no)
on conflict (class_id, student_id) do update set student_no = excluded.student_no;
elsif r.role = 'teacher' then
update classes set homeroom_teacher_id = uid where id = cid;
end if;
end $$;
-- ꡬ글 둜그인으둜 auth.users κ°€ μƒˆλ‘œ 생길 λ•Œ: λͺ…단에 μžˆμ–΄μ•Όλ§Œ 톡과
create or replace function handle_new_user()
returns trigger language plpgsql security definer set search_path = public as $$
declare r allowed_users;
begin
select * into r from allowed_users where email = lower(new.email) and active;
if not found then
raise exception 'not_allowed: ν—ˆμš© λͺ…단에 μ—†λŠ” κ³„μ •μž…λ‹ˆλ‹€.';
end if;
insert into profiles (id, email, name, role)
values (
new.id, lower(new.email),
coalesce(r.name, new.raw_user_meta_data ->> 'full_name', new.raw_user_meta_data ->> 'name'),
r.role
)
on conflict (id) do nothing;
perform apply_roster_row(new.id, r);
return new;
end $$;
drop trigger if exists on_auth_user_created on auth.users;
create trigger on_auth_user_created after insert on auth.users
for each row execute function handle_new_user();
-- 이미 κ°€μž…ν•œ μ‚¬λžŒμ˜ 역할·이름·학급을 λͺ…λ‹¨λŒ€λ‘œ λ‹€μ‹œ 맞좀. 맞좘 μ‚¬λžŒ 수λ₯Ό 돌렀쀌
create or replace function sync_roster()
returns integer language plpgsql security definer set search_path = public as $$
declare a allowed_users; n integer := 0;
begin
for a in select * from allowed_users where active loop
update profiles set role = a.role, name = coalesce(a.name, name)
where lower(email) = a.email;
if found then
perform apply_roster_row((select id from profiles where lower(email) = a.email limit 1), a);
n := n + 1;
end if;
end loop;
return n;
end $$;
revoke execute on function sync_roster() from public, anon, authenticated;
revoke execute on function apply_roster_row(uuid, allowed_users) from public, anon, authenticated;
grant execute on function sync_roster() to service_role;
grant execute on function apply_roster_row(uuid, allowed_users) to service_role;