-
-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added member and invitation endpoints
- Loading branch information
Showing
48 changed files
with
1,186 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Actions\Jetstream; | ||
|
||
use App\Enums\Role; | ||
use App\Models\Membership; | ||
use App\Models\Organization; | ||
use App\Models\User; | ||
use App\Service\PermissionStore; | ||
use App\Service\UserService; | ||
use Illuminate\Auth\Access\AuthorizationException; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Validation\Rule; | ||
use Illuminate\Validation\ValidationException; | ||
use Laravel\Jetstream\Events\TeamMemberUpdated; | ||
|
||
class UpdateMemberRole | ||
{ | ||
/** | ||
* Update the role for the given team member. | ||
* | ||
* @throws AuthorizationException | ||
* @throws ValidationException | ||
*/ | ||
public function update(User $actingUser, Organization $organization, string $userId, string $role): void | ||
{ | ||
if (! app(PermissionStore::class)->has($organization, 'members:change-role')) { | ||
throw new AuthorizationException(); | ||
} | ||
|
||
$user = User::where('id', '=', $userId)->firstOrFail(); | ||
$member = Membership::whereBelongsTo($user)->whereBelongsTo($organization)->firstOrFail(); | ||
if ($member->role === Role::Placeholder->value) { | ||
abort(403, 'Cannot update the role of a placeholder member.'); | ||
} | ||
|
||
Validator::make([ | ||
'role' => $role, | ||
], [ | ||
'role' => [ | ||
'required', | ||
'string', | ||
Rule::in([ | ||
Role::Owner->value, | ||
Role::Admin->value, | ||
Role::Manager->value, | ||
Role::Employee->value, | ||
]), | ||
], | ||
])->validate(); | ||
|
||
DB::transaction(function () use ($organization, $userId, $role, $user) { | ||
$organization->users()->updateExistingPivot($userId, [ | ||
'role' => $role, | ||
]); | ||
|
||
if ($role === Role::Owner->value) { | ||
app(UserService::class)->changeOwnership($organization, $user); | ||
} | ||
}); | ||
|
||
TeamMemberUpdated::dispatch($organization->fresh(), User::findOrFail($userId)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Enums; | ||
|
||
enum Role: string | ||
{ | ||
case Owner = 'owner'; | ||
case Admin = 'admin'; | ||
case Manager = 'manager'; | ||
case Employee = 'employee'; | ||
case Placeholder = 'placeholder'; | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
app/Exceptions/Api/InactiveUserCanNotBeUsedApiException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Exceptions\Api; | ||
|
||
class InactiveUserCanNotBeUsedApiException extends ApiException | ||
{ | ||
public const string KEY = 'inactive_user_can_not_be_used'; | ||
} |
10 changes: 10 additions & 0 deletions
10
app/Exceptions/Api/UserIsAlreadyMemberOfProjectApiException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Exceptions\Api; | ||
|
||
class UserIsAlreadyMemberOfProjectApiException extends ApiException | ||
{ | ||
public const string KEY = 'user_is_already_member_of_project'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers\Api\V1; | ||
|
||
use App\Http\Requests\V1\Invitation\InvitationIndexRequest; | ||
use App\Http\Requests\V1\Invitation\InvitationStoreRequest; | ||
use App\Http\Resources\V1\Invitation\InvitationCollection; | ||
use App\Http\Resources\V1\Invitation\InvitationResource; | ||
use App\Models\Organization; | ||
use Illuminate\Auth\Access\AuthorizationException; | ||
use Illuminate\Http\JsonResponse; | ||
use Laravel\Jetstream\Contracts\InvitesTeamMembers; | ||
|
||
class InvitationController extends Controller | ||
{ | ||
/** | ||
* List all invitations of an organization | ||
* | ||
* @return InvitationCollection<InvitationResource> | ||
* | ||
* @throws AuthorizationException | ||
* | ||
* @operationId getInvitations | ||
*/ | ||
public function index(Organization $organization, InvitationIndexRequest $request): InvitationCollection | ||
{ | ||
$this->checkPermission($organization, 'invitations:view'); | ||
|
||
$invitations = $organization->teamInvitations() | ||
->paginate(); | ||
|
||
return InvitationCollection::make($invitations); | ||
} | ||
|
||
/** | ||
* Invite a user to the organization | ||
* | ||
* @throws AuthorizationException | ||
* | ||
* @operationId invite | ||
*/ | ||
public function store(Organization $organization, InvitationStoreRequest $request): JsonResponse | ||
{ | ||
$this->checkPermission($organization, 'invitations:create'); | ||
|
||
app(InvitesTeamMembers::class)->invite( | ||
$request->user(), | ||
$organization, | ||
$request->input('email'), | ||
$request->input('role') | ||
); | ||
|
||
return response()->json(null, 204); | ||
} | ||
} |
Oops, something went wrong.