@@ -7,218 +7,218 @@ |
||
| 7 | 7 | class ApiConsumer |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | - private $app; |
|
| 11 | - |
|
| 12 | - private $router; |
|
| 13 | - |
|
| 14 | - private $request; |
|
| 15 | - |
|
| 16 | - private $disableMiddleware = false; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @param \Illuminate\Foundation\Application $app |
|
| 20 | - * @param \Illuminate\Http\Request $request, |
|
| 21 | - * @param \Illuminate\Routing\Router $router |
|
| 22 | - */ |
|
| 23 | - public function __construct(Application $app, Request $request, LaravelRouter $router) |
|
| 24 | - { |
|
| 25 | - $this->app = $app; |
|
| 26 | - $this->request = $request; |
|
| 27 | - $this->router = $router; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @param string $uri |
|
| 32 | - * @param array $data |
|
| 33 | - * @param array $headers |
|
| 34 | - * @param string $content |
|
| 35 | - * @return \Illuminate\Http\Response |
|
| 36 | - */ |
|
| 37 | - public function get() |
|
| 38 | - { |
|
| 39 | - return $this->quickCall('GET', func_get_args()); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @param string $uri |
|
| 44 | - * @param array $data |
|
| 45 | - * @param array $headers |
|
| 46 | - * @param string $content |
|
| 47 | - * @return \Illuminate\Http\Response |
|
| 48 | - */ |
|
| 49 | - public function post() |
|
| 50 | - { |
|
| 51 | - return $this->quickCall('POST', func_get_args()); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @param string $uri |
|
| 56 | - * @param array $data |
|
| 57 | - * @param array $headers |
|
| 58 | - * @param string $content |
|
| 59 | - * @return \Illuminate\Http\Response |
|
| 60 | - */ |
|
| 61 | - public function put() |
|
| 62 | - { |
|
| 63 | - return $this->quickCall('PUT', func_get_args()); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @param string $uri |
|
| 68 | - * @param array $data |
|
| 69 | - * @param array $headers |
|
| 70 | - * @param string $content |
|
| 71 | - * @return \Illuminate\Http\Response |
|
| 72 | - */ |
|
| 73 | - public function delete() |
|
| 74 | - { |
|
| 75 | - return $this->quickCall('DELETE', func_get_args()); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @param array $requests An array of requests |
|
| 80 | - * @return array |
|
| 81 | - */ |
|
| 82 | - public function batchRequest(array $requests) |
|
| 83 | - { |
|
| 84 | - foreach ($requests as $i => $request) { |
|
| 85 | - $requests[$i] = call_user_func_array([$this, 'singleRequest'], $request); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - return $requests; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @param string $method |
|
| 93 | - * @param array $args |
|
| 94 | - * @return \Illuminate\Http\Response |
|
| 95 | - */ |
|
| 96 | - public function quickCall($method, array $args) |
|
| 97 | - { |
|
| 98 | - array_unshift($args, $method); |
|
| 99 | - return call_user_func_array([$this, "singleRequest"], $args); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param string $method |
|
| 104 | - * @param string $uri |
|
| 105 | - * @param array $data |
|
| 106 | - * @param array $headers |
|
| 107 | - * @param string $content |
|
| 108 | - * @return \Illuminate\Http\Response |
|
| 109 | - */ |
|
| 110 | - public function singleRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 111 | - { |
|
| 112 | - // Save the current request so we can reset the router back to it |
|
| 113 | - // after we've completed our internal request. |
|
| 114 | - $currentRequest = $this->request->instance()->duplicate(); |
|
| 115 | - |
|
| 116 | - $headers = $this->overrideHeaders($currentRequest->server->getHeaders(), $headers); |
|
| 117 | - |
|
| 118 | - if ($this->disableMiddleware) { |
|
| 119 | - $this->app->instance('middleware.disable', true); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - $response = $this->request($method, $uri, $data, $headers, $content); |
|
| 123 | - |
|
| 124 | - if ($this->disableMiddleware) { |
|
| 125 | - $this->app->instance('middleware.disable', false); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - // Once the request has completed we reset the currentRequest of the router |
|
| 129 | - // to match the original request. |
|
| 130 | - $this->request->instance()->initialize( |
|
| 131 | - $currentRequest->query->all(), |
|
| 132 | - $currentRequest->request->all(), |
|
| 133 | - $currentRequest->attributes->all(), |
|
| 134 | - $currentRequest->cookies->all(), |
|
| 135 | - $currentRequest->files->all(), |
|
| 136 | - $currentRequest->server->all(), |
|
| 137 | - $currentRequest->content |
|
| 138 | - ); |
|
| 139 | - |
|
| 140 | - return $response; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - private function overrideHeaders(array $default, array $headers) |
|
| 144 | - { |
|
| 145 | - $headers = $this->transformHeadersToUppercaseUnderscoreType($headers); |
|
| 146 | - return array_merge($default, $headers); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - public function enableMiddleware() |
|
| 150 | - { |
|
| 151 | - $this->disableMiddleware = false; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - public function disableMiddleware() |
|
| 155 | - { |
|
| 156 | - $this->disableMiddleware = true; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @param string $method |
|
| 161 | - * @param string $uri |
|
| 162 | - * @param array $data |
|
| 163 | - * @param array $headers |
|
| 164 | - * @param string $content |
|
| 165 | - * @return \Illuminate\Http\Response |
|
| 166 | - */ |
|
| 167 | - private function request($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 168 | - { |
|
| 169 | - // Create a new request object for the internal request |
|
| 170 | - $request = $this->createRequest($method, $uri, $data, $headers, $content); |
|
| 171 | - |
|
| 172 | - // Handle the request in the kernel and prepare a response |
|
| 173 | - $response = $this->router->prepareResponse($request, $this->app->handle($request)); |
|
| 174 | - |
|
| 175 | - return $response; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @param string $method |
|
| 180 | - * @param string $uri |
|
| 181 | - * @param array $data |
|
| 182 | - * @param array $headers |
|
| 183 | - * @param string $content |
|
| 184 | - * @return \Illuminate\Http\Request |
|
| 185 | - */ |
|
| 186 | - private function createRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 187 | - { |
|
| 188 | - $server = $this->transformHeadersToServerVariables($headers); |
|
| 189 | - |
|
| 190 | - return $this->request->create($uri, $method, $data, [], [], $server, $content); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - private function transformHeadersToUppercaseUnderscoreType($headers) |
|
| 194 | - { |
|
| 195 | - $transformed = []; |
|
| 196 | - |
|
| 197 | - foreach ($headers as $headerType => $headerValue) { |
|
| 198 | - $headerType = strtoupper(str_replace('-', '_', $headerType)); |
|
| 199 | - |
|
| 200 | - $transformed[$headerType] = $headerValue; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - return $transformed; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * https://github.com/symfony/symfony/issues/5074 |
|
| 208 | - * |
|
| 209 | - * @param array $headers |
|
| 210 | - * @return array |
|
| 211 | - */ |
|
| 212 | - private function transformHeadersToServerVariables($headers) |
|
| 213 | - { |
|
| 214 | - $server = []; |
|
| 215 | - |
|
| 216 | - foreach ($headers as $headerType => $headerValue) { |
|
| 217 | - $headerType = 'HTTP_' . $headerType; |
|
| 218 | - |
|
| 219 | - $server[$headerType] = $headerValue; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - return $server; |
|
| 223 | - } |
|
| 10 | + private $app; |
|
| 11 | + |
|
| 12 | + private $router; |
|
| 13 | + |
|
| 14 | + private $request; |
|
| 15 | + |
|
| 16 | + private $disableMiddleware = false; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @param \Illuminate\Foundation\Application $app |
|
| 20 | + * @param \Illuminate\Http\Request $request, |
|
| 21 | + * @param \Illuminate\Routing\Router $router |
|
| 22 | + */ |
|
| 23 | + public function __construct(Application $app, Request $request, LaravelRouter $router) |
|
| 24 | + { |
|
| 25 | + $this->app = $app; |
|
| 26 | + $this->request = $request; |
|
| 27 | + $this->router = $router; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @param string $uri |
|
| 32 | + * @param array $data |
|
| 33 | + * @param array $headers |
|
| 34 | + * @param string $content |
|
| 35 | + * @return \Illuminate\Http\Response |
|
| 36 | + */ |
|
| 37 | + public function get() |
|
| 38 | + { |
|
| 39 | + return $this->quickCall('GET', func_get_args()); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @param string $uri |
|
| 44 | + * @param array $data |
|
| 45 | + * @param array $headers |
|
| 46 | + * @param string $content |
|
| 47 | + * @return \Illuminate\Http\Response |
|
| 48 | + */ |
|
| 49 | + public function post() |
|
| 50 | + { |
|
| 51 | + return $this->quickCall('POST', func_get_args()); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @param string $uri |
|
| 56 | + * @param array $data |
|
| 57 | + * @param array $headers |
|
| 58 | + * @param string $content |
|
| 59 | + * @return \Illuminate\Http\Response |
|
| 60 | + */ |
|
| 61 | + public function put() |
|
| 62 | + { |
|
| 63 | + return $this->quickCall('PUT', func_get_args()); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @param string $uri |
|
| 68 | + * @param array $data |
|
| 69 | + * @param array $headers |
|
| 70 | + * @param string $content |
|
| 71 | + * @return \Illuminate\Http\Response |
|
| 72 | + */ |
|
| 73 | + public function delete() |
|
| 74 | + { |
|
| 75 | + return $this->quickCall('DELETE', func_get_args()); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @param array $requests An array of requests |
|
| 80 | + * @return array |
|
| 81 | + */ |
|
| 82 | + public function batchRequest(array $requests) |
|
| 83 | + { |
|
| 84 | + foreach ($requests as $i => $request) { |
|
| 85 | + $requests[$i] = call_user_func_array([$this, 'singleRequest'], $request); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + return $requests; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @param string $method |
|
| 93 | + * @param array $args |
|
| 94 | + * @return \Illuminate\Http\Response |
|
| 95 | + */ |
|
| 96 | + public function quickCall($method, array $args) |
|
| 97 | + { |
|
| 98 | + array_unshift($args, $method); |
|
| 99 | + return call_user_func_array([$this, "singleRequest"], $args); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param string $method |
|
| 104 | + * @param string $uri |
|
| 105 | + * @param array $data |
|
| 106 | + * @param array $headers |
|
| 107 | + * @param string $content |
|
| 108 | + * @return \Illuminate\Http\Response |
|
| 109 | + */ |
|
| 110 | + public function singleRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 111 | + { |
|
| 112 | + // Save the current request so we can reset the router back to it |
|
| 113 | + // after we've completed our internal request. |
|
| 114 | + $currentRequest = $this->request->instance()->duplicate(); |
|
| 115 | + |
|
| 116 | + $headers = $this->overrideHeaders($currentRequest->server->getHeaders(), $headers); |
|
| 117 | + |
|
| 118 | + if ($this->disableMiddleware) { |
|
| 119 | + $this->app->instance('middleware.disable', true); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + $response = $this->request($method, $uri, $data, $headers, $content); |
|
| 123 | + |
|
| 124 | + if ($this->disableMiddleware) { |
|
| 125 | + $this->app->instance('middleware.disable', false); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + // Once the request has completed we reset the currentRequest of the router |
|
| 129 | + // to match the original request. |
|
| 130 | + $this->request->instance()->initialize( |
|
| 131 | + $currentRequest->query->all(), |
|
| 132 | + $currentRequest->request->all(), |
|
| 133 | + $currentRequest->attributes->all(), |
|
| 134 | + $currentRequest->cookies->all(), |
|
| 135 | + $currentRequest->files->all(), |
|
| 136 | + $currentRequest->server->all(), |
|
| 137 | + $currentRequest->content |
|
| 138 | + ); |
|
| 139 | + |
|
| 140 | + return $response; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + private function overrideHeaders(array $default, array $headers) |
|
| 144 | + { |
|
| 145 | + $headers = $this->transformHeadersToUppercaseUnderscoreType($headers); |
|
| 146 | + return array_merge($default, $headers); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + public function enableMiddleware() |
|
| 150 | + { |
|
| 151 | + $this->disableMiddleware = false; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + public function disableMiddleware() |
|
| 155 | + { |
|
| 156 | + $this->disableMiddleware = true; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @param string $method |
|
| 161 | + * @param string $uri |
|
| 162 | + * @param array $data |
|
| 163 | + * @param array $headers |
|
| 164 | + * @param string $content |
|
| 165 | + * @return \Illuminate\Http\Response |
|
| 166 | + */ |
|
| 167 | + private function request($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 168 | + { |
|
| 169 | + // Create a new request object for the internal request |
|
| 170 | + $request = $this->createRequest($method, $uri, $data, $headers, $content); |
|
| 171 | + |
|
| 172 | + // Handle the request in the kernel and prepare a response |
|
| 173 | + $response = $this->router->prepareResponse($request, $this->app->handle($request)); |
|
| 174 | + |
|
| 175 | + return $response; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @param string $method |
|
| 180 | + * @param string $uri |
|
| 181 | + * @param array $data |
|
| 182 | + * @param array $headers |
|
| 183 | + * @param string $content |
|
| 184 | + * @return \Illuminate\Http\Request |
|
| 185 | + */ |
|
| 186 | + private function createRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 187 | + { |
|
| 188 | + $server = $this->transformHeadersToServerVariables($headers); |
|
| 189 | + |
|
| 190 | + return $this->request->create($uri, $method, $data, [], [], $server, $content); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + private function transformHeadersToUppercaseUnderscoreType($headers) |
|
| 194 | + { |
|
| 195 | + $transformed = []; |
|
| 196 | + |
|
| 197 | + foreach ($headers as $headerType => $headerValue) { |
|
| 198 | + $headerType = strtoupper(str_replace('-', '_', $headerType)); |
|
| 199 | + |
|
| 200 | + $transformed[$headerType] = $headerValue; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + return $transformed; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * https://github.com/symfony/symfony/issues/5074 |
|
| 208 | + * |
|
| 209 | + * @param array $headers |
|
| 210 | + * @return array |
|
| 211 | + */ |
|
| 212 | + private function transformHeadersToServerVariables($headers) |
|
| 213 | + { |
|
| 214 | + $server = []; |
|
| 215 | + |
|
| 216 | + foreach ($headers as $headerType => $headerValue) { |
|
| 217 | + $headerType = 'HTTP_' . $headerType; |
|
| 218 | + |
|
| 219 | + $server[$headerType] = $headerValue; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + return $server; |
|
| 223 | + } |
|
| 224 | 224 | } |
@@ -9,34 +9,34 @@ |
||
| 9 | 9 | |
| 10 | 10 | class ReportController extends BaseApiController |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * List of all route actions that the base api controller |
|
| 14 | - * will skip permissions check for them. |
|
| 15 | - * @var array |
|
| 16 | - */ |
|
| 17 | - protected $skipPermissionCheck = ['getReport']; |
|
| 12 | + /** |
|
| 13 | + * List of all route actions that the base api controller |
|
| 14 | + * will skip permissions check for them. |
|
| 15 | + * @var array |
|
| 16 | + */ |
|
| 17 | + protected $skipPermissionCheck = ['getReport']; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Init new object. |
|
| 21 | - * |
|
| 22 | - * @param ReportRepository $repo |
|
| 23 | - * @param CoreConfig $config |
|
| 24 | - * @return void |
|
| 25 | - */ |
|
| 26 | - public function __construct(ReportRepository $repo, CoreConfig $config) |
|
| 27 | - { |
|
| 28 | - parent::__construct($repo, $config, 'App\Modules\Reporting\Http\Resources\Report'); |
|
| 29 | - } |
|
| 19 | + /** |
|
| 20 | + * Init new object. |
|
| 21 | + * |
|
| 22 | + * @param ReportRepository $repo |
|
| 23 | + * @param CoreConfig $config |
|
| 24 | + * @return void |
|
| 25 | + */ |
|
| 26 | + public function __construct(ReportRepository $repo, CoreConfig $config) |
|
| 27 | + { |
|
| 28 | + parent::__construct($repo, $config, 'App\Modules\Reporting\Http\Resources\Report'); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Render the given report name with the given conditions. |
|
| 33 | - * |
|
| 34 | - * @param Request $request |
|
| 35 | - * @param string $reportName Name of the requested report |
|
| 36 | - * @return \Illuminate\Http\Response |
|
| 37 | - */ |
|
| 38 | - public function getReport(Request $request, $reportName) |
|
| 39 | - { |
|
| 40 | - return \Response::json($this->repo->getReport($reportName, $request->all(), $request->query('perPage')), 200); |
|
| 41 | - } |
|
| 31 | + /** |
|
| 32 | + * Render the given report name with the given conditions. |
|
| 33 | + * |
|
| 34 | + * @param Request $request |
|
| 35 | + * @param string $reportName Name of the requested report |
|
| 36 | + * @return \Illuminate\Http\Response |
|
| 37 | + */ |
|
| 38 | + public function getReport(Request $request, $reportName) |
|
| 39 | + { |
|
| 40 | + return \Response::json($this->repo->getReport($reportName, $request->all(), $request->query('perPage')), 200); |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -1,7 +1,5 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -use Illuminate\Http\Request; |
|
| 4 | - |
|
| 5 | 3 | /* |
| 6 | 4 | |-------------------------------------------------------------------------- |
| 7 | 5 | | API Routes |
@@ -1,7 +1,5 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -use Illuminate\Http\Request; |
|
| 4 | - |
|
| 5 | 3 | /* |
| 6 | 4 | |-------------------------------------------------------------------------- |
| 7 | 5 | | API Routes |
@@ -1,7 +1,5 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -use Illuminate\Http\Request; |
|
| 4 | - |
|
| 5 | 3 | /* |
| 6 | 4 | |-------------------------------------------------------------------------- |
| 7 | 5 | | API Routes |
@@ -1,7 +1,5 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -use Illuminate\Http\Request; |
|
| 4 | - |
|
| 5 | 3 | /* |
| 6 | 4 | |-------------------------------------------------------------------------- |
| 7 | 5 | | API Routes |
@@ -6,391 +6,391 @@ |
||
| 6 | 6 | |
| 7 | 7 | class UserRepository extends BaseRepository |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Init new object. |
|
| 11 | - * |
|
| 12 | - * @param AclUser $model |
|
| 13 | - * @return void |
|
| 14 | - */ |
|
| 15 | - public function __construct(AclUser $model) |
|
| 16 | - { |
|
| 17 | - parent::__construct($model); |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Return the logged in user account. |
|
| 22 | - * |
|
| 23 | - * @param array $relations |
|
| 24 | - * @return boolean |
|
| 25 | - */ |
|
| 26 | - public function account($relations = []) |
|
| 27 | - { |
|
| 28 | - $permissions = []; |
|
| 29 | - $user = $this->find(\Auth::id(), $relations); |
|
| 30 | - foreach ($user->groups()->get() as $group) { |
|
| 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 mixed $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 | - |
|
| 58 | - return in_array($nameOfPermission, $permissions); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Check if the logged in user has the given group. |
|
| 63 | - * |
|
| 64 | - * @param string[] $groups |
|
| 65 | - * @param mixed $user |
|
| 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 $userId |
|
| 78 | - * @param array $groupIds |
|
| 79 | - * @return object |
|
| 80 | - */ |
|
| 81 | - public function assignGroups($userId, $groupIds) |
|
| 82 | - { |
|
| 83 | - \DB::transaction(function () use ($userId, $groupIds) { |
|
| 84 | - $user = $this->find($userId); |
|
| 85 | - $user->groups()->detach(); |
|
| 86 | - $user->groups()->attach($groupIds); |
|
| 87 | - }); |
|
| 88 | - |
|
| 89 | - return $this->find($userId); |
|
| 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 | - \ErrorHandler::loginFailed(); |
|
| 104 | - } elseif ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) { |
|
| 105 | - \ErrorHandler::loginFailed(); |
|
| 106 | - } elseif (! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) { |
|
| 107 | - \ErrorHandler::loginFailed(); |
|
| 108 | - } elseif ($user->blocked) { |
|
| 109 | - \ErrorHandler::userIsBlocked(); |
|
| 110 | - } elseif (! config('skeleton.disable_confirm_email') && ! $user->confirmed) { |
|
| 111 | - \ErrorHandler::emailNotConfirmed(); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return $user; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Handle a social login request of the none admin to the application. |
|
| 119 | - * |
|
| 120 | - * @param string $authCode |
|
| 121 | - * @param string $accessToken |
|
| 122 | - * @param string $type |
|
| 123 | - * @return array |
|
| 124 | - */ |
|
| 125 | - public function loginSocial($authCode, $accessToken, $type) |
|
| 126 | - { |
|
| 127 | - $access_token = $authCode ? Arr::get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
| 128 | - $user = \Socialite::driver($type)->userFromToken($access_token); |
|
| 129 | - |
|
| 130 | - if (! $user->email) { |
|
| 131 | - \ErrorHandler::noSocialEmail(); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if (! $this->model->where('email', $user->email)->first()) { |
|
| 135 | - $this->register(['email' => $user->email, 'password' => ''], true); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - $loginProxy = \App::make('App\Modules\Users\Proxy\LoginProxy'); |
|
| 139 | - return $loginProxy->login(['email' => $user->email, 'password' => config('skeleton.social_pass')], 0); |
|
| 140 | - } |
|
| 9 | + /** |
|
| 10 | + * Init new object. |
|
| 11 | + * |
|
| 12 | + * @param AclUser $model |
|
| 13 | + * @return void |
|
| 14 | + */ |
|
| 15 | + public function __construct(AclUser $model) |
|
| 16 | + { |
|
| 17 | + parent::__construct($model); |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Return the logged in user account. |
|
| 22 | + * |
|
| 23 | + * @param array $relations |
|
| 24 | + * @return boolean |
|
| 25 | + */ |
|
| 26 | + public function account($relations = []) |
|
| 27 | + { |
|
| 28 | + $permissions = []; |
|
| 29 | + $user = $this->find(\Auth::id(), $relations); |
|
| 30 | + foreach ($user->groups()->get() as $group) { |
|
| 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 mixed $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 | + |
|
| 58 | + return in_array($nameOfPermission, $permissions); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Check if the logged in user has the given group. |
|
| 63 | + * |
|
| 64 | + * @param string[] $groups |
|
| 65 | + * @param mixed $user |
|
| 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 $userId |
|
| 78 | + * @param array $groupIds |
|
| 79 | + * @return object |
|
| 80 | + */ |
|
| 81 | + public function assignGroups($userId, $groupIds) |
|
| 82 | + { |
|
| 83 | + \DB::transaction(function () use ($userId, $groupIds) { |
|
| 84 | + $user = $this->find($userId); |
|
| 85 | + $user->groups()->detach(); |
|
| 86 | + $user->groups()->attach($groupIds); |
|
| 87 | + }); |
|
| 88 | + |
|
| 89 | + return $this->find($userId); |
|
| 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 | + \ErrorHandler::loginFailed(); |
|
| 104 | + } elseif ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) { |
|
| 105 | + \ErrorHandler::loginFailed(); |
|
| 106 | + } elseif (! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) { |
|
| 107 | + \ErrorHandler::loginFailed(); |
|
| 108 | + } elseif ($user->blocked) { |
|
| 109 | + \ErrorHandler::userIsBlocked(); |
|
| 110 | + } elseif (! config('skeleton.disable_confirm_email') && ! $user->confirmed) { |
|
| 111 | + \ErrorHandler::emailNotConfirmed(); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return $user; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Handle a social login request of the none admin to the application. |
|
| 119 | + * |
|
| 120 | + * @param string $authCode |
|
| 121 | + * @param string $accessToken |
|
| 122 | + * @param string $type |
|
| 123 | + * @return array |
|
| 124 | + */ |
|
| 125 | + public function loginSocial($authCode, $accessToken, $type) |
|
| 126 | + { |
|
| 127 | + $access_token = $authCode ? Arr::get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
| 128 | + $user = \Socialite::driver($type)->userFromToken($access_token); |
|
| 129 | + |
|
| 130 | + if (! $user->email) { |
|
| 131 | + \ErrorHandler::noSocialEmail(); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if (! $this->model->where('email', $user->email)->first()) { |
|
| 135 | + $this->register(['email' => $user->email, 'password' => ''], true); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + $loginProxy = \App::make('App\Modules\Users\Proxy\LoginProxy'); |
|
| 139 | + return $loginProxy->login(['email' => $user->email, 'password' => config('skeleton.social_pass')], 0); |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Handle a registration request. |
|
| 144 | - * |
|
| 145 | - * @param array $credentials |
|
| 146 | - * @param boolean $skipConfirmEmail |
|
| 147 | - * @return array |
|
| 148 | - */ |
|
| 149 | - public function register($credentials, $skipConfirmEmail = false) |
|
| 150 | - { |
|
| 151 | - $user = $this->save($credentials); |
|
| 152 | - |
|
| 153 | - if ($skipConfirmEmail) { |
|
| 154 | - $user->confirmed = 1; |
|
| 155 | - $user->save(); |
|
| 156 | - } elseif (! config('skeleton.disable_confirm_email')) { |
|
| 157 | - $this->sendConfirmationEmail($user->email); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return $user; |
|
| 161 | - } |
|
| 142 | + /** |
|
| 143 | + * Handle a registration request. |
|
| 144 | + * |
|
| 145 | + * @param array $credentials |
|
| 146 | + * @param boolean $skipConfirmEmail |
|
| 147 | + * @return array |
|
| 148 | + */ |
|
| 149 | + public function register($credentials, $skipConfirmEmail = false) |
|
| 150 | + { |
|
| 151 | + $user = $this->save($credentials); |
|
| 152 | + |
|
| 153 | + if ($skipConfirmEmail) { |
|
| 154 | + $user->confirmed = 1; |
|
| 155 | + $user->save(); |
|
| 156 | + } elseif (! config('skeleton.disable_confirm_email')) { |
|
| 157 | + $this->sendConfirmationEmail($user->email); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return $user; |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - /** |
|
| 164 | - * Block the user. |
|
| 165 | - * |
|
| 166 | - * @param integer $userId |
|
| 167 | - * @return object |
|
| 168 | - */ |
|
| 169 | - public function block($userId) |
|
| 170 | - { |
|
| 171 | - if (! $user = $this->find($userId)) { |
|
| 172 | - \ErrorHandler::notFound('user'); |
|
| 173 | - } |
|
| 174 | - if (! $this->hasGroup(['Admin'])) { |
|
| 175 | - \ErrorHandler::noPermissions(); |
|
| 176 | - } elseif (\Auth::id() == $userId) { |
|
| 177 | - \ErrorHandler::noPermissions(); |
|
| 178 | - } elseif ($user->groups->pluck('name')->search('Admin', true) !== false) { |
|
| 179 | - \ErrorHandler::noPermissions(); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - $user->blocked = 1; |
|
| 183 | - $user->save(); |
|
| 163 | + /** |
|
| 164 | + * Block the user. |
|
| 165 | + * |
|
| 166 | + * @param integer $userId |
|
| 167 | + * @return object |
|
| 168 | + */ |
|
| 169 | + public function block($userId) |
|
| 170 | + { |
|
| 171 | + if (! $user = $this->find($userId)) { |
|
| 172 | + \ErrorHandler::notFound('user'); |
|
| 173 | + } |
|
| 174 | + if (! $this->hasGroup(['Admin'])) { |
|
| 175 | + \ErrorHandler::noPermissions(); |
|
| 176 | + } elseif (\Auth::id() == $userId) { |
|
| 177 | + \ErrorHandler::noPermissions(); |
|
| 178 | + } elseif ($user->groups->pluck('name')->search('Admin', true) !== false) { |
|
| 179 | + \ErrorHandler::noPermissions(); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + $user->blocked = 1; |
|
| 183 | + $user->save(); |
|
| 184 | 184 | |
| 185 | - return $user; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Unblock the user. |
|
| 190 | - * |
|
| 191 | - * @param integer $userId |
|
| 192 | - * @return object |
|
| 193 | - */ |
|
| 194 | - public function unblock($userId) |
|
| 195 | - { |
|
| 196 | - if (! $this->hasGroup(['Admin'])) { |
|
| 197 | - \ErrorHandler::noPermissions(); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - $user = $this->find($userId); |
|
| 201 | - $user->blocked = 0; |
|
| 202 | - $user->save(); |
|
| 203 | - |
|
| 204 | - return $user; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Send a reset link to the given user. |
|
| 209 | - * |
|
| 210 | - * @param string $email |
|
| 211 | - * @return void |
|
| 212 | - */ |
|
| 213 | - public function sendReset($email) |
|
| 214 | - { |
|
| 215 | - if (! $user = $this->model->where('email', $email)->first()) { |
|
| 216 | - \ErrorHandler::notFound('email'); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $token = \Password::getRepository()->create($user); |
|
| 220 | - \Core::notifications()->notify($user, 'ResetPassword', $token); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Reset the given user's password. |
|
| 225 | - * |
|
| 226 | - * @param array $credentials |
|
| 227 | - * @return string|null |
|
| 228 | - */ |
|
| 229 | - public function resetPassword($credentials) |
|
| 230 | - { |
|
| 231 | - $response = \Password::reset($credentials, function ($user, $password) { |
|
| 232 | - $user->password = $password; |
|
| 233 | - $user->save(); |
|
| 234 | - }); |
|
| 235 | - |
|
| 236 | - switch ($response) { |
|
| 237 | - case \Password::PASSWORD_RESET: |
|
| 238 | - return 'success'; |
|
| 239 | - |
|
| 240 | - case \Password::INVALID_TOKEN: |
|
| 241 | - \ErrorHandler::invalidResetToken('token'); |
|
| 242 | - //no break |
|
| 243 | - |
|
| 244 | - case \Password::INVALID_PASSWORD: |
|
| 245 | - \ErrorHandler::invalidResetPassword('email'); |
|
| 246 | - //no break |
|
| 247 | - |
|
| 248 | - case \Password::INVALID_USER: |
|
| 249 | - \ErrorHandler::notFound('user'); |
|
| 250 | - //no break |
|
| 251 | - |
|
| 252 | - default: |
|
| 253 | - \ErrorHandler::generalError(); |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Change the logged in user password. |
|
| 259 | - * |
|
| 260 | - * @param array $credentials |
|
| 261 | - * @return void |
|
| 262 | - */ |
|
| 263 | - public function changePassword($credentials) |
|
| 264 | - { |
|
| 265 | - $user = \Auth::user(); |
|
| 266 | - if (! \Hash::check($credentials['old_password'], $user->password)) { |
|
| 267 | - \ErrorHandler::invalidOldPassword(); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - $user->password = $credentials['password']; |
|
| 271 | - $user->save(); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Confirm email using the confirmation code. |
|
| 276 | - * |
|
| 277 | - * @param string $confirmationCode |
|
| 278 | - * @return void |
|
| 279 | - */ |
|
| 280 | - public function confirmEmail($confirmationCode) |
|
| 281 | - { |
|
| 282 | - if (! $user = $this->first(['confirmation_code' => $confirmationCode])) { |
|
| 283 | - \ErrorHandler::invalidConfirmationCode(); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - $user->confirmed = 1; |
|
| 287 | - $user->confirmation_code = null; |
|
| 288 | - $user->save(); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Send the confirmation mail. |
|
| 293 | - * |
|
| 294 | - * @param string $email |
|
| 295 | - * @return void |
|
| 296 | - */ |
|
| 297 | - public function sendConfirmationEmail($email) |
|
| 298 | - { |
|
| 299 | - $user = $this->first(['email' => $email]); |
|
| 300 | - if ($user->confirmed) { |
|
| 301 | - \ErrorHandler::emailAlreadyConfirmed(); |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - $user->confirmed = 0; |
|
| 305 | - $user->confirmation_code = sha1(microtime()); |
|
| 306 | - $user->save(); |
|
| 307 | - \Core::notifications()->notify($user, 'ConfirmEmail'); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Paginate all users in the given group based on the given conditions. |
|
| 312 | - * |
|
| 313 | - * @param string $groupName |
|
| 314 | - * @param array $relations |
|
| 315 | - * @param integer $perPage |
|
| 316 | - * @param string $sortBy |
|
| 317 | - * @param boolean $desc |
|
| 318 | - * @return \Illuminate\Http\Response |
|
| 319 | - */ |
|
| 320 | - public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
| 321 | - { |
|
| 322 | - unset($conditions['page']); |
|
| 323 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 324 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 325 | - $model = $this->model->with($relations); |
|
| 326 | - |
|
| 327 | - $model->whereHas('groups', function ($q) use ($groupName) { |
|
| 328 | - $q->where('name', $groupName); |
|
| 329 | - }); |
|
| 185 | + return $user; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Unblock the user. |
|
| 190 | + * |
|
| 191 | + * @param integer $userId |
|
| 192 | + * @return object |
|
| 193 | + */ |
|
| 194 | + public function unblock($userId) |
|
| 195 | + { |
|
| 196 | + if (! $this->hasGroup(['Admin'])) { |
|
| 197 | + \ErrorHandler::noPermissions(); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + $user = $this->find($userId); |
|
| 201 | + $user->blocked = 0; |
|
| 202 | + $user->save(); |
|
| 203 | + |
|
| 204 | + return $user; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Send a reset link to the given user. |
|
| 209 | + * |
|
| 210 | + * @param string $email |
|
| 211 | + * @return void |
|
| 212 | + */ |
|
| 213 | + public function sendReset($email) |
|
| 214 | + { |
|
| 215 | + if (! $user = $this->model->where('email', $email)->first()) { |
|
| 216 | + \ErrorHandler::notFound('email'); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $token = \Password::getRepository()->create($user); |
|
| 220 | + \Core::notifications()->notify($user, 'ResetPassword', $token); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Reset the given user's password. |
|
| 225 | + * |
|
| 226 | + * @param array $credentials |
|
| 227 | + * @return string|null |
|
| 228 | + */ |
|
| 229 | + public function resetPassword($credentials) |
|
| 230 | + { |
|
| 231 | + $response = \Password::reset($credentials, function ($user, $password) { |
|
| 232 | + $user->password = $password; |
|
| 233 | + $user->save(); |
|
| 234 | + }); |
|
| 235 | + |
|
| 236 | + switch ($response) { |
|
| 237 | + case \Password::PASSWORD_RESET: |
|
| 238 | + return 'success'; |
|
| 239 | + |
|
| 240 | + case \Password::INVALID_TOKEN: |
|
| 241 | + \ErrorHandler::invalidResetToken('token'); |
|
| 242 | + //no break |
|
| 243 | + |
|
| 244 | + case \Password::INVALID_PASSWORD: |
|
| 245 | + \ErrorHandler::invalidResetPassword('email'); |
|
| 246 | + //no break |
|
| 247 | + |
|
| 248 | + case \Password::INVALID_USER: |
|
| 249 | + \ErrorHandler::notFound('user'); |
|
| 250 | + //no break |
|
| 251 | + |
|
| 252 | + default: |
|
| 253 | + \ErrorHandler::generalError(); |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Change the logged in user password. |
|
| 259 | + * |
|
| 260 | + * @param array $credentials |
|
| 261 | + * @return void |
|
| 262 | + */ |
|
| 263 | + public function changePassword($credentials) |
|
| 264 | + { |
|
| 265 | + $user = \Auth::user(); |
|
| 266 | + if (! \Hash::check($credentials['old_password'], $user->password)) { |
|
| 267 | + \ErrorHandler::invalidOldPassword(); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + $user->password = $credentials['password']; |
|
| 271 | + $user->save(); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Confirm email using the confirmation code. |
|
| 276 | + * |
|
| 277 | + * @param string $confirmationCode |
|
| 278 | + * @return void |
|
| 279 | + */ |
|
| 280 | + public function confirmEmail($confirmationCode) |
|
| 281 | + { |
|
| 282 | + if (! $user = $this->first(['confirmation_code' => $confirmationCode])) { |
|
| 283 | + \ErrorHandler::invalidConfirmationCode(); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + $user->confirmed = 1; |
|
| 287 | + $user->confirmation_code = null; |
|
| 288 | + $user->save(); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Send the confirmation mail. |
|
| 293 | + * |
|
| 294 | + * @param string $email |
|
| 295 | + * @return void |
|
| 296 | + */ |
|
| 297 | + public function sendConfirmationEmail($email) |
|
| 298 | + { |
|
| 299 | + $user = $this->first(['email' => $email]); |
|
| 300 | + if ($user->confirmed) { |
|
| 301 | + \ErrorHandler::emailAlreadyConfirmed(); |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + $user->confirmed = 0; |
|
| 305 | + $user->confirmation_code = sha1(microtime()); |
|
| 306 | + $user->save(); |
|
| 307 | + \Core::notifications()->notify($user, 'ConfirmEmail'); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Paginate all users in the given group based on the given conditions. |
|
| 312 | + * |
|
| 313 | + * @param string $groupName |
|
| 314 | + * @param array $relations |
|
| 315 | + * @param integer $perPage |
|
| 316 | + * @param string $sortBy |
|
| 317 | + * @param boolean $desc |
|
| 318 | + * @return \Illuminate\Http\Response |
|
| 319 | + */ |
|
| 320 | + public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
| 321 | + { |
|
| 322 | + unset($conditions['page']); |
|
| 323 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 324 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 325 | + $model = $this->model->with($relations); |
|
| 326 | + |
|
| 327 | + $model->whereHas('groups', function ($q) use ($groupName) { |
|
| 328 | + $q->where('name', $groupName); |
|
| 329 | + }); |
|
| 330 | 330 | |
| 331 | 331 | |
| 332 | - if (count($conditions['conditionValues'])) { |
|
| 333 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - if ($perPage) { |
|
| 337 | - return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - return $model->orderBy($sortBy, $sort)->get(); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Save the given data to the logged in user. |
|
| 345 | - * |
|
| 346 | - * @param array $data |
|
| 347 | - * @return void |
|
| 348 | - */ |
|
| 349 | - public function saveProfile($data) |
|
| 350 | - { |
|
| 351 | - if (Arr::has($data, 'profile_picture')) { |
|
| 352 | - $data['profile_picture'] = \Media::uploadImageBas64($data['profile_picture'], 'admins/profile_pictures'); |
|
| 353 | - } |
|
| 332 | + if (count($conditions['conditionValues'])) { |
|
| 333 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + if ($perPage) { |
|
| 337 | + return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + return $model->orderBy($sortBy, $sort)->get(); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Save the given data to the logged in user. |
|
| 345 | + * |
|
| 346 | + * @param array $data |
|
| 347 | + * @return void |
|
| 348 | + */ |
|
| 349 | + public function saveProfile($data) |
|
| 350 | + { |
|
| 351 | + if (Arr::has($data, 'profile_picture')) { |
|
| 352 | + $data['profile_picture'] = \Media::uploadImageBas64($data['profile_picture'], 'admins/profile_pictures'); |
|
| 353 | + } |
|
| 354 | 354 | |
| 355 | - $data['id'] = \Auth::id(); |
|
| 356 | - return $this->save($data); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Ensure access token hasn't expired or revoked. |
|
| 361 | - * |
|
| 362 | - * @param string $accessToken |
|
| 363 | - * @return boolean |
|
| 364 | - */ |
|
| 365 | - public function accessTokenExpiredOrRevoked($accessToken) |
|
| 366 | - { |
|
| 367 | - $accessTokenId = json_decode($accessToken, true)['id']; |
|
| 368 | - $accessToken = \DB::table('oauth_access_tokens') |
|
| 369 | - ->where('id', $accessTokenId) |
|
| 370 | - ->first(); |
|
| 355 | + $data['id'] = \Auth::id(); |
|
| 356 | + return $this->save($data); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Ensure access token hasn't expired or revoked. |
|
| 361 | + * |
|
| 362 | + * @param string $accessToken |
|
| 363 | + * @return boolean |
|
| 364 | + */ |
|
| 365 | + public function accessTokenExpiredOrRevoked($accessToken) |
|
| 366 | + { |
|
| 367 | + $accessTokenId = json_decode($accessToken, true)['id']; |
|
| 368 | + $accessToken = \DB::table('oauth_access_tokens') |
|
| 369 | + ->where('id', $accessTokenId) |
|
| 370 | + ->first(); |
|
| 371 | 371 | |
| 372 | - if (\Carbon\Carbon::parse($accessToken->expires_at)->isPast() || $accessToken->revoked) { |
|
| 373 | - return true; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - return false; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * Revoke the given access token and all |
|
| 381 | - * associated refresh tokens. |
|
| 382 | - * |
|
| 383 | - * @param oject $accessToken |
|
| 384 | - * @return void |
|
| 385 | - */ |
|
| 386 | - public function revokeAccessToken($accessToken) |
|
| 387 | - { |
|
| 388 | - \DB::table('oauth_refresh_tokens') |
|
| 389 | - ->where('access_token_id', $accessToken->id) |
|
| 390 | - ->update([ |
|
| 391 | - 'revoked' => true |
|
| 392 | - ]); |
|
| 393 | - |
|
| 394 | - $accessToken->revoke(); |
|
| 395 | - } |
|
| 372 | + if (\Carbon\Carbon::parse($accessToken->expires_at)->isPast() || $accessToken->revoked) { |
|
| 373 | + return true; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + return false; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * Revoke the given access token and all |
|
| 381 | + * associated refresh tokens. |
|
| 382 | + * |
|
| 383 | + * @param oject $accessToken |
|
| 384 | + * @return void |
|
| 385 | + */ |
|
| 386 | + public function revokeAccessToken($accessToken) |
|
| 387 | + { |
|
| 388 | + \DB::table('oauth_refresh_tokens') |
|
| 389 | + ->where('access_token_id', $accessToken->id) |
|
| 390 | + ->update([ |
|
| 391 | + 'revoked' => true |
|
| 392 | + ]); |
|
| 393 | + |
|
| 394 | + $accessToken->revoke(); |
|
| 395 | + } |
|
| 396 | 396 | } |
@@ -8,60 +8,60 @@ |
||
| 8 | 8 | |
| 9 | 9 | class PushNotificationDeviceRepository extends BaseRepository |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Init new object. |
|
| 13 | - * |
|
| 14 | - * @param PushNotificationDevice $model |
|
| 15 | - * @return void |
|
| 16 | - */ |
|
| 17 | - public function __construct(PushNotificationDevice $model) |
|
| 18 | - { |
|
| 19 | - parent::__construct($model); |
|
| 20 | - } |
|
| 11 | + /** |
|
| 12 | + * Init new object. |
|
| 13 | + * |
|
| 14 | + * @param PushNotificationDevice $model |
|
| 15 | + * @return void |
|
| 16 | + */ |
|
| 17 | + public function __construct(PushNotificationDevice $model) |
|
| 18 | + { |
|
| 19 | + parent::__construct($model); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Register the given device to the logged in user. |
|
| 24 | - * |
|
| 25 | - * @param array $data |
|
| 26 | - * @return void |
|
| 27 | - */ |
|
| 28 | - public function registerDevice($data) |
|
| 29 | - { |
|
| 30 | - $data['access_token'] = \Auth::user()->token(); |
|
| 31 | - $data['user_id'] = \Auth::id(); |
|
| 32 | - $device = $this->model->where('device_token', $data['device_token'])-> |
|
| 33 | - where('user_id', $data['user_id'])-> |
|
| 34 | - first(); |
|
| 22 | + /** |
|
| 23 | + * Register the given device to the logged in user. |
|
| 24 | + * |
|
| 25 | + * @param array $data |
|
| 26 | + * @return void |
|
| 27 | + */ |
|
| 28 | + public function registerDevice($data) |
|
| 29 | + { |
|
| 30 | + $data['access_token'] = \Auth::user()->token(); |
|
| 31 | + $data['user_id'] = \Auth::id(); |
|
| 32 | + $device = $this->model->where('device_token', $data['device_token'])-> |
|
| 33 | + where('user_id', $data['user_id'])-> |
|
| 34 | + first(); |
|
| 35 | 35 | |
| 36 | - if ($device) { |
|
| 37 | - $data['id'] = $device->id; |
|
| 38 | - } |
|
| 36 | + if ($device) { |
|
| 37 | + $data['id'] = $device->id; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - return $this->save($data); |
|
| 41 | - } |
|
| 40 | + return $this->save($data); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Generate the given message data. |
|
| 45 | - * |
|
| 46 | - * @param string $title |
|
| 47 | - * @param string $message |
|
| 48 | - * @param array $data |
|
| 49 | - * @return void |
|
| 50 | - */ |
|
| 51 | - public function generateMessageData($title, $message, $data = []) |
|
| 52 | - { |
|
| 53 | - $optionBuilder = new OptionsBuilder(); |
|
| 54 | - $notificationBuilder = new PayloadNotificationBuilder($title); |
|
| 55 | - $dataBuilder = new PayloadDataBuilder(); |
|
| 43 | + /** |
|
| 44 | + * Generate the given message data. |
|
| 45 | + * |
|
| 46 | + * @param string $title |
|
| 47 | + * @param string $message |
|
| 48 | + * @param array $data |
|
| 49 | + * @return void |
|
| 50 | + */ |
|
| 51 | + public function generateMessageData($title, $message, $data = []) |
|
| 52 | + { |
|
| 53 | + $optionBuilder = new OptionsBuilder(); |
|
| 54 | + $notificationBuilder = new PayloadNotificationBuilder($title); |
|
| 55 | + $dataBuilder = new PayloadDataBuilder(); |
|
| 56 | 56 | |
| 57 | - $optionBuilder->setTimeToLive(60 * 20); |
|
| 58 | - $notificationBuilder->setBody($message); |
|
| 59 | - $dataBuilder->addData($data); |
|
| 57 | + $optionBuilder->setTimeToLive(60 * 20); |
|
| 58 | + $notificationBuilder->setBody($message); |
|
| 59 | + $dataBuilder->addData($data); |
|
| 60 | 60 | |
| 61 | - $options = $optionBuilder->build(); |
|
| 62 | - $notification = $notificationBuilder->build(); |
|
| 63 | - $data = $dataBuilder->build(); |
|
| 61 | + $options = $optionBuilder->build(); |
|
| 62 | + $notification = $notificationBuilder->build(); |
|
| 63 | + $data = $dataBuilder->build(); |
|
| 64 | 64 | |
| 65 | - return compact('options', 'notification', 'data'); |
|
| 66 | - } |
|
| 65 | + return compact('options', 'notification', 'data'); |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -7,255 +7,255 @@ |
||
| 7 | 7 | |
| 8 | 8 | class GenerateDoc extends Command |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * The name and signature of the console command. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - protected $signature = 'doc:generate'; |
|
| 10 | + /** |
|
| 11 | + * The name and signature of the console command. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + protected $signature = 'doc:generate'; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The console command description. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $description = 'Generate api documentation'; |
|
| 17 | + /** |
|
| 18 | + * The console command description. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $description = 'Generate api documentation'; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Init new object. |
|
| 26 | - * |
|
| 27 | - * @return void |
|
| 28 | - */ |
|
| 29 | - public function __construct() |
|
| 30 | - { |
|
| 31 | - parent::__construct(); |
|
| 32 | - } |
|
| 24 | + /** |
|
| 25 | + * Init new object. |
|
| 26 | + * |
|
| 27 | + * @return void |
|
| 28 | + */ |
|
| 29 | + public function __construct() |
|
| 30 | + { |
|
| 31 | + parent::__construct(); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Execute the console command. |
|
| 36 | - * |
|
| 37 | - * @return mixed |
|
| 38 | - */ |
|
| 39 | - public function handle() |
|
| 40 | - { |
|
| 41 | - $docData = []; |
|
| 42 | - $docData['models'] = []; |
|
| 43 | - $routes = $this->getRoutes(); |
|
| 44 | - foreach ($routes as $route) { |
|
| 45 | - if ($route) { |
|
| 46 | - $actoinArray = explode('@', $route['action']); |
|
| 47 | - if (Arr::get($actoinArray, 1, false)) { |
|
| 48 | - $controller = $actoinArray[0]; |
|
| 49 | - $method = $actoinArray[1]; |
|
| 50 | - $route['name'] = $method !== 'index' ? $method : 'list'; |
|
| 34 | + /** |
|
| 35 | + * Execute the console command. |
|
| 36 | + * |
|
| 37 | + * @return mixed |
|
| 38 | + */ |
|
| 39 | + public function handle() |
|
| 40 | + { |
|
| 41 | + $docData = []; |
|
| 42 | + $docData['models'] = []; |
|
| 43 | + $routes = $this->getRoutes(); |
|
| 44 | + foreach ($routes as $route) { |
|
| 45 | + if ($route) { |
|
| 46 | + $actoinArray = explode('@', $route['action']); |
|
| 47 | + if (Arr::get($actoinArray, 1, false)) { |
|
| 48 | + $controller = $actoinArray[0]; |
|
| 49 | + $method = $actoinArray[1]; |
|
| 50 | + $route['name'] = $method !== 'index' ? $method : 'list'; |
|
| 51 | 51 | |
| 52 | - $reflectionClass = new \ReflectionClass($controller); |
|
| 53 | - $reflectionMethod = $reflectionClass->getMethod($method); |
|
| 54 | - $classProperties = $reflectionClass->getDefaultProperties(); |
|
| 55 | - $skipLoginCheck = Arr::get($classProperties, 'skipLoginCheck', false); |
|
| 56 | - $validationRules = Arr::get($classProperties, 'validationRules', false); |
|
| 52 | + $reflectionClass = new \ReflectionClass($controller); |
|
| 53 | + $reflectionMethod = $reflectionClass->getMethod($method); |
|
| 54 | + $classProperties = $reflectionClass->getDefaultProperties(); |
|
| 55 | + $skipLoginCheck = Arr::get($classProperties, 'skipLoginCheck', false); |
|
| 56 | + $validationRules = Arr::get($classProperties, 'validationRules', false); |
|
| 57 | 57 | |
| 58 | - dd($classProperties); |
|
| 59 | - $this->processDocBlock($route, $reflectionMethod); |
|
| 60 | - $this->getHeaders($route, $method, $skipLoginCheck); |
|
| 61 | - $this->getPostData($route, $reflectionMethod, $validationRules); |
|
| 58 | + dd($classProperties); |
|
| 59 | + $this->processDocBlock($route, $reflectionMethod); |
|
| 60 | + $this->getHeaders($route, $method, $skipLoginCheck); |
|
| 61 | + $this->getPostData($route, $reflectionMethod, $validationRules); |
|
| 62 | 62 | |
| 63 | - $route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']); |
|
| 63 | + $route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']); |
|
| 64 | 64 | |
| 65 | - preg_match('/api\/([^#]+)\//iU', $route['uri'], $module); |
|
| 66 | - $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/'.$module[1].'/') - 1)][] = $route; |
|
| 65 | + preg_match('/api\/([^#]+)\//iU', $route['uri'], $module); |
|
| 66 | + $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/'.$module[1].'/') - 1)][] = $route; |
|
| 67 | 67 | |
| 68 | - $this->getModels($classProperties['model'], $docData); |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - } |
|
| 68 | + $this->getModels($classProperties['model'], $docData); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - $docData['errors'] = $this->getErrors(); |
|
| 74 | - $docData['reports'] = \Core::reports()->all(); |
|
| 75 | - \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData)); |
|
| 76 | - } |
|
| 73 | + $docData['errors'] = $this->getErrors(); |
|
| 74 | + $docData['reports'] = \Core::reports()->all(); |
|
| 75 | + \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData)); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Get list of all registered routes. |
|
| 80 | - * |
|
| 81 | - * @return collection |
|
| 82 | - */ |
|
| 83 | - protected function getRoutes() |
|
| 84 | - { |
|
| 85 | - return collect(\Route::getRoutes())->map(function ($route) { |
|
| 86 | - if (strpos($route->uri(), 'api/') !== false) { |
|
| 87 | - return [ |
|
| 88 | - 'method' => $route->methods()[0], |
|
| 89 | - 'uri' => $route->uri(), |
|
| 90 | - 'action' => $route->getActionName(), |
|
| 91 | - 'prefix' => $route->getPrefix() |
|
| 92 | - ]; |
|
| 93 | - } |
|
| 94 | - return false; |
|
| 95 | - })->all(); |
|
| 96 | - } |
|
| 78 | + /** |
|
| 79 | + * Get list of all registered routes. |
|
| 80 | + * |
|
| 81 | + * @return collection |
|
| 82 | + */ |
|
| 83 | + protected function getRoutes() |
|
| 84 | + { |
|
| 85 | + return collect(\Route::getRoutes())->map(function ($route) { |
|
| 86 | + if (strpos($route->uri(), 'api/') !== false) { |
|
| 87 | + return [ |
|
| 88 | + 'method' => $route->methods()[0], |
|
| 89 | + 'uri' => $route->uri(), |
|
| 90 | + 'action' => $route->getActionName(), |
|
| 91 | + 'prefix' => $route->getPrefix() |
|
| 92 | + ]; |
|
| 93 | + } |
|
| 94 | + return false; |
|
| 95 | + })->all(); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Generate headers for the given route. |
|
| 100 | - * |
|
| 101 | - * @param array &$route |
|
| 102 | - * @param string $method |
|
| 103 | - * @param array $skipLoginCheck |
|
| 104 | - * @return void |
|
| 105 | - */ |
|
| 106 | - protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
| 107 | - { |
|
| 108 | - $route['headers'] = [ |
|
| 109 | - 'Accept' => 'application/json', |
|
| 110 | - 'Content-Type' => 'application/json', |
|
| 111 | - 'locale' => 'The language of the returned data: ar, en or all.', |
|
| 112 | - 'time-zone' => 'Your locale time zone', |
|
| 113 | - ]; |
|
| 98 | + /** |
|
| 99 | + * Generate headers for the given route. |
|
| 100 | + * |
|
| 101 | + * @param array &$route |
|
| 102 | + * @param string $method |
|
| 103 | + * @param array $skipLoginCheck |
|
| 104 | + * @return void |
|
| 105 | + */ |
|
| 106 | + protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
| 107 | + { |
|
| 108 | + $route['headers'] = [ |
|
| 109 | + 'Accept' => 'application/json', |
|
| 110 | + 'Content-Type' => 'application/json', |
|
| 111 | + 'locale' => 'The language of the returned data: ar, en or all.', |
|
| 112 | + 'time-zone' => 'Your locale time zone', |
|
| 113 | + ]; |
|
| 114 | 114 | |
| 115 | 115 | |
| 116 | - if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) { |
|
| 117 | - $route['headers']['Authorization'] = 'Bearer {token}'; |
|
| 118 | - } |
|
| 119 | - } |
|
| 116 | + if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) { |
|
| 117 | + $route['headers']['Authorization'] = 'Bearer {token}'; |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * Generate description and params for the given route |
|
| 123 | - * based on the docblock. |
|
| 124 | - * |
|
| 125 | - * @param array &$route |
|
| 126 | - * @param \ReflectionMethod $reflectionMethod |
|
| 127 | - * @return void |
|
| 128 | - */ |
|
| 129 | - protected function processDocBlock(&$route, $reflectionMethod) |
|
| 130 | - { |
|
| 131 | - $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
| 132 | - $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
| 133 | - $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
| 134 | - $params = $docblock->getTagsByName('param'); |
|
| 135 | - $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
| 136 | - foreach ($params as $param) { |
|
| 137 | - $name = $param->getVariableName(); |
|
| 138 | - if ($name !== 'request') { |
|
| 139 | - $route['parametars'][$param->getVariableName()] = $param->getDescription()->render(); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - } |
|
| 121 | + /** |
|
| 122 | + * Generate description and params for the given route |
|
| 123 | + * based on the docblock. |
|
| 124 | + * |
|
| 125 | + * @param array &$route |
|
| 126 | + * @param \ReflectionMethod $reflectionMethod |
|
| 127 | + * @return void |
|
| 128 | + */ |
|
| 129 | + protected function processDocBlock(&$route, $reflectionMethod) |
|
| 130 | + { |
|
| 131 | + $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
| 132 | + $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
| 133 | + $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
| 134 | + $params = $docblock->getTagsByName('param'); |
|
| 135 | + $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
| 136 | + foreach ($params as $param) { |
|
| 137 | + $name = $param->getVariableName(); |
|
| 138 | + if ($name !== 'request') { |
|
| 139 | + $route['parametars'][$param->getVariableName()] = $param->getDescription()->render(); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * Generate post body for the given route. |
|
| 146 | - * |
|
| 147 | - * @param array &$route |
|
| 148 | - * @param \ReflectionMethod $reflectionMethod |
|
| 149 | - * @param array $validationRules |
|
| 150 | - * @return void |
|
| 151 | - */ |
|
| 152 | - protected function getPostData(&$route, $reflectionMethod, $validationRules) |
|
| 153 | - { |
|
| 154 | - if ($route['method'] == 'POST') { |
|
| 155 | - $body = $this->getMethodBody($reflectionMethod); |
|
| 144 | + /** |
|
| 145 | + * Generate post body for the given route. |
|
| 146 | + * |
|
| 147 | + * @param array &$route |
|
| 148 | + * @param \ReflectionMethod $reflectionMethod |
|
| 149 | + * @param array $validationRules |
|
| 150 | + * @return void |
|
| 151 | + */ |
|
| 152 | + protected function getPostData(&$route, $reflectionMethod, $validationRules) |
|
| 153 | + { |
|
| 154 | + if ($route['method'] == 'POST') { |
|
| 155 | + $body = $this->getMethodBody($reflectionMethod); |
|
| 156 | 156 | |
| 157 | - preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match); |
|
| 158 | - if (count($match)) { |
|
| 159 | - if ($match[1] == '$this->validationRules') { |
|
| 160 | - $route['body'] = $validationRules; |
|
| 161 | - } else { |
|
| 162 | - $route['body'] = eval('return '.str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]).';'); |
|
| 163 | - } |
|
| 157 | + preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match); |
|
| 158 | + if (count($match)) { |
|
| 159 | + if ($match[1] == '$this->validationRules') { |
|
| 160 | + $route['body'] = $validationRules; |
|
| 161 | + } else { |
|
| 162 | + $route['body'] = eval('return '.str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]).';'); |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - foreach ($route['body'] as &$rule) { |
|
| 166 | - if (strpos($rule, 'unique')) { |
|
| 167 | - $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
| 168 | - } elseif (strpos($rule, 'exists')) { |
|
| 169 | - $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - } else { |
|
| 173 | - $route['body'] = 'conditions'; |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - } |
|
| 165 | + foreach ($route['body'] as &$rule) { |
|
| 166 | + if (strpos($rule, 'unique')) { |
|
| 167 | + $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
| 168 | + } elseif (strpos($rule, 'exists')) { |
|
| 169 | + $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + } else { |
|
| 173 | + $route['body'] = 'conditions'; |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * Generate application errors. |
|
| 180 | - * |
|
| 181 | - * @return array |
|
| 182 | - */ |
|
| 183 | - protected function getErrors() |
|
| 184 | - { |
|
| 185 | - $errors = []; |
|
| 186 | - $reflectionClass = new \ReflectionClass('App\Modules\Core\Utl\ErrorHandler'); |
|
| 187 | - foreach ($reflectionClass->getMethods() as $method) { |
|
| 188 | - $methodName = $method->name; |
|
| 189 | - $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
| 190 | - $body = $this->getMethodBody($reflectionMethod); |
|
| 178 | + /** |
|
| 179 | + * Generate application errors. |
|
| 180 | + * |
|
| 181 | + * @return array |
|
| 182 | + */ |
|
| 183 | + protected function getErrors() |
|
| 184 | + { |
|
| 185 | + $errors = []; |
|
| 186 | + $reflectionClass = new \ReflectionClass('App\Modules\Core\Utl\ErrorHandler'); |
|
| 187 | + foreach ($reflectionClass->getMethods() as $method) { |
|
| 188 | + $methodName = $method->name; |
|
| 189 | + $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
| 190 | + $body = $this->getMethodBody($reflectionMethod); |
|
| 191 | 191 | |
| 192 | - preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match); |
|
| 192 | + preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match); |
|
| 193 | 193 | |
| 194 | - if (count($match)) { |
|
| 195 | - $errors[$match[1]][] = $methodName; |
|
| 196 | - } |
|
| 197 | - } |
|
| 194 | + if (count($match)) { |
|
| 195 | + $errors[$match[1]][] = $methodName; |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - return $errors; |
|
| 200 | - } |
|
| 199 | + return $errors; |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | - /** |
|
| 203 | - * Get the given method body code. |
|
| 204 | - * |
|
| 205 | - * @param object $reflectionMethod |
|
| 206 | - * @return string |
|
| 207 | - */ |
|
| 208 | - protected function getMethodBody($reflectionMethod) |
|
| 209 | - { |
|
| 210 | - $filename = $reflectionMethod->getFileName(); |
|
| 211 | - $start_line = $reflectionMethod->getStartLine() - 1; |
|
| 212 | - $end_line = $reflectionMethod->getEndLine(); |
|
| 213 | - $length = $end_line - $start_line; |
|
| 214 | - $source = file($filename); |
|
| 215 | - $body = implode("", array_slice($source, $start_line, $length)); |
|
| 216 | - $body = trim(preg_replace('/\s+/', '', $body)); |
|
| 202 | + /** |
|
| 203 | + * Get the given method body code. |
|
| 204 | + * |
|
| 205 | + * @param object $reflectionMethod |
|
| 206 | + * @return string |
|
| 207 | + */ |
|
| 208 | + protected function getMethodBody($reflectionMethod) |
|
| 209 | + { |
|
| 210 | + $filename = $reflectionMethod->getFileName(); |
|
| 211 | + $start_line = $reflectionMethod->getStartLine() - 1; |
|
| 212 | + $end_line = $reflectionMethod->getEndLine(); |
|
| 213 | + $length = $end_line - $start_line; |
|
| 214 | + $source = file($filename); |
|
| 215 | + $body = implode("", array_slice($source, $start_line, $length)); |
|
| 216 | + $body = trim(preg_replace('/\s+/', '', $body)); |
|
| 217 | 217 | |
| 218 | - return $body; |
|
| 219 | - } |
|
| 218 | + return $body; |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | - /** |
|
| 222 | - * Get example object of all availble models. |
|
| 223 | - * |
|
| 224 | - * @param string $modelName |
|
| 225 | - * @param array $docData |
|
| 226 | - * @return string |
|
| 227 | - */ |
|
| 228 | - protected function getModels($modelName, &$docData) |
|
| 229 | - { |
|
| 230 | - if ($modelName && ! Arr::has($docData['models'], $modelName)) { |
|
| 231 | - $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass; |
|
| 232 | - $model = factory($modelClass)->make(); |
|
| 233 | - $modelArr = $model->toArray(); |
|
| 221 | + /** |
|
| 222 | + * Get example object of all availble models. |
|
| 223 | + * |
|
| 224 | + * @param string $modelName |
|
| 225 | + * @param array $docData |
|
| 226 | + * @return string |
|
| 227 | + */ |
|
| 228 | + protected function getModels($modelName, &$docData) |
|
| 229 | + { |
|
| 230 | + if ($modelName && ! Arr::has($docData['models'], $modelName)) { |
|
| 231 | + $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass; |
|
| 232 | + $model = factory($modelClass)->make(); |
|
| 233 | + $modelArr = $model->toArray(); |
|
| 234 | 234 | |
| 235 | - if ($model->trans && ! $model->trans->count()) { |
|
| 236 | - $modelArr['trans'] = [ |
|
| 237 | - 'en' => factory($modelClass.'Translation')->make()->toArray() |
|
| 238 | - ]; |
|
| 239 | - } |
|
| 235 | + if ($model->trans && ! $model->trans->count()) { |
|
| 236 | + $modelArr['trans'] = [ |
|
| 237 | + 'en' => factory($modelClass.'Translation')->make()->toArray() |
|
| 238 | + ]; |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
| 242 | - } |
|
| 243 | - } |
|
| 241 | + $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - /** |
|
| 246 | - * Get the route response object type. |
|
| 247 | - * |
|
| 248 | - * @param string $modelName |
|
| 249 | - * @param string $method |
|
| 250 | - * @param string $returnDocBlock |
|
| 251 | - * @return array |
|
| 252 | - */ |
|
| 253 | - protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
| 254 | - { |
|
| 255 | - $config = \CoreConfig::getConfig(); |
|
| 256 | - $relations = Arr::has($config['relations'], $modelName) ? Arr::has($config['relations'][$modelName], $method) ? $config['relations'][$modelName] : false : false; |
|
| 257 | - $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
| 245 | + /** |
|
| 246 | + * Get the route response object type. |
|
| 247 | + * |
|
| 248 | + * @param string $modelName |
|
| 249 | + * @param string $method |
|
| 250 | + * @param string $returnDocBlock |
|
| 251 | + * @return array |
|
| 252 | + */ |
|
| 253 | + protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
| 254 | + { |
|
| 255 | + $config = \CoreConfig::getConfig(); |
|
| 256 | + $relations = Arr::has($config['relations'], $modelName) ? Arr::has($config['relations'][$modelName], $method) ? $config['relations'][$modelName] : false : false; |
|
| 257 | + $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
| 258 | 258 | |
| 259 | - return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
| 260 | - } |
|
| 259 | + return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
| 260 | + } |
|
| 261 | 261 | } |