Login dengan Google UNTAN (OpenID Connect) yang dihubungkan ke akun aplikasi
- app/google_auth.py: authorization-code flow tanpa dependensi tambahan; klaim id_token diperiksa (iss, aud, exp, nonce, email_verified) dan email wajib domain GOOGLE_DOMAIN (bawaan untan.ac.id + subdomain). Aktif hanya bila GOOGLE_CLIENT_ID & SECRET terisi. - Akun Google ditautkan (pengguna.google_sub) oleh pemilik dari menu Akun, atau otomatis pada login pertama bila email sama dengan email akun / data induk mahasiswa. Tanpa pembuatan akun otomatis; satu Google satu akun; akun nonaktif ditolak. - Putus oleh pemilik mematikan tautan otomatis via email; putus oleh kaprodi (Data Master -> Dosen) mengizinkannya lagi. Login NIP/NPM + password tetap tersedia. - Uji: tests/uji_google.py (tiruan endpoint token Google). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
549fe53a11
commit
b09785fc62
@@ -605,6 +605,9 @@ def _migrasi(conn) -> None:
|
||||
conn.execute("ALTER TABLE pengguna ADD COLUMN aktif INTEGER NOT NULL DEFAULT 1")
|
||||
if "email" not in kol:
|
||||
conn.execute("ALTER TABLE pengguna ADD COLUMN email TEXT NOT NULL DEFAULT ''")
|
||||
# login Google: ID akun Google (klaim 'sub', tetap meski email berubah) yang dihubungkan
|
||||
if "google_sub" not in kol:
|
||||
conn.execute("ALTER TABLE pengguna ADD COLUMN google_sub TEXT NOT NULL DEFAULT ''")
|
||||
|
||||
|
||||
def kode_periode(tahun: str, jenis: str) -> str:
|
||||
@@ -926,6 +929,53 @@ def pengguna_by_id(user_id: int) -> dict | None:
|
||||
return dict(row) if row else None
|
||||
|
||||
|
||||
# ── akun Google terhubung ────────────────────────────────────────────────────
|
||||
|
||||
def pengguna_by_google(sub: str) -> dict | None:
|
||||
if not sub or sub == GOOGLE_DIPUTUS:
|
||||
return None
|
||||
with get_conn() as conn:
|
||||
row = conn.execute("SELECT * FROM pengguna WHERE google_sub=? AND aktif=1", (sub,)).fetchone()
|
||||
return dict(row) if row else None
|
||||
|
||||
|
||||
def pengguna_by_email(email: str) -> dict | None:
|
||||
"""Akun aktif ber-email ini (email akun, atau email data induk mahasiswa → akun NPM).
|
||||
Ambigu (lebih dari satu akun) → None, agar tidak salah menautkan."""
|
||||
email = (email or "").strip().lower()
|
||||
if not email:
|
||||
return None
|
||||
with get_conn() as conn:
|
||||
rows = conn.execute("SELECT * FROM pengguna WHERE lower(email)=? AND aktif=1", (email,)).fetchall()
|
||||
if not rows:
|
||||
rows = conn.execute(
|
||||
"SELECT p.* FROM pengguna p JOIN mhs m ON m.npm = p.username "
|
||||
"WHERE lower(m.email)=? AND p.aktif=1", (email,)).fetchall()
|
||||
return dict(rows[0]) if len(rows) == 1 else None
|
||||
|
||||
|
||||
def hubungkan_google(uid: int, sub: str, email: str) -> None:
|
||||
"""Tautkan akun Google ke akun; email akun diisi bila masih kosong."""
|
||||
with get_conn() as conn:
|
||||
conn.execute("UPDATE pengguna SET google_sub=?, email=CASE WHEN email='' THEN ? ELSE email END "
|
||||
"WHERE id=?", (sub, email, uid))
|
||||
|
||||
|
||||
GOOGLE_DIPUTUS = "-" # diputus pemilik akun: tautan otomatis lewat email dimatikan
|
||||
|
||||
|
||||
def putus_google(uid: int, oleh_pemilik: bool = True) -> None:
|
||||
"""Putus tautan. Oleh pemilik → tidak tertaut otomatis lagi (hanya lewat Hubungkan);
|
||||
oleh kaprodi (mis. salah akun) → tautan otomatis via email kembali diizinkan."""
|
||||
with get_conn() as conn:
|
||||
conn.execute("UPDATE pengguna SET google_sub=? WHERE id=?",
|
||||
(GOOGLE_DIPUTUS if oleh_pemilik else "", uid))
|
||||
|
||||
|
||||
def google_terhubung(p: dict) -> bool:
|
||||
return (p.get("google_sub") or "") not in ("", GOOGLE_DIPUTUS)
|
||||
|
||||
|
||||
def daftar_pengguna(termasuk_mahasiswa: bool = False) -> list[dict]:
|
||||
"""Akun dosen/kaprodi (+ mahasiswa bila diminta) beserta penugasan & jumlah wali."""
|
||||
syarat = "" if termasuk_mahasiswa else "WHERE role<>'mahasiswa'"
|
||||
@@ -1404,7 +1454,8 @@ def daftar_dosen(periode: str | None = None, termasuk_nonaktif: bool = True) ->
|
||||
periode = periode or tahun_aktif()
|
||||
with get_conn() as conn:
|
||||
rows = [dict(r) for r in conn.execute(
|
||||
"SELECT id, username, nama, role, aktif, email FROM pengguna WHERE role<>'mahasiswa'"
|
||||
"SELECT id, username, nama, role, aktif, email, google_sub NOT IN ('', '-') AS google FROM pengguna "
|
||||
"WHERE role<>'mahasiswa'"
|
||||
+ ("" if termasuk_nonaktif else " AND aktif=1") + " ORDER BY aktif DESC, nama")]
|
||||
kls = {r[0]: r[1] for r in conn.execute(
|
||||
"SELECT user_id, COUNT(*) FROM pengajar WHERE periode=? GROUP BY user_id", (periode,))}
|
||||
|
||||
Reference in New Issue
Block a user