Spaces:
Running
Running
Download db/003_roster.sql from lilyjeongwon/math-tutor: direct link, hf CLI and curl.
- Browser
- Download file 5.09 kB
-
https://huggingface.co/spaces/lilyjeongwon/math-tutor/resolve/main/db/003_roster.sql
- Command line
-
hf download hf://spaces/lilyjeongwon/math-tutor/db/003_roster.sql
-
curl -L -o 003_roster.sql https://huggingface.co/spaces/lilyjeongwon/math-tutor/resolve/main/db/003_roster.sql
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; | |