|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Copyright (C) 2020-2025 Iain Cambridge |
|
7
|
|
|
* |
|
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by |
|
10
|
|
|
* the Free Software Foundation, either version 2.1 of the License, or |
|
11
|
|
|
* (at your option) any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* This program is distributed in the hope that it will be useful, |
|
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16
|
|
|
* GNU Lesser General Public License for more details. |
|
17
|
|
|
* |
|
18
|
|
|
* You should have received a copy of the GNU General Public License |
|
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace Parthenon\User\Controller; |
|
23
|
|
|
|
|
24
|
|
|
use Parthenon\User\Entity\MemberInterface; |
|
25
|
|
|
use Parthenon\User\Entity\TeamInviteCode; |
|
26
|
|
|
use Parthenon\User\Entity\User; |
|
27
|
|
|
use Parthenon\User\Entity\UserInterface; |
|
28
|
|
|
use Parthenon\User\Repository\TeamInviteCodeRepositoryInterface; |
|
29
|
|
|
use Parthenon\User\Repository\UserRepositoryInterface; |
|
30
|
|
|
use Parthenon\User\RequestProcessor\TeamInvite; |
|
31
|
|
|
use Parthenon\User\Team\CurrentTeamProvider; |
|
32
|
|
|
use Psr\Log\LoggerInterface; |
|
33
|
|
|
use Symfony\Bridge\Twig\Attribute\Template; |
|
34
|
|
|
use Symfony\Bundle\SecurityBundle\Security; |
|
35
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
36
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
37
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
|
38
|
|
|
|
|
39
|
|
|
class TeamController |
|
40
|
|
|
{ |
|
41
|
|
|
#[Route('/user/team', name: 'parthenon_team_view', methods: ['GET'])] |
|
42
|
|
|
public function viewTeam( |
|
43
|
|
|
LoggerInterface $logger, |
|
44
|
|
|
Security $security, |
|
45
|
|
|
CurrentTeamProvider $teamProvider, |
|
46
|
|
|
TeamInviteCodeRepositoryInterface $inviteCodeRepository, |
|
47
|
|
|
) { |
|
48
|
|
|
$logger->info('A user viewed their team'); |
|
49
|
|
|
/** @var User $user */ |
|
50
|
|
|
$user = $security->getUser(); |
|
|
|
|
|
|
51
|
|
|
$team = $teamProvider->getCurrentTeam(); |
|
52
|
|
|
|
|
53
|
|
|
$sentInvites = []; |
|
54
|
|
|
foreach ($inviteCodeRepository->findAllUnusedInvitesForTeam($team) as $inviteCode) { |
|
55
|
|
|
$sentInvites[] = [ |
|
56
|
|
|
'id' => (string) $inviteCode->getId(), |
|
57
|
|
|
'email' => $inviteCode->getEmail(), |
|
58
|
|
|
'created_at' => $inviteCode->getCreatedAt()->format(\DATE_ATOM), |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$members = []; |
|
63
|
|
|
foreach ($team->getMembers() as $member) { |
|
64
|
|
|
$members[] = [ |
|
65
|
|
|
'id' => (string) $member->getId(), |
|
66
|
|
|
'email' => $member->getEmail(), |
|
67
|
|
|
'name' => $member->getName(), |
|
68
|
|
|
'created_at' => $member->getCreatedAt()->format(\DATE_ATOM), |
|
69
|
|
|
'is_deleted' => $member->isDeleted(), |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$body = [ |
|
74
|
|
|
'sent_invites' => $sentInvites, |
|
75
|
|
|
'members' => $members, |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
return new JsonResponse($body); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
#[Route('/user/team/invite', name: 'parthenon_user_team_invite')] |
|
82
|
|
|
#[Template('user/team_invite.html.twig')] |
|
83
|
|
|
public function inviteUser(Request $request, TeamInvite $processor, LoggerInterface $logger) |
|
84
|
|
|
{ |
|
85
|
|
|
$logger->info('A user has visited the invite page'); |
|
86
|
|
|
|
|
87
|
|
|
return $processor->process($request); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
#[Route('/user/team/invite/{id}/cancel', name: 'parthenon_team_invite_cancel', methods: ['POST'])] |
|
91
|
|
|
public function cancelInvite(Request $request, LoggerInterface $logger, TeamInviteCodeRepositoryInterface $inviteCodeRepository) |
|
92
|
|
|
{ |
|
93
|
|
|
$logger->info('A user has cancelled an invite', ['invite_code_id' => $request->get('id')]); |
|
94
|
|
|
|
|
95
|
|
|
try { |
|
96
|
|
|
/** @var TeamInviteCode $inviteCode */ |
|
97
|
|
|
$inviteCode = $inviteCodeRepository->findById($request->get('id')); |
|
98
|
|
|
|
|
99
|
|
|
$inviteCode->setUsed(true); |
|
100
|
|
|
$inviteCode->setUsedAt(new \DateTime('now')); |
|
101
|
|
|
$inviteCode->setCancelled(true); |
|
102
|
|
|
|
|
103
|
|
|
$inviteCodeRepository->save($inviteCode); |
|
104
|
|
|
} catch (\Throwable $e) { |
|
105
|
|
|
$logger->error('An error occurred while sending an invite', ['error_message' => $e->getMessage()]); |
|
106
|
|
|
|
|
107
|
|
|
return new JsonResponse(['success' => false]); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return new JsonResponse(['success' => true]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
#[Route('/user/team/member/{id}/disable', name: 'parthenon_team_invite_disable', methods: ['POST'])] |
|
114
|
|
|
public function disableMember(Request $request, LoggerInterface $logger, Security $security, CurrentTeamProvider $teamProvider, UserRepositoryInterface $userRepository) |
|
115
|
|
|
{ |
|
116
|
|
|
$id = $request->get('id'); |
|
117
|
|
|
$logger->info('A user has disable a member', ['id' => $id]); |
|
118
|
|
|
/** @var User $user */ |
|
119
|
|
|
$user = $security->getUser(); |
|
120
|
|
|
$team = $teamProvider->getCurrentTeam(); |
|
121
|
|
|
|
|
122
|
|
|
try { |
|
123
|
|
|
if ($user->getId()->toString() == $id) { |
|
124
|
|
|
$logger->warning('A user has tried disable themselves', ['id' => $id]); |
|
125
|
|
|
|
|
126
|
|
|
return new JsonResponse(['success' => false]); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** @var UserInterface $deletedUser */ |
|
130
|
|
|
$deletedUser = $userRepository->findById($id); |
|
131
|
|
|
|
|
132
|
|
|
if (!$deletedUser instanceof MemberInterface) { |
|
133
|
|
|
throw new \Exception('Not a member'); |
|
134
|
|
|
} |
|
135
|
|
|
if ($deletedUser->getTeam()->getId()->toString() != $team->getId()->toString()) { |
|
136
|
|
|
$logger->warning('A user has tried disable a user from a different team', ['id' => $id]); |
|
137
|
|
|
|
|
138
|
|
|
return new JsonResponse(['success' => false]); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$deletedUser->markAsDeleted(); /* @phpstan-ignore-line */ |
|
142
|
|
|
$userRepository->save($deletedUser); |
|
143
|
|
|
} catch (\Throwable $e) { |
|
144
|
|
|
$logger->error('An error occured while trying to disable a user', ['error_message' => $e->getMessage()]); |
|
145
|
|
|
|
|
146
|
|
|
return new JsonResponse(['success' => false]); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return new JsonResponse(['success' => true]); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|