avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | namespace DAFME\Covid; |
| 3 | |
| 4 | class 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 | } |