• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miaoxing / app / 10654299229

01 Sep 2024 01:03PM UTC coverage: 24.699% (-0.3%) from 25.0%
10654299229

push

github

semantic-release-bot
chore(release): publish

See CHANGELOG.md for more details.

41 of 166 relevant lines covered (24.7%)

2.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Model/HasPermissionTrait.php
1
<?php
2

3
namespace Miaoxing\App\Model;
4

5
use Miaoxing\App\Service\Permission;
6
use Wei\Ret;
7

8
/**
9
 * @mixin \LoggerPropMixin
10
 * @mixin \PermissionMapPropMixin
11
 * @mixin \PermissionPropMixin
12
 */
13
trait HasPermissionTrait
14
{
15
    public function getPermissionCodes(): array
16
    {
17
        if (!$this->permission->isEnabledCheck()) {
×
18
            return ['*'];
×
19
        }
20

21
        return array_unique(array_merge(
×
22
            $this->getActionPermissionCodes(),
×
23
            $this->enabledRoles->enabledPermissions->getAll('code'),
×
24
            $this->enabledPermissions->getAll('code')
×
25
        ));
×
26
    }
27

28
    /**
29
     * @return string[]
30
     */
31
    public function getActionPermissionCodes(): array
32
    {
33
        if ($this->isSuperAdmin()) {
×
34
            return ['*'];
×
35
        }
36

37
        $actions = $this->enabledRoles->getAll('actions');
×
38
        return array_unique(array_merge(...$actions));
×
39
    }
40

41
    /**
42
     * Check if user have the specified permission
43
     *
44
     * @param string $code
45
     * @return Ret
46
     */
47
    public function checkPermission(string $code): Ret
48
    {
49
        if ($this->hasPermission($code)) {
×
50
            return suc();
×
51
        }
52
        return err('很抱歉,您没有权限执行该操作');
×
53
    }
54

55
    /**
56
     * Whether the user have the specified permission
57
     *
58
     * @param string $code
59
     * @return bool
60
     */
61
    public function hasPermission(string $code): bool
62
    {
63
        // TODO 根据场景实现逐级查找,变量查找按需查找
64
        return in_array($code, $this->getPermissionCodes(), true);
×
65
    }
66

67
    /**
68
     * @param string $method
69
     * @param string $path
70
     * @return Ret
71
     */
72
    public function checkPagePermission(string $method, string $path): Ret
73
    {
74
        if ($this->hasPagePermission($method, $path)) {
×
75
            return suc();
×
76
        }
77
        return err('很抱歉,您没有权限执行该操作');
×
78
    }
79

80
    public function hasPagePermission(string $method, string $path): bool
81
    {
82
        if ($this->isSuperAdmin()) {
×
83
            return true;
×
84
        }
85

86
        // 1. 获取权限
87
        $permissions = $this->getActionPermissionCodes();
×
88
        $this->logger->debug('Get user menu permissions', $permissions);
×
89

90
        // 2. 转换菜单为页面
91
        $map = $this->permissionMap->getMap();
×
92
        $map = array_intersect_key($map, array_flip($permissions));
×
93
        $map = array_unique(array_merge(...array_values($map)));
×
94
        $this->logger->debug('Get user action permissions', $map);
×
95

96
        // 3. 检查当前页面是否在里面
97
        $path = ltrim($path, '/');
×
98
        if ($this->hasPagePermissionIn($method, $path, $map)) {
×
99
            return true;
×
100
        }
101

102
        // Whether has role permission
103
        $rolePermissionCodes = $this->enabledRoles->enabledPermissions->getAll('code');
×
104
        if ($this->hasPagePermissionIn($method, $path, $rolePermissionCodes)) {
×
105
            return true;
×
106
        }
107

108
        // Whether has direct permission
109
        $permissionCodes = $this->enabledPermissions->getAll('code');
×
110
        return $this->hasPagePermissionIn($method, $path, $permissionCodes);
×
111
    }
112

113
    /**
114
     * Whether has page permission in the specified permission codes
115
     *
116
     * @param string $method
117
     * @param string $path
118
     * @param array $permissions
119
     * @return bool
120
     */
121
    protected function hasPagePermissionIn(string $method, string $path, array $permissions): bool
122
    {
123
        $path = ltrim($path, '/');
×
124
        foreach ($permissions as $permission) {
×
125
            $parts = explode(' ', $permission, 2);
×
126
            $apiMethod = $parts[0];
×
127
            $apiPath = $parts[1] ?? null;
×
128

129
            if ($method !== $apiMethod) {
×
130
                continue;
×
131
            }
132

133
            if ($apiPath === $path) {
×
134
                return true;
×
135
            }
136

137
            if (false !== strpos($apiPath, '[')) {
×
138
                $regex = preg_replace('#[.\+*?[^\]${}=!|:-]#', '\\\$0', $apiPath);
×
139
                $regex = str_replace(['\[', '\]'], ['(?P<', '>.+?)'], $regex);
×
140
                $regex = '#^' . $regex . '$#uUD';
×
141
                if (preg_match($regex, $path)) {
×
142
                    return true;
×
143
                }
144
            }
145
        }
146
        return false;
×
147
    }
148
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc