UserTrait.php
1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?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);
}
}