Codexshaper /
laravel-database-manager
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace CodexShaper\DBM; |
||||||
| 4 | |||||||
| 5 | use CodexShaper\DBM\Facades\Driver; |
||||||
| 6 | use CodexShaper\DBM\Models\DBM_Field; |
||||||
| 7 | use CodexShaper\DBM\Models\DBM_Menu; |
||||||
| 8 | use CodexShaper\DBM\Models\DBM_MenuItem; |
||||||
| 9 | use CodexShaper\DBM\Models\DBM_MongoField; |
||||||
| 10 | use CodexShaper\DBM\Models\DBM_MongoMenu; |
||||||
| 11 | use CodexShaper\DBM\Models\DBM_MongoMenuItem; |
||||||
| 12 | use CodexShaper\DBM\Models\DBM_MongoObject; |
||||||
| 13 | use CodexShaper\DBM\Models\DBM_MongoPermission; |
||||||
| 14 | use CodexShaper\DBM\Models\DBM_MongoTemplate; |
||||||
| 15 | use CodexShaper\DBM\Models\DBM_Object; |
||||||
| 16 | use CodexShaper\DBM\Models\DBM_Permission; |
||||||
| 17 | use CodexShaper\DBM\Models\DBM_Template; |
||||||
| 18 | use Illuminate\Filesystem\Filesystem; |
||||||
| 19 | use Illuminate\Support\Facades\App; |
||||||
| 20 | use Illuminate\Support\Facades\Artisan; |
||||||
| 21 | use Illuminate\Support\Facades\Auth; |
||||||
| 22 | use Illuminate\Support\Facades\File; |
||||||
| 23 | use Illuminate\Support\Facades\Response; |
||||||
| 24 | use Illuminate\Support\Facades\Route; |
||||||
| 25 | use Illuminate\Support\Facades\Storage; |
||||||
| 26 | use Illuminate\Support\Str; |
||||||
| 27 | |||||||
| 28 | class Manager |
||||||
| 29 | { |
||||||
| 30 | /** |
||||||
| 31 | * Include Web routes. |
||||||
| 32 | * |
||||||
| 33 | * @return void |
||||||
| 34 | */ |
||||||
| 35 | public function webRoutes() |
||||||
| 36 | { |
||||||
| 37 | require __DIR__.'/../routes/web.php'; |
||||||
| 38 | } |
||||||
| 39 | |||||||
| 40 | /** |
||||||
| 41 | * Include API routes. |
||||||
| 42 | * |
||||||
| 43 | * @return void |
||||||
| 44 | */ |
||||||
| 45 | public function apiRoutes() |
||||||
| 46 | { |
||||||
| 47 | require __DIR__.'/../routes/api.php'; |
||||||
| 48 | } |
||||||
| 49 | |||||||
| 50 | /** |
||||||
| 51 | * Load assests. |
||||||
| 52 | * |
||||||
| 53 | * @param string $path |
||||||
| 54 | * |
||||||
| 55 | * @return \Illuminate\Http\Response |
||||||
| 56 | */ |
||||||
| 57 | public function assets($path) |
||||||
| 58 | { |
||||||
| 59 | $file = base_path(trim(config('dbm.resources_path'), '/').'/'.urldecode($path)); |
||||||
| 60 | |||||||
| 61 | if (File::exists($file)) { |
||||||
| 62 | switch ($extension = pathinfo($file, PATHINFO_EXTENSION)) { |
||||||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||||||
| 63 | case 'js': |
||||||
| 64 | $mimeType = 'text/javascript'; |
||||||
| 65 | break; |
||||||
| 66 | case 'css': |
||||||
| 67 | $mimeType = 'text/css'; |
||||||
| 68 | break; |
||||||
| 69 | default: |
||||||
| 70 | $mimeType = File::mimeType($file); |
||||||
| 71 | break; |
||||||
| 72 | } |
||||||
| 73 | |||||||
| 74 | if (! $mimeType) { |
||||||
| 75 | $mimeType = 'text/plain'; |
||||||
| 76 | } |
||||||
| 77 | |||||||
| 78 | $response = Response::make(File::get($file), 200); |
||||||
| 79 | $response->header('Content-Type', $mimeType); |
||||||
| 80 | $response->setSharedMaxAge(31536000); |
||||||
| 81 | $response->setMaxAge(31536000); |
||||||
| 82 | $response->setExpires(new \DateTime('+1 year')); |
||||||
| 83 | |||||||
| 84 | return $response; |
||||||
| 85 | } |
||||||
| 86 | |||||||
| 87 | return response('', 404); |
||||||
| 88 | } |
||||||
| 89 | |||||||
| 90 | /** |
||||||
| 91 | * Get Model Namespace. |
||||||
| 92 | * |
||||||
| 93 | * @return string |
||||||
| 94 | */ |
||||||
| 95 | public function getModelNamespace() |
||||||
| 96 | { |
||||||
| 97 | return trim(config('dbm.modal_namespace', App::getNamespace()), '\\'); |
||||||
| 98 | } |
||||||
| 99 | |||||||
| 100 | /** |
||||||
| 101 | * Get model name with namespace. |
||||||
| 102 | * |
||||||
| 103 | * @param string $className |
||||||
| 104 | * |
||||||
| 105 | * @return string |
||||||
| 106 | */ |
||||||
| 107 | public function generateModelName($className) |
||||||
| 108 | { |
||||||
| 109 | return static::getModelNamespace().'\\'.ucfirst(Str::singular($className)); |
||||||
|
0 ignored issues
–
show
The method
CodexShaper\DBM\Manager::getModelNamespace() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 110 | } |
||||||
| 111 | |||||||
| 112 | /** |
||||||
| 113 | * Make new model. |
||||||
| 114 | * |
||||||
| 115 | * @param string $model |
||||||
| 116 | * @param string $table |
||||||
| 117 | * |
||||||
| 118 | * @return void |
||||||
| 119 | */ |
||||||
| 120 | public function makeModel($model, $table) |
||||||
| 121 | { |
||||||
| 122 | try { |
||||||
| 123 | $partials = explode('\\', $model); |
||||||
| 124 | $className = array_pop($partials); |
||||||
| 125 | $namespace = implode('\\', $partials); |
||||||
| 126 | |||||||
| 127 | $app = array_shift($partials); |
||||||
| 128 | $directory = implode(DIRECTORY_SEPARATOR, $partials); |
||||||
| 129 | if (strtolower($app) != 'app') { |
||||||
| 130 | $namespace = 'App\\'.$namespace; |
||||||
| 131 | $directory = $app.DIRECTORY_SEPARATOR.$directory; |
||||||
| 132 | } |
||||||
| 133 | |||||||
| 134 | $path = app_path().DIRECTORY_SEPARATOR.$directory; |
||||||
| 135 | |||||||
| 136 | if (! File::isDirectory($path)) { |
||||||
| 137 | File::makeDirectory($path, 0777, true, true); |
||||||
| 138 | } |
||||||
| 139 | |||||||
| 140 | $contents = "<?php\n\n"; |
||||||
| 141 | $contents .= 'namespace '.$namespace.";\n\n"; |
||||||
| 142 | if (Driver::isMongoDB()) { |
||||||
| 143 | $contents .= "use Jenssegers\Mongodb\Eloquent\Model;\n\n"; |
||||||
| 144 | } else { |
||||||
| 145 | $contents .= "use Illuminate\Database\Eloquent\Model;\n\n"; |
||||||
| 146 | } |
||||||
| 147 | $contents .= 'class '.$className." extends Model\n"; |
||||||
| 148 | $contents .= "{\n\n"; |
||||||
| 149 | if (Driver::isMongoDB()) { |
||||||
| 150 | $contents .= "\tprotected \$collection = '".$table."';\n"; |
||||||
| 151 | } else { |
||||||
| 152 | $contents .= "\tprotected \$table = '".$table."';\n"; |
||||||
| 153 | } |
||||||
| 154 | |||||||
| 155 | // $content .= "\tpublic \$timestamps = false;\n"; |
||||||
| 156 | $contents .= "}\n"; |
||||||
| 157 | |||||||
| 158 | $filesystem = new Filesystem; |
||||||
| 159 | $filesystem->put($path.DIRECTORY_SEPARATOR.$className.'.php', $contents); |
||||||
| 160 | } catch (\Exception $e) { |
||||||
| 161 | throw new \Exception('There has an error when create model. The error is :'.$e->getMessage(), 1); |
||||||
| 162 | } |
||||||
| 163 | } |
||||||
| 164 | |||||||
| 165 | /** |
||||||
| 166 | * Make new controller. |
||||||
| 167 | * |
||||||
| 168 | * @param string $controller |
||||||
| 169 | * |
||||||
| 170 | * @return void |
||||||
| 171 | */ |
||||||
| 172 | public function makeController($controller) |
||||||
| 173 | { |
||||||
| 174 | try { |
||||||
| 175 | Artisan::call('make:controller', [ |
||||||
| 176 | 'name' => $controller, |
||||||
| 177 | ]); |
||||||
| 178 | } catch (\Exception $e) { |
||||||
| 179 | throw new \Exception('There has an error when create Controller. The error is :'.$e->getMessage(), 1); |
||||||
| 180 | } |
||||||
| 181 | } |
||||||
| 182 | |||||||
| 183 | /** |
||||||
| 184 | * Create new model instance. |
||||||
| 185 | * |
||||||
| 186 | * @param string $model |
||||||
| 187 | * @param string|null $table |
||||||
| 188 | * |
||||||
| 189 | * @return object |
||||||
| 190 | */ |
||||||
| 191 | public function model($model, $table = null) |
||||||
| 192 | { |
||||||
| 193 | if ($table == null) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 194 | return new $model; |
||||||
| 195 | } |
||||||
| 196 | |||||||
| 197 | return (new $model)->setTable($table); |
||||||
| 198 | } |
||||||
| 199 | |||||||
| 200 | /** |
||||||
| 201 | * Create new model instance. |
||||||
| 202 | * |
||||||
| 203 | * @return \CodexShaper\DBM\Models\DBM_MongoObject|\CodexShaper\DBM\Models\DBM_Object |
||||||
| 204 | */ |
||||||
| 205 | public function Object() |
||||||
| 206 | { |
||||||
| 207 | if (Driver::isMongoDB()) { |
||||||
| 208 | return new DBM_MongoObject(); |
||||||
| 209 | } |
||||||
| 210 | |||||||
| 211 | return new DBM_Object; |
||||||
| 212 | } |
||||||
| 213 | |||||||
| 214 | /** |
||||||
| 215 | * Create new model instance. |
||||||
| 216 | * |
||||||
| 217 | * @return \CodexShaper\DBM\Models\DBM_MongoField|\CodexShaper\DBM\Models\DBM_Field |
||||||
| 218 | */ |
||||||
| 219 | public function Field() |
||||||
| 220 | { |
||||||
| 221 | if (Driver::isMongoDB()) { |
||||||
| 222 | return new DBM_MongoField; |
||||||
| 223 | } |
||||||
| 224 | |||||||
| 225 | return new DBM_Field; |
||||||
| 226 | } |
||||||
| 227 | |||||||
| 228 | /** |
||||||
| 229 | * Create new model instance. |
||||||
| 230 | * |
||||||
| 231 | * @return \CodexShaper\DBM\Models\DBM_MongoPermission|\CodexShaper\DBM\Models\DBM_Permission |
||||||
| 232 | */ |
||||||
| 233 | public function Permission() |
||||||
| 234 | { |
||||||
| 235 | if (Driver::isMongoDB()) { |
||||||
| 236 | return new DBM_MongoPermission; |
||||||
| 237 | } |
||||||
| 238 | |||||||
| 239 | return new DBM_Permission; |
||||||
| 240 | } |
||||||
| 241 | |||||||
| 242 | /** |
||||||
| 243 | * Create new model instance. |
||||||
| 244 | * |
||||||
| 245 | * @return \CodexShaper\DBM\Models\DBM_MongoTemplate|\CodexShaper\DBM\Models\DBM_Template |
||||||
| 246 | */ |
||||||
| 247 | public function Template() |
||||||
| 248 | { |
||||||
| 249 | if (Driver::isMongoDB()) { |
||||||
| 250 | return new DBM_MongoTemplate; |
||||||
| 251 | } |
||||||
| 252 | |||||||
| 253 | return new DBM_Template; |
||||||
| 254 | } |
||||||
| 255 | |||||||
| 256 | /** |
||||||
| 257 | * Create new model instance. |
||||||
| 258 | * |
||||||
| 259 | * @return \CodexShaper\DBM\Models\DBM_MongoMenu|\CodexShaper\DBM\Models\DBM_Menu |
||||||
| 260 | */ |
||||||
| 261 | public function Menu() |
||||||
| 262 | { |
||||||
| 263 | if (Driver::isMongoDB()) { |
||||||
| 264 | return new DBM_MongoMenu; |
||||||
| 265 | } |
||||||
| 266 | |||||||
| 267 | return new DBM_Menu; |
||||||
| 268 | } |
||||||
| 269 | |||||||
| 270 | /** |
||||||
| 271 | * Create new model instance. |
||||||
| 272 | * |
||||||
| 273 | * @return \CodexShaper\DBM\Models\DBM_MongoMenuItem|\CodexShaper\DBM\Models\DBM_MenuItem |
||||||
| 274 | */ |
||||||
| 275 | public function MenuItem() |
||||||
| 276 | { |
||||||
| 277 | if (Driver::isMongoDB()) { |
||||||
| 278 | return new DBM_MongoMenuItem; |
||||||
| 279 | } |
||||||
| 280 | |||||||
| 281 | return new DBM_MenuItem; |
||||||
| 282 | } |
||||||
| 283 | |||||||
| 284 | /** |
||||||
| 285 | * Get all templates. |
||||||
| 286 | * |
||||||
| 287 | * @return array |
||||||
| 288 | */ |
||||||
| 289 | public function templates() |
||||||
| 290 | { |
||||||
| 291 | $templates = static::Template()->get(); |
||||||
|
0 ignored issues
–
show
The method
CodexShaper\DBM\Manager::Template() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 292 | $newTemplates = []; |
||||||
| 293 | |||||||
| 294 | foreach ($templates as $template) { |
||||||
| 295 | $newTemplates[] = (object) [ |
||||||
| 296 | 'name' => $template->name, |
||||||
| 297 | 'oldName' => $template->old_name, |
||||||
| 298 | 'type' => [ |
||||||
| 299 | 'name' => $template->type, |
||||||
| 300 | ], |
||||||
| 301 | 'notnull' => $template->notnull, |
||||||
| 302 | 'unsigned' => $template->unsigned, |
||||||
| 303 | 'autoincrement' => $template->auto_increment, |
||||||
| 304 | 'default' => $template->default, |
||||||
| 305 | 'length' => $template->length, |
||||||
| 306 | 'index' => ($template->index != null) ? $template->index : '', |
||||||
| 307 | ]; |
||||||
| 308 | } |
||||||
| 309 | |||||||
| 310 | return $newTemplates; |
||||||
| 311 | } |
||||||
| 312 | |||||||
| 313 | /** |
||||||
| 314 | * Get file path prefix. |
||||||
| 315 | * |
||||||
| 316 | * @param string @driver |
||||||
|
0 ignored issues
–
show
|
|||||||
| 317 | * |
||||||
| 318 | * @return string |
||||||
| 319 | */ |
||||||
| 320 | public function getPathPrefix($driver = 'local') |
||||||
| 321 | { |
||||||
| 322 | return trim(Storage::disk($driver)->getDriver()->getAdapter()->getPathPrefix(), DIRECTORY_SEPARATOR); |
||||||
| 323 | } |
||||||
| 324 | |||||||
| 325 | /** |
||||||
| 326 | * Get all templates. |
||||||
| 327 | * |
||||||
| 328 | * @return \Illuminate\Support\Collection |
||||||
| 329 | */ |
||||||
| 330 | public function userPermissions() |
||||||
| 331 | { |
||||||
| 332 | $user = Auth::user(); |
||||||
| 333 | |||||||
| 334 | return self::Object() |
||||||
|
0 ignored issues
–
show
The method
CodexShaper\DBM\Manager::Object() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 335 | ->setManyToManyRelation( |
||||||
| 336 | $user, |
||||||
| 337 | static::Permission(), |
||||||
|
0 ignored issues
–
show
The method
CodexShaper\DBM\Manager::Permission() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 338 | 'dbm_user_permissions', |
||||||
| 339 | 'user_id', |
||||||
| 340 | 'dbm_permission_id' |
||||||
| 341 | ) |
||||||
| 342 | ->belongs_to_many; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 343 | } |
||||||
| 344 | |||||||
| 345 | /** |
||||||
| 346 | * Check user loggedin or not. |
||||||
| 347 | * |
||||||
| 348 | * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\View\View |
||||||
| 349 | */ |
||||||
| 350 | public function isLoggedIn() |
||||||
| 351 | { |
||||||
| 352 | if (Auth::guest()) { |
||||||
| 353 | return Route::has('login') ? redirect(route('login')) : Response::view('dbm::errors.404', [], 404); |
||||||
| 354 | } |
||||||
| 355 | |||||||
| 356 | return true; |
||||||
| 357 | } |
||||||
| 358 | |||||||
| 359 | /** |
||||||
| 360 | * Check user permission. |
||||||
| 361 | * |
||||||
| 362 | * @param string $prefix |
||||||
| 363 | * @param string $slug |
||||||
| 364 | * |
||||||
| 365 | * @return string |
||||||
| 366 | */ |
||||||
| 367 | public function checkPermission($prefix, $slug) |
||||||
| 368 | { |
||||||
| 369 | if (Auth::guest()) { |
||||||
| 370 | return 'not_logged_in'; |
||||||
| 371 | } |
||||||
| 372 | |||||||
| 373 | $user_model = config('dbm.auth.user.model'); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 374 | $user_table = config('dbm.auth.user.table'); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 375 | $user_local_key = config('dbm.auth.user.local_key'); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 376 | $user_display_name = config('dbm.auth.user.display_name'); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 377 | |||||||
| 378 | $permissions = $this->userPermissions(); |
||||||
| 379 | |||||||
| 380 | foreach ($permissions as $permission) { |
||||||
| 381 | if ($permission->prefix == $prefix && $permission->slug == $slug) { |
||||||
| 382 | return 'authorized'; |
||||||
| 383 | } |
||||||
| 384 | } |
||||||
| 385 | |||||||
| 386 | return 'not_authorized'; |
||||||
| 387 | } |
||||||
| 388 | |||||||
| 389 | /** |
||||||
| 390 | * Check user aurization. |
||||||
| 391 | * |
||||||
| 392 | * @param string $permission |
||||||
| 393 | * |
||||||
| 394 | * @return \Illuminate\Http\JsonResponse|true |
||||||
| 395 | */ |
||||||
| 396 | public function authorize($permission) |
||||||
| 397 | { |
||||||
| 398 | $permission = explode('.', $permission); |
||||||
| 399 | |||||||
| 400 | $prefix = $permission[0]; |
||||||
| 401 | $slug = $permission[1]; |
||||||
| 402 | |||||||
| 403 | switch ($this->checkPermission($prefix, $slug)) { |
||||||
| 404 | case 'not_logged_in': |
||||||
| 405 | return response()->json([ |
||||||
| 406 | 'success' => false, |
||||||
| 407 | 'url' => route('login'), |
||||||
| 408 | ]); |
||||||
| 409 | break; |
||||||
|
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other Loading history...
|
|||||||
| 410 | |||||||
| 411 | case 'not_authorized': |
||||||
| 412 | return response()->json([ |
||||||
| 413 | 'success' => false, |
||||||
| 414 | 'errors' => ["You don't have permission to ".$slug.' '.$prefix], |
||||||
| 415 | ], 401); |
||||||
| 416 | break; |
||||||
| 417 | case 'authorized': |
||||||
| 418 | return true; |
||||||
| 419 | break; |
||||||
| 420 | } |
||||||
| 421 | } |
||||||
| 422 | } |
||||||
| 423 |