Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 53 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
DeleteUserEndpoint | |
0.00% |
0 / 53 |
|
0.00% |
0 / 2 |
156 | |
0.00% |
0 / 1 |
configure | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
handle | |
0.00% |
0 / 50 |
|
0.00% |
0 / 1 |
132 |
1 | <?php |
2 | |
3 | namespace Olz\Users\Endpoints; |
4 | |
5 | use Olz\Api\OlzDeleteEntityTypedEndpoint; |
6 | use Olz\Entity\AccessToken; |
7 | use Olz\Entity\News\NewsEntry; |
8 | use Olz\Entity\NotificationSubscription; |
9 | use Olz\Entity\StravaLink; |
10 | use Olz\Entity\TelegramLink; |
11 | use PhpTypeScriptApi\HttpError; |
12 | |
13 | /** |
14 | * @phpstan-import-type OlzUserId from UserEndpointTrait |
15 | * @phpstan-import-type OlzUserData from UserEndpointTrait |
16 | * |
17 | * @extends OlzDeleteEntityTypedEndpoint<OlzUserId, OlzUserData> |
18 | */ |
19 | class DeleteUserEndpoint extends OlzDeleteEntityTypedEndpoint { |
20 | use UserEndpointTrait; |
21 | |
22 | public function configure(): void { |
23 | parent::configure(); |
24 | $this->configureUserEndpointTrait(); |
25 | $this->phpStanUtils->registerTypeImport(UserEndpointTrait::class); |
26 | } |
27 | |
28 | protected function handle(mixed $input): mixed { |
29 | $entity = $this->getEntityById($input['id']); |
30 | |
31 | $current_user = $this->authUtils()->getCurrentUser(); |
32 | $is_me = ( |
33 | $current_user |
34 | && $entity->getUsername() === $current_user->getUsername() |
35 | && $entity->getId() === $current_user->getId() |
36 | ); |
37 | $can_update = $this->entityUtils()->canUpdateOlzEntity($entity, null, 'users'); |
38 | if (!$is_me && !$can_update) { |
39 | throw new HttpError(403, "Kein Zugriff!"); |
40 | } |
41 | |
42 | // Remove news ownership |
43 | $news_repo = $this->entityManager()->getRepository(NewsEntry::class); |
44 | $news_entries = $news_repo->findBy(['owner_user' => $entity]); |
45 | foreach ($news_entries as $news_entry) { |
46 | $news_entry->setOwnerUser(null); |
47 | $this->entityManager()->remove($news_entry); |
48 | } |
49 | $this->entityManager()->flush(); |
50 | |
51 | // Remove notification subscriptions |
52 | $notification_subscription_repo = $this->entityManager()->getRepository(NotificationSubscription::class); |
53 | $subscriptions = $notification_subscription_repo->findBy(['user' => $entity]); |
54 | foreach ($subscriptions as $subscription) { |
55 | $this->entityManager()->remove($subscription); |
56 | } |
57 | $this->entityManager()->flush(); |
58 | |
59 | // Remove telegram links |
60 | $telegram_link_repo = $this->entityManager()->getRepository(TelegramLink::class); |
61 | $telegram_links = $telegram_link_repo->findBy(['user' => $entity]); |
62 | foreach ($telegram_links as $telegram_link) { |
63 | $this->entityManager()->remove($telegram_link); |
64 | } |
65 | $this->entityManager()->flush(); |
66 | |
67 | // Remove strava links |
68 | $strava_link_repo = $this->entityManager()->getRepository(StravaLink::class); |
69 | $strava_links = $strava_link_repo->findBy(['user' => $entity]); |
70 | foreach ($strava_links as $strava_link) { |
71 | $this->entityManager()->remove($strava_link); |
72 | } |
73 | $this->entityManager()->flush(); |
74 | |
75 | // Remove access tokens |
76 | $access_token_repo = $this->entityManager()->getRepository(AccessToken::class); |
77 | $access_tokens = $access_token_repo->findBy(['user' => $entity]); |
78 | foreach ($access_tokens as $access_token) { |
79 | $this->entityManager()->remove($access_token); |
80 | } |
81 | $this->entityManager()->flush(); |
82 | |
83 | // Remove avatar |
84 | $data_path = $this->envUtils()->getDataPath(); |
85 | $avatar_path = "{$data_path}img/users/{$entity->getId()}"; |
86 | $this->generalUtils()->removeRecursive($avatar_path); |
87 | |
88 | // Log out |
89 | if ($this->session()->get('user') === $entity->getUsername()) { |
90 | $this->session()->delete('auth'); |
91 | $this->session()->delete('root'); |
92 | $this->session()->delete('user'); |
93 | $this->session()->delete('user_id'); |
94 | $this->session()->delete('auth_user'); |
95 | $this->session()->delete('auth_user_id'); |
96 | $this->session()->clear(); |
97 | } |
98 | |
99 | $entity->softDelete(); |
100 | $this->entityManager()->flush(); |
101 | |
102 | return []; |
103 | } |
104 | } |