Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 122 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| OlzUserDetail | |
0.00% |
0 / 122 |
|
0.00% |
0 / 5 |
992 | |
0.00% |
0 / 1 |
| hasAccess | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSearchTitle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSearchResultsWhenHasAccess | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getHtmlWhenHasAccess | |
0.00% |
0 / 111 |
|
0.00% |
0 / 1 |
552 | |||
| prettyPrintPermissionMap | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Users\Components\OlzUserDetail; |
| 4 | |
| 5 | use Olz\Components\Common\OlzRootComponent; |
| 6 | use Olz\Components\Page\OlzFooter\OlzFooter; |
| 7 | use Olz\Components\Page\OlzHeader\OlzHeader; |
| 8 | use Olz\Entity\Roles\Role; |
| 9 | use Olz\Entity\Users\User; |
| 10 | use Olz\Repository\Roles\PredefinedRole; |
| 11 | use Olz\Roles\Components\OlzRoleInfoModal\OlzRoleInfoModal; |
| 12 | |
| 13 | /** @extends OlzRootComponent<array<string, mixed>> */ |
| 14 | class OlzUserDetail extends OlzRootComponent { |
| 15 | public function hasAccess(): bool { |
| 16 | return true; |
| 17 | } |
| 18 | |
| 19 | public function getSearchTitle(): string { |
| 20 | return 'TODO'; |
| 21 | } |
| 22 | |
| 23 | public function getSearchResultsWhenHasAccess(array $terms): array { |
| 24 | return []; |
| 25 | } |
| 26 | |
| 27 | public function getHtmlWhenHasAccess(mixed $args): string { |
| 28 | $code_href = $this->envUtils()->getCodeHref(); |
| 29 | $user_repo = $this->entityManager()->getRepository(User::class); |
| 30 | $user = $user_repo->findOneBy(['id' => $args['id']]); |
| 31 | |
| 32 | if (!$user) { |
| 33 | $this->httpUtils()->dieWithHttpError(404); |
| 34 | throw new \Exception('should already have failed'); |
| 35 | } |
| 36 | |
| 37 | $role_repo = $this->entityManager()->getRepository(Role::class); |
| 38 | $sysadmin_role = $role_repo->getPredefinedRole(PredefinedRole::Sysadmin); |
| 39 | |
| 40 | $out = OlzHeader::render([ |
| 41 | 'back_link' => "{$code_href}verein", |
| 42 | 'title' => $user->getFullName(), |
| 43 | 'description' => "{$user->getFullName()} - Profil.", |
| 44 | 'norobots' => true, |
| 45 | ]); |
| 46 | |
| 47 | $out .= "<div class='content-full olz-user-detail'>"; |
| 48 | |
| 49 | $image_paths = $this->authUtils()->getUserAvatar($user); |
| 50 | $image_src_html = $this->htmlUtils()->getImageSrcHtml($image_paths); |
| 51 | $img_html = "<img {$image_src_html} alt='' class='image'>"; |
| 52 | |
| 53 | $auth_user_id = $this->session()->get('auth_user_id'); |
| 54 | $is_parent = $auth_user_id && intval($user->getParentUserId()) === intval($auth_user_id); |
| 55 | $is_self = $auth_user_id && intval($user->getId()) === intval($auth_user_id); |
| 56 | $has_permissions = $this->authUtils()->hasPermission('users'); |
| 57 | $can_edit = $is_parent || $is_self || $has_permissions; |
| 58 | $edit_admin = ''; |
| 59 | $edit_password = ''; |
| 60 | if ($can_edit) { |
| 61 | $json_id = json_encode($user->getId()); |
| 62 | $edit_admin = <<<ZZZZZZZZZZ |
| 63 | <div> |
| 64 | <button |
| 65 | id='edit-user-button' |
| 66 | class='btn btn-primary' |
| 67 | onclick='return olz.editUser({$json_id})' |
| 68 | > |
| 69 | <img src='{$code_href}assets/icns/edit_white_16.svg' class='noborder' /> |
| 70 | Bearbeiten |
| 71 | </button> |
| 72 | </div> |
| 73 | ZZZZZZZZZZ; |
| 74 | $edit_password = <<<'ZZZZZZZZZZ' |
| 75 | <button |
| 76 | class='btn btn-secondary' |
| 77 | onclick='return olz.initOlzChangePasswordModal()' |
| 78 | id='change-password-button' |
| 79 | > |
| 80 | Passwort ändern |
| 81 | </button> |
| 82 | ZZZZZZZZZZ; |
| 83 | } |
| 84 | |
| 85 | $street = $user->getStreet() ?? '(Keine Adresse)'; |
| 86 | $postal_code = $user->getPostalCode() ?? '(Keine PLZ)'; |
| 87 | $city = $user->getCity() ?? '(Kein Ort)'; |
| 88 | $region = $user->getRegion() ?? 'Keine Region'; |
| 89 | $country_code = $user->getCountryCode() ?? 'Kein Land'; |
| 90 | $birthdate = $user->getBirthdate()?->format('d.m.Y') ?? '(Unbekannt)'; |
| 91 | $phone = $user->getPhone() ?? '(Unbekannt)'; |
| 92 | |
| 93 | if ( |
| 94 | !$user->getParentUserId() |
| 95 | && !$user->isEmailVerified() |
| 96 | && !$this->authUtils()->hasPermission('verified_email', $user) |
| 97 | ) { |
| 98 | if ($user->getEmailVerificationToken()) { |
| 99 | $out .= <<<'ZZZZZZZZZZ' |
| 100 | <div class='alert alert-danger' role='alert'> |
| 101 | Deine E-Mail-Adresse ist noch nicht bestätigt. Bitte prüfe deine Inbox (und dein Spam-Postfach) auf unsere Bestätigungs-E-Mail (Betreff: "[OLZ] E-Mail bestätigen"). |
| 102 | <a |
| 103 | href='#' |
| 104 | onclick='olz.initOlzVerifyUserEmailModal()' |
| 105 | id='verify-user-email-link' |
| 106 | > |
| 107 | Erneut senden |
| 108 | </a> |
| 109 | </div> |
| 110 | ZZZZZZZZZZ; |
| 111 | } else { |
| 112 | $out .= <<<'ZZZZZZZZZZ' |
| 113 | <div class='alert alert-danger' role='alert'> |
| 114 | Deine E-Mail-Adresse ist noch nicht bestätigt. |
| 115 | <a |
| 116 | href='#' |
| 117 | onclick='olz.initOlzVerifyUserEmailModal()' |
| 118 | id='verify-user-email-link' |
| 119 | > |
| 120 | Jetzt bestätigen |
| 121 | </a> |
| 122 | </div> |
| 123 | ZZZZZZZZZZ; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | $out .= <<<ZZZZZZZZZZ |
| 128 | <div class='edit-user-container'>{$edit_admin}</div> |
| 129 | <div class='image-container'>{$img_html}</div> |
| 130 | <h1 class='name-container'>{$user->getFullName()}</h1> |
| 131 | <div class='info-container username'>Benutzername: {$user->getUsername()}</div> |
| 132 | ZZZZZZZZZZ; |
| 133 | if ($can_edit) { |
| 134 | $out .= <<<ZZZZZZZZZZ |
| 135 | <div class='info-container address'> |
| 136 | <div>{$street}</div> |
| 137 | <div>{$postal_code} {$city} ({$region}, {$country_code})</div> |
| 138 | </div> |
| 139 | <div class='info-container birthdate'>Geburtsdatum: {$birthdate}</div> |
| 140 | <div class='info-container phone'>Telephon: {$phone}</div> |
| 141 | ZZZZZZZZZZ; |
| 142 | } |
| 143 | |
| 144 | $has_official_email = $this->authUtils()->hasPermission('user_email', $user); |
| 145 | $email_html = ''; |
| 146 | if ($has_official_email) { |
| 147 | $host = $this->envUtils()->getEmailForwardingHost(); |
| 148 | $olz_email = "{$user->getUsername()}@{$host}"; |
| 149 | $email = $user->getEmail() ? $olz_email : null; |
| 150 | $email_html = "<div class='info-container'>Du hast eine <b>offizielle</b> OLZ E-Mail-Adresse: <b>{$olz_email}</b></div>"; |
| 151 | if ($user->getOldUsername()) { |
| 152 | $old_olz_email = "{$user->getOldUsername()}@{$host}"; |
| 153 | $email_html .= "<div class='info-container'>Du hast ausserdem eine <b>alte</b> offizielle OLZ E-Mail-Adresse: <b>{$old_olz_email}</b> <i>(nicht mehr benutzen!)</i></div>"; |
| 154 | } |
| 155 | $email_html .= "<div class='info-container'>Die E-Mails <b>werden weitergeleitet</b> an: <b>{$user->getEmail()}</b></div>"; |
| 156 | } else { |
| 157 | $email = $user->getEmail(); |
| 158 | $email_html = "<div class='info-container'>Du hast <b>keine offizielle</b> OLZ E-Mail-Adresse.</div>"; |
| 159 | $sysadmin_modal = $sysadmin_role ? OlzRoleInfoModal::render(['role' => $sysadmin_role]) : '"Website"'; |
| 160 | $email_html .= "<div class='info-container'>Bei Fragen: kontaktiere das Ressort {$sysadmin_modal}.</div>"; |
| 161 | } |
| 162 | if ($email) { |
| 163 | $email_out = $this->htmlUtils()->replaceEmailAdresses($email); |
| 164 | $out .= "<div class='info-container email'>{$email_out}</div>"; |
| 165 | } |
| 166 | $out .= $edit_password; |
| 167 | |
| 168 | $out .= <<<ZZZZZZZZZZ |
| 169 | <h2>Berechtigungen</h2> |
| 170 | <div class='info-container'>Persönliche Berechtigungen: <b>{$this->prettyPrintPermissionMap($user->getPermissionMap())}</b></div> |
| 171 | ZZZZZZZZZZ; |
| 172 | foreach ($user->getRoles() as $role) { |
| 173 | $role_modal = OlzRoleInfoModal::render(['role' => $role]); |
| 174 | $out .= "<div class='info-container'>Berechtigungen im Rahmen von {$role_modal}: <b>{$this->prettyPrintPermissionMap($role->getPermissionMap())}</b></div>"; |
| 175 | } |
| 176 | |
| 177 | $out .= <<<ZZZZZZZZZZ |
| 178 | <h2>E-Mail Weiterleitung</h2> |
| 179 | {$email_html} |
| 180 | ZZZZZZZZZZ; |
| 181 | |
| 182 | if ($can_edit) { |
| 183 | $out .= "<h2>Familie</h2>"; |
| 184 | $child_users = $user_repo->findBy(['parent_user' => $user->getId()]); |
| 185 | if ($child_users) { |
| 186 | $out .= "<ul class='info-container'>"; |
| 187 | foreach ($child_users as $child_user) { |
| 188 | $out .= "<li>Familienmitglied <a href='{$code_href}benutzer/{$child_user->getId()}'>{$child_user->getFullName()}</a></li>"; |
| 189 | } |
| 190 | $out .= "</ul>"; |
| 191 | } |
| 192 | if ($user->getParentUserId()) { |
| 193 | $parent_user = $user_repo->findOneBy(['id' => $user->getParentUserId()]); |
| 194 | $out .= "<div class='info-container'>Familienmitglied von <a href='{$code_href}benutzer/{$parent_user?->getId()}'>{$parent_user?->getFullName()}</a></div>"; |
| 195 | if ($child_users) { |
| 196 | $this->log()->warning("User {$user->getId()} has parent and children."); |
| 197 | } |
| 198 | } else { |
| 199 | $json_id = json_encode($user->getId()); |
| 200 | $out .= <<<ZZZZZZZZZZ |
| 201 | <div> |
| 202 | <button |
| 203 | id='add-child-user-button' |
| 204 | class='btn btn-secondary' |
| 205 | onclick='return olz.addChildUser({$json_id})' |
| 206 | > |
| 207 | <img src='{$code_href}assets/icns/new_white_16.svg' class='noborder' /> |
| 208 | Familienmitglied hinzufügen |
| 209 | </button> |
| 210 | </div> |
| 211 | ZZZZZZZZZZ; |
| 212 | } |
| 213 | } |
| 214 | $out .= "</div>"; |
| 215 | |
| 216 | $out .= OlzFooter::render(); |
| 217 | |
| 218 | return $out; |
| 219 | } |
| 220 | |
| 221 | /** @param array<string, bool> $permissions_map */ |
| 222 | protected function prettyPrintPermissionMap(array $permissions_map): string { |
| 223 | $out = ''; |
| 224 | foreach ($permissions_map as $permission => $is_given) { |
| 225 | if (!$is_given) { |
| 226 | continue; |
| 227 | } |
| 228 | if ($out !== '') { |
| 229 | $out .= ', '; |
| 230 | } |
| 231 | $out .= $permission; |
| 232 | } |
| 233 | return $out !== '' ? $out : '(keine Berechtigungen)'; |
| 234 | } |
| 235 | } |