Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.59% |
70 / 71 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| EntityUtils | |
98.59% |
70 / 71 |
|
83.33% |
5 / 6 |
23 | |
0.00% |
0 / 1 |
| createOlzEntity | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
4 | |||
| updateOlzEntity | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
6 | |||
| deleteOlzEntity | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| restoreOlzEntity | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| canUpdateOlzEntity | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
10 | |||
| olzEntityClasses | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Common\Utils; |
| 4 | |
| 5 | use Olz\Entity\Anmelden\Booking; |
| 6 | use Olz\Entity\Anmelden\Registration; |
| 7 | use Olz\Entity\Anmelden\RegistrationInfo; |
| 8 | use Olz\Entity\Anniversary\RunRecord; |
| 9 | use Olz\Entity\Common\OlzEntity; |
| 10 | use Olz\Entity\Faq\Question; |
| 11 | use Olz\Entity\Faq\QuestionCategory; |
| 12 | use Olz\Entity\Karten\Karte; |
| 13 | use Olz\Entity\Members\Member; |
| 14 | use Olz\Entity\News\NewsEntry; |
| 15 | use Olz\Entity\Panini2024\Panini2024Picture; |
| 16 | use Olz\Entity\Quiz\Skill; |
| 17 | use Olz\Entity\Quiz\SkillCategory; |
| 18 | use Olz\Entity\Quiz\SkillLevel; |
| 19 | use Olz\Entity\Roles\Role; |
| 20 | use Olz\Entity\Service\Download; |
| 21 | use Olz\Entity\Service\Link; |
| 22 | use Olz\Entity\Snippets\Snippet; |
| 23 | use Olz\Entity\Startseite\WeeklyPicture; |
| 24 | use Olz\Entity\Termine\Termin; |
| 25 | use Olz\Entity\Termine\TerminLabel; |
| 26 | use Olz\Entity\Termine\TerminLocation; |
| 27 | use Olz\Entity\Termine\TerminTemplate; |
| 28 | use Olz\Entity\Users\User; |
| 29 | |
| 30 | class EntityUtils { |
| 31 | use WithUtilsTrait; |
| 32 | |
| 33 | /** @var array<class-string<OlzEntity>> */ |
| 34 | protected static array $olzEntityClasses = [ |
| 35 | Booking::class, |
| 36 | Registration::class, |
| 37 | RegistrationInfo::class, |
| 38 | RunRecord::class, |
| 39 | Question::class, |
| 40 | QuestionCategory::class, |
| 41 | Karte::class, |
| 42 | Member::class, |
| 43 | NewsEntry::class, |
| 44 | Panini2024Picture::class, |
| 45 | Skill::class, |
| 46 | SkillCategory::class, |
| 47 | SkillLevel::class, |
| 48 | Role::class, |
| 49 | Download::class, |
| 50 | Link::class, |
| 51 | Snippet::class, |
| 52 | WeeklyPicture::class, |
| 53 | Termin::class, |
| 54 | TerminLabel::class, |
| 55 | TerminLocation::class, |
| 56 | TerminTemplate::class, |
| 57 | User::class, |
| 58 | ]; |
| 59 | |
| 60 | /** @param ?array{onOff?: ?bool, ownerUserId?: ?int, ownerRoleId?: ?int} $input */ |
| 61 | public function createOlzEntity(OlzEntity $entity, ?array $input): void { |
| 62 | $user_repo = $this->entityManager()->getRepository(User::class); |
| 63 | $role_repo = $this->entityManager()->getRepository(Role::class); |
| 64 | $current_user = $this->authUtils()->getCurrentUser(); |
| 65 | $now_datetime = new \DateTime($this->dateUtils()->getIsoNow()); |
| 66 | |
| 67 | if (($input['onOff'] ?? true) === false) { |
| 68 | $entity->setDeletedAt($now_datetime); |
| 69 | $entity->setDeletedByUser($current_user); |
| 70 | } |
| 71 | |
| 72 | $owner_user_id = $input['ownerUserId'] ?? null; |
| 73 | $owner_user = $current_user; |
| 74 | if ($owner_user_id) { |
| 75 | $owner_user = $user_repo->findOneBy(['id' => $owner_user_id]); |
| 76 | } |
| 77 | |
| 78 | $owner_role_id = $input['ownerRoleId'] ?? null; |
| 79 | $owner_role = null; |
| 80 | if ($owner_role_id) { |
| 81 | $owner_role = $role_repo->findOneBy(['id' => $owner_role_id]); |
| 82 | } |
| 83 | |
| 84 | $entity->setOwnerUser($owner_user); |
| 85 | $entity->setOwnerRole($owner_role); |
| 86 | $entity->setCreatedAt($now_datetime); |
| 87 | $entity->setCreatedByUser($current_user); |
| 88 | $entity->setLastModifiedAt($now_datetime); |
| 89 | $entity->setLastModifiedByUser($current_user); |
| 90 | } |
| 91 | |
| 92 | /** @param ?array{onOff?: ?bool, ownerUserId?: ?int, ownerRoleId?: ?int} $input */ |
| 93 | public function updateOlzEntity(OlzEntity $entity, ?array $input): void { |
| 94 | $user_repo = $this->entityManager()->getRepository(User::class); |
| 95 | $role_repo = $this->entityManager()->getRepository(Role::class); |
| 96 | $current_user = $this->authUtils()->getCurrentUser(); |
| 97 | $now_datetime = new \DateTime($this->dateUtils()->getIsoNow()); |
| 98 | |
| 99 | $on_off = $input['onOff'] ?? null; |
| 100 | if ($on_off !== null) { |
| 101 | $entity->setDeletedAt($on_off ? null : $now_datetime); |
| 102 | $entity->setDeletedByUser($on_off ? null : $current_user); |
| 103 | } |
| 104 | |
| 105 | $owner_user_id = $input['ownerUserId'] ?? null; |
| 106 | if ($owner_user_id) { |
| 107 | $owner_user = $user_repo->findOneBy(['id' => $owner_user_id]); |
| 108 | $entity->setOwnerUser($owner_user); |
| 109 | } |
| 110 | |
| 111 | $owner_role_id = $input['ownerRoleId'] ?? null; |
| 112 | if ($owner_role_id) { |
| 113 | $owner_role = $role_repo->findOneBy(['id' => $owner_role_id]); |
| 114 | $entity->setOwnerRole($owner_role); |
| 115 | } |
| 116 | |
| 117 | $entity->setLastModifiedAt($now_datetime); |
| 118 | $entity->setLastModifiedByUser($current_user); |
| 119 | } |
| 120 | |
| 121 | public function deleteOlzEntity(OlzEntity $entity): void { |
| 122 | $current_user = $this->authUtils()->getCurrentUser(); |
| 123 | $now_datetime = new \DateTime($this->dateUtils()->getIsoNow()); |
| 124 | $entity->setDeletedAt($now_datetime); |
| 125 | $entity->setDeletedByUser($current_user); |
| 126 | $entity->setLastModifiedAt($now_datetime); |
| 127 | $entity->setLastModifiedByUser($current_user); |
| 128 | } |
| 129 | |
| 130 | public function restoreOlzEntity(OlzEntity $entity): void { |
| 131 | $current_user = $this->authUtils()->getCurrentUser(); |
| 132 | $now_datetime = new \DateTime($this->dateUtils()->getIsoNow()); |
| 133 | $entity->setDeletedAt(null); |
| 134 | $entity->setDeletedByUser(null); |
| 135 | $entity->setLastModifiedAt($now_datetime); |
| 136 | $entity->setLastModifiedByUser($current_user); |
| 137 | } |
| 138 | |
| 139 | /** @param ?array{onOff?: bool, ownerUserId?: ?int, ownerRoleId?: ?int} $meta_arg */ |
| 140 | public function canUpdateOlzEntity( |
| 141 | ?OlzEntity $entity, |
| 142 | ?array $meta_arg, |
| 143 | string $edit_permission = 'all', |
| 144 | ): bool { |
| 145 | $meta = $meta_arg ?? []; |
| 146 | $auth_utils = $this->authUtils(); |
| 147 | $current_user = $auth_utils->getCurrentUser(); |
| 148 | |
| 149 | if ($auth_utils->hasPermission($edit_permission)) { |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | $owner_user = $entity?->getOwnerUser(); |
| 154 | if ($owner_user && $current_user?->getId() === $owner_user->getId()) { |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | $created_by_user = $entity?->getCreatedByUser(); |
| 159 | if ($created_by_user && $current_user?->getId() === $created_by_user->getId()) { |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | $owner_role = $entity?->getOwnerRole(); |
| 164 | $owner_role_id = $owner_role?->getId(); |
| 165 | if ($owner_role_id !== null) { |
| 166 | $current_roles = $auth_utils->getAuthenticatedRoles() ?? []; |
| 167 | foreach ($current_roles as $current_role) { |
| 168 | if ($current_role->getId() === $owner_role_id && $current_role->exists()) { |
| 169 | return true; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | /** @return array<class-string<OlzEntity>> */ |
| 178 | public function olzEntityClasses(): array { |
| 179 | return $this::$olzEntityClasses; |
| 180 | } |
| 181 | } |