@@ -6,276 +6,276 @@ |
||
6 | 6 | |
7 | 7 | class GenerateDoc extends Command |
8 | 8 | { |
9 | - /** |
|
10 | - * The name and signature of the console command. |
|
11 | - * |
|
12 | - * @var string |
|
13 | - */ |
|
14 | - protected $signature = 'doc:generate'; |
|
9 | + /** |
|
10 | + * The name and signature of the console command. |
|
11 | + * |
|
12 | + * @var string |
|
13 | + */ |
|
14 | + protected $signature = 'doc:generate'; |
|
15 | 15 | |
16 | - /** |
|
17 | - * The console command description. |
|
18 | - * |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - protected $description = 'Generate api documentation'; |
|
16 | + /** |
|
17 | + * The console command description. |
|
18 | + * |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + protected $description = 'Generate api documentation'; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Create a new command instance. |
|
25 | - * |
|
26 | - * @return void |
|
27 | - */ |
|
28 | - public function __construct() |
|
29 | - { |
|
30 | - parent::__construct(); |
|
31 | - } |
|
23 | + /** |
|
24 | + * Create a new command instance. |
|
25 | + * |
|
26 | + * @return void |
|
27 | + */ |
|
28 | + public function __construct() |
|
29 | + { |
|
30 | + parent::__construct(); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Execute the console command. |
|
35 | - * |
|
36 | - * @return mixed |
|
37 | - */ |
|
38 | - public function handle() |
|
39 | - { |
|
40 | - $docData = []; |
|
41 | - $docData['models'] = []; |
|
42 | - $routes = $this->getRoutes(); |
|
43 | - foreach ($routes as $route) |
|
44 | - { |
|
45 | - if ($route) |
|
46 | - { |
|
47 | - $actoinArray = explode('@', $route['action']); |
|
48 | - if(array_get($actoinArray, 1, false)) |
|
49 | - { |
|
50 | - $controller = $actoinArray[0]; |
|
51 | - $method = $actoinArray[1]; |
|
52 | - $route['name'] = $method !== 'index' ? $method : 'list'; |
|
33 | + /** |
|
34 | + * Execute the console command. |
|
35 | + * |
|
36 | + * @return mixed |
|
37 | + */ |
|
38 | + public function handle() |
|
39 | + { |
|
40 | + $docData = []; |
|
41 | + $docData['models'] = []; |
|
42 | + $routes = $this->getRoutes(); |
|
43 | + foreach ($routes as $route) |
|
44 | + { |
|
45 | + if ($route) |
|
46 | + { |
|
47 | + $actoinArray = explode('@', $route['action']); |
|
48 | + if(array_get($actoinArray, 1, false)) |
|
49 | + { |
|
50 | + $controller = $actoinArray[0]; |
|
51 | + $method = $actoinArray[1]; |
|
52 | + $route['name'] = $method !== 'index' ? $method : 'list'; |
|
53 | 53 | |
54 | - $reflectionClass = new \ReflectionClass($controller); |
|
55 | - $reflectionMethod = $reflectionClass->getMethod($method); |
|
56 | - $classProperties = $reflectionClass->getDefaultProperties(); |
|
57 | - $skipLoginCheck = array_key_exists('skipLoginCheck', $classProperties) ? $classProperties['skipLoginCheck'] : false; |
|
58 | - $validationRules = array_key_exists('validationRules', $classProperties) ? $classProperties['validationRules'] : false; |
|
54 | + $reflectionClass = new \ReflectionClass($controller); |
|
55 | + $reflectionMethod = $reflectionClass->getMethod($method); |
|
56 | + $classProperties = $reflectionClass->getDefaultProperties(); |
|
57 | + $skipLoginCheck = array_key_exists('skipLoginCheck', $classProperties) ? $classProperties['skipLoginCheck'] : false; |
|
58 | + $validationRules = array_key_exists('validationRules', $classProperties) ? $classProperties['validationRules'] : false; |
|
59 | 59 | |
60 | - $this->processDocBlock($route, $reflectionMethod); |
|
61 | - $this->getHeaders($route, $method, $skipLoginCheck); |
|
62 | - $this->getPostData($route, $reflectionMethod, $validationRules); |
|
60 | + $this->processDocBlock($route, $reflectionMethod); |
|
61 | + $this->getHeaders($route, $method, $skipLoginCheck); |
|
62 | + $this->getPostData($route, $reflectionMethod, $validationRules); |
|
63 | 63 | |
64 | - $route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']); |
|
64 | + $route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']); |
|
65 | 65 | |
66 | - preg_match('/api\/([^#]+)\//iU', $route['uri'], $module); |
|
67 | - $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/' . $module[1] . '/') - 1)][] = $route; |
|
66 | + preg_match('/api\/([^#]+)\//iU', $route['uri'], $module); |
|
67 | + $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/' . $module[1] . '/') - 1)][] = $route; |
|
68 | 68 | |
69 | - $this->getModels($classProperties['model'], $docData); |
|
70 | - } |
|
71 | - } |
|
72 | - } |
|
69 | + $this->getModels($classProperties['model'], $docData); |
|
70 | + } |
|
71 | + } |
|
72 | + } |
|
73 | 73 | |
74 | - $docData['errors'] = $this->getErrors(); |
|
75 | - $docData['reports'] = \Core::reports()->all(); |
|
76 | - \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData)); |
|
77 | - } |
|
74 | + $docData['errors'] = $this->getErrors(); |
|
75 | + $docData['reports'] = \Core::reports()->all(); |
|
76 | + \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData)); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Get list of all registered routes. |
|
81 | - * |
|
82 | - * @return collection |
|
83 | - */ |
|
84 | - protected function getRoutes() |
|
85 | - { |
|
86 | - return collect(\Route::getRoutes())->map(function ($route) { |
|
87 | - if (strpos($route->uri(), 'api') !== false) |
|
88 | - { |
|
89 | - return [ |
|
90 | - 'method' => $route->methods()[0], |
|
91 | - 'uri' => $route->uri(), |
|
92 | - 'action' => $route->getActionName(), |
|
93 | - 'prefix' => $route->getPrefix() |
|
94 | - ]; |
|
95 | - } |
|
96 | - return false; |
|
97 | - })->all(); |
|
98 | - } |
|
79 | + /** |
|
80 | + * Get list of all registered routes. |
|
81 | + * |
|
82 | + * @return collection |
|
83 | + */ |
|
84 | + protected function getRoutes() |
|
85 | + { |
|
86 | + return collect(\Route::getRoutes())->map(function ($route) { |
|
87 | + if (strpos($route->uri(), 'api') !== false) |
|
88 | + { |
|
89 | + return [ |
|
90 | + 'method' => $route->methods()[0], |
|
91 | + 'uri' => $route->uri(), |
|
92 | + 'action' => $route->getActionName(), |
|
93 | + 'prefix' => $route->getPrefix() |
|
94 | + ]; |
|
95 | + } |
|
96 | + return false; |
|
97 | + })->all(); |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * Generate headers for the given route. |
|
102 | - * |
|
103 | - * @param array &$route |
|
104 | - * @param string $method |
|
105 | - * @param array $skipLoginCheck |
|
106 | - * @return void |
|
107 | - */ |
|
108 | - protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
109 | - { |
|
110 | - $route['headers'] = [ |
|
111 | - 'Accept' => 'application/json', |
|
112 | - 'Content-Type' => 'application/json', |
|
113 | - 'locale' => 'The language of the returned data: ar, en or all.', |
|
114 | - 'time-zone' => 'Your locale time zone', |
|
115 | - ]; |
|
100 | + /** |
|
101 | + * Generate headers for the given route. |
|
102 | + * |
|
103 | + * @param array &$route |
|
104 | + * @param string $method |
|
105 | + * @param array $skipLoginCheck |
|
106 | + * @return void |
|
107 | + */ |
|
108 | + protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
109 | + { |
|
110 | + $route['headers'] = [ |
|
111 | + 'Accept' => 'application/json', |
|
112 | + 'Content-Type' => 'application/json', |
|
113 | + 'locale' => 'The language of the returned data: ar, en or all.', |
|
114 | + 'time-zone' => 'Your locale time zone', |
|
115 | + ]; |
|
116 | 116 | |
117 | 117 | |
118 | - if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) |
|
119 | - { |
|
120 | - $route['headers']['Authorization'] = 'Bearer {token}'; |
|
121 | - } |
|
122 | - } |
|
118 | + if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) |
|
119 | + { |
|
120 | + $route['headers']['Authorization'] = 'Bearer {token}'; |
|
121 | + } |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * Generate description and params for the given route |
|
126 | - * based on the docblock. |
|
127 | - * |
|
128 | - * @param array &$route |
|
129 | - * @param object $reflectionMethod] |
|
130 | - * @return void |
|
131 | - */ |
|
132 | - protected function processDocBlock(&$route, $reflectionMethod) |
|
133 | - { |
|
134 | - $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
135 | - $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
136 | - $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
137 | - $params = $docblock->getTagsByName('param'); |
|
138 | - $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
139 | - foreach ($params as $param) |
|
140 | - { |
|
141 | - $name = $param->getVariableName(); |
|
142 | - if ($name !== 'request') |
|
143 | - { |
|
144 | - $route['parametars'][$param->getVariableName()] = $param->getDescription()->render(); |
|
145 | - } |
|
146 | - } |
|
147 | - } |
|
124 | + /** |
|
125 | + * Generate description and params for the given route |
|
126 | + * based on the docblock. |
|
127 | + * |
|
128 | + * @param array &$route |
|
129 | + * @param object $reflectionMethod] |
|
130 | + * @return void |
|
131 | + */ |
|
132 | + protected function processDocBlock(&$route, $reflectionMethod) |
|
133 | + { |
|
134 | + $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
135 | + $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
136 | + $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
137 | + $params = $docblock->getTagsByName('param'); |
|
138 | + $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
139 | + foreach ($params as $param) |
|
140 | + { |
|
141 | + $name = $param->getVariableName(); |
|
142 | + if ($name !== 'request') |
|
143 | + { |
|
144 | + $route['parametars'][$param->getVariableName()] = $param->getDescription()->render(); |
|
145 | + } |
|
146 | + } |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * Generate post body for the given route. |
|
151 | - * |
|
152 | - * @param array &$route |
|
153 | - * @param object $reflectionMethod |
|
154 | - * @param array $validationRules |
|
155 | - * @return void |
|
156 | - */ |
|
157 | - protected function getPostData(&$route, $reflectionMethod, $validationRules) |
|
158 | - { |
|
159 | - if ($route['method'] == 'POST') |
|
160 | - { |
|
161 | - $body = $this->getMethodBody($reflectionMethod); |
|
149 | + /** |
|
150 | + * Generate post body for the given route. |
|
151 | + * |
|
152 | + * @param array &$route |
|
153 | + * @param object $reflectionMethod |
|
154 | + * @param array $validationRules |
|
155 | + * @return void |
|
156 | + */ |
|
157 | + protected function getPostData(&$route, $reflectionMethod, $validationRules) |
|
158 | + { |
|
159 | + if ($route['method'] == 'POST') |
|
160 | + { |
|
161 | + $body = $this->getMethodBody($reflectionMethod); |
|
162 | 162 | |
163 | - preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match); |
|
164 | - if (count($match)) |
|
165 | - { |
|
166 | - if ($match[1] == '$this->validationRules') |
|
167 | - { |
|
168 | - $route['body'] = $validationRules; |
|
169 | - } |
|
170 | - else |
|
171 | - { |
|
172 | - $route['body'] = eval('return ' . str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]) . ';'); |
|
173 | - } |
|
163 | + preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match); |
|
164 | + if (count($match)) |
|
165 | + { |
|
166 | + if ($match[1] == '$this->validationRules') |
|
167 | + { |
|
168 | + $route['body'] = $validationRules; |
|
169 | + } |
|
170 | + else |
|
171 | + { |
|
172 | + $route['body'] = eval('return ' . str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]) . ';'); |
|
173 | + } |
|
174 | 174 | |
175 | - foreach ($route['body'] as &$rule) |
|
176 | - { |
|
177 | - if(strpos($rule, 'unique')) |
|
178 | - { |
|
179 | - $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
180 | - } |
|
181 | - elseif(strpos($rule, 'exists')) |
|
182 | - { |
|
183 | - $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
184 | - } |
|
185 | - } |
|
186 | - } |
|
187 | - else |
|
188 | - { |
|
189 | - $route['body'] = 'conditions'; |
|
190 | - } |
|
191 | - } |
|
192 | - } |
|
175 | + foreach ($route['body'] as &$rule) |
|
176 | + { |
|
177 | + if(strpos($rule, 'unique')) |
|
178 | + { |
|
179 | + $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
180 | + } |
|
181 | + elseif(strpos($rule, 'exists')) |
|
182 | + { |
|
183 | + $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
184 | + } |
|
185 | + } |
|
186 | + } |
|
187 | + else |
|
188 | + { |
|
189 | + $route['body'] = 'conditions'; |
|
190 | + } |
|
191 | + } |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * Generate application errors. |
|
196 | - * |
|
197 | - * @return array |
|
198 | - */ |
|
199 | - protected function getErrors() |
|
200 | - { |
|
201 | - $errors = []; |
|
202 | - $reflectionClass = new \ReflectionClass('App\Modules\Core\Utl\ErrorHandler'); |
|
203 | - foreach ($reflectionClass->getMethods() as $method) |
|
204 | - { |
|
205 | - $methodName = $method->getName(); |
|
206 | - $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
207 | - $body = $this->getMethodBody($reflectionMethod); |
|
194 | + /** |
|
195 | + * Generate application errors. |
|
196 | + * |
|
197 | + * @return array |
|
198 | + */ |
|
199 | + protected function getErrors() |
|
200 | + { |
|
201 | + $errors = []; |
|
202 | + $reflectionClass = new \ReflectionClass('App\Modules\Core\Utl\ErrorHandler'); |
|
203 | + foreach ($reflectionClass->getMethods() as $method) |
|
204 | + { |
|
205 | + $methodName = $method->getName(); |
|
206 | + $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
207 | + $body = $this->getMethodBody($reflectionMethod); |
|
208 | 208 | |
209 | - preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match); |
|
209 | + preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match); |
|
210 | 210 | |
211 | - if (count($match)) |
|
212 | - { |
|
213 | - $errors[$match[1]][] = $methodName; |
|
214 | - } |
|
215 | - } |
|
211 | + if (count($match)) |
|
212 | + { |
|
213 | + $errors[$match[1]][] = $methodName; |
|
214 | + } |
|
215 | + } |
|
216 | 216 | |
217 | - return $errors; |
|
218 | - } |
|
217 | + return $errors; |
|
218 | + } |
|
219 | 219 | |
220 | - /** |
|
221 | - * Get the given method body code. |
|
222 | - * |
|
223 | - * @param object $reflectionMethod |
|
224 | - * @return string |
|
225 | - */ |
|
226 | - protected function getMethodBody($reflectionMethod) |
|
227 | - { |
|
228 | - $filename = $reflectionMethod->getFileName(); |
|
229 | - $start_line = $reflectionMethod->getStartLine() - 1; |
|
230 | - $end_line = $reflectionMethod->getEndLine(); |
|
231 | - $length = $end_line - $start_line; |
|
232 | - $source = file($filename); |
|
233 | - $body = implode("", array_slice($source, $start_line, $length)); |
|
234 | - $body = trim(preg_replace('/\s+/', '', $body)); |
|
220 | + /** |
|
221 | + * Get the given method body code. |
|
222 | + * |
|
223 | + * @param object $reflectionMethod |
|
224 | + * @return string |
|
225 | + */ |
|
226 | + protected function getMethodBody($reflectionMethod) |
|
227 | + { |
|
228 | + $filename = $reflectionMethod->getFileName(); |
|
229 | + $start_line = $reflectionMethod->getStartLine() - 1; |
|
230 | + $end_line = $reflectionMethod->getEndLine(); |
|
231 | + $length = $end_line - $start_line; |
|
232 | + $source = file($filename); |
|
233 | + $body = implode("", array_slice($source, $start_line, $length)); |
|
234 | + $body = trim(preg_replace('/\s+/', '', $body)); |
|
235 | 235 | |
236 | - return $body; |
|
237 | - } |
|
236 | + return $body; |
|
237 | + } |
|
238 | 238 | |
239 | - /** |
|
240 | - * Get example object of all availble models. |
|
241 | - * |
|
242 | - * @param string $modelName |
|
243 | - * @param array $docData |
|
244 | - * @return string |
|
245 | - */ |
|
246 | - protected function getModels($modelName, &$docData) |
|
247 | - { |
|
248 | - if ($modelName && ! array_key_exists($modelName, $docData['models'])) |
|
249 | - { |
|
250 | - $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass; |
|
251 | - $model = factory($modelClass)->make(); |
|
252 | - $modelArr = $model->toArray(); |
|
239 | + /** |
|
240 | + * Get example object of all availble models. |
|
241 | + * |
|
242 | + * @param string $modelName |
|
243 | + * @param array $docData |
|
244 | + * @return string |
|
245 | + */ |
|
246 | + protected function getModels($modelName, &$docData) |
|
247 | + { |
|
248 | + if ($modelName && ! array_key_exists($modelName, $docData['models'])) |
|
249 | + { |
|
250 | + $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass; |
|
251 | + $model = factory($modelClass)->make(); |
|
252 | + $modelArr = $model->toArray(); |
|
253 | 253 | |
254 | - if ( $model->trans && ! $model->trans->count()) |
|
255 | - { |
|
256 | - $modelArr['trans'] = [ |
|
257 | - 'en' => factory($modelClass . 'Translation')->make()->toArray() |
|
258 | - ]; |
|
259 | - } |
|
254 | + if ( $model->trans && ! $model->trans->count()) |
|
255 | + { |
|
256 | + $modelArr['trans'] = [ |
|
257 | + 'en' => factory($modelClass . 'Translation')->make()->toArray() |
|
258 | + ]; |
|
259 | + } |
|
260 | 260 | |
261 | - $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
262 | - } |
|
263 | - } |
|
261 | + $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
262 | + } |
|
263 | + } |
|
264 | 264 | |
265 | - /** |
|
266 | - * Get the route response object type. |
|
267 | - * |
|
268 | - * @param string $modelName |
|
269 | - * @param string $method |
|
270 | - * @param string $returnDocBlock |
|
271 | - * @return array |
|
272 | - */ |
|
273 | - protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
274 | - { |
|
275 | - $config = \CoreConfig::getConfig(); |
|
276 | - $relations = array_key_exists($modelName, $config['relations']) ? array_key_exists($method, $config['relations'][$modelName]) ? $config['relations'][$modelName] : false : false; |
|
277 | - $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
265 | + /** |
|
266 | + * Get the route response object type. |
|
267 | + * |
|
268 | + * @param string $modelName |
|
269 | + * @param string $method |
|
270 | + * @param string $returnDocBlock |
|
271 | + * @return array |
|
272 | + */ |
|
273 | + protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
274 | + { |
|
275 | + $config = \CoreConfig::getConfig(); |
|
276 | + $relations = array_key_exists($modelName, $config['relations']) ? array_key_exists($method, $config['relations'][$modelName]) ? $config['relations'][$modelName] : false : false; |
|
277 | + $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
278 | 278 | |
279 | - return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
280 | - } |
|
279 | + return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
280 | + } |
|
281 | 281 | } |
@@ -6,283 +6,283 @@ |
||
6 | 6 | |
7 | 7 | class BaseApiController extends Controller |
8 | 8 | { |
9 | - /** |
|
10 | - * The config implementation. |
|
11 | - * |
|
12 | - * @var array |
|
13 | - */ |
|
14 | - protected $config; |
|
9 | + /** |
|
10 | + * The config implementation. |
|
11 | + * |
|
12 | + * @var array |
|
13 | + */ |
|
14 | + protected $config; |
|
15 | 15 | |
16 | - /** |
|
17 | - * The relations implementation. |
|
18 | - * |
|
19 | - * @var array |
|
20 | - */ |
|
21 | - protected $relations; |
|
16 | + /** |
|
17 | + * The relations implementation. |
|
18 | + * |
|
19 | + * @var array |
|
20 | + */ |
|
21 | + protected $relations; |
|
22 | 22 | |
23 | - /** |
|
24 | - * The repo implementation. |
|
25 | - * |
|
26 | - * @var object |
|
27 | - */ |
|
28 | - protected $repo; |
|
23 | + /** |
|
24 | + * The repo implementation. |
|
25 | + * |
|
26 | + * @var object |
|
27 | + */ |
|
28 | + protected $repo; |
|
29 | 29 | |
30 | - public function __construct() |
|
31 | - { |
|
32 | - $this->config = \CoreConfig::getConfig(); |
|
33 | - $this->model = property_exists($this, 'model') ? $this->model : false; |
|
34 | - $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
35 | - $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
36 | - $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
37 | - $route = explode('@',\Route::currentRouteAction())[1]; |
|
30 | + public function __construct() |
|
31 | + { |
|
32 | + $this->config = \CoreConfig::getConfig(); |
|
33 | + $this->model = property_exists($this, 'model') ? $this->model : false; |
|
34 | + $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
35 | + $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
36 | + $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
37 | + $route = explode('@',\Route::currentRouteAction())[1]; |
|
38 | 38 | |
39 | - $this->middleware(function ($request, $next) { |
|
39 | + $this->middleware(function ($request, $next) { |
|
40 | 40 | |
41 | - $this->repo = call_user_func_array("\Core::{$this->model}", []); |
|
42 | - return $next($request); |
|
43 | - }); |
|
41 | + $this->repo = call_user_func_array("\Core::{$this->model}", []); |
|
42 | + return $next($request); |
|
43 | + }); |
|
44 | 44 | |
45 | - $this->checkPermission($route); |
|
46 | - $this->setRelations($route); |
|
47 | - $this->setSessions(); |
|
48 | - } |
|
45 | + $this->checkPermission($route); |
|
46 | + $this->setRelations($route); |
|
47 | + $this->setSessions(); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Fetch all records with relations from storage. |
|
52 | - * |
|
53 | - * @param string $sortBy The name of the column to sort by. |
|
54 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
55 | - * @return \Illuminate\Http\Response |
|
56 | - */ |
|
57 | - public function index($sortBy = 'created_at', $desc = 1) |
|
58 | - { |
|
59 | - return \Response::json($this->repo->all($this->relations, $sortBy, $desc), 200); |
|
60 | - } |
|
50 | + /** |
|
51 | + * Fetch all records with relations from storage. |
|
52 | + * |
|
53 | + * @param string $sortBy The name of the column to sort by. |
|
54 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
55 | + * @return \Illuminate\Http\Response |
|
56 | + */ |
|
57 | + public function index($sortBy = 'created_at', $desc = 1) |
|
58 | + { |
|
59 | + return \Response::json($this->repo->all($this->relations, $sortBy, $desc), 200); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Fetch the single object with relations from storage. |
|
64 | - * |
|
65 | - * @param integer $id Id of the requested model. |
|
66 | - * @return \Illuminate\Http\Response |
|
67 | - */ |
|
68 | - public function find($id) |
|
69 | - { |
|
70 | - return \Response::json($this->repo->find($id, $this->relations), 200); |
|
71 | - } |
|
62 | + /** |
|
63 | + * Fetch the single object with relations from storage. |
|
64 | + * |
|
65 | + * @param integer $id Id of the requested model. |
|
66 | + * @return \Illuminate\Http\Response |
|
67 | + */ |
|
68 | + public function find($id) |
|
69 | + { |
|
70 | + return \Response::json($this->repo->find($id, $this->relations), 200); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Paginate all records with relations from storage |
|
75 | - * that matche the given query. |
|
76 | - * |
|
77 | - * @param string $query The search text. |
|
78 | - * @param integer $perPage Number of rows per page default 15. |
|
79 | - * @param string $sortBy The name of the column to sort by. |
|
80 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
81 | - * @return \Illuminate\Http\Response |
|
82 | - */ |
|
83 | - public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
84 | - { |
|
85 | - return \Response::json($this->repo->search($query, $perPage, $this->relations, $sortBy, $desc), 200); |
|
86 | - } |
|
73 | + /** |
|
74 | + * Paginate all records with relations from storage |
|
75 | + * that matche the given query. |
|
76 | + * |
|
77 | + * @param string $query The search text. |
|
78 | + * @param integer $perPage Number of rows per page default 15. |
|
79 | + * @param string $sortBy The name of the column to sort by. |
|
80 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
81 | + * @return \Illuminate\Http\Response |
|
82 | + */ |
|
83 | + public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
84 | + { |
|
85 | + return \Response::json($this->repo->search($query, $perPage, $this->relations, $sortBy, $desc), 200); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Fetch records from the storage based on the given |
|
90 | - * condition. |
|
91 | - * |
|
92 | - * @param \Illuminate\Http\Request $request |
|
93 | - * @param string $sortBy The name of the column to sort by. |
|
94 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
95 | - * @return \Illuminate\Http\Response |
|
96 | - */ |
|
97 | - public function findby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
98 | - { |
|
99 | - return \Response::json($this->repo->findBy($request->all(), $this->relations, $sortBy, $desc), 200); |
|
100 | - } |
|
88 | + /** |
|
89 | + * Fetch records from the storage based on the given |
|
90 | + * condition. |
|
91 | + * |
|
92 | + * @param \Illuminate\Http\Request $request |
|
93 | + * @param string $sortBy The name of the column to sort by. |
|
94 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
95 | + * @return \Illuminate\Http\Response |
|
96 | + */ |
|
97 | + public function findby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
98 | + { |
|
99 | + return \Response::json($this->repo->findBy($request->all(), $this->relations, $sortBy, $desc), 200); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Fetch the first record from the storage based on the given |
|
104 | - * condition. |
|
105 | - * |
|
106 | - * @param \Illuminate\Http\Request $request |
|
107 | - * @return \Illuminate\Http\Response |
|
108 | - */ |
|
109 | - public function first(Request $request) |
|
110 | - { |
|
111 | - return \Response::json($this->repo->first($request->all(), $this->relations), 200); |
|
112 | - } |
|
102 | + /** |
|
103 | + * Fetch the first record from the storage based on the given |
|
104 | + * condition. |
|
105 | + * |
|
106 | + * @param \Illuminate\Http\Request $request |
|
107 | + * @return \Illuminate\Http\Response |
|
108 | + */ |
|
109 | + public function first(Request $request) |
|
110 | + { |
|
111 | + return \Response::json($this->repo->first($request->all(), $this->relations), 200); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Paginate all records with relations from storage. |
|
116 | - * |
|
117 | - * @param integer $perPage Number of rows per page default 15. |
|
118 | - * @param string $sortBy The name of the column to sort by. |
|
119 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
120 | - * @return \Illuminate\Http\Response |
|
121 | - */ |
|
122 | - public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
123 | - { |
|
124 | - return \Response::json($this->repo->paginate($perPage, $this->relations, $sortBy, $desc), 200); |
|
125 | - } |
|
114 | + /** |
|
115 | + * Paginate all records with relations from storage. |
|
116 | + * |
|
117 | + * @param integer $perPage Number of rows per page default 15. |
|
118 | + * @param string $sortBy The name of the column to sort by. |
|
119 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
120 | + * @return \Illuminate\Http\Response |
|
121 | + */ |
|
122 | + public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
123 | + { |
|
124 | + return \Response::json($this->repo->paginate($perPage, $this->relations, $sortBy, $desc), 200); |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * Fetch all records with relations based on |
|
129 | - * the given condition from storage in pages. |
|
130 | - * |
|
131 | - * @param \Illuminate\Http\Request $request |
|
132 | - * @param integer $perPage Number of rows per page default 15. |
|
133 | - * @param string $sortBy The name of the column to sort by. |
|
134 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
135 | - * @return \Illuminate\Http\Response |
|
136 | - */ |
|
137 | - public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
138 | - { |
|
139 | - return \Response::json($this->repo->paginateBy($request->all(), $perPage, $this->relations, $sortBy, $desc), 200); |
|
140 | - } |
|
127 | + /** |
|
128 | + * Fetch all records with relations based on |
|
129 | + * the given condition from storage in pages. |
|
130 | + * |
|
131 | + * @param \Illuminate\Http\Request $request |
|
132 | + * @param integer $perPage Number of rows per page default 15. |
|
133 | + * @param string $sortBy The name of the column to sort by. |
|
134 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
135 | + * @return \Illuminate\Http\Response |
|
136 | + */ |
|
137 | + public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
138 | + { |
|
139 | + return \Response::json($this->repo->paginateBy($request->all(), $perPage, $this->relations, $sortBy, $desc), 200); |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * Save the given model to storage. |
|
144 | - * |
|
145 | - * @param \Illuminate\Http\Request $request |
|
146 | - * @return \Illuminate\Http\Response |
|
147 | - */ |
|
148 | - public function save(Request $request) |
|
149 | - { |
|
150 | - foreach ($this->validationRules as &$rule) |
|
151 | - { |
|
152 | - if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
153 | - { |
|
154 | - $rule .= ',deleted_at,NULL'; |
|
155 | - } |
|
142 | + /** |
|
143 | + * Save the given model to storage. |
|
144 | + * |
|
145 | + * @param \Illuminate\Http\Request $request |
|
146 | + * @return \Illuminate\Http\Response |
|
147 | + */ |
|
148 | + public function save(Request $request) |
|
149 | + { |
|
150 | + foreach ($this->validationRules as &$rule) |
|
151 | + { |
|
152 | + if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
153 | + { |
|
154 | + $rule .= ',deleted_at,NULL'; |
|
155 | + } |
|
156 | 156 | |
157 | - if ($request->has('id')) |
|
158 | - { |
|
159 | - $rule = str_replace('{id}', $request->get('id'), $rule); |
|
160 | - } |
|
161 | - else |
|
162 | - { |
|
163 | - $rule = str_replace(',{id}', '', $rule); |
|
164 | - } |
|
165 | - } |
|
157 | + if ($request->has('id')) |
|
158 | + { |
|
159 | + $rule = str_replace('{id}', $request->get('id'), $rule); |
|
160 | + } |
|
161 | + else |
|
162 | + { |
|
163 | + $rule = str_replace(',{id}', '', $rule); |
|
164 | + } |
|
165 | + } |
|
166 | 166 | |
167 | - $this->validate($request, $this->validationRules); |
|
167 | + $this->validate($request, $this->validationRules); |
|
168 | 168 | |
169 | - return \Response::json($this->repo->save($request->all()), 200); |
|
170 | - } |
|
169 | + return \Response::json($this->repo->save($request->all()), 200); |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * Delete by the given id from storage. |
|
174 | - * |
|
175 | - * @param integer $id Id of the deleted model. |
|
176 | - * @return \Illuminate\Http\Response |
|
177 | - */ |
|
178 | - public function delete($id) |
|
179 | - { |
|
180 | - return \Response::json($this->repo->delete($id), 200); |
|
181 | - } |
|
172 | + /** |
|
173 | + * Delete by the given id from storage. |
|
174 | + * |
|
175 | + * @param integer $id Id of the deleted model. |
|
176 | + * @return \Illuminate\Http\Response |
|
177 | + */ |
|
178 | + public function delete($id) |
|
179 | + { |
|
180 | + return \Response::json($this->repo->delete($id), 200); |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * Return the deleted models in pages based on the given conditions. |
|
185 | - * |
|
186 | - * @param \Illuminate\Http\Request $request |
|
187 | - * @param integer $perPage Number of rows per page default 15. |
|
188 | - * @param string $sortBy The name of the column to sort by. |
|
189 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
190 | - * @return \Illuminate\Http\Response |
|
191 | - */ |
|
192 | - public function deleted(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
193 | - { |
|
194 | - return \Response::json($this->repo->deleted($request->all(), $perPage, $sortBy, $desc), 200); |
|
195 | - } |
|
183 | + /** |
|
184 | + * Return the deleted models in pages based on the given conditions. |
|
185 | + * |
|
186 | + * @param \Illuminate\Http\Request $request |
|
187 | + * @param integer $perPage Number of rows per page default 15. |
|
188 | + * @param string $sortBy The name of the column to sort by. |
|
189 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
190 | + * @return \Illuminate\Http\Response |
|
191 | + */ |
|
192 | + public function deleted(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
193 | + { |
|
194 | + return \Response::json($this->repo->deleted($request->all(), $perPage, $sortBy, $desc), 200); |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Restore the deleted model. |
|
199 | - * |
|
200 | - * @param integer $id Id of the restored model. |
|
201 | - * @return \Illuminate\Http\Response |
|
202 | - */ |
|
203 | - public function restore($id) |
|
204 | - { |
|
205 | - return \Response::json($this->repo->restore($id), 200); |
|
206 | - } |
|
197 | + /** |
|
198 | + * Restore the deleted model. |
|
199 | + * |
|
200 | + * @param integer $id Id of the restored model. |
|
201 | + * @return \Illuminate\Http\Response |
|
202 | + */ |
|
203 | + public function restore($id) |
|
204 | + { |
|
205 | + return \Response::json($this->repo->restore($id), 200); |
|
206 | + } |
|
207 | 207 | |
208 | - /** |
|
209 | - * Check if the logged in user can do the given permission. |
|
210 | - * |
|
211 | - * @param string $permission |
|
212 | - * @return void |
|
213 | - */ |
|
214 | - private function checkPermission($permission) |
|
215 | - { |
|
216 | - \Auth::shouldUse('api'); |
|
217 | - $this->middleware('auth:api', ['except' => $this->skipLoginCheck]); |
|
208 | + /** |
|
209 | + * Check if the logged in user can do the given permission. |
|
210 | + * |
|
211 | + * @param string $permission |
|
212 | + * @return void |
|
213 | + */ |
|
214 | + private function checkPermission($permission) |
|
215 | + { |
|
216 | + \Auth::shouldUse('api'); |
|
217 | + $this->middleware('auth:api', ['except' => $this->skipLoginCheck]); |
|
218 | 218 | |
219 | - if ( ! in_array($permission, $this->skipLoginCheck) && $user = \Auth::user()) |
|
220 | - { |
|
221 | - $user = \Auth::user(); |
|
222 | - $permission = $permission !== 'index' ? $permission : 'list'; |
|
223 | - $isPasswordClient = $user->token()->client->password_client; |
|
219 | + if ( ! in_array($permission, $this->skipLoginCheck) && $user = \Auth::user()) |
|
220 | + { |
|
221 | + $user = \Auth::user(); |
|
222 | + $permission = $permission !== 'index' ? $permission : 'list'; |
|
223 | + $isPasswordClient = $user->token()->client->password_client; |
|
224 | 224 | |
225 | - if ($user->blocked) |
|
226 | - { |
|
227 | - \ErrorHandler::userIsBlocked(); |
|
228 | - } |
|
225 | + if ($user->blocked) |
|
226 | + { |
|
227 | + \ErrorHandler::userIsBlocked(); |
|
228 | + } |
|
229 | 229 | |
230 | - if ($isPasswordClient && (in_array($permission, $this->skipPermissionCheck) || \Core::users()->can($permission, $this->model))) |
|
231 | - {} |
|
232 | - elseif ( ! $isPasswordClient && $user->tokenCan($this->model . '-' . $permission)) |
|
233 | - {} |
|
234 | - else |
|
235 | - { |
|
230 | + if ($isPasswordClient && (in_array($permission, $this->skipPermissionCheck) || \Core::users()->can($permission, $this->model))) |
|
231 | + {} |
|
232 | + elseif ( ! $isPasswordClient && $user->tokenCan($this->model . '-' . $permission)) |
|
233 | + {} |
|
234 | + else |
|
235 | + { |
|
236 | 236 | |
237 | - \ErrorHandler::noPermissions(); |
|
238 | - } |
|
239 | - } |
|
240 | - } |
|
237 | + \ErrorHandler::noPermissions(); |
|
238 | + } |
|
239 | + } |
|
240 | + } |
|
241 | 241 | |
242 | - /** |
|
243 | - * Set sessions based on the given headers in the request. |
|
244 | - * |
|
245 | - * @return void |
|
246 | - */ |
|
247 | - private function setSessions() |
|
248 | - { |
|
249 | - \Session::put('time-zone', \Request::header('time-zone') ?: 0); |
|
242 | + /** |
|
243 | + * Set sessions based on the given headers in the request. |
|
244 | + * |
|
245 | + * @return void |
|
246 | + */ |
|
247 | + private function setSessions() |
|
248 | + { |
|
249 | + \Session::put('time-zone', \Request::header('time-zone') ?: 0); |
|
250 | 250 | |
251 | - $locale = \Request::header('locale'); |
|
252 | - switch ($locale) |
|
253 | - { |
|
254 | - case 'en': |
|
255 | - \App::setLocale('en'); |
|
256 | - \Session::put('locale', 'en'); |
|
257 | - break; |
|
251 | + $locale = \Request::header('locale'); |
|
252 | + switch ($locale) |
|
253 | + { |
|
254 | + case 'en': |
|
255 | + \App::setLocale('en'); |
|
256 | + \Session::put('locale', 'en'); |
|
257 | + break; |
|
258 | 258 | |
259 | - case 'ar': |
|
260 | - \App::setLocale('ar'); |
|
261 | - \Session::put('locale', 'ar'); |
|
262 | - break; |
|
259 | + case 'ar': |
|
260 | + \App::setLocale('ar'); |
|
261 | + \Session::put('locale', 'ar'); |
|
262 | + break; |
|
263 | 263 | |
264 | - case 'all': |
|
265 | - \App::setLocale('en'); |
|
266 | - \Session::put('locale', 'all'); |
|
267 | - break; |
|
264 | + case 'all': |
|
265 | + \App::setLocale('en'); |
|
266 | + \Session::put('locale', 'all'); |
|
267 | + break; |
|
268 | 268 | |
269 | - default: |
|
270 | - \App::setLocale('en'); |
|
271 | - \Session::put('locale', 'en'); |
|
272 | - break; |
|
273 | - } |
|
274 | - } |
|
269 | + default: |
|
270 | + \App::setLocale('en'); |
|
271 | + \Session::put('locale', 'en'); |
|
272 | + break; |
|
273 | + } |
|
274 | + } |
|
275 | 275 | |
276 | - /** |
|
277 | - * Set relation based on the called api. |
|
278 | - * |
|
279 | - * @param string $route |
|
280 | - * @return void |
|
281 | - */ |
|
282 | - private function setRelations($route) |
|
283 | - { |
|
284 | - $route = $route !== 'index' ? $route : 'list'; |
|
285 | - $relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; |
|
286 | - $this->relations = $relations && isset($relations[$route]) ? $relations[$route] : []; |
|
287 | - } |
|
276 | + /** |
|
277 | + * Set relation based on the called api. |
|
278 | + * |
|
279 | + * @param string $route |
|
280 | + * @return void |
|
281 | + */ |
|
282 | + private function setRelations($route) |
|
283 | + { |
|
284 | + $route = $route !== 'index' ? $route : 'list'; |
|
285 | + $relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; |
|
286 | + $this->relations = $relations && isset($relations[$route]) ? $relations[$route] : []; |
|
287 | + } |
|
288 | 288 | } |
@@ -5,412 +5,412 @@ |
||
5 | 5 | |
6 | 6 | class UserRepository extends AbstractRepository |
7 | 7 | { |
8 | - /** |
|
9 | - * Return the model full namespace. |
|
10 | - * |
|
11 | - * @return string |
|
12 | - */ |
|
13 | - protected function getModel() |
|
14 | - { |
|
15 | - return 'App\Modules\Acl\AclUser'; |
|
16 | - } |
|
17 | - |
|
18 | - |
|
19 | - /** |
|
20 | - * Return the logged in user account. |
|
21 | - * |
|
22 | - * @param array $relations |
|
23 | - * @return boolean |
|
24 | - */ |
|
25 | - public function account($relations = []) |
|
26 | - { |
|
27 | - $permissions = []; |
|
28 | - $user = \Core::users()->find(\Auth::id(), $relations); |
|
29 | - foreach ($user->groups()->get() as $group) |
|
30 | - { |
|
31 | - $group->permissions->each(function ($permission) use (&$permissions){ |
|
32 | - $permissions[$permission->model][$permission->id] = $permission->name; |
|
33 | - }); |
|
34 | - } |
|
35 | - $user->permissions = $permissions; |
|
36 | - |
|
37 | - return $user; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * Check if the logged in user or the given user |
|
42 | - * has the given permissions on the given model. |
|
43 | - * |
|
44 | - * @param string $nameOfPermission |
|
45 | - * @param string $model |
|
46 | - * @param boolean $user |
|
47 | - * @return boolean |
|
48 | - */ |
|
49 | - public function can($nameOfPermission, $model, $user = false) |
|
50 | - { |
|
51 | - $user = $user ?: $this->find(\Auth::id(), ['groups.permissions']); |
|
52 | - $permissions = []; |
|
53 | - |
|
54 | - $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
55 | - $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
|
56 | - }); |
|
8 | + /** |
|
9 | + * Return the model full namespace. |
|
10 | + * |
|
11 | + * @return string |
|
12 | + */ |
|
13 | + protected function getModel() |
|
14 | + { |
|
15 | + return 'App\Modules\Acl\AclUser'; |
|
16 | + } |
|
17 | + |
|
18 | + |
|
19 | + /** |
|
20 | + * Return the logged in user account. |
|
21 | + * |
|
22 | + * @param array $relations |
|
23 | + * @return boolean |
|
24 | + */ |
|
25 | + public function account($relations = []) |
|
26 | + { |
|
27 | + $permissions = []; |
|
28 | + $user = \Core::users()->find(\Auth::id(), $relations); |
|
29 | + foreach ($user->groups()->get() as $group) |
|
30 | + { |
|
31 | + $group->permissions->each(function ($permission) use (&$permissions){ |
|
32 | + $permissions[$permission->model][$permission->id] = $permission->name; |
|
33 | + }); |
|
34 | + } |
|
35 | + $user->permissions = $permissions; |
|
36 | + |
|
37 | + return $user; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * Check if the logged in user or the given user |
|
42 | + * has the given permissions on the given model. |
|
43 | + * |
|
44 | + * @param string $nameOfPermission |
|
45 | + * @param string $model |
|
46 | + * @param boolean $user |
|
47 | + * @return boolean |
|
48 | + */ |
|
49 | + public function can($nameOfPermission, $model, $user = false) |
|
50 | + { |
|
51 | + $user = $user ?: $this->find(\Auth::id(), ['groups.permissions']); |
|
52 | + $permissions = []; |
|
53 | + |
|
54 | + $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
55 | + $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
|
56 | + }); |
|
57 | 57 | |
58 | - return in_array($nameOfPermission, $permissions); |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Check if the logged in user has the given group. |
|
63 | - * |
|
64 | - * @param string $groupName |
|
65 | - * @param integer $userId |
|
66 | - * @return boolean |
|
67 | - */ |
|
68 | - public function hasGroup($groups, $user = false) |
|
69 | - { |
|
70 | - $user = $user ?: $this->find(\Auth::id()); |
|
71 | - return $user->groups->whereIn('name', $groups)->count() ? true : false; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Assign the given group ids to the given user. |
|
76 | - * |
|
77 | - * @param integer $user_id |
|
78 | - * @param array $group_ids |
|
79 | - * @return object |
|
80 | - */ |
|
81 | - public function assignGroups($user_id, $group_ids) |
|
82 | - { |
|
83 | - \DB::transaction(function () use ($user_id, $group_ids) { |
|
84 | - $user = $this->find($user_id); |
|
85 | - $user->groups()->detach(); |
|
86 | - $user->groups()->attach($group_ids); |
|
87 | - }); |
|
88 | - |
|
89 | - return $this->find($user_id); |
|
90 | - } |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Handle a login request to the application. |
|
95 | - * |
|
96 | - * @param array $credentials |
|
97 | - * @param boolean $adminLogin |
|
98 | - * @return object |
|
99 | - */ |
|
100 | - public function login($credentials, $adminLogin = false) |
|
101 | - { |
|
102 | - if ( ! $user = $this->first(['email' => $credentials['email']])) |
|
103 | - { |
|
104 | - \ErrorHandler::loginFailed(); |
|
105 | - } |
|
106 | - else if ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) |
|
107 | - { |
|
108 | - \ErrorHandler::loginFailed(); |
|
109 | - } |
|
110 | - else if ( ! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) |
|
111 | - { |
|
112 | - \ErrorHandler::loginFailed(); |
|
113 | - } |
|
114 | - else if ($user->blocked) |
|
115 | - { |
|
116 | - \ErrorHandler::userIsBlocked(); |
|
117 | - } |
|
118 | - else if ( ! config('skeleton.disable_confirm_email') && ! $user->confirmed) |
|
119 | - { |
|
120 | - \ErrorHandler::emailNotConfirmed(); |
|
121 | - } |
|
122 | - |
|
123 | - return $user; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Handle a social login request of the none admin to the application. |
|
128 | - * |
|
129 | - * @param array $credentials |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - public function loginSocial($credentials) |
|
133 | - { |
|
134 | - $access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token']; |
|
135 | - $user = \Socialite::driver($credentials['type'])->userFromToken($access_token); |
|
136 | - |
|
137 | - if ( ! $user->email) |
|
138 | - { |
|
139 | - \ErrorHandler::noSocialEmail(); |
|
140 | - } |
|
141 | - |
|
142 | - if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) |
|
143 | - { |
|
144 | - $data = ['email' => $user->email, 'password' => '']; |
|
145 | - return $this->register($data); |
|
146 | - } |
|
147 | - else |
|
148 | - { |
|
149 | - if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => ''])) |
|
150 | - { |
|
151 | - \ErrorHandler::userAlreadyRegistered(); |
|
152 | - } |
|
153 | - |
|
154 | - $loginProxy = \App::make('App\Modules\Acl\Proxy\LoginProxy'); |
|
155 | - return $loginProxy->login(['email' => $registeredUser->email, 'password' => ''], 0); |
|
156 | - } |
|
157 | - } |
|
58 | + return in_array($nameOfPermission, $permissions); |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Check if the logged in user has the given group. |
|
63 | + * |
|
64 | + * @param string $groupName |
|
65 | + * @param integer $userId |
|
66 | + * @return boolean |
|
67 | + */ |
|
68 | + public function hasGroup($groups, $user = false) |
|
69 | + { |
|
70 | + $user = $user ?: $this->find(\Auth::id()); |
|
71 | + return $user->groups->whereIn('name', $groups)->count() ? true : false; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Assign the given group ids to the given user. |
|
76 | + * |
|
77 | + * @param integer $user_id |
|
78 | + * @param array $group_ids |
|
79 | + * @return object |
|
80 | + */ |
|
81 | + public function assignGroups($user_id, $group_ids) |
|
82 | + { |
|
83 | + \DB::transaction(function () use ($user_id, $group_ids) { |
|
84 | + $user = $this->find($user_id); |
|
85 | + $user->groups()->detach(); |
|
86 | + $user->groups()->attach($group_ids); |
|
87 | + }); |
|
88 | + |
|
89 | + return $this->find($user_id); |
|
90 | + } |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Handle a login request to the application. |
|
95 | + * |
|
96 | + * @param array $credentials |
|
97 | + * @param boolean $adminLogin |
|
98 | + * @return object |
|
99 | + */ |
|
100 | + public function login($credentials, $adminLogin = false) |
|
101 | + { |
|
102 | + if ( ! $user = $this->first(['email' => $credentials['email']])) |
|
103 | + { |
|
104 | + \ErrorHandler::loginFailed(); |
|
105 | + } |
|
106 | + else if ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) |
|
107 | + { |
|
108 | + \ErrorHandler::loginFailed(); |
|
109 | + } |
|
110 | + else if ( ! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) |
|
111 | + { |
|
112 | + \ErrorHandler::loginFailed(); |
|
113 | + } |
|
114 | + else if ($user->blocked) |
|
115 | + { |
|
116 | + \ErrorHandler::userIsBlocked(); |
|
117 | + } |
|
118 | + else if ( ! config('skeleton.disable_confirm_email') && ! $user->confirmed) |
|
119 | + { |
|
120 | + \ErrorHandler::emailNotConfirmed(); |
|
121 | + } |
|
122 | + |
|
123 | + return $user; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Handle a social login request of the none admin to the application. |
|
128 | + * |
|
129 | + * @param array $credentials |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + public function loginSocial($credentials) |
|
133 | + { |
|
134 | + $access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token']; |
|
135 | + $user = \Socialite::driver($credentials['type'])->userFromToken($access_token); |
|
136 | + |
|
137 | + if ( ! $user->email) |
|
138 | + { |
|
139 | + \ErrorHandler::noSocialEmail(); |
|
140 | + } |
|
141 | + |
|
142 | + if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) |
|
143 | + { |
|
144 | + $data = ['email' => $user->email, 'password' => '']; |
|
145 | + return $this->register($data); |
|
146 | + } |
|
147 | + else |
|
148 | + { |
|
149 | + if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => ''])) |
|
150 | + { |
|
151 | + \ErrorHandler::userAlreadyRegistered(); |
|
152 | + } |
|
153 | + |
|
154 | + $loginProxy = \App::make('App\Modules\Acl\Proxy\LoginProxy'); |
|
155 | + return $loginProxy->login(['email' => $registeredUser->email, 'password' => ''], 0); |
|
156 | + } |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * Handle a registration request. |
|
161 | - * |
|
162 | - * @param array $credentials |
|
163 | - * @return array |
|
164 | - */ |
|
165 | - public function register($credentials) |
|
166 | - { |
|
167 | - $user = $this->save($credentials); |
|
168 | - |
|
169 | - if ( ! config('skeleton.disable_confirm_email')) |
|
170 | - { |
|
171 | - $this->sendConfirmationEmail($user->email); |
|
172 | - } |
|
173 | - } |
|
159 | + /** |
|
160 | + * Handle a registration request. |
|
161 | + * |
|
162 | + * @param array $credentials |
|
163 | + * @return array |
|
164 | + */ |
|
165 | + public function register($credentials) |
|
166 | + { |
|
167 | + $user = $this->save($credentials); |
|
168 | + |
|
169 | + if ( ! config('skeleton.disable_confirm_email')) |
|
170 | + { |
|
171 | + $this->sendConfirmationEmail($user->email); |
|
172 | + } |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * Block the user. |
|
177 | - * |
|
178 | - * @param integer $user_id |
|
179 | - * @return object |
|
180 | - */ |
|
181 | - public function block($user_id) |
|
182 | - { |
|
183 | - if ( ! $user = $this->find($user_id)) |
|
184 | - { |
|
185 | - \ErrorHandler::notFound('user'); |
|
186 | - } |
|
187 | - if ( ! $this->hasGroup(['Admin'])) |
|
188 | - { |
|
189 | - \ErrorHandler::noPermissions(); |
|
190 | - } |
|
191 | - else if (\Auth::id() == $user_id) |
|
192 | - { |
|
193 | - \ErrorHandler::noPermissions(); |
|
194 | - } |
|
195 | - else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
196 | - { |
|
197 | - \ErrorHandler::noPermissions(); |
|
198 | - } |
|
199 | - |
|
200 | - $user->blocked = 1; |
|
201 | - $user->save(); |
|
175 | + /** |
|
176 | + * Block the user. |
|
177 | + * |
|
178 | + * @param integer $user_id |
|
179 | + * @return object |
|
180 | + */ |
|
181 | + public function block($user_id) |
|
182 | + { |
|
183 | + if ( ! $user = $this->find($user_id)) |
|
184 | + { |
|
185 | + \ErrorHandler::notFound('user'); |
|
186 | + } |
|
187 | + if ( ! $this->hasGroup(['Admin'])) |
|
188 | + { |
|
189 | + \ErrorHandler::noPermissions(); |
|
190 | + } |
|
191 | + else if (\Auth::id() == $user_id) |
|
192 | + { |
|
193 | + \ErrorHandler::noPermissions(); |
|
194 | + } |
|
195 | + else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
196 | + { |
|
197 | + \ErrorHandler::noPermissions(); |
|
198 | + } |
|
199 | + |
|
200 | + $user->blocked = 1; |
|
201 | + $user->save(); |
|
202 | 202 | |
203 | - return $user; |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Unblock the user. |
|
208 | - * |
|
209 | - * @param integer $user_id |
|
210 | - * @return object |
|
211 | - */ |
|
212 | - public function unblock($user_id) |
|
213 | - { |
|
214 | - if ( ! $this->hasGroup(['Admin'])) |
|
215 | - { |
|
216 | - \ErrorHandler::noPermissions(); |
|
217 | - } |
|
218 | - |
|
219 | - $user = $this->find($user_id); |
|
220 | - $user->blocked = 0; |
|
221 | - $user->save(); |
|
222 | - |
|
223 | - return $user; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Send a reset link to the given user. |
|
228 | - * |
|
229 | - * @param string $email |
|
230 | - * @return void |
|
231 | - */ |
|
232 | - public function sendReset($email) |
|
233 | - { |
|
234 | - if ( ! $user = $this->model->where('email', $email)->first()) |
|
235 | - { |
|
236 | - \ErrorHandler::notFound('email'); |
|
237 | - } |
|
238 | - |
|
239 | - $token = \Password::getRepository()->create($user); |
|
240 | - \Core::notifications()->notify($user, 'ResetPassword', $token); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Reset the given user's password. |
|
245 | - * |
|
246 | - * @param array $credentials |
|
247 | - * @return array |
|
248 | - */ |
|
249 | - public function resetPassword($credentials) |
|
250 | - { |
|
251 | - $response = \Password::reset($credentials, function ($user, $password) { |
|
252 | - $user->password = $password; |
|
253 | - $user->save(); |
|
254 | - }); |
|
255 | - |
|
256 | - switch ($response) { |
|
257 | - case \Password::PASSWORD_RESET: |
|
258 | - return 'success'; |
|
203 | + return $user; |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Unblock the user. |
|
208 | + * |
|
209 | + * @param integer $user_id |
|
210 | + * @return object |
|
211 | + */ |
|
212 | + public function unblock($user_id) |
|
213 | + { |
|
214 | + if ( ! $this->hasGroup(['Admin'])) |
|
215 | + { |
|
216 | + \ErrorHandler::noPermissions(); |
|
217 | + } |
|
218 | + |
|
219 | + $user = $this->find($user_id); |
|
220 | + $user->blocked = 0; |
|
221 | + $user->save(); |
|
222 | + |
|
223 | + return $user; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Send a reset link to the given user. |
|
228 | + * |
|
229 | + * @param string $email |
|
230 | + * @return void |
|
231 | + */ |
|
232 | + public function sendReset($email) |
|
233 | + { |
|
234 | + if ( ! $user = $this->model->where('email', $email)->first()) |
|
235 | + { |
|
236 | + \ErrorHandler::notFound('email'); |
|
237 | + } |
|
238 | + |
|
239 | + $token = \Password::getRepository()->create($user); |
|
240 | + \Core::notifications()->notify($user, 'ResetPassword', $token); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Reset the given user's password. |
|
245 | + * |
|
246 | + * @param array $credentials |
|
247 | + * @return array |
|
248 | + */ |
|
249 | + public function resetPassword($credentials) |
|
250 | + { |
|
251 | + $response = \Password::reset($credentials, function ($user, $password) { |
|
252 | + $user->password = $password; |
|
253 | + $user->save(); |
|
254 | + }); |
|
255 | + |
|
256 | + switch ($response) { |
|
257 | + case \Password::PASSWORD_RESET: |
|
258 | + return 'success'; |
|
259 | 259 | |
260 | - case \Password::INVALID_TOKEN: |
|
261 | - \ErrorHandler::invalidResetToken('token'); |
|
262 | - |
|
263 | - case \Password::INVALID_PASSWORD: |
|
264 | - \ErrorHandler::invalidResetPassword('email'); |
|
265 | - |
|
266 | - case \Password::INVALID_USER: |
|
267 | - \ErrorHandler::notFound('user'); |
|
268 | - |
|
269 | - default: |
|
270 | - \ErrorHandler::generalError(); |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * Change the logged in user password. |
|
276 | - * |
|
277 | - * @param array $credentials |
|
278 | - * @return void |
|
279 | - */ |
|
280 | - public function changePassword($credentials) |
|
281 | - { |
|
282 | - $user = \Auth::user(); |
|
283 | - if ( ! \Hash::check($credentials['old_password'], $user->password)) |
|
284 | - { |
|
285 | - \ErrorHandler::invalidOldPassword(); |
|
286 | - } |
|
287 | - |
|
288 | - $user->password = $credentials['password']; |
|
289 | - $user->save(); |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * Confirm email using the confirmation code. |
|
294 | - * |
|
295 | - * @param string $confirmationCode |
|
296 | - * @return void |
|
297 | - */ |
|
298 | - public function confirmEmail($confirmationCode) |
|
299 | - { |
|
300 | - $user = $this->first(['confirmation_code' => $confirmationCode]); |
|
301 | - $user->confirmed = 1; |
|
302 | - $user->confirmation_code = null; |
|
303 | - $user->save(); |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * Send the confirmation mail. |
|
308 | - * |
|
309 | - * @param string $email |
|
310 | - * @return void |
|
311 | - */ |
|
312 | - public function sendConfirmationEmail($email) |
|
313 | - { |
|
314 | - $user = $this->first(['email' => $email]); |
|
315 | - if ($user->confirmed) |
|
316 | - { |
|
317 | - \ErrorHandler::emailAlreadyConfirmed(); |
|
318 | - } |
|
319 | - |
|
320 | - $user->confirmed = 0; |
|
321 | - $user->confirmation_code = sha1(microtime()); |
|
322 | - $user->save(); |
|
323 | - \Core::notifications()->notify($user, 'ConfirmEmail'); |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Paginate all users in the given group based on the given conditions. |
|
328 | - * |
|
329 | - * @param string $groupName |
|
330 | - * @param array $relations |
|
331 | - * @param integer $perPage |
|
332 | - * @param string $sortBy |
|
333 | - * @param boolean $desc |
|
334 | - * @return \Illuminate\Http\Response |
|
335 | - */ |
|
336 | - public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
337 | - { |
|
338 | - unset($conditions['page']); |
|
339 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
340 | - $sort = $desc ? 'desc' : 'asc'; |
|
341 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
342 | - |
|
343 | - $model->whereHas('groups', function($q) use ($groupName){ |
|
344 | - $q->where('name', $groupName); |
|
345 | - }); |
|
260 | + case \Password::INVALID_TOKEN: |
|
261 | + \ErrorHandler::invalidResetToken('token'); |
|
262 | + |
|
263 | + case \Password::INVALID_PASSWORD: |
|
264 | + \ErrorHandler::invalidResetPassword('email'); |
|
265 | + |
|
266 | + case \Password::INVALID_USER: |
|
267 | + \ErrorHandler::notFound('user'); |
|
268 | + |
|
269 | + default: |
|
270 | + \ErrorHandler::generalError(); |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * Change the logged in user password. |
|
276 | + * |
|
277 | + * @param array $credentials |
|
278 | + * @return void |
|
279 | + */ |
|
280 | + public function changePassword($credentials) |
|
281 | + { |
|
282 | + $user = \Auth::user(); |
|
283 | + if ( ! \Hash::check($credentials['old_password'], $user->password)) |
|
284 | + { |
|
285 | + \ErrorHandler::invalidOldPassword(); |
|
286 | + } |
|
287 | + |
|
288 | + $user->password = $credentials['password']; |
|
289 | + $user->save(); |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * Confirm email using the confirmation code. |
|
294 | + * |
|
295 | + * @param string $confirmationCode |
|
296 | + * @return void |
|
297 | + */ |
|
298 | + public function confirmEmail($confirmationCode) |
|
299 | + { |
|
300 | + $user = $this->first(['confirmation_code' => $confirmationCode]); |
|
301 | + $user->confirmed = 1; |
|
302 | + $user->confirmation_code = null; |
|
303 | + $user->save(); |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * Send the confirmation mail. |
|
308 | + * |
|
309 | + * @param string $email |
|
310 | + * @return void |
|
311 | + */ |
|
312 | + public function sendConfirmationEmail($email) |
|
313 | + { |
|
314 | + $user = $this->first(['email' => $email]); |
|
315 | + if ($user->confirmed) |
|
316 | + { |
|
317 | + \ErrorHandler::emailAlreadyConfirmed(); |
|
318 | + } |
|
319 | + |
|
320 | + $user->confirmed = 0; |
|
321 | + $user->confirmation_code = sha1(microtime()); |
|
322 | + $user->save(); |
|
323 | + \Core::notifications()->notify($user, 'ConfirmEmail'); |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Paginate all users in the given group based on the given conditions. |
|
328 | + * |
|
329 | + * @param string $groupName |
|
330 | + * @param array $relations |
|
331 | + * @param integer $perPage |
|
332 | + * @param string $sortBy |
|
333 | + * @param boolean $desc |
|
334 | + * @return \Illuminate\Http\Response |
|
335 | + */ |
|
336 | + public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
337 | + { |
|
338 | + unset($conditions['page']); |
|
339 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
340 | + $sort = $desc ? 'desc' : 'asc'; |
|
341 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
342 | + |
|
343 | + $model->whereHas('groups', function($q) use ($groupName){ |
|
344 | + $q->where('name', $groupName); |
|
345 | + }); |
|
346 | 346 | |
347 | 347 | |
348 | - if (count($conditions['conditionValues'])) |
|
349 | - { |
|
350 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
351 | - } |
|
352 | - |
|
353 | - if ($perPage) |
|
354 | - { |
|
355 | - return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
356 | - } |
|
357 | - |
|
358 | - return $model->orderBy($sortBy, $sort)->get(); |
|
359 | - } |
|
360 | - |
|
361 | - /** |
|
362 | - * Save the given data to the logged in user. |
|
363 | - * |
|
364 | - * @param array $credentials |
|
365 | - * @return void |
|
366 | - */ |
|
367 | - public function saveProfile($data) |
|
368 | - { |
|
369 | - if (array_key_exists('profile_picture', $data)) |
|
370 | - { |
|
371 | - $data['profile_picture'] = \Media::uploadImageBas64($data['profile_picture'], 'admins/profile_pictures'); |
|
372 | - } |
|
348 | + if (count($conditions['conditionValues'])) |
|
349 | + { |
|
350 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
351 | + } |
|
352 | + |
|
353 | + if ($perPage) |
|
354 | + { |
|
355 | + return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
356 | + } |
|
357 | + |
|
358 | + return $model->orderBy($sortBy, $sort)->get(); |
|
359 | + } |
|
360 | + |
|
361 | + /** |
|
362 | + * Save the given data to the logged in user. |
|
363 | + * |
|
364 | + * @param array $credentials |
|
365 | + * @return void |
|
366 | + */ |
|
367 | + public function saveProfile($data) |
|
368 | + { |
|
369 | + if (array_key_exists('profile_picture', $data)) |
|
370 | + { |
|
371 | + $data['profile_picture'] = \Media::uploadImageBas64($data['profile_picture'], 'admins/profile_pictures'); |
|
372 | + } |
|
373 | 373 | |
374 | - $data['id'] = \Auth::id(); |
|
375 | - $this->save($data); |
|
376 | - } |
|
377 | - |
|
378 | - /** |
|
379 | - * Ensure access token hasn't expired or revoked. |
|
380 | - * |
|
381 | - * @param string $accessToken |
|
382 | - * @return boolean |
|
383 | - */ |
|
384 | - public function accessTokenExpiredOrRevoked($accessToken) |
|
385 | - { |
|
386 | - |
|
387 | - $accessTokenRepository = \App::make('League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface'); |
|
388 | - $data = new ValidationData(); |
|
389 | - $data->setCurrentTime(time()); |
|
390 | - |
|
391 | - if ($accessToken->validate($data) === false || $accessTokenRepository->isAccessTokenRevoked($accessToken->getClaim('jti'))) |
|
392 | - { |
|
393 | - return true; |
|
394 | - } |
|
395 | - |
|
396 | - return false; |
|
397 | - } |
|
398 | - |
|
399 | - /** |
|
400 | - * Revoke the given access token and all |
|
401 | - * associated refresh tokens. |
|
402 | - * |
|
403 | - * @param string $accessToken |
|
404 | - * @return void |
|
405 | - */ |
|
406 | - public function revokeAccessToken($accessToken) |
|
407 | - { |
|
408 | - \DB::table('oauth_refresh_tokens') |
|
409 | - ->where('access_token_id', $accessToken->id) |
|
410 | - ->update([ |
|
411 | - 'revoked' => true |
|
412 | - ]); |
|
413 | - |
|
414 | - $accessToken->revoke(); |
|
415 | - } |
|
374 | + $data['id'] = \Auth::id(); |
|
375 | + $this->save($data); |
|
376 | + } |
|
377 | + |
|
378 | + /** |
|
379 | + * Ensure access token hasn't expired or revoked. |
|
380 | + * |
|
381 | + * @param string $accessToken |
|
382 | + * @return boolean |
|
383 | + */ |
|
384 | + public function accessTokenExpiredOrRevoked($accessToken) |
|
385 | + { |
|
386 | + |
|
387 | + $accessTokenRepository = \App::make('League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface'); |
|
388 | + $data = new ValidationData(); |
|
389 | + $data->setCurrentTime(time()); |
|
390 | + |
|
391 | + if ($accessToken->validate($data) === false || $accessTokenRepository->isAccessTokenRevoked($accessToken->getClaim('jti'))) |
|
392 | + { |
|
393 | + return true; |
|
394 | + } |
|
395 | + |
|
396 | + return false; |
|
397 | + } |
|
398 | + |
|
399 | + /** |
|
400 | + * Revoke the given access token and all |
|
401 | + * associated refresh tokens. |
|
402 | + * |
|
403 | + * @param string $accessToken |
|
404 | + * @return void |
|
405 | + */ |
|
406 | + public function revokeAccessToken($accessToken) |
|
407 | + { |
|
408 | + \DB::table('oauth_refresh_tokens') |
|
409 | + ->where('access_token_id', $accessToken->id) |
|
410 | + ->update([ |
|
411 | + 'revoked' => true |
|
412 | + ]); |
|
413 | + |
|
414 | + $accessToken->revoke(); |
|
415 | + } |
|
416 | 416 | } |
@@ -5,63 +5,63 @@ |
||
5 | 5 | */ |
6 | 6 | class AclUserObserver { |
7 | 7 | |
8 | - public function saving($model) |
|
9 | - { |
|
10 | - // |
|
11 | - } |
|
8 | + public function saving($model) |
|
9 | + { |
|
10 | + // |
|
11 | + } |
|
12 | 12 | |
13 | - public function saved($model) |
|
14 | - { |
|
15 | - // |
|
16 | - } |
|
13 | + public function saved($model) |
|
14 | + { |
|
15 | + // |
|
16 | + } |
|
17 | 17 | |
18 | - public function creating($model) |
|
19 | - { |
|
20 | - // |
|
21 | - } |
|
18 | + public function creating($model) |
|
19 | + { |
|
20 | + // |
|
21 | + } |
|
22 | 22 | |
23 | - public function created($model) |
|
24 | - { |
|
25 | - // |
|
26 | - } |
|
23 | + public function created($model) |
|
24 | + { |
|
25 | + // |
|
26 | + } |
|
27 | 27 | |
28 | - public function updating($model) |
|
29 | - { |
|
30 | - // |
|
31 | - } |
|
28 | + public function updating($model) |
|
29 | + { |
|
30 | + // |
|
31 | + } |
|
32 | 32 | |
33 | - public function updated($model) |
|
34 | - { |
|
35 | - if ($model->isDirty('blocked') && $model->blocked) |
|
36 | - { |
|
37 | - $model->tokens()->each(function($token){ |
|
33 | + public function updated($model) |
|
34 | + { |
|
35 | + if ($model->isDirty('blocked') && $model->blocked) |
|
36 | + { |
|
37 | + $model->tokens()->each(function($token){ |
|
38 | 38 | |
39 | - \Core::users()->revokeAccessToken($token); |
|
39 | + \Core::users()->revokeAccessToken($token); |
|
40 | 40 | |
41 | - }); |
|
42 | - } |
|
43 | - } |
|
41 | + }); |
|
42 | + } |
|
43 | + } |
|
44 | 44 | |
45 | - public function deleting($model) |
|
46 | - { |
|
47 | - if ($model->getOriginal('id') == \Auth::id()) |
|
48 | - { |
|
49 | - \ErrorHandler::noPermissions(); |
|
50 | - } |
|
51 | - } |
|
45 | + public function deleting($model) |
|
46 | + { |
|
47 | + if ($model->getOriginal('id') == \Auth::id()) |
|
48 | + { |
|
49 | + \ErrorHandler::noPermissions(); |
|
50 | + } |
|
51 | + } |
|
52 | 52 | |
53 | - public function deleted($model) |
|
54 | - { |
|
55 | - // |
|
56 | - } |
|
53 | + public function deleted($model) |
|
54 | + { |
|
55 | + // |
|
56 | + } |
|
57 | 57 | |
58 | - public function restoring($model) |
|
59 | - { |
|
60 | - // |
|
61 | - } |
|
58 | + public function restoring($model) |
|
59 | + { |
|
60 | + // |
|
61 | + } |
|
62 | 62 | |
63 | - public function restored($model) |
|
64 | - { |
|
65 | - // |
|
66 | - } |
|
63 | + public function restored($model) |
|
64 | + { |
|
65 | + // |
|
66 | + } |
|
67 | 67 | } |
68 | 68 | \ No newline at end of file |
@@ -34,7 +34,7 @@ |
||
34 | 34 | { |
35 | 35 | if ($model->isDirty('blocked') && $model->blocked) |
36 | 36 | { |
37 | - $model->tokens()->each(function($token){ |
|
37 | + $model->tokens()->each(function($token) { |
|
38 | 38 | |
39 | 39 | \Core::users()->revokeAccessToken($token); |
40 | 40 |
@@ -5,95 +5,95 @@ |
||
5 | 5 | |
6 | 6 | class LoginProxy |
7 | 7 | { |
8 | - private $apiConsumer; |
|
9 | - |
|
10 | - private $auth; |
|
11 | - |
|
12 | - private $request; |
|
13 | - |
|
14 | - private $userRepository; |
|
15 | - |
|
16 | - public function __construct(Application $app) |
|
17 | - { |
|
18 | - |
|
19 | - $this->userRepository = $app->make('App\Modules\Acl\Repositories\UserRepository'); |
|
20 | - $this->apiConsumer = $app->make('apiconsumer'); |
|
21 | - $this->auth = $app->make('auth'); |
|
22 | - $this->request = $app->make('request'); |
|
23 | - } |
|
24 | - |
|
25 | - /** |
|
26 | - * Attempt to create an access token using user credentials. |
|
27 | - * |
|
28 | - * @param array $credentials |
|
29 | - * @param boolean $adminLogin |
|
30 | - * @return array |
|
31 | - */ |
|
32 | - public function login($credentials, $adminLogin = false) |
|
33 | - { |
|
34 | - $this->userRepository->login($credentials, $adminLogin); |
|
35 | - |
|
36 | - return $this->proxy('password', [ |
|
37 | - 'username' => $credentials['email'], |
|
38 | - 'password' => $credentials['password'] |
|
39 | - ]); |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Attempt to refresh the access token useing the given refresh token. |
|
44 | - * |
|
45 | - * @param string $refreshToken |
|
46 | - * @return array |
|
47 | - */ |
|
48 | - public function refreshtoken($refreshToken) |
|
49 | - { |
|
50 | - return $this->proxy('refresh_token', [ |
|
51 | - 'refresh_token' => $refreshToken |
|
52 | - ]); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Proxy a request to the OAuth server. |
|
57 | - * |
|
58 | - * @param string $grantType what type of grant type should be proxied |
|
59 | - * @param array |
|
60 | - */ |
|
61 | - public function proxy($grantType, array $data = []) |
|
62 | - { |
|
63 | - $data = array_merge($data, [ |
|
64 | - 'client_id' => config('skeleton.passport_client_id'), |
|
65 | - 'client_secret' => config('skeleton.passport_client_secret'), |
|
66 | - 'grant_type' => $grantType |
|
67 | - ]); |
|
68 | - |
|
69 | - $response = $this->apiConsumer->post('/oauth/token', $data); |
|
70 | - |
|
71 | - if ( ! $response->isSuccessful()) |
|
72 | - { |
|
73 | - if ($grantType == 'refresh_token') |
|
74 | - { |
|
75 | - \ErrorHandler::invalidRefreshToken(); |
|
76 | - } |
|
77 | - |
|
78 | - \ErrorHandler::loginFailed(); |
|
79 | - } |
|
80 | - |
|
81 | - $data = json_decode($response->getContent()); |
|
82 | - |
|
83 | - return [ |
|
84 | - 'access_token' => $data->access_token, |
|
85 | - 'refresh_token' => $data->refresh_token, |
|
86 | - 'expires_in' => $data->expires_in |
|
87 | - ]; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Logs out the user. We revoke access token and refresh token. |
|
92 | - * |
|
93 | - * @return void |
|
94 | - */ |
|
95 | - public function logout() |
|
96 | - { |
|
97 | - \Core::users()->revokeAccessToken($this->auth->user()->token()); |
|
98 | - } |
|
8 | + private $apiConsumer; |
|
9 | + |
|
10 | + private $auth; |
|
11 | + |
|
12 | + private $request; |
|
13 | + |
|
14 | + private $userRepository; |
|
15 | + |
|
16 | + public function __construct(Application $app) |
|
17 | + { |
|
18 | + |
|
19 | + $this->userRepository = $app->make('App\Modules\Acl\Repositories\UserRepository'); |
|
20 | + $this->apiConsumer = $app->make('apiconsumer'); |
|
21 | + $this->auth = $app->make('auth'); |
|
22 | + $this->request = $app->make('request'); |
|
23 | + } |
|
24 | + |
|
25 | + /** |
|
26 | + * Attempt to create an access token using user credentials. |
|
27 | + * |
|
28 | + * @param array $credentials |
|
29 | + * @param boolean $adminLogin |
|
30 | + * @return array |
|
31 | + */ |
|
32 | + public function login($credentials, $adminLogin = false) |
|
33 | + { |
|
34 | + $this->userRepository->login($credentials, $adminLogin); |
|
35 | + |
|
36 | + return $this->proxy('password', [ |
|
37 | + 'username' => $credentials['email'], |
|
38 | + 'password' => $credentials['password'] |
|
39 | + ]); |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Attempt to refresh the access token useing the given refresh token. |
|
44 | + * |
|
45 | + * @param string $refreshToken |
|
46 | + * @return array |
|
47 | + */ |
|
48 | + public function refreshtoken($refreshToken) |
|
49 | + { |
|
50 | + return $this->proxy('refresh_token', [ |
|
51 | + 'refresh_token' => $refreshToken |
|
52 | + ]); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Proxy a request to the OAuth server. |
|
57 | + * |
|
58 | + * @param string $grantType what type of grant type should be proxied |
|
59 | + * @param array |
|
60 | + */ |
|
61 | + public function proxy($grantType, array $data = []) |
|
62 | + { |
|
63 | + $data = array_merge($data, [ |
|
64 | + 'client_id' => config('skeleton.passport_client_id'), |
|
65 | + 'client_secret' => config('skeleton.passport_client_secret'), |
|
66 | + 'grant_type' => $grantType |
|
67 | + ]); |
|
68 | + |
|
69 | + $response = $this->apiConsumer->post('/oauth/token', $data); |
|
70 | + |
|
71 | + if ( ! $response->isSuccessful()) |
|
72 | + { |
|
73 | + if ($grantType == 'refresh_token') |
|
74 | + { |
|
75 | + \ErrorHandler::invalidRefreshToken(); |
|
76 | + } |
|
77 | + |
|
78 | + \ErrorHandler::loginFailed(); |
|
79 | + } |
|
80 | + |
|
81 | + $data = json_decode($response->getContent()); |
|
82 | + |
|
83 | + return [ |
|
84 | + 'access_token' => $data->access_token, |
|
85 | + 'refresh_token' => $data->refresh_token, |
|
86 | + 'expires_in' => $data->expires_in |
|
87 | + ]; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Logs out the user. We revoke access token and refresh token. |
|
92 | + * |
|
93 | + * @return void |
|
94 | + */ |
|
95 | + public function logout() |
|
96 | + { |
|
97 | + \Core::users()->revokeAccessToken($this->auth->user()->token()); |
|
98 | + } |
|
99 | 99 | } |
100 | 100 | \ No newline at end of file |
@@ -8,282 +8,282 @@ |
||
8 | 8 | |
9 | 9 | class UsersController extends BaseApiController |
10 | 10 | { |
11 | - /** |
|
12 | - * The name of the model that is used by the base api controller |
|
13 | - * to preform actions like (add, edit ... etc). |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $model = 'users'; |
|
17 | - |
|
18 | - /** |
|
19 | - * List of all route actions that the base api controller |
|
20 | - * will skip permissions check for them. |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected $skipPermissionCheck = ['account', 'logout', 'changePassword', 'saveProfile', 'account']; |
|
24 | - |
|
25 | - /** |
|
26 | - * List of all route actions that the base api controller |
|
27 | - * will skip login check for them. |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken', 'confirmEmail', 'resendEmailConfirmation']; |
|
31 | - |
|
32 | - /** |
|
33 | - * The validations rules used by the base api controller |
|
34 | - * to check before add. |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - protected $validationRules = [ |
|
38 | - 'name' => 'nullable|string', |
|
39 | - 'email' => 'required|email|unique:users,email,{id}', |
|
40 | - 'password' => 'nullable|min:6' |
|
41 | - ]; |
|
42 | - |
|
43 | - /** |
|
44 | - * The loginProxy implementation. |
|
45 | - * |
|
46 | - * @var \App\Modules\Acl\Proxy\LoginProxy |
|
47 | - */ |
|
48 | - protected $loginProxy; |
|
49 | - |
|
50 | - public function __construct(LoginProxy $loginProxy) |
|
51 | - { |
|
52 | - $this->loginProxy = $loginProxy; |
|
53 | - parent::__construct(); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Return the logged in user account. |
|
58 | - * |
|
59 | - * @return \Illuminate\Http\Response |
|
60 | - */ |
|
61 | - public function account() |
|
62 | - { |
|
63 | - return \Response::json($this->repo->account($this->relations), 200); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Block the user. |
|
68 | - * |
|
69 | - * @param integer $id Id of the user. |
|
70 | - * @return \Illuminate\Http\Response |
|
71 | - */ |
|
72 | - public function block($id) |
|
73 | - { |
|
74 | - return \Response::json($this->repo->block($id), 200); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Unblock the user. |
|
79 | - * |
|
80 | - * @param integer $id Id of the user. |
|
81 | - * @return \Illuminate\Http\Response |
|
82 | - */ |
|
83 | - public function unblock($id) |
|
84 | - { |
|
85 | - return \Response::json($this->repo->unblock($id), 200); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Logout the user. |
|
90 | - * |
|
91 | - * @return \Illuminate\Http\Response |
|
92 | - */ |
|
93 | - public function logout() |
|
94 | - { |
|
95 | - return \Response::json($this->loginProxy->logout(), 200); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Handle a registration request. |
|
100 | - * |
|
101 | - * @param \Illuminate\Http\Request $request |
|
102 | - * @return \Illuminate\Http\Response |
|
103 | - */ |
|
104 | - public function register(Request $request) |
|
105 | - { |
|
106 | - $this->validate($request, [ |
|
107 | - 'name' => 'nullable|string', |
|
108 | - 'email' => 'required|email|unique:users,email,{id}', |
|
109 | - 'password' => 'required|min:6' |
|
110 | - ]); |
|
111 | - |
|
112 | - return \Response::json($this->repo->register($request->only('name', 'email', 'password')), 200); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Handle a login request to the application. |
|
117 | - * |
|
118 | - * @param \Illuminate\Http\Request $request |
|
119 | - * @return \Illuminate\Http\Response |
|
120 | - */ |
|
121 | - public function login(Request $request) |
|
122 | - { |
|
123 | - $this->validate($request, [ |
|
124 | - 'email' => 'required|email', |
|
125 | - 'password' => 'required|min:6', |
|
126 | - 'admin' => 'nullable|boolean' |
|
127 | - ]); |
|
128 | - |
|
129 | - return \Response::json($this->loginProxy->login($request->only('email', 'password'), $request->get('admin')), 200); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Handle a social login request of the none admin to the application. |
|
134 | - * |
|
135 | - * @param \Illuminate\Http\Request $request |
|
136 | - * @return \Illuminate\Http\Response |
|
137 | - */ |
|
138 | - public function loginSocial(Request $request) |
|
139 | - { |
|
140 | - $this->validate($request, [ |
|
141 | - 'auth_code' => 'required_without:access_token', |
|
142 | - 'access_token' => 'required_without:auth_code', |
|
143 | - 'type' => 'required|in:facebook,google' |
|
144 | - ]); |
|
145 | - |
|
146 | - return \Response::json($this->repo->loginSocial($request->only('auth_code', 'access_token', 'type')), 200); |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Assign the given groups to the given user. |
|
151 | - * |
|
152 | - * @param \Illuminate\Http\Request $request |
|
153 | - * @return \Illuminate\Http\Response |
|
154 | - */ |
|
155 | - public function assigngroups(Request $request) |
|
156 | - { |
|
157 | - $this->validate($request, [ |
|
158 | - 'group_ids' => 'required|exists:groups,id', |
|
159 | - 'user_id' => 'required|exists:users,id' |
|
160 | - ]); |
|
161 | - |
|
162 | - return \Response::json($this->repo->assignGroups($request->get('user_id'), $request->get('group_ids')), 200); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Send a reset link to the given user. |
|
167 | - * |
|
168 | - * @param \Illuminate\Http\Request $request |
|
169 | - * @return \Illuminate\Http\Response |
|
170 | - */ |
|
171 | - public function sendreset(Request $request) |
|
172 | - { |
|
173 | - $this->validate($request, ['email' => 'required|email']); |
|
174 | - |
|
175 | - return \Response::json($this->repo->sendReset($request->get('email')), 200); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Reset the given user's password. |
|
180 | - * |
|
181 | - * @param \Illuminate\Http\Request $request |
|
182 | - * @return \Illuminate\Http\Response |
|
183 | - */ |
|
184 | - public function resetpassword(Request $request) |
|
185 | - { |
|
186 | - $this->validate($request, [ |
|
187 | - 'token' => 'required', |
|
188 | - 'email' => 'required|email', |
|
189 | - 'password' => 'required|confirmed|min:6', |
|
190 | - 'password_confirmation' => 'required', |
|
191 | - ]); |
|
192 | - |
|
193 | - return \Response::json($this->repo->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Change the logged in user password. |
|
198 | - * |
|
199 | - * @param \Illuminate\Http\Request $request |
|
200 | - * @return \Illuminate\Http\Response |
|
201 | - */ |
|
202 | - public function changePassword(Request $request) |
|
203 | - { |
|
204 | - $this->validate($request, [ |
|
205 | - 'old_password' => 'required', |
|
206 | - 'password' => 'required|confirmed|min:6', |
|
207 | - 'password_confirmation' => 'required', |
|
208 | - ]); |
|
209 | - |
|
210 | - return \Response::json($this->repo->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200); |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Confirm email using the confirmation code. |
|
215 | - * |
|
216 | - * @param \Illuminate\Http\Request $request |
|
217 | - * @return \Illuminate\Http\Response |
|
218 | - */ |
|
219 | - public function confirmEmail(Request $request) |
|
220 | - { |
|
221 | - $this->validate($request, [ |
|
222 | - 'confirmation_code' => 'required|string|exists:users,confirmation_code' |
|
223 | - ]); |
|
224 | - |
|
225 | - return \Response::json($this->repo->confirmEmail($request->only('confirmation_code')), 200); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Resend the email confirmation mail. |
|
230 | - * |
|
231 | - * @param \Illuminate\Http\Request $request |
|
232 | - * @return \Illuminate\Http\Response |
|
233 | - */ |
|
234 | - public function resendEmailConfirmation(Request $request) |
|
235 | - { |
|
236 | - $this->validate($request, [ |
|
237 | - 'email' => 'required|exists:users,email' |
|
238 | - ]); |
|
239 | - |
|
240 | - return \Response::json($this->repo->sendConfirmationEmail($request->get('email')), 200); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Refresh the expired login token. |
|
245 | - * |
|
246 | - * @param \Illuminate\Http\Request $request |
|
247 | - * @return \Illuminate\Http\Response |
|
248 | - */ |
|
249 | - public function refreshtoken(Request $request) |
|
250 | - { |
|
251 | - $this->validate($request, [ |
|
252 | - 'refreshtoken' => 'required', |
|
253 | - ]); |
|
254 | - |
|
255 | - return \Response::json($this->loginProxy->refreshtoken($request->get('refreshtoken')), 200); |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Paginate all users with in the given group. |
|
260 | - * |
|
261 | - * @param \Illuminate\Http\Request $request |
|
262 | - * @param string $groupName The name of the requested group. |
|
263 | - * @param integer $perPage Number of rows per page default 15. |
|
264 | - * @param string $sortBy The name of the column to sort by. |
|
265 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
266 | - * @return \Illuminate\Http\Response |
|
267 | - */ |
|
268 | - public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
|
269 | - { |
|
270 | - return \Response::json($this->repo->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc), 200); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Save the given data to the logged in user. |
|
275 | - * |
|
276 | - * @param \Illuminate\Http\Request $request |
|
277 | - * @return \Illuminate\Http\Response |
|
278 | - */ |
|
279 | - public function saveProfile(Request $request) |
|
280 | - { |
|
281 | - $this->validate($request, [ |
|
282 | - 'profile_picture' => 'nullable|base64image', |
|
283 | - 'name' => 'nullable|string', |
|
284 | - 'email' => 'required|email|unique:users,email,' . \Auth::id() |
|
285 | - ]); |
|
286 | - |
|
287 | - return \Response::json($this->repo->saveProfile($request->only('name', 'email', 'profile_picture')), 200); |
|
288 | - } |
|
11 | + /** |
|
12 | + * The name of the model that is used by the base api controller |
|
13 | + * to preform actions like (add, edit ... etc). |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $model = 'users'; |
|
17 | + |
|
18 | + /** |
|
19 | + * List of all route actions that the base api controller |
|
20 | + * will skip permissions check for them. |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected $skipPermissionCheck = ['account', 'logout', 'changePassword', 'saveProfile', 'account']; |
|
24 | + |
|
25 | + /** |
|
26 | + * List of all route actions that the base api controller |
|
27 | + * will skip login check for them. |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken', 'confirmEmail', 'resendEmailConfirmation']; |
|
31 | + |
|
32 | + /** |
|
33 | + * The validations rules used by the base api controller |
|
34 | + * to check before add. |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + protected $validationRules = [ |
|
38 | + 'name' => 'nullable|string', |
|
39 | + 'email' => 'required|email|unique:users,email,{id}', |
|
40 | + 'password' => 'nullable|min:6' |
|
41 | + ]; |
|
42 | + |
|
43 | + /** |
|
44 | + * The loginProxy implementation. |
|
45 | + * |
|
46 | + * @var \App\Modules\Acl\Proxy\LoginProxy |
|
47 | + */ |
|
48 | + protected $loginProxy; |
|
49 | + |
|
50 | + public function __construct(LoginProxy $loginProxy) |
|
51 | + { |
|
52 | + $this->loginProxy = $loginProxy; |
|
53 | + parent::__construct(); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Return the logged in user account. |
|
58 | + * |
|
59 | + * @return \Illuminate\Http\Response |
|
60 | + */ |
|
61 | + public function account() |
|
62 | + { |
|
63 | + return \Response::json($this->repo->account($this->relations), 200); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Block the user. |
|
68 | + * |
|
69 | + * @param integer $id Id of the user. |
|
70 | + * @return \Illuminate\Http\Response |
|
71 | + */ |
|
72 | + public function block($id) |
|
73 | + { |
|
74 | + return \Response::json($this->repo->block($id), 200); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Unblock the user. |
|
79 | + * |
|
80 | + * @param integer $id Id of the user. |
|
81 | + * @return \Illuminate\Http\Response |
|
82 | + */ |
|
83 | + public function unblock($id) |
|
84 | + { |
|
85 | + return \Response::json($this->repo->unblock($id), 200); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Logout the user. |
|
90 | + * |
|
91 | + * @return \Illuminate\Http\Response |
|
92 | + */ |
|
93 | + public function logout() |
|
94 | + { |
|
95 | + return \Response::json($this->loginProxy->logout(), 200); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Handle a registration request. |
|
100 | + * |
|
101 | + * @param \Illuminate\Http\Request $request |
|
102 | + * @return \Illuminate\Http\Response |
|
103 | + */ |
|
104 | + public function register(Request $request) |
|
105 | + { |
|
106 | + $this->validate($request, [ |
|
107 | + 'name' => 'nullable|string', |
|
108 | + 'email' => 'required|email|unique:users,email,{id}', |
|
109 | + 'password' => 'required|min:6' |
|
110 | + ]); |
|
111 | + |
|
112 | + return \Response::json($this->repo->register($request->only('name', 'email', 'password')), 200); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Handle a login request to the application. |
|
117 | + * |
|
118 | + * @param \Illuminate\Http\Request $request |
|
119 | + * @return \Illuminate\Http\Response |
|
120 | + */ |
|
121 | + public function login(Request $request) |
|
122 | + { |
|
123 | + $this->validate($request, [ |
|
124 | + 'email' => 'required|email', |
|
125 | + 'password' => 'required|min:6', |
|
126 | + 'admin' => 'nullable|boolean' |
|
127 | + ]); |
|
128 | + |
|
129 | + return \Response::json($this->loginProxy->login($request->only('email', 'password'), $request->get('admin')), 200); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Handle a social login request of the none admin to the application. |
|
134 | + * |
|
135 | + * @param \Illuminate\Http\Request $request |
|
136 | + * @return \Illuminate\Http\Response |
|
137 | + */ |
|
138 | + public function loginSocial(Request $request) |
|
139 | + { |
|
140 | + $this->validate($request, [ |
|
141 | + 'auth_code' => 'required_without:access_token', |
|
142 | + 'access_token' => 'required_without:auth_code', |
|
143 | + 'type' => 'required|in:facebook,google' |
|
144 | + ]); |
|
145 | + |
|
146 | + return \Response::json($this->repo->loginSocial($request->only('auth_code', 'access_token', 'type')), 200); |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Assign the given groups to the given user. |
|
151 | + * |
|
152 | + * @param \Illuminate\Http\Request $request |
|
153 | + * @return \Illuminate\Http\Response |
|
154 | + */ |
|
155 | + public function assigngroups(Request $request) |
|
156 | + { |
|
157 | + $this->validate($request, [ |
|
158 | + 'group_ids' => 'required|exists:groups,id', |
|
159 | + 'user_id' => 'required|exists:users,id' |
|
160 | + ]); |
|
161 | + |
|
162 | + return \Response::json($this->repo->assignGroups($request->get('user_id'), $request->get('group_ids')), 200); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Send a reset link to the given user. |
|
167 | + * |
|
168 | + * @param \Illuminate\Http\Request $request |
|
169 | + * @return \Illuminate\Http\Response |
|
170 | + */ |
|
171 | + public function sendreset(Request $request) |
|
172 | + { |
|
173 | + $this->validate($request, ['email' => 'required|email']); |
|
174 | + |
|
175 | + return \Response::json($this->repo->sendReset($request->get('email')), 200); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Reset the given user's password. |
|
180 | + * |
|
181 | + * @param \Illuminate\Http\Request $request |
|
182 | + * @return \Illuminate\Http\Response |
|
183 | + */ |
|
184 | + public function resetpassword(Request $request) |
|
185 | + { |
|
186 | + $this->validate($request, [ |
|
187 | + 'token' => 'required', |
|
188 | + 'email' => 'required|email', |
|
189 | + 'password' => 'required|confirmed|min:6', |
|
190 | + 'password_confirmation' => 'required', |
|
191 | + ]); |
|
192 | + |
|
193 | + return \Response::json($this->repo->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Change the logged in user password. |
|
198 | + * |
|
199 | + * @param \Illuminate\Http\Request $request |
|
200 | + * @return \Illuminate\Http\Response |
|
201 | + */ |
|
202 | + public function changePassword(Request $request) |
|
203 | + { |
|
204 | + $this->validate($request, [ |
|
205 | + 'old_password' => 'required', |
|
206 | + 'password' => 'required|confirmed|min:6', |
|
207 | + 'password_confirmation' => 'required', |
|
208 | + ]); |
|
209 | + |
|
210 | + return \Response::json($this->repo->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200); |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Confirm email using the confirmation code. |
|
215 | + * |
|
216 | + * @param \Illuminate\Http\Request $request |
|
217 | + * @return \Illuminate\Http\Response |
|
218 | + */ |
|
219 | + public function confirmEmail(Request $request) |
|
220 | + { |
|
221 | + $this->validate($request, [ |
|
222 | + 'confirmation_code' => 'required|string|exists:users,confirmation_code' |
|
223 | + ]); |
|
224 | + |
|
225 | + return \Response::json($this->repo->confirmEmail($request->only('confirmation_code')), 200); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Resend the email confirmation mail. |
|
230 | + * |
|
231 | + * @param \Illuminate\Http\Request $request |
|
232 | + * @return \Illuminate\Http\Response |
|
233 | + */ |
|
234 | + public function resendEmailConfirmation(Request $request) |
|
235 | + { |
|
236 | + $this->validate($request, [ |
|
237 | + 'email' => 'required|exists:users,email' |
|
238 | + ]); |
|
239 | + |
|
240 | + return \Response::json($this->repo->sendConfirmationEmail($request->get('email')), 200); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Refresh the expired login token. |
|
245 | + * |
|
246 | + * @param \Illuminate\Http\Request $request |
|
247 | + * @return \Illuminate\Http\Response |
|
248 | + */ |
|
249 | + public function refreshtoken(Request $request) |
|
250 | + { |
|
251 | + $this->validate($request, [ |
|
252 | + 'refreshtoken' => 'required', |
|
253 | + ]); |
|
254 | + |
|
255 | + return \Response::json($this->loginProxy->refreshtoken($request->get('refreshtoken')), 200); |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Paginate all users with in the given group. |
|
260 | + * |
|
261 | + * @param \Illuminate\Http\Request $request |
|
262 | + * @param string $groupName The name of the requested group. |
|
263 | + * @param integer $perPage Number of rows per page default 15. |
|
264 | + * @param string $sortBy The name of the column to sort by. |
|
265 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
266 | + * @return \Illuminate\Http\Response |
|
267 | + */ |
|
268 | + public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
|
269 | + { |
|
270 | + return \Response::json($this->repo->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc), 200); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Save the given data to the logged in user. |
|
275 | + * |
|
276 | + * @param \Illuminate\Http\Request $request |
|
277 | + * @return \Illuminate\Http\Response |
|
278 | + */ |
|
279 | + public function saveProfile(Request $request) |
|
280 | + { |
|
281 | + $this->validate($request, [ |
|
282 | + 'profile_picture' => 'nullable|base64image', |
|
283 | + 'name' => 'nullable|string', |
|
284 | + 'email' => 'required|email|unique:users,email,' . \Auth::id() |
|
285 | + ]); |
|
286 | + |
|
287 | + return \Response::json($this->repo->saveProfile($request->only('name', 'email', 'profile_picture')), 200); |
|
288 | + } |
|
289 | 289 | } |