| <?php |
| namespace DAFME\Covid; |
| |
| class Users { |
| private static function getUserIdFromSub($sub) { |
| global $con; |
| $query = $con->prepare('SELECT id FROM users WHERE sub = ?'); |
| if (!$query->execute([$sub])) |
| return false; |
| |
| if ($query->rowCount() < 1) |
| return false; |
| |
| $row = $query->fetch(); |
| return $row['id'] ?? false; |
| } |
| |
| public static function add($sub, $email): int { |
| global $con; |
| $query = $con->prepare('INSERT INTO users (sub, email) VALUES (?, ?)'); |
| if (!$query->execute([$sub, $email])) |
| return false; |
| |
| return $con->lastInsertId(); |
| } |
| |
| public static function signIn($sub, $email): bool { |
| global $_SESSION; |
| |
| $userId = self::getUserIdFromSub($sub); |
| |
| if ($userId === false) |
| $userId = self::add($sub, $email); |
| |
| if ($userId === false) |
| return false; |
| |
| $_SESSION['userId'] = $userId; |
| return true; |
| } |
| |
| public static function signOut(): void { |
| global $_SESSION; |
| unset($_SESSION['userId']); |
| } |
| |
| public static function isSignedIn(): bool { |
| global $_SESSION; |
| return isset($_SESSION['userId']); |
| } |
| |
| public static function getUserId(): int { |
| global $_SESSION; |
| return (self::isSignedIn() ? $_SESSION['userId'] : -1); |
| } |
| } |