Skip to content

Commit c947840

Browse files
authored
Merge pull request #2179 from BacLuc/adapt-collaborators-to-api-platform
Adapt collaborators to api platform
2 parents 0c14982 + b527425 commit c947840

File tree

5 files changed

+34
-18
lines changed

5 files changed

+34
-18
lines changed

api/src/DataPersister/CampCollaborationDataPersister.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\DataPersister;
44

5+
use ApiPlatform\Core\Validator\ValidatorInterface;
56
use App\DataPersister\Util\AbstractDataPersister;
67
use App\DataPersister\Util\CustomActionListener;
78
use App\DataPersister\Util\DataPersisterObservable;
@@ -23,6 +24,7 @@ public function __construct(
2324
private Security $security,
2425
private UserRepository $userRepository,
2526
private MailService $mailService,
27+
private ValidatorInterface $validator
2628
) {
2729
$resendInvitationListener = CustomActionListener::of(
2830
CampCollaboration::RESEND_INVITATION,
@@ -47,6 +49,7 @@ public function beforeCreate($data): BaseEntity {
4749
if (null != $userByInviteEmail) {
4850
$data->user = $userByInviteEmail;
4951
}
52+
$data->inviteKey = IdGenerator::generateRandomHexString(64);
5053
}
5154

5255
return $data;
@@ -56,11 +59,16 @@ public function afterCreate($data): void {
5659
/** @var User $user */
5760
$user = $this->security->getUser();
5861
if (CampCollaboration::STATUS_INVITED == $data->status && $data->inviteEmail) {
59-
$data->inviteKey = IdGenerator::generateRandomHexString(64);
6062
$this->mailService->sendInviteToCampMail($user, $data->camp, $data->inviteKey, $data->inviteEmail);
6163
}
6264
}
6365

66+
public function beforeRemove($data): ?BaseEntity {
67+
$this->validator->validate($data, ['groups' => ['delete']]);
68+
69+
return null;
70+
}
71+
6472
public function onStatusChange(CampCollaboration $data) {
6573
/** @var User $user */
6674
$user = $this->security->getUser();

api/src/Entity/CampCollaboration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ class CampCollaboration extends BaseEntity implements BelongsToCampInterface {
160160
*/
161161
#[Assert\Choice(choices: self::VALID_STATUS)]
162162
#[Assert\EqualTo(value: self::STATUS_INVITED, groups: ['resend_invitation'])]
163+
#[Assert\EqualTo(value: self::STATUS_INACTIVE, groups: ['delete'])]
163164
#[AssertAllowTransitions(
164165
[
165166
['from' => self::STATUS_INVITED, 'to' => [self::STATUS_INACTIVE]],

api/tests/Api/CampCollaborations/DeleteCampCollaborationTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
class DeleteCampCollaborationTest extends ECampApiTestCase {
1212
public function testDeleteCampCollaborationIsDeniedForAnonymousUser() {
13-
$campCollaboration = static::$fixtures['campCollaboration1manager'];
13+
$campCollaboration = static::$fixtures['campCollaboration5inactive'];
1414
static::createBasicClient()->request('DELETE', '/camp_collaborations/'.$campCollaboration->getId());
1515
$this->assertResponseStatusCodeSame(401);
1616
$this->assertJsonContains([
@@ -20,7 +20,7 @@ public function testDeleteCampCollaborationIsDeniedForAnonymousUser() {
2020
}
2121

2222
public function testDeleteCampCollaborationIsDeniedForUnrelatedUser() {
23-
$campCollaboration = static::$fixtures['campCollaboration1manager'];
23+
$campCollaboration = static::$fixtures['campCollaboration5inactive'];
2424
static::createClientWithCredentials(['username' => static::$fixtures['user4unrelated']->getUsername()])
2525
->request('DELETE', '/camp_collaborations/'.$campCollaboration->getId())
2626
;
@@ -33,7 +33,7 @@ public function testDeleteCampCollaborationIsDeniedForUnrelatedUser() {
3333
}
3434

3535
public function testDeleteCampCollaborationIsDeniedForInactiveCollaborator() {
36-
$campCollaboration = static::$fixtures['campCollaboration1manager'];
36+
$campCollaboration = static::$fixtures['campCollaboration5inactive'];
3737
static::createClientWithCredentials(['username' => static::$fixtures['user5inactive']->getUsername()])
3838
->request('DELETE', '/camp_collaborations/'.$campCollaboration->getId())
3939
;
@@ -46,7 +46,7 @@ public function testDeleteCampCollaborationIsDeniedForInactiveCollaborator() {
4646
}
4747

4848
public function testDeleteCampCollaborationIsDeniedForGuest() {
49-
$campCollaboration = static::$fixtures['campCollaboration1manager'];
49+
$campCollaboration = static::$fixtures['campCollaboration5inactive'];
5050
static::createClientWithCredentials(['username' => static::$fixtures['user3guest']->getUsername()])
5151
->request('DELETE', '/camp_collaborations/'.$campCollaboration->getId())
5252
;
@@ -59,7 +59,7 @@ public function testDeleteCampCollaborationIsDeniedForGuest() {
5959
}
6060

6161
public function testDeleteCampCollaborationIsAllowedForMember() {
62-
$campCollaboration = static::$fixtures['campCollaboration1manager'];
62+
$campCollaboration = static::$fixtures['campCollaboration5inactive'];
6363
static::createClientWithCredentials(['username' => static::$fixtures['user2member']->getUsername()])
6464
->request('DELETE', '/camp_collaborations/'.$campCollaboration->getId())
6565
;
@@ -68,12 +68,18 @@ public function testDeleteCampCollaborationIsAllowedForMember() {
6868
}
6969

7070
public function testDeleteCampCollaborationIsAllowedForManager() {
71-
$campCollaboration = static::$fixtures['campCollaboration1manager'];
71+
$campCollaboration = static::$fixtures['campCollaboration5inactive'];
7272
static::createClientWithCredentials()->request('DELETE', '/camp_collaborations/'.$campCollaboration->getId());
7373
$this->assertResponseStatusCodeSame(204);
7474
$this->assertNull($this->getEntityManager()->getRepository(CampCollaboration::class)->find($campCollaboration->getId()));
7575
}
7676

77+
public function testDeleteCampCollaborationIsDeniedIfStatusNotInactive() {
78+
$campCollaboration = static::$fixtures['campCollaboration1manager'];
79+
static::createClientWithCredentials()->request('DELETE', '/camp_collaborations/'.$campCollaboration->getId());
80+
$this->assertResponseStatusCodeSame(422);
81+
}
82+
7783
public function testDeleteCampCollaborationFromCampPrototypeIsDeniedForUnrelatedUser() {
7884
$campCollaboration = static::$fixtures['campCollaboration1campPrototype'];
7985
static::createClientWithCredentials()->request('DELETE', '/camp_collaborations/'.$campCollaboration->getId());

frontend/src/components/camp/CollaboratorListItem.vue

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<button-delete
5959
:disabled="(disabled && !isOwnCampCollaboration) || isLastManager"
6060
icon="mdi-cancel"
61-
@click="api.del(collaborator)">
61+
@click="api.patch(collaborator, {status: 'inactive'})">
6262
{{ $tc("components.camp.collaboratorListItem.deactivate") }}
6363
</button-delete>
6464
</div>
@@ -107,10 +107,9 @@ export default {
107107
methods: {
108108
resendInvitation () {
109109
this.resendingEmail = true
110-
this.api.href(this.api.get(), 'invitation', {
111-
action: 'resend',
112-
campCollaborationId: this.collaborator.id
113-
}).then(postUrl => this.api.post(postUrl, {}))
110+
this.api.href(this.api.get(), 'campCollaborations', {
111+
id: this.collaborator.id
112+
}).then(postUrl => this.api.patch(postUrl + '/resend_invitation', {}))
114113
.finally(() => { this.resendingEmail = false })
115114
}
116115
}

frontend/src/views/camp/Collaborators.vue

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,14 @@ export default {
130130
},
131131
methods: {
132132
invite () {
133-
this.api.post('/camp-collaborations', {
134-
campId: this.camp().id,
135-
inviteEmail: this.inviteEmail,
136-
role: this.inviteRole
137-
}).then(this.refreshCamp,
138-
this.handleError)
133+
this.api.href(this.api.get(), 'campCollaborations')
134+
.then(url => this.api.post(url, {
135+
camp: this.camp()._meta.self,
136+
inviteEmail: this.inviteEmail,
137+
role: this.inviteRole
138+
}))
139+
.then(this.refreshCamp,
140+
this.handleError)
139141
},
140142
handleError (e) {
141143
if (e.response) {

0 commit comments

Comments
 (0)