Skip to content

Commit

Permalink
Renamed jetstream team to organization
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Jan 21, 2024
1 parent b95baeb commit 25be1e6
Show file tree
Hide file tree
Showing 63 changed files with 461 additions and 329 deletions.
6 changes: 3 additions & 3 deletions app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Actions\Fortify;

use App\Models\Team;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
Expand Down Expand Up @@ -46,9 +46,9 @@ public function create(array $input): User
*/
protected function createTeam(User $user): void
{
$user->ownedTeams()->save(Team::forceCreate([
$user->ownedTeams()->save(Organization::forceCreate([
'user_id' => $user->id,
'name' => explode(' ', $user->name, 2)[0]."'s Team",
'name' => explode(' ', $user->name, 2)[0]."'s Organization",
'personal_team' => true,
]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Actions\Jetstream;

use App\Models\Team;
use App\Models\Organization;
use App\Models\User;
use Closure;
use Illuminate\Support\Facades\Gate;
Expand All @@ -15,40 +15,40 @@
use Laravel\Jetstream\Jetstream;
use Laravel\Jetstream\Rules\Role;

class AddTeamMember implements AddsTeamMembers
class AddOrganizationMember implements AddsTeamMembers
{
/**
* Add a new team member to the given team.
*/
public function add(User $user, Team $team, string $email, ?string $role = null): void
public function add(User $user, Organization $organization, string $email, ?string $role = null): void
{
Gate::forUser($user)->authorize('addTeamMember', $team);
Gate::forUser($user)->authorize('addTeamMember', $organization);

$this->validate($team, $email, $role);
$this->validate($organization, $email, $role);

$newTeamMember = Jetstream::findUserByEmailOrFail($email);

AddingTeamMember::dispatch($team, $newTeamMember);
AddingTeamMember::dispatch($organization, $newTeamMember);

$team->users()->attach(
$organization->users()->attach(
$newTeamMember, ['role' => $role]
);

TeamMemberAdded::dispatch($team, $newTeamMember);
TeamMemberAdded::dispatch($organization, $newTeamMember);
}

/**
* Validate the add member operation.
*/
protected function validate(Team $team, string $email, ?string $role): void
protected function validate(Organization $organization, string $email, ?string $role): void
{
Validator::make([
'email' => $email,
'role' => $role,
], $this->rules(), [
'email.exists' => __('We were unable to find a registered user with this email address.'),
])->after(
$this->ensureUserIsNotAlreadyOnTeam($team, $email)
$this->ensureUserIsNotAlreadyOnTeam($organization, $email)
)->validateWithBag('addTeamMember');
}

Expand All @@ -70,7 +70,7 @@ protected function rules(): array
/**
* Ensure that the user is not already on the team.
*/
protected function ensureUserIsNotAlreadyOnTeam(Team $team, string $email): Closure
protected function ensureUserIsNotAlreadyOnTeam(Organization $team, string $email): Closure
{
return function ($validator) use ($team, $email) {
$validator->errors()->addIf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@

namespace App\Actions\Jetstream;

use App\Models\Team;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Laravel\Jetstream\Contracts\CreatesTeams;
use Laravel\Jetstream\Events\AddingTeam;
use Laravel\Jetstream\Jetstream;

class CreateTeam implements CreatesTeams
class CreateOrganization implements CreatesTeams
{
/**
* Validate and create a new team for the given user.
*
* @param array<string, string> $input
*
* @throws AuthorizationException
* @throws ValidationException
*/
public function create(User $user, array $input): Team
public function create(User $user, array $input): Organization
{
Gate::forUser($user)->authorize('create', Jetstream::newTeamModel());

Expand All @@ -29,11 +34,14 @@ public function create(User $user, array $input): Team

AddingTeam::dispatch($user);

$user->switchTeam($team = $user->ownedTeams()->create([
/** @var Organization $organization */
$organization = $user->ownedTeams()->create([
'name' => $input['name'],
'personal_team' => false,
]));
]);

$user->switchTeam($organization);

return $team;
return $organization;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

namespace App\Actions\Jetstream;

use App\Models\Team;
use App\Models\Organization;
use Laravel\Jetstream\Contracts\DeletesTeams;

class DeleteTeam implements DeletesTeams
class DeleteOrganization implements DeletesTeams
{
/**
* Delete the given team.
*/
public function delete(Team $team): void
public function delete(Organization $team): void
{
$team->purge();
}
Expand Down
4 changes: 2 additions & 2 deletions app/Actions/Jetstream/DeleteUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Actions\Jetstream;

use App\Models\Team;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Laravel\Jetstream\Contracts\DeletesTeams;
Expand Down Expand Up @@ -47,7 +47,7 @@ protected function deleteTeams(User $user): void
{
$user->teams()->detach();

$user->ownedTeams->each(function (Team $team) {
$user->ownedTeams->each(function (Organization $team) {
$this->deletesTeams->delete($team);
});
}
Expand Down
94 changes: 94 additions & 0 deletions app/Actions/Jetstream/InviteOrganizationMember.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace App\Actions\Jetstream;

use App\Models\Organization;
use App\Models\OrganizationInvitation;
use App\Models\User;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
use Korridor\LaravelModelValidationRules\Rules\UniqueEloquent;
use Laravel\Jetstream\Contracts\InvitesTeamMembers;
use Laravel\Jetstream\Events\InvitingTeamMember;
use Laravel\Jetstream\Jetstream;
use Laravel\Jetstream\Mail\TeamInvitation;
use Laravel\Jetstream\Rules\Role;

class InviteOrganizationMember implements InvitesTeamMembers
{
/**
* Invite a new team member to the given team.
*/
public function invite(User $user, Organization $organization, string $email, ?string $role = null): void
{
Gate::forUser($user)->authorize('addTeamMember', $organization);

$this->validate($organization, $email, $role);

InvitingTeamMember::dispatch($organization, $email, $role);

$invitation = $organization->teamInvitations()->create([
'email' => $email,
'role' => $role,
]);

Mail::to($email)->send(new TeamInvitation($invitation));
}

/**
* Validate the invite member operation.
*/
protected function validate(Organization $organization, string $email, ?string $role): void
{
Validator::make([
'email' => $email,
'role' => $role,
], $this->rules($organization), [
'email.unique' => __('This user has already been invited to the team.'),
])->after(
$this->ensureUserIsNotAlreadyOnTeam($organization, $email)
)->validateWithBag('addTeamMember');
}

/**
* Get the validation rules for inviting a team member.
*
* @return array<string, ValidationRule|array|string>
*/
protected function rules(Organization $organization): array
{
return array_filter([
'email' => [
'required',
'email',
new UniqueEloquent(OrganizationInvitation::class, 'email', function (Builder $builder) use ($organization) {
/** @var Builder<OrganizationInvitation> $builder */
return $builder->whereBelongsTo($organization, 'organization');
}),
],
'role' => Jetstream::hasRoles()
? ['required', 'string', new Role]
: null,
]);
}

/**
* Ensure that the user is not already on the team.
*/
protected function ensureUserIsNotAlreadyOnTeam(Organization $organization, string $email): Closure
{
return function ($validator) use ($organization, $email) {
$validator->errors()->addIf(
$organization->hasUserWithEmail($email),
'email',
__('This user already belongs to the team.')
);
};
}
}
90 changes: 0 additions & 90 deletions app/Actions/Jetstream/InviteTeamMember.php

This file was deleted.

Loading

0 comments on commit 25be1e6

Please sign in to comment.