| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
namespace Liuch\DmarcSrg; |
| 33 |
|
| 34 |
use Liuch\DmarcSrg\ErrorCodes; |
| 35 |
use Liuch\DmarcSrg\Users\UserList; |
| 36 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 37 |
use Liuch\DmarcSrg\Exception\AuthException; |
| 38 |
use Liuch\DmarcSrg\Exception\ForbiddenException; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class Auth |
| 44 |
{ |
| 45 |
|
| 46 |
private $core = null; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function __construct(object $core) |
| 54 |
{ |
| 55 |
$this->core = $core; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function isEnabled(): bool |
| 67 |
{ |
| 68 |
return $this->core->config('admin/password') !== null; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function login(string $username, string $password): array |
| 84 |
{ |
| 85 |
$atype = $this->authenticationType(); |
| 86 |
if ($atype === 'password-only') { |
| 87 |
$username = empty($username) ? 'admin' : ''; |
| 88 |
} |
| 89 |
if (!empty($username)) { |
| 90 |
try { |
| 91 |
$user = UserList::getUserByName($username, $this->core); |
| 92 |
if ($user->verifyPassword($password)) { |
| 93 |
if (!$user->isEnabled()) { |
| 94 |
throw new AuthException('The user is disabled. Contact the administrator'); |
| 95 |
} |
| 96 |
$this->core->setCurrentUser($user); |
| 97 |
return [ |
| 98 |
'error_code' => 0, |
| 99 |
'message' => 'Authentication succeeded' |
| 100 |
]; |
| 101 |
} |
| 102 |
} catch (AuthException $e) { |
| 103 |
throw $e; |
| 104 |
} catch (SoftException $e) { |
| 105 |
} |
| 106 |
} |
| 107 |
throw new AuthException('Authentication failed. Try again'); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public function logout(): array |
| 116 |
{ |
| 117 |
$this->core->setCurrentUser(null); |
| 118 |
return [ |
| 119 |
'error_code' => 0, |
| 120 |
'message' => 'Logged out successfully' |
| 121 |
]; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function isAllowed(int $level): void |
| 134 |
{ |
| 135 |
if ($this->isEnabled()) { |
| 136 |
$user = $this->core->getCurrentUser(); |
| 137 |
if (!$user) { |
| 138 |
throw new AuthException('Authentication needed', ErrorCodes::AUTH_NEEDED, $this->authenticationType()); |
| 139 |
} |
| 140 |
if ($user->level() < $level) { |
| 141 |
throw new ForbiddenException('Forbidden'); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function authenticationType(): string |
| 154 |
{ |
| 155 |
if ($this->isEnabled()) { |
| 156 |
return $this->core->config('users/user_management', false) ? 'base' : 'password-only'; |
| 157 |
} |
| 158 |
return 'none'; |
| 159 |
} |
| 160 |
} |