UserPermissionHelper.php
2.27 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace App\Helpers;
use App\Models\SystemTag;
use App\Models\User;
use App\Models\UserTagRelation;
use Arr;
class UserPermissionHelper
{
public User $user;
/**
* @param \App\Models\User $user
*/
private function __construct(User $user)
{
$this->user = $user;
}
/**
* @param int|\App\Models\User $user
* @return \App\Helpers\UserPermissionHelper
*/
public static function user(int|User $user): UserPermissionHelper
{
return new self(is_numeric($user) ? User::findOrFail($user, ['id', 'identity', 'demo_type', 'scope']) : $user);
}
/**
* @param int|array $tagIds
* @return bool
*/
public function checkTag(int|array $tagIds): bool
{
return UserTagRelation::query()->where('type', 4)->where('user_id', $this->user->getKey())->whereIn('tag_id', Arr::wrap($tagIds))->exists();
}
/**
* @return bool
*/
public function hasDemoPermission(): bool
{
if ($this->user->getAttribute('demo_type') === 0) {
return false;
}
$config = ConfigHelper::getDemoList();
if (in_array($this->user->getKey(), $config->get(ConfigHelper::DEMO_USER_KEY), false)) {
return true;
}
return $this->checkTag($config->get(ConfigHelper::DEMO_TAG_KEY));
}
/**
* @return bool
*/
public function hasSingPermission(): bool
{
if (in_array($this->user->getKey(), ConfigHelper::getSingUserIds(), false)) {
return true;
}
return $this->checkTag(SystemTag::query()->whereJsonContains('expand->permission', [2])->pluck('id')->merge(ConfigHelper::getSingerTagIds())->toArray());
}
/**
* @return bool
*/
public function hasListenPermission(): bool
{
if ($this->hasSingPermission() || in_array($this->user->getAttribute('identity'), [2, 3], false) || in_array($this->user->getKey(), ConfigHelper::getListenUserIds(), false)) {
return true;
}
return $this->checkTag(SystemTag::query()->whereJsonContains('expand->permission', [1])->pluck('id')->toArray());
}
/**
* @return bool
*/
public function isSinger(): bool
{
return $this->checkTag(ConfigHelper::getSingerTagIds());
}
}