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

miaoxing / app / 6756064869

04 Nov 2023 04:44PM UTC coverage: 2.381% (-0.2%) from 2.614%
6756064869

push

github

twinh
ci: add PHP 8, remove PHP 7.2, 7.3

4 of 168 relevant lines covered (2.38%)

0.04 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 Wei\Ret;
6

7
/**
8
 * @mixin \LoggerPropMixin
9
 * @mixin \PermissionMapPropMixin
10
 */
11
trait HasPermissionTrait
12
{
13
    public function getPermissionCodes(): array
14
    {
15
        return array_unique(array_merge(
×
16
            $this->getActionPermissionCodes(),
×
17
            $this->enabledRoles->enabledPermissions->getAll('code'),
×
18
            $this->enabledPermissions->getAll('code')
×
19
        ));
×
20
    }
21

22
    /**
23
     * @return string[]
24
     */
25
    public function getActionPermissionCodes(): array
26
    {
27
        if ($this->isSuperAdmin()) {
×
28
            return ['*'];
×
29
        }
30

31
        $actions = $this->enabledRoles->getAll('actions');
×
32
        return array_unique(array_merge(...$actions));
×
33
    }
34

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

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

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

74
    public function hasPagePermission(string $method, string $path): bool
75
    {
76
        if ($this->isSuperAdmin()) {
×
77
            return true;
×
78
        }
79

80
        // 1. 获取权限
81
        $permissions = $this->getActionPermissionCodes();
×
82
        $this->logger->debug('Get user menu permissions', $permissions);
×
83

84
        // 2. 转换菜单为页面
85
        $map = $this->permissionMap->getMap();
×
86
        $map = array_intersect_key($map, array_flip($permissions));
×
87
        $map = array_unique(array_merge(...array_values($map)));
×
88
        $this->logger->debug('Get user action permissions', $map);
×
89

90
        // 3. 检查当前页面是否在里面
91
        $path = ltrim($path, '/');
×
92
        if ($this->hasPagePermissionIn($method, $path, $map)) {
×
93
            return true;
×
94
        }
95

96
        // Whether has role permission
97
        $rolePermissionCodes = $this->enabledRoles->enabledPermissions->getAll('code');
×
98
        if ($this->hasPagePermissionIn($method, $path, $rolePermissionCodes)) {
×
99
            return true;
×
100
        }
101

102
        // Whether has direct permission
103
        $permissionCodes = $this->enabledPermissions->getAll('code');
×
104
        return $this->hasPagePermissionIn($method, $path, $permissionCodes);
×
105
    }
106

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

123
            if ($method !== $apiMethod) {
×
124
                continue;
×
125
            }
126

127
            if ($apiPath === $path) {
×
128
                return true;
×
129
            }
130

131
            if (false !== strpos($apiPath, '[')) {
×
132
                $regex = preg_replace('#[.\+*?[^\]${}=!|:-]#', '\\\\$0', $apiPath);
×
133
                $regex = str_replace(['\[', '\]'], ['(?P<', '>.+?)'], $regex);
×
134
                $regex = '#^' . $regex . '$#uUD';
×
135
                if (preg_match($regex, $path)) {
×
136
                    return true;
×
137
                }
138
            }
139
        }
140
        return false;
×
141
    }
142
}
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