Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
3.28% |
2 / 61 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
| User | |
3.28% |
2 / 61 |
|
33.33% |
2 / 6 |
420.03 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| list | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| profile | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| saveProfile | |
0.00% |
0 / 45 |
|
0.00% |
0 / 1 |
240 | |||
| save | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| create | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Controller; |
| 6 | |
| 7 | use Celemas\Core\Exception\HttpBadRequest; |
| 8 | use Celemas\Core\Request; |
| 9 | use Celemas\Quma\Database; |
| 10 | use Cosray\Config; |
| 11 | use Cosray\Middleware\Permission; |
| 12 | use Cosray\Util\Password; |
| 13 | |
| 14 | class User |
| 15 | { |
| 16 | public function __construct( |
| 17 | protected readonly Database $db, |
| 18 | ) {} |
| 19 | |
| 20 | #[Permission('authenticated')] |
| 21 | public function list() {} |
| 22 | |
| 23 | #[Permission('authenticated')] |
| 24 | public function profile(Request $request): array |
| 25 | { |
| 26 | $usr = $request->get('session')->authenticatedUserId(); |
| 27 | $user = $this->db->users->get(['usr' => $usr])->one(); |
| 28 | |
| 29 | if ($user['data']) { |
| 30 | $data = json_decode($user['data'], true); |
| 31 | $name = $data['name'] ?? ''; |
| 32 | } else { |
| 33 | $name = ''; |
| 34 | } |
| 35 | |
| 36 | return [ |
| 37 | 'uid' => $user['uid'], |
| 38 | 'username' => $user['username'], |
| 39 | 'email' => $user['email'], |
| 40 | 'name' => $name, |
| 41 | ]; |
| 42 | } |
| 43 | |
| 44 | #[Permission('authenticated')] |
| 45 | public function saveProfile(Request $request, Config $config): array |
| 46 | { |
| 47 | $data = $request->json(); |
| 48 | |
| 49 | $usr = $request->get('session')->authenticatedUserId(); |
| 50 | $user = $this->db->users->get(['usr' => $usr])->one(); |
| 51 | $user['data'] = json_decode($user['data'], true); |
| 52 | |
| 53 | if ($data['uid'] !== $user['uid']) { |
| 54 | throw new HttpBadRequest($request, payload: ['error' => 'Falsche uid']); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | $email = trim($data['email'] ?? ''); |
| 59 | |
| 60 | if ($email) { |
| 61 | if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { |
| 62 | throw new HttpBadRequest($request, ['error' => 'Die E-Mail-Adresse ist ungültig!']); |
| 63 | } |
| 64 | } else { |
| 65 | throw new HttpBadRequest($request, ['error' => 'Die E-Mail-Adresse muss angegeben werden!']); |
| 66 | } |
| 67 | |
| 68 | if (strtolower($email) !== strtolower($user['email'])) { |
| 69 | $existing = $this->db->users->get(['login' => $email])->first(); |
| 70 | |
| 71 | if ($existing) { |
| 72 | throw new HttpBadRequest($request, ['error' => 'Die E-Mail-Adresse ist bereits vergeben']); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // User name |
| 77 | $username = trim($data['username'] ?? ''); |
| 78 | |
| 79 | if ($username) { |
| 80 | if ($username !== ($user['username'] ?? null) && strlen($username) > 64) { |
| 81 | throw new HttpBadRequest($request, ['error' => 'Der Benutzername ist zu lang']); |
| 82 | } |
| 83 | } else { |
| 84 | $username = $user['username'] ?? null; |
| 85 | } |
| 86 | |
| 87 | // Full name |
| 88 | $name = trim($data['name'] ?? ''); |
| 89 | |
| 90 | if ($name) { |
| 91 | if ($name !== ($user['data']['name'] ?? '') && strlen($name) > 64) { |
| 92 | throw new HttpBadRequest($request, ['error' => 'Der vollständige Name ist zu lang']); |
| 93 | } |
| 94 | } else { |
| 95 | $name = $user['data']['name'] ?? null; |
| 96 | } |
| 97 | |
| 98 | // Password |
| 99 | $pw = trim($data['password'] ?? ''); |
| 100 | |
| 101 | if ($pw) { |
| 102 | $passwordUtil = Password::fromConfig($config); |
| 103 | |
| 104 | if (!$passwordUtil->strongEnough($pw)) { |
| 105 | throw new HttpBadRequest($request, [ |
| 106 | 'error' => 'Das Passwort ist zu schwach. Es sollte mindestens 12 Zeichen haben.', |
| 107 | ]); |
| 108 | } |
| 109 | |
| 110 | if (trim($data['password']) !== trim($data['passwordRepeat'])) { |
| 111 | throw new HttpBadRequest($request, ['error' => 'Die neuen Passwörder stimmen nicht überein']); |
| 112 | } |
| 113 | |
| 114 | $pwHash = $passwordUtil->hash($pw); |
| 115 | } else { |
| 116 | $pwHash = $user['password']; |
| 117 | } |
| 118 | |
| 119 | $this->db->users->save([ |
| 120 | 'usr' => $usr, |
| 121 | 'email' => $email, |
| 122 | 'username' => $username, |
| 123 | 'data' => ['name' => $name], |
| 124 | 'password' => $pwHash, |
| 125 | 'editor' => $usr, |
| 126 | ])->run(); |
| 127 | |
| 128 | return ['success' => true]; |
| 129 | } |
| 130 | |
| 131 | #[Permission('authenticated')] |
| 132 | public function save(string $uid) {} |
| 133 | |
| 134 | #[Permission('authenticated')] |
| 135 | public function create() {} |
| 136 | } |