|
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; |
|
16
|
|
|
use Illuminate\Http\Request; |
|
17
|
|
|
use Tinyissue\Model\Project as ProjectModel; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Project is a Middleware class to for checking if the route parameters are correct. |
|
21
|
|
|
* e.g. The issue id is belongs to the project id. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class Project |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* List of callbacks to handle the incoming request |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $callbacks = [ |
|
33
|
|
|
'Issue', |
|
34
|
|
|
'IssueFilter', |
|
35
|
|
|
'Note', |
|
36
|
|
|
'Project' |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Handle an incoming request. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Request $request |
|
43
|
|
|
* @param \Closure $next |
|
44
|
|
|
* |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
37 |
|
public function handle(Request $request, Closure $next) |
|
48
|
|
|
{ |
|
49
|
|
|
// Current callback |
|
50
|
37 |
|
$callback = current($this->callbacks); |
|
51
|
37 |
|
$method = 'handle' . $callback . 'Request'; |
|
52
|
|
|
|
|
53
|
37 |
|
if ($callback && !$this->$method($request)) { |
|
54
|
|
|
|
|
55
|
|
|
// Current callback does not belong to the request - move next |
|
56
|
32 |
|
next($this->callbacks); |
|
57
|
|
|
|
|
58
|
32 |
|
return $this->handle($request, $next); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
37 |
|
return $next($request); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Whether or not the incoming request is valid project issue request |
|
66
|
|
|
* |
|
67
|
|
|
* @param Request $request |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
37 |
|
protected function handleIssueRequest(Request $request) |
|
72
|
|
|
{ |
|
73
|
37 |
|
return $this->isBelongToProject($request, 'issue'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Whether or not a model entity relationship with the project is correct |
|
78
|
|
|
* |
|
79
|
|
|
* @param Request $request |
|
80
|
|
|
* @param string $entityName |
|
81
|
|
|
* |
|
82
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException |
|
83
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
37 |
|
protected function isBelongToProject(Request $request, $entityName) |
|
88
|
|
|
{ |
|
89
|
|
|
/** @var Model $entity */ |
|
90
|
37 |
|
$entity = $request->route()->getParameter($entityName); |
|
91
|
|
|
|
|
92
|
|
|
/** @var ProjectModel|null $project */ |
|
93
|
37 |
|
$project = $request->route()->getParameter('project'); |
|
94
|
|
|
|
|
95
|
37 |
|
if (!$entity instanceof Model || !$project instanceof ProjectModel) { |
|
96
|
32 |
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Abort request invalid data |
|
100
|
20 |
|
if ($entity->project_id !== $project->id) { |
|
101
|
2 |
|
abort(401); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
19 |
|
return true; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Whether or not the incoming uri is for the issue filter "project/issue/{issue}" |
|
109
|
|
|
* |
|
110
|
|
|
* @param Request $request |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
32 |
|
protected function handleIssueFilterRequest(Request $request) |
|
115
|
|
|
{ |
|
116
|
|
|
/** @var ProjectModel|null $project */ |
|
117
|
32 |
|
$project = $request->route()->getParameter('project'); |
|
118
|
|
|
/** @var ProjectModel\Issue|null $issue */ |
|
119
|
32 |
|
$issue = $request->route()->getParameter('issue'); |
|
120
|
|
|
|
|
121
|
32 |
|
if ($project === null && $issue && $request->route()->getUri() === 'project/issue/{issue}') { |
|
122
|
|
|
// Load the project from the issue model |
|
123
|
1 |
|
$request->route()->forgetParameter('issue'); |
|
124
|
1 |
|
$request->route()->setParameter('project', $issue->project); |
|
125
|
1 |
|
$request->route()->setParameter('issue', $issue); |
|
126
|
|
|
|
|
127
|
1 |
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
31 |
|
return false; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Whether or not the incoming request is valid project note request |
|
135
|
|
|
* |
|
136
|
|
|
* @param Request $request |
|
137
|
|
|
* |
|
138
|
|
|
* @return bool |
|
139
|
|
|
*/ |
|
140
|
31 |
|
protected function handleNoteRequest(Request $request) |
|
141
|
|
|
{ |
|
142
|
31 |
|
return $this->isBelongToProject($request, 'note'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Whether or not the incoming request is valid project note request |
|
147
|
|
|
* |
|
148
|
|
|
* @param Request $request |
|
149
|
|
|
* |
|
150
|
|
|
* @return bool |
|
151
|
|
|
*/ |
|
152
|
31 |
|
protected function handleProjectRequest(Request $request) |
|
153
|
|
|
{ |
|
154
|
|
|
/** @var ProjectModel|null $project */ |
|
155
|
31 |
|
$project = $request->route()->getParameter('project'); |
|
156
|
|
|
|
|
157
|
31 |
|
if (auth()->guest() && $project instanceof ProjectModel && $project->isPrivate()) { |
|
158
|
|
|
abort(401); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
31 |
|
return true; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|