Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.14% |
52 / 59 |
|
75.00% |
15 / 20 |
CRAP | |
0.00% |
0 / 1 |
| Auth | |
87.72% |
50 / 57 |
|
75.00% |
15 / 20 |
46.42 | |
0.00% |
0 / 1 |
| generateLoginToken | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| init | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
7 | |||
| isLoggedIn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getUserID | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getUser | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| isAdmin | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| hasPremium | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| premiumLevel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| saveUser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| logout | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getWithDefault | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| isInLevelMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isInRatingMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isInTimeMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addSuspicion | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| XPisGainedInCurrentMode | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| ratingisGainedInCurrentMode | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| getRemainingHealth | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| lightMode | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
7.33 | |||
| 1 | <?php |
| 2 | |
| 3 | App::uses('Constants', 'Utility'); |
| 4 | App::uses('JwtAuth', 'Utility'); |
| 5 | |
| 6 | class Auth |
| 7 | { |
| 8 | /** |
| 9 | * Generate random login token for phpBB2 forum SSO |
| 10 | * The forum reads this cookie to authenticate users automatically |
| 11 | */ |
| 12 | private static function generateLoginToken(int $user_id): void |
| 13 | { |
| 14 | $token = Util::generateRandomString(50); |
| 15 | Auth::getUser()['login_token'] = $token; |
| 16 | Auth::saveUser(); |
| 17 | Util::setCookie('login_token', $token); |
| 18 | } |
| 19 | |
| 20 | public static function init($user = null): void |
| 21 | { |
| 22 | // a hack to inject login in test environment |
| 23 | if (Util::isInTestEnvironment() && !empty($_COOKIE["hackedLoggedInUserID"])) |
| 24 | { |
| 25 | $userData = ClassRegistry::init('User')->findById((int) $_COOKIE["hackedLoggedInUserID"]); |
| 26 | if ($userData) |
| 27 | { |
| 28 | Auth::$user = $userData['User']; |
| 29 | return; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | if ($user) |
| 34 | { |
| 35 | Auth::$user = $user['User']; |
| 36 | // Set JWT cookie for stateless auth |
| 37 | JwtAuth::setAuthCookie(Auth::getUserID()); |
| 38 | self::generateLoginToken(Auth::getUserID()); // For phpBB2 forum SSO |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | // Try JWT cookie (stateless auth) |
| 43 | $userIdFromJwt = JwtAuth::getUserIdFromCookie(); |
| 44 | if ($userIdFromJwt) |
| 45 | { |
| 46 | $userData = ClassRegistry::init('User')->findById($userIdFromJwt); |
| 47 | if ($userData) |
| 48 | { |
| 49 | Auth::$user = $userData['User']; |
| 50 | return; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Not logged in |
| 55 | Auth::$user = null; |
| 56 | } |
| 57 | |
| 58 | public static function isLoggedIn(): bool |
| 59 | { |
| 60 | return (bool) Auth::$user; |
| 61 | } |
| 62 | |
| 63 | public static function getUserID(): int |
| 64 | { |
| 65 | return Auth::$user ? Auth::$user['id'] : 0; |
| 66 | } |
| 67 | |
| 68 | public static function &getUser() |
| 69 | { |
| 70 | if (!Auth::$user) |
| 71 | throw new Exception("Accessing user for writing when null"); |
| 72 | return Auth::$user; |
| 73 | } |
| 74 | |
| 75 | public static function isAdmin(): bool |
| 76 | { |
| 77 | return Auth::isLoggedIn() && Auth::getUser()['isAdmin']; |
| 78 | } |
| 79 | |
| 80 | public static function hasPremium(): bool |
| 81 | { |
| 82 | return Auth::isLoggedIn() && Auth::getUser()['premium']; |
| 83 | } |
| 84 | |
| 85 | public static function premiumLevel(): int |
| 86 | { |
| 87 | return Auth::isLoggedIn() ? Auth::getUser()['premium'] : 0; |
| 88 | } |
| 89 | |
| 90 | public static function saveUser(): void |
| 91 | { |
| 92 | assert(Auth::isLoggedIn()); |
| 93 | ClassRegistry::init('User')->save(Auth::getUser()); |
| 94 | } |
| 95 | |
| 96 | public static function logout(): void |
| 97 | { |
| 98 | // Clear JWT cookie and phpBB2 SSO token |
| 99 | JwtAuth::clearAuthCookie(); |
| 100 | Util::clearCookie('login_token'); |
| 101 | Auth::$user = null; |
| 102 | } |
| 103 | |
| 104 | public static function getWithDefault($key, $default) |
| 105 | { |
| 106 | if (!Auth::isLoggedIn()) |
| 107 | return $default; |
| 108 | return Auth::getUser()[$key]; |
| 109 | } |
| 110 | |
| 111 | public static function getMode(): int |
| 112 | { |
| 113 | return Auth::isLoggedIn() ? (int) Auth::getUser()['mode'] : Constants::$LEVEL_MODE; |
| 114 | } |
| 115 | |
| 116 | public static function isInLevelMode(): bool |
| 117 | { |
| 118 | return Auth::getMode() == Constants::$LEVEL_MODE; |
| 119 | } |
| 120 | |
| 121 | public static function isInRatingMode(): bool |
| 122 | { |
| 123 | return Auth::getMode() == Constants::$RATING_MODE; |
| 124 | } |
| 125 | |
| 126 | public static function isInTimeMode(): bool |
| 127 | { |
| 128 | return Auth::getMode() == Constants::$TIME_MODE; |
| 129 | } |
| 130 | |
| 131 | public static function addSuspicion(): void |
| 132 | { |
| 133 | Auth::getUser()['penalty'] += 1; |
| 134 | Auth::saveUser(); |
| 135 | } |
| 136 | |
| 137 | public static function XPisGainedInCurrentMode() |
| 138 | { |
| 139 | if (!Auth::isLoggedIn()) |
| 140 | return false; |
| 141 | return Auth::isInLevelMode() || Auth::isInRatingMode(); |
| 142 | } |
| 143 | |
| 144 | public static function ratingisGainedInCurrentMode() |
| 145 | { |
| 146 | if (!Auth::isLoggedIn()) |
| 147 | return false; |
| 148 | return Auth::isInLevelMode() || Auth::isInRatingMode(); |
| 149 | } |
| 150 | |
| 151 | public static function getRemainingHealth() |
| 152 | { |
| 153 | if (!Auth::isLoggedIn()) |
| 154 | return 1000; |
| 155 | return Util::getHealthBasedOnLevel(Auth::getUser()['level']) - Auth::getUser()['damage']; |
| 156 | } |
| 157 | |
| 158 | public static function lightMode() |
| 159 | { |
| 160 | if (Auth::isLoggedIn()) |
| 161 | return (Auth::getUser()['lastLight'] == 0 || Auth::getUser()['lastLight'] == 2) ? self::$LIGHT_MODE : self::$DARK_MODE; |
| 162 | if (!empty($_COOKIE['lightDark'])) |
| 163 | if ($_COOKIE['lightDark'] == 'dark') |
| 164 | return self::$DARK_MODE; |
| 165 | return self::$LIGHT_MODE; |
| 166 | } |
| 167 | |
| 168 | private static $user = null; |
| 169 | public static int $LIGHT_MODE = 1; |
| 170 | public static int $DARK_MODE = 2; |
| 171 | } |