1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: sheldon |
||
5 | * Date: 18-3-27 |
||
6 | * Time: 下午5:18. |
||
7 | */ |
||
8 | |||
9 | namespace Yeelight\Models; |
||
10 | |||
11 | use Illuminate\Support\Facades\Auth; |
||
12 | use Illuminate\Support\MessageBag; |
||
13 | use Yeelight\Http\Middleware\Pjax; |
||
14 | |||
15 | /** |
||
16 | * Class Permission |
||
17 | * |
||
18 | * @category Yeelight |
||
19 | * |
||
20 | * @package Yeelight\Models |
||
21 | * |
||
22 | * @author Sheldon Lee <[email protected]> |
||
23 | * |
||
24 | * @license https://opensource.org/licenses/MIT MIT |
||
25 | * |
||
26 | * @link https://www.yeelight.com |
||
27 | */ |
||
28 | class Permission |
||
29 | { |
||
30 | /** |
||
31 | * Check permission. |
||
32 | * |
||
33 | * @param $permission |
||
34 | * |
||
35 | * @return true |
||
36 | */ |
||
37 | public static function check($permission) |
||
38 | { |
||
39 | if (static::isAdministrator()) { |
||
40 | return true; |
||
41 | } |
||
42 | |||
43 | if (is_array($permission)) { |
||
44 | collect($permission)->each(function ($permission) { |
||
45 | call_user_func([Permission::class, 'check'], $permission); |
||
46 | }); |
||
47 | |||
48 | return; |
||
49 | } |
||
50 | |||
51 | if (Auth::guard(config('yeelight.backend.route.prefix'))->user()->cannot($permission)) { |
||
52 | static::error(); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Roles allowed to access. |
||
58 | * |
||
59 | * @param $roles |
||
60 | * |
||
61 | * @return true |
||
62 | */ |
||
63 | public static function allow($roles) |
||
64 | { |
||
65 | if (static::isAdministrator()) { |
||
66 | return true; |
||
67 | } |
||
68 | |||
69 | if (!Auth::guard(config('yeelight.backend.route.prefix'))->user()->inRoles($roles)) { |
||
70 | static::error(); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Don't check permission. |
||
76 | * |
||
77 | * @return bool |
||
78 | */ |
||
79 | public static function free() |
||
80 | { |
||
81 | return true; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Roles denied to access. |
||
86 | * |
||
87 | * @param $roles |
||
88 | * |
||
89 | * @return true |
||
90 | */ |
||
91 | public static function deny($roles) |
||
92 | { |
||
93 | if (static::isAdministrator()) { |
||
94 | return true; |
||
95 | } |
||
96 | |||
97 | if (Auth::guard(config('yeelight.backend.route.prefix'))->user()->inRoles($roles)) { |
||
98 | static::error(); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Send error response page. |
||
104 | */ |
||
105 | public static function error() |
||
106 | { |
||
107 | $error = new MessageBag(trans('backend.deny')); |
||
108 | |||
109 | session()->flash('error', $error); |
||
110 | |||
111 | $response = response($error); |
||
112 | |||
113 | Pjax::respond($response); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
114 | } |
||
115 | |||
116 | /** |
||
117 | * If current user is administrator. |
||
118 | * |
||
119 | * @return mixed |
||
120 | */ |
||
121 | public static function isAdministrator() |
||
122 | { |
||
123 | return Auth::guard(config('yeelight.backend.route.prefix'))->user()->isRole('administrator'); |
||
124 | } |
||
125 | } |
||
126 |