blob: 7679e04348202c581638744cf2e4d34c3119c4e5 [file] [log] [blame]
avm9996370995382020-09-23 01:03:01 +02001<?php
2namespace DAFME\Covid;
3
4class Users {
5 private static function getUserId($sub) {
6 global $con;
7 $query = $con->prepare('SELECT id FROM users WHERE sub = ?');
8 if (!$query->execute([$sub]))
9 return false;
10
11 if ($query->rowCount() < 1)
12 return false;
13
14 $row = $query->fetch();
15 return $row['id'] ?? false;
16 }
17
18 public static function add($sub, $email) {
19 global $con;
20 $query = $con->prepare('INSERT INTO users (sub, email) VALUES (?, ?)');
21 if (!$query->execute([$sub, $email]))
22 return false;
23
24 return $con->lastInsertId();
25 }
26
27 public static function signIn($sub, $email) {
28 global $_SESSION;
29
30 $userId = self::getUserId($sub);
31
32 if ($userId === false)
33 $userId = self::add($sub, $email);
34
35 if ($userId === false)
36 return false;
37
38 $_SESSION['userId'] = $userId;
39 return true;
40 }
41
42 public static function signOut() {
43 global $_SESSION;
44 unset($_SESSION['userId']);
45 }
46
47 public static function isSignedIn() {
48 global $_SESSION;
49 return isset($_SESSION['userId']);
50 }
51}