mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-08 00:02:15 +01:00
Replaces all Jetstream model trait functions and relations
This commit is contained in:
committed by
Constantin Graf
parent
5eb1a6a006
commit
734aab6353
@@ -9,6 +9,7 @@ use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
use App\Service\IpLookup\IpLookupServiceContract;
|
||||
use App\Service\OrganizationService;
|
||||
use App\Service\UserService;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
@@ -50,10 +51,8 @@ class CreateOrganization implements CreatesTeams
|
||||
$currency
|
||||
);
|
||||
|
||||
$user->switchTeam($organization);
|
||||
app(UserService::class)->switchCurrentOrganization($user, $organization);
|
||||
|
||||
// Note: The refresh is necessary for currently unknown reasons. Do not remove it.
|
||||
$organization = $organization->refresh();
|
||||
AfterCreateOrganization::dispatch($organization);
|
||||
|
||||
return $organization;
|
||||
|
||||
@@ -69,7 +69,7 @@ class UserCreateCommand extends Command
|
||||
);
|
||||
});
|
||||
/** @var Organization|null $organization */
|
||||
$organization = $user->ownedTeams->first();
|
||||
$organization = $user->ownedOrganizations->first();
|
||||
if ($organization === null) {
|
||||
throw new LogicException('User does not have an organization');
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ use Illuminate\Validation\Rule;
|
||||
|
||||
class InvitationsRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'teamInvitations';
|
||||
protected static string $relationship = 'organizationInvitations';
|
||||
|
||||
protected static ?string $title = 'Invitations';
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ use App\Filament\Resources\UserResource\RelationManagers\OwnedOrganizationsRelat
|
||||
use App\Models\User;
|
||||
use App\Service\DeletionService;
|
||||
use App\Service\TimezoneService;
|
||||
use App\Service\UserService;
|
||||
use Brick\Money\ISOCurrencyProvider;
|
||||
use Exception;
|
||||
use Filament\Forms;
|
||||
@@ -179,7 +180,7 @@ class UserResource extends Resource
|
||||
])
|
||||
->actions([
|
||||
Impersonate::make()->before(function (User $record): void {
|
||||
if ($record->currentTeam === null) {
|
||||
if ($record->currentOrganization === null) {
|
||||
$organization = $record->organizations()->where('personal_team', '=', true)->first();
|
||||
if ($organization === null) {
|
||||
$organization = $record->organizations()->first();
|
||||
@@ -187,8 +188,7 @@ class UserResource extends Resource
|
||||
if ($organization === null) {
|
||||
throw new Exception('User has no organization');
|
||||
}
|
||||
$record->currentTeam()->associate($organization);
|
||||
$record->save();
|
||||
app(UserService::class)->switchCurrentOrganization($record, $organization);
|
||||
}
|
||||
}),
|
||||
Tables\Actions\EditAction::make(),
|
||||
|
||||
@@ -16,7 +16,7 @@ class OwnedOrganizationsRelationManager extends RelationManager
|
||||
{
|
||||
protected static ?string $title = 'Owned Organizations';
|
||||
|
||||
protected static string $relationship = 'ownedTeams';
|
||||
protected static string $relationship = 'ownedOrganizations';
|
||||
|
||||
public function form(Form $form): Form
|
||||
{
|
||||
|
||||
@@ -40,7 +40,7 @@ class InvitationController extends Controller
|
||||
{
|
||||
$this->checkPermission($organization, 'invitations:view');
|
||||
|
||||
$invitations = $organization->teamInvitations()
|
||||
$invitations = $organization->organizationInvitations()
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate(config('app.pagination_per_page_default'));
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ use App\Service\BillableRateService;
|
||||
use App\Service\DeletionService;
|
||||
use App\Service\IpLookup\IpLookupServiceContract;
|
||||
use App\Service\OrganizationService;
|
||||
use App\Service\UserService;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
@@ -106,10 +107,8 @@ class OrganizationController extends Controller
|
||||
$currency
|
||||
);
|
||||
|
||||
$user->switchTeam($organization);
|
||||
app(UserService::class)->switchCurrentOrganization($user, $organization);
|
||||
|
||||
// Note: The refresh is necessary for currently unknown reasons. Do not remove it.
|
||||
$organization = $organization->refresh();
|
||||
AfterCreateOrganization::dispatch($organization);
|
||||
|
||||
return new OrganizationResource($organization, true);
|
||||
|
||||
@@ -59,7 +59,7 @@ class Controller extends BaseController
|
||||
protected function currentOrganization(): Organization
|
||||
{
|
||||
$user = $this->user();
|
||||
$organization = $user->currentTeam;
|
||||
$organization = $user->currentOrganization;
|
||||
if ($organization === null) {
|
||||
$organization = $user->organizations()->first();
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ class HandleInertiaRequests extends Middleware
|
||||
/** @var BillingContract $billing */
|
||||
$billing = app(BillingContract::class);
|
||||
|
||||
$currentOrganization = $request->user()?->currentTeam;
|
||||
$currentOrganization = $request->user()?->currentOrganization;
|
||||
|
||||
return array_merge(parent::share($request), [
|
||||
'has_billing_extension' => $hasBilling,
|
||||
|
||||
@@ -43,7 +43,7 @@ use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
|
||||
* @property Carbon|null $updated_at
|
||||
* @property Collection<int, User> $users
|
||||
* @property Collection<int, User> $realUsers
|
||||
* @property-read Collection<int, OrganizationInvitation> $teamInvitations
|
||||
* @property-read Collection<int, OrganizationInvitation> $organizationInvitations
|
||||
* @property Member $membership
|
||||
* @property NumberFormat $number_format
|
||||
* @property CurrencyFormat $currency_format
|
||||
@@ -51,7 +51,6 @@ use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
|
||||
* @property IntervalFormat $interval_format
|
||||
* @property TimeFormat $time_format
|
||||
*
|
||||
* @method HasMany<OrganizationInvitation, $this> teamInvitations()
|
||||
* @method static OrganizationFactory factory()
|
||||
*/
|
||||
class Organization extends JetstreamTeam implements AuditableContract
|
||||
@@ -111,23 +110,6 @@ class Organization extends JetstreamTeam implements AuditableContract
|
||||
protected $attributes = [
|
||||
];
|
||||
|
||||
/**
|
||||
* Get all the non-placeholder users of the organization including its owner.
|
||||
*
|
||||
* @return Collection<int, User>
|
||||
*/
|
||||
public function allRealUsers(): Collection
|
||||
{
|
||||
return $this->realUsers->merge([$this->owner]);
|
||||
}
|
||||
|
||||
public function hasRealUserWithEmail(string $email): bool
|
||||
{
|
||||
return $this->allRealUsers()->contains(function (User $user) use ($email): bool {
|
||||
return $user->email === $email;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the users that belong to the team.
|
||||
*
|
||||
@@ -172,6 +154,14 @@ class Organization extends JetstreamTeam implements AuditableContract
|
||||
->where('is_placeholder', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<OrganizationInvitation, $this>
|
||||
*/
|
||||
public function organizationInvitations(): HasMany
|
||||
{
|
||||
return $this->hasMany(OrganizationInvitation::class, 'organization_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* This method prevents an unhandled exception when the ID is not a UUID.
|
||||
* Normally this can be fixed with a route pattern, but Jetstream does not use route model binding.
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\Role;
|
||||
use App\Enums\Weekday;
|
||||
use App\Models\Concerns\CustomAuditable;
|
||||
use App\Models\Concerns\HasUuids;
|
||||
@@ -52,6 +53,7 @@ use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
|
||||
* @property Carbon|null $updated_at
|
||||
* @property string|null $current_team_id
|
||||
* @property Collection<int, Organization> $organizations
|
||||
* @property Collection<int, Organization> $ownedOrganizations
|
||||
* @property Collection<int, TimeEntry> $timeEntries
|
||||
* @property Member $membership
|
||||
*
|
||||
@@ -131,16 +133,39 @@ class User extends Authenticatable implements AuditableContract, FilamentUser, M
|
||||
{
|
||||
return Attribute::get(function (): string {
|
||||
return $this->profile_photo_path
|
||||
? Storage::disk($this->profilePhotoDisk())->url($this->profile_photo_path)
|
||||
? Storage::disk(config('filesystems.public'))->url($this->profile_photo_path)
|
||||
: $this->defaultProfilePhotoUrl();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default profile photo URL if no profile photo has been uploaded.
|
||||
*/
|
||||
protected function defaultProfilePhotoUrl(): string
|
||||
{
|
||||
$name = trim(collect(explode(' ', $this->name))->map(function ($segment) {
|
||||
return mb_substr($segment, 0, 1);
|
||||
})->join(' '));
|
||||
|
||||
return 'https://ui-avatars.com/api/?name='.urlencode($name).'&color=7F9CF5&background=EBF4FF';
|
||||
}
|
||||
|
||||
public function canAccessPanel(Panel $panel): bool
|
||||
{
|
||||
return in_array($this->email, config('auth.super_admins', []), true) && $this->hasVerifiedEmail();
|
||||
}
|
||||
|
||||
public function isMemberOfOrganization(Organization $organization): bool
|
||||
{
|
||||
if ($this->relationLoaded('organizations')) {
|
||||
return $this->organizations->contains(function (Organization $o) use ($organization): bool {
|
||||
return $o->getKey() === $organization->getKey();
|
||||
});
|
||||
}
|
||||
|
||||
return $this->organizations()->whereKey($organization->getKey())->exists();
|
||||
}
|
||||
|
||||
public function canBeImpersonated(): bool
|
||||
{
|
||||
return $this->is_placeholder === false;
|
||||
@@ -161,6 +186,14 @@ class User extends Authenticatable implements AuditableContract, FilamentUser, M
|
||||
->as('membership');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsToMany<Organization, $this, Pivot, 'membership'>
|
||||
*/
|
||||
public function ownedOrganizations(): BelongsToMany
|
||||
{
|
||||
return $this->organizations()->wherePivot('role', Role::Owner->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<TimeEntry, $this>
|
||||
*/
|
||||
@@ -215,12 +248,8 @@ class User extends Authenticatable implements AuditableContract, FilamentUser, M
|
||||
*/
|
||||
public function scopeBelongsToOrganization(Builder $builder, Organization $organization): Builder
|
||||
{
|
||||
return $builder->where(function (Builder $builder) use ($organization): Builder {
|
||||
return $builder->whereHas('organizations', function (Builder $query) use ($organization): void {
|
||||
$query->whereKey($organization->getKey());
|
||||
})->orWhereHas('ownedTeams', function (Builder $query) use ($organization): void {
|
||||
$query->whereKey($organization->getKey());
|
||||
});
|
||||
return $builder->whereHas('organizations', function (Builder $query) use ($organization): void {
|
||||
$query->whereKey($organization->getKey());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ class OrganizationPolicy
|
||||
return true;
|
||||
}
|
||||
|
||||
return $user->belongsToTeam($organization);
|
||||
return $user->isMemberOfOrganization($organization);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,6 +97,6 @@ class OrganizationPolicy
|
||||
return true;
|
||||
}
|
||||
|
||||
return $user->ownsTeam($organization);
|
||||
return app(PermissionStore::class)->userHas($organization, $user, 'organizations:delete');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,7 +173,7 @@ class DeletionService
|
||||
$user->authCodes()->delete();
|
||||
|
||||
// Note: Since the deletion of the profile photo is not reversible via a database rollback this needs to be done last
|
||||
$user->deleteProfilePhoto();
|
||||
$this->userService->deleteProfilePhoto($user);
|
||||
|
||||
$user->delete();
|
||||
|
||||
|
||||
@@ -97,7 +97,7 @@ class MemberService
|
||||
$isPlaceholder = $user->is_placeholder;
|
||||
|
||||
if (! $isPlaceholder && $user->current_team_id === $member->organization_id) {
|
||||
$user->currentTeam()->disassociate();
|
||||
$user->currentOrganization()->disassociate();
|
||||
$user->save();
|
||||
}
|
||||
|
||||
@@ -216,7 +216,7 @@ class MemberService
|
||||
{
|
||||
$user = $member->user;
|
||||
if ($user->current_team_id === $member->organization_id) {
|
||||
$user->currentTeam()->disassociate();
|
||||
$user->currentOrganization()->disassociate();
|
||||
$user->save();
|
||||
}
|
||||
|
||||
|
||||
@@ -291,7 +291,7 @@ class PermissionStore
|
||||
public function userHas(Organization $organization, User $user, string $permission): bool
|
||||
{
|
||||
if (! isset($this->permissionCache[$user->getKey().'|'.$organization->getKey()])) {
|
||||
if (! $user->belongsToTeam($organization)) {
|
||||
if (! $user->isMemberOfOrganization($organization)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -309,7 +309,7 @@ class PermissionStore
|
||||
*/
|
||||
private function getPermissionsByUser(Organization $organization, User $user): array
|
||||
{
|
||||
if (! $user->belongsToTeam($organization)) {
|
||||
if (! $user->isMemberOfOrganization($organization)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ use App\Models\TimeEntry;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class UserService
|
||||
{
|
||||
@@ -61,7 +62,6 @@ class UserService
|
||||
$intervalFormat,
|
||||
$timeFormat,
|
||||
);
|
||||
$user->ownedTeams()->save($organization);
|
||||
}
|
||||
|
||||
return $user;
|
||||
@@ -103,13 +103,17 @@ class UserService
|
||||
true
|
||||
);
|
||||
|
||||
// Set the organization as the user's current organization
|
||||
$user->currentOrganization()->associate($organization);
|
||||
$user->save();
|
||||
$this->switchCurrentOrganization($user, $organization);
|
||||
|
||||
AfterCreateOrganization::dispatch($organization);
|
||||
}
|
||||
|
||||
public function switchCurrentOrganization(User $user, Organization $organization): void
|
||||
{
|
||||
$user->currentOrganization()->associate($organization);
|
||||
$user->save();
|
||||
}
|
||||
|
||||
public function getOrganizationNameForUserName(string $username): string
|
||||
{
|
||||
return explode(' ', $username, 2)[0]."'s Organization";
|
||||
@@ -157,4 +161,16 @@ class UserService
|
||||
$oldOwner->save();
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteProfilePhoto(User $user): void
|
||||
{
|
||||
if ($user->profile_photo_path === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Storage::disk(config('filesystems.public'))->delete($user->profile_photo_path);
|
||||
|
||||
$user->profile_photo_path = null;
|
||||
$user->save();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user