| 
									
										
										
										
											2019-12-29 21:02:26 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Illuminate\Database\Migrations\Migration; | 
					
						
							|  |  |  | use Illuminate\Database\Schema\Blueprint; | 
					
						
							|  |  |  | use Illuminate\Support\Carbon; | 
					
						
							|  |  |  | use Illuminate\Support\Facades\Schema; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AddApiAuth extends Migration | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Run the migrations. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function up() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Add API tokens table
 | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         Schema::create('api_tokens', function (Blueprint $table) { | 
					
						
							| 
									
										
										
										
											2019-12-29 21:02:26 +08:00
										 |  |  |             $table->increments('id'); | 
					
						
							| 
									
										
										
										
											2019-12-30 01:03:52 +08:00
										 |  |  |             $table->string('name'); | 
					
						
							| 
									
										
										
										
											2019-12-30 04:07:28 +08:00
										 |  |  |             $table->string('token_id')->unique(); | 
					
						
							|  |  |  |             $table->string('secret'); | 
					
						
							| 
									
										
										
										
											2019-12-29 21:02:26 +08:00
										 |  |  |             $table->integer('user_id')->unsigned()->index(); | 
					
						
							| 
									
										
										
										
											2019-12-30 03:46:46 +08:00
										 |  |  |             $table->date('expires_at')->index(); | 
					
						
							| 
									
										
										
										
											2019-12-29 21:02:26 +08:00
										 |  |  |             $table->nullableTimestamps(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Add access-api permission
 | 
					
						
							|  |  |  |         $adminRoleId = DB::table('roles')->where('system_name', '=', 'admin')->first()->id; | 
					
						
							|  |  |  |         $permissionId = DB::table('role_permissions')->insertGetId([ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'name'         => 'access-api', | 
					
						
							| 
									
										
										
										
											2019-12-29 21:02:26 +08:00
										 |  |  |             'display_name' => 'Access system API', | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'created_at'   => Carbon::now()->toDateTimeString(), | 
					
						
							|  |  |  |             'updated_at'   => Carbon::now()->toDateTimeString(), | 
					
						
							| 
									
										
										
										
											2019-12-29 21:02:26 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |         DB::table('permission_role')->insert([ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'role_id'       => $adminRoleId, | 
					
						
							|  |  |  |             'permission_id' => $permissionId, | 
					
						
							| 
									
										
										
										
											2019-12-29 21:02:26 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Reverse the migrations. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function down() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Remove API tokens table
 | 
					
						
							|  |  |  |         Schema::dropIfExists('api_tokens'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Remove access-api permission
 | 
					
						
							|  |  |  |         $apiAccessPermission = DB::table('role_permissions') | 
					
						
							|  |  |  |             ->where('name', '=', 'access-api')->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         DB::table('permission_role')->where('permission_id', '=', $apiAccessPermission->id)->delete(); | 
					
						
							|  |  |  |         DB::table('role_permissions')->where('name', '=', 'access-api')->delete(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |