UserTrait.php 1.25 KB
<?php

namespace App\Traits;

use App\Enums\UserScopeEnum;
use App\Http\Service\UserService;
use App\Models\Project;
use App\Models\User;
use Auth;

trait UserTrait
{
    protected ?User $user;

    /**
     * @return int
     */
    protected function getUserKey(): int
    {
        return $this->user?->getKey() ?? 0;
    }

    /**
     * @param string $key
     * @return array|array[]|\array[][]|mixed
     */
    protected function getUserAttribute(string $key): mixed
    {
        return $this->user->getAttribute($key);
    }

    /**
     * @return bool
     */
    protected function isMine(): bool
    {
        return $this->getUserKey() === Auth::id();
    }

    /**
     * @param \App\Enums\UserScopeEnum $scope
     * @return bool
     */
    public function isScopeEquals(UserScopeEnum $scope): bool
    {
        return $this->getUserAttribute('scope') === $scope;
    }

    /**
     * @param \App\Models\Project|int $project
     * @return bool
     */
    public function isProjectManager(Project|int $project): bool
    {
        return UserService::isProjectManager($this->user, $project);
    }

    /**
     * @return bool
     */
    public function hasSingPermission(): bool
    {
        return UserService::hasSingPermission($this->user);
    }
}