|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Tinyissue package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Tinyissue\Http\Middleware; |
|
13
|
|
|
|
|
14
|
|
|
use Closure; |
|
15
|
|
|
use Illuminate\Database\Eloquent\Model as ModelAbstract; |
|
16
|
|
|
use Illuminate\Http\Request; |
|
17
|
|
|
use Illuminate\Routing\Route; |
|
18
|
|
|
use Tinyissue\Contracts\Model\AccessControl; |
|
19
|
|
|
use Tinyissue\Model\Permission as PermissionModel; |
|
20
|
|
|
use Tinyissue\Model\Project as ProjectModel; |
|
21
|
|
|
use Tinyissue\Model\User; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Permission is a Middleware class to for checking if current user has the permission to access the request. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class Permission extends MiddlewareAbstract |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* List of permissions that can be accessed by public users. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $publicAccess = [ |
|
36
|
|
|
'issue-view', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Ordered list of contexts. |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $contexts = [ |
|
45
|
|
|
'comment', |
|
46
|
|
|
'attachment', |
|
47
|
43 |
|
'issue', |
|
48
|
|
|
'project', |
|
49
|
43 |
|
]; |
|
50
|
43 |
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Handle an incoming request. |
|
53
|
|
|
* |
|
54
|
|
|
* @param Request $request |
|
55
|
|
|
* @param \Closure $next |
|
56
|
|
|
* |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
|
|
public function handle(Request $request, Closure $next) |
|
60
|
42 |
|
{ |
|
61
|
|
|
$permission = $this->getPermission($request); |
|
62
|
42 |
|
|
|
63
|
42 |
|
// Can't access if public project disabled or user does not have access |
|
64
|
|
|
if (!$this->isInPublicProjectContext($request, $permission) && !$this->canAccess($request, $permission)) { |
|
65
|
42 |
|
abort(401); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $next($request); |
|
69
|
42 |
|
} |
|
70
|
42 |
|
|
|
71
|
42 |
|
/** |
|
72
|
|
|
* Whether or not the current context is in public project. |
|
73
|
42 |
|
* |
|
74
|
42 |
|
* @param Request $request |
|
75
|
8 |
|
* @param string $permission |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
38 |
|
*/ |
|
79
|
|
|
protected function isInPublicProjectContext(Request $request, $permission) |
|
80
|
|
|
{ |
|
81
|
|
|
/** @var ProjectModel|null $project */ |
|
82
|
|
|
$project = $request->route()->getParameter('project'); |
|
83
|
|
|
$isPublicEnabled = app('tinyissue.settings')->isPublicProjectsEnabled(); |
|
84
|
|
|
$isPublicAccess = in_array($permission, $this->publicAccess); |
|
85
|
|
|
$isPublicProject = $project instanceof ProjectModel && $project->isPublic(); |
|
86
|
|
|
|
|
87
|
|
|
return $isPublicEnabled && $isPublicAccess && $isPublicProject; |
|
88
|
42 |
|
} |
|
89
|
|
|
|
|
90
|
42 |
|
/** |
|
91
|
|
|
* Whether or not the user can access the current context. |
|
92
|
42 |
|
* |
|
93
|
|
|
* @param Request $request |
|
94
|
|
|
* @param string $permission |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function canAccess(Request $request, $permission) |
|
99
|
|
|
{ |
|
100
|
|
|
try { |
|
101
|
|
|
$user = $this->getLoggedUser(); |
|
102
|
|
|
} catch (\Exception $e) { |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// Can access all projects |
|
107
|
|
|
if ($permission !== PermissionModel::PERM_ADMIN && $user->permission(PermissionModel::PERM_PROJECT_ALL)) { |
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$hasPermission = $user->permission($permission); |
|
112
|
|
|
|
|
113
|
|
|
// Can access the current context |
|
114
|
|
|
$context = $this->getCurrentContext($request->route()); |
|
|
|
|
|
|
115
|
|
|
$contextAccess = true; |
|
|
|
|
|
|
116
|
|
|
if ($context instanceof AccessControl) { |
|
117
|
|
|
$contextAccess = $context->can($permission, $user); |
|
118
|
|
|
if (!$contextAccess) { |
|
119
|
|
|
return false; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return true; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $hasPermission; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Return the model object of the current context. |
|
130
|
|
|
* We check the lowest ( Comment ) first, to the highest ( Project ). |
|
131
|
|
|
* |
|
132
|
|
|
* @param Route $route |
|
133
|
|
|
* |
|
134
|
|
|
* @return ModelAbstract|null |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function getCurrentContext(Route $route) |
|
137
|
|
|
{ |
|
138
|
|
|
foreach ($this->contexts as $context) { |
|
139
|
|
|
$parameter = $route->getParameter($context); |
|
140
|
|
|
if ($parameter instanceof ModelAbstract) { |
|
141
|
|
|
return $parameter; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return null; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Returns the permission defined in route action. |
|
150
|
|
|
* |
|
151
|
|
|
* @param Request $request |
|
152
|
|
|
* |
|
153
|
|
|
* @return mixed |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function getPermission(Request $request) |
|
156
|
|
|
{ |
|
157
|
|
|
$actions = $request->route()->getAction(); |
|
158
|
|
|
|
|
159
|
|
|
return $actions['permission']; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: