Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Models/Permissions/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@
namespace Seatplus\Auth\Models\Permissions;

use Illuminate\Database\Eloquent\Relations\HasMany;
use Seatplus\Auth\Enums\RoleType;
use Seatplus\Auth\Models\AccessControl\RoleMembership;
use Spatie\Permission\Models\Role as SpatieRole;

/**
* @property string $type
* @property RoleType $type
*/
class Role extends SpatieRole
{
protected $casts = [
'type' => RoleType::class,
];

public function affiliations(): HasMany
{
return $this->hasMany(Affiliation::class, 'role_id');
Expand Down
4 changes: 2 additions & 2 deletions src/Services/Roles/AbstractRoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ public function setRoleType(RoleType $roleType): void
$originalRoleType = $this->role->type;

// if the role type has not changed, we return early
if ($originalRoleType === $roleType->value) {
if ($originalRoleType === $roleType) {
return;
}

$this->role->update([
'type' => $roleType->value,
'type' => $roleType,
]);

$this->resetRoleMemberships();
Expand Down
16 changes: 10 additions & 6 deletions src/Services/Roles/BaseRoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,19 @@ public function optIn(): OptInRoleService
*/
public function getTypeService(): RoleServiceInterface
{
return match ($this->role->type) {
RoleType::AUTOMATIC->value => $this->automatic(),
RoleType::ON_REQUEST->value => $this->onRequest(),
RoleType::MANUAL->value => $this->manual(),
RoleType::OPT_IN->value => $this->optIn(),
default => throw new \Exception('Role type supported'),
return match ($this->getType()) {
RoleType::AUTOMATIC => $this->automatic(),
RoleType::ON_REQUEST => $this->onRequest(),
RoleType::MANUAL => $this->manual(),
RoleType::OPT_IN => $this->optIn(),
};
}

public function getType(): RoleType
{
return $this->role->type;
}

public function handleMembers(): void
{
$this->getTypeService()->handleMembers();
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Roles/OptInRoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ public function canJoin(User $user): bool

public function canModerate(User $user): bool
{
return false;
return $this->isModerator($user);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Actions/ManageManualRoleActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

$action->execute($role_request);

expect($role->refresh()->type)->toBe('manual');
expect($role->refresh()->type)->toBe(\Seatplus\Auth\Enums\RoleType::MANUAL);
});

it('updates the role name', function () {
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Models/RoleModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
});

it('has default type attribute', function () {
expect(test()->role->fresh()->type)->toEqual('manual');
expect(test()->role->fresh()->type)->toEqual(\Seatplus\Auth\Enums\RoleType::MANUAL);
});

it('has role memberships', function () {
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Services/Roles/AbstractRoleServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function canModerate(\Seatplus\Auth\Models\User $user): bool
it('returns early when setting same role type', function () {

// Arrange
$this->role->type = RoleType::AUTOMATIC->value;
$this->role->type = RoleType::AUTOMATIC;
$this->role->save();

// Act
Expand All @@ -64,7 +64,7 @@ public function canModerate(\Seatplus\Auth\Models\User $user): bool
]);

// Assert
expect($this->role->refresh()->type)->toEqual(RoleType::AUTOMATIC->value);
expect($this->role->refresh()->type)->toEqual(RoleType::AUTOMATIC);
});

it('sets role type to', function (RoleType $role_type) {
Expand All @@ -73,7 +73,7 @@ public function canModerate(\Seatplus\Auth\Models\User $user): bool
$this->service->setRoleType($role_type);

// Assert
expect($this->role->refresh()->type)->toEqual($role_type->value);
expect($this->role->refresh()->type)->toEqual($role_type);
})->with([
RoleType::AUTOMATIC,
RoleType::ON_REQUEST,
Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/Services/Roles/AutomaticRoleServiceTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Seatplus\Auth\Enums\RoleType;
use Seatplus\Auth\Models\AccessControl\RoleMembership;
use Seatplus\Auth\Models\Permissions\Role;
use Seatplus\Auth\Models\User;
Expand Down Expand Up @@ -91,11 +92,11 @@

it('sets role type to automatic', function () {

expect($this->role->type)->toBe('manual');
expect($this->role->type)->toBe(RoleType::MANUAL);

$this->service->setRoleType(\Seatplus\Auth\Enums\RoleType::AUTOMATIC);
$this->service->setRoleType(RoleType::AUTOMATIC);

expect($this->role->type)->toBe('automatic');
expect($this->role->type)->toBe(RoleType::AUTOMATIC);
});

it('cannot view', function () {
Expand Down