diff --git a/composer.json b/composer.json index 98a89fd..a8e0054 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "tripteki/laravelphp-acl", - "version": "1.0.0", + "version": "1.1.0", "description": "Trip Teknologi's Laravel.php ACLs", "readme": "README.md", diff --git a/stubs/app/Exports/ACLs/.gitkeep b/stubs/app/Exports/ACLs/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/stubs/app/Exports/ACLs/PermissionExport.php b/stubs/app/Exports/ACLs/PermissionExport.php new file mode 100644 index 0000000..f79b0c0 --- /dev/null +++ b/stubs/app/Exports/ACLs/PermissionExport.php @@ -0,0 +1,47 @@ + [ "font" => [ "bold" => true, ], ], + ]; + } + + /** + * @return array + */ + public function headings(): array + { + return [ + + "Name", + ]; + } + + /** + * @return \Illuminate\Database\Eloquent\Collection + */ + public function collection() + { + return app(PermissionModel::class)->all([ + + "name", + ]); + } +}; diff --git a/stubs/app/Exports/ACLs/RoleExport.php b/stubs/app/Exports/ACLs/RoleExport.php new file mode 100644 index 0000000..c499e16 --- /dev/null +++ b/stubs/app/Exports/ACLs/RoleExport.php @@ -0,0 +1,47 @@ + [ "font" => [ "bold" => true, ], ], + ]; + } + + /** + * @return array + */ + public function headings(): array + { + return [ + + "Name", + ]; + } + + /** + * @return \Illuminate\Database\Eloquent\Collection + */ + public function collection() + { + return app(RoleModel::class)->all([ + + "name", + ]); + } +}; diff --git a/stubs/app/Http/Controllers/Admin/ACL/PermissionAdminController.php b/stubs/app/Http/Controllers/Admin/ACL/PermissionAdminController.php index c2b8df3..aae0608 100644 --- a/stubs/app/Http/Controllers/Admin/ACL/PermissionAdminController.php +++ b/stubs/app/Http/Controllers/Admin/ACL/PermissionAdminController.php @@ -5,10 +5,15 @@ use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; +use Maatwebsite\Excel\Facades\Excel; use Tripteki\ACL\Contracts\Repository\Admin\IACLPermissionRepository; +use App\Imports\ACLs\PermissionImport; +use App\Exports\ACLs\PermissionExport; use App\Http\Requests\Admin\ACLs\Permissions\PermissionShowValidation; use App\Http\Requests\Admin\ACLs\Permissions\PermissionStoreValidation; use App\Http\Requests\Admin\ACLs\Permissions\PermissionDestroyValidation; +use Tripteki\Helpers\Http\Requests\FileImportValidation; +use Tripteki\Helpers\Http\Requests\FileExportValidation; use Tripteki\Helpers\Http\Controllers\Controller; class PermissionAdminController extends Controller @@ -201,4 +206,103 @@ public function destroy(PermissionDestroyValidation $request, $permission) return iresponse($data, $statecode); } + + /** + * @OA\Post( + * path="/admin/acls/permissions-import", + * tags={"Admin ACL Permission"}, + * summary="Import", + * @OA\RequestBody( + * @OA\MediaType( + * mediaType="multipart/form-data", + * @OA\Schema( + * @OA\Property( + * property="file", + * type="file", + * description="Permission's File." + * ) + * ) + * ) + * ), + * @OA\Response( + * response=200, + * description="Success." + * ), + * @OA\Response( + * response=422, + * description="Unprocessable Entity." + * ) + * ) + * + * @param \Tripteki\Helpers\Http\Requests\FileImportValidation $request + * @return \Illuminate\Http\JsonResponse + */ + public function import(FileImportValidation $request) + { + $form = $request->validated(); + $data = []; + $statecode = 200; + + if ($form["file"]->getClientOriginalExtension() == "csv" || $form["file"]->getClientOriginalExtension() == "txt") { + + $data = Excel::import(new PermissionImport(), $form["file"], null, \Maatwebsite\Excel\Excel::CSV); + + } else if ($form["file"]->getClientOriginalExtension() == "xls") { + + $data = Excel::import(new PermissionImport(), $form["file"], null, \Maatwebsite\Excel\Excel::XLS); + + } else if ($form["file"]->getClientOriginalExtension() == "xlsx") { + + $data = Excel::import(new PermissionImport(), $form["file"], null, \Maatwebsite\Excel\Excel::XLSX); + } + + return iresponse($data, $statecode); + } + + /** + * @OA\Get( + * path="/admin/acls/permissions-export", + * tags={"Admin ACL Permission"}, + * summary="Export", + * @OA\Parameter( + * required=false, + * in="query", + * name="file", + * schema={"type": "string", "enum": {"csv", "xls", "xlsx"}}, + * description="Permission's File." + * ), + * @OA\Response( + * response=200, + * description="Success." + * ), + * @OA\Response( + * response=422, + * description="Unprocessable Entity." + * ) + * ) + * + * @param \Tripteki\Helpers\Http\Requests\FileExportValidation $request + * @return mixed + */ + public function export(FileExportValidation $request) + { + $form = $request->validated(); + $data = []; + $statecode = 200; + + if ($form["file"] == "csv") { + + $data = Excel::download(new PermissionExport(), "Permission.csv", \Maatwebsite\Excel\Excel::CSV); + + } else if ($form["file"] == "xls") { + + $data = Excel::download(new PermissionExport(), "Permission.xls", \Maatwebsite\Excel\Excel::XLS); + + } else if ($form["file"] == "xlsx") { + + $data = Excel::download(new PermissionExport(), "Permission.xlsx", \Maatwebsite\Excel\Excel::XLSX); + } + + return $data; + } }; diff --git a/stubs/app/Http/Controllers/Admin/ACL/RoleAdminController.php b/stubs/app/Http/Controllers/Admin/ACL/RoleAdminController.php index 324013d..418731d 100644 --- a/stubs/app/Http/Controllers/Admin/ACL/RoleAdminController.php +++ b/stubs/app/Http/Controllers/Admin/ACL/RoleAdminController.php @@ -5,10 +5,15 @@ use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; +use Maatwebsite\Excel\Facades\Excel; use Tripteki\ACL\Contracts\Repository\Admin\IACLRoleRepository; +use App\Imports\ACLs\RoleImport; +use App\Exports\ACLs\RoleExport; use App\Http\Requests\Admin\ACLs\Roles\RoleShowValidation; use App\Http\Requests\Admin\ACLs\Roles\RoleStoreValidation; use App\Http\Requests\Admin\ACLs\Roles\RoleDestroyValidation; +use Tripteki\Helpers\Http\Requests\FileImportValidation; +use Tripteki\Helpers\Http\Requests\FileExportValidation; use Tripteki\Helpers\Http\Controllers\Controller; class RoleAdminController extends Controller @@ -201,4 +206,103 @@ public function destroy(RoleDestroyValidation $request, $role) return iresponse($data, $statecode); } + + /** + * @OA\Post( + * path="/admin/acls/roles-import", + * tags={"Admin ACL Role"}, + * summary="Import", + * @OA\RequestBody( + * @OA\MediaType( + * mediaType="multipart/form-data", + * @OA\Schema( + * @OA\Property( + * property="file", + * type="file", + * description="Role's File." + * ) + * ) + * ) + * ), + * @OA\Response( + * response=200, + * description="Success." + * ), + * @OA\Response( + * response=422, + * description="Unprocessable Entity." + * ) + * ) + * + * @param \Tripteki\Helpers\Http\Requests\FileImportValidation $request + * @return \Illuminate\Http\JsonResponse + */ + public function import(FileImportValidation $request) + { + $form = $request->validated(); + $data = []; + $statecode = 200; + + if ($form["file"]->getClientOriginalExtension() == "csv" || $form["file"]->getClientOriginalExtension() == "txt") { + + $data = Excel::import(new RoleImport(), $form["file"], null, \Maatwebsite\Excel\Excel::CSV); + + } else if ($form["file"]->getClientOriginalExtension() == "xls") { + + $data = Excel::import(new RoleImport(), $form["file"], null, \Maatwebsite\Excel\Excel::XLS); + + } else if ($form["file"]->getClientOriginalExtension() == "xlsx") { + + $data = Excel::import(new RoleImport(), $form["file"], null, \Maatwebsite\Excel\Excel::XLSX); + } + + return iresponse($data, $statecode); + } + + /** + * @OA\Get( + * path="/admin/acls/roles-export", + * tags={"Admin ACL Role"}, + * summary="Export", + * @OA\Parameter( + * required=false, + * in="query", + * name="file", + * schema={"type": "string", "enum": {"csv", "xls", "xlsx"}}, + * description="Role's File." + * ), + * @OA\Response( + * response=200, + * description="Success." + * ), + * @OA\Response( + * response=422, + * description="Unprocessable Entity." + * ) + * ) + * + * @param \Tripteki\Helpers\Http\Requests\FileExportValidation $request + * @return mixed + */ + public function export(FileExportValidation $request) + { + $form = $request->validated(); + $data = []; + $statecode = 200; + + if ($form["file"] == "csv") { + + $data = Excel::download(new RoleExport(), "Role.csv", \Maatwebsite\Excel\Excel::CSV); + + } else if ($form["file"] == "xls") { + + $data = Excel::download(new RoleExport(), "Role.xls", \Maatwebsite\Excel\Excel::XLS); + + } else if ($form["file"] == "xlsx") { + + $data = Excel::download(new RoleExport(), "Role.xlsx", \Maatwebsite\Excel\Excel::XLSX); + } + + return $data; + } }; diff --git a/stubs/app/Imports/ACLs/.gitkeep b/stubs/app/Imports/ACLs/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/stubs/app/Imports/ACLs/PermissionImport.php b/stubs/app/Imports/ACLs/PermissionImport.php new file mode 100644 index 0000000..d29e4ea --- /dev/null +++ b/stubs/app/Imports/ACLs/PermissionImport.php @@ -0,0 +1,52 @@ +rules(); + + Validator::make($rows->toArray(), [ + + "*.0" => $validator["permission"], + + ])->validate(); + } + + /** + * @param \Illuminate\Support\Collection $rows + * @return void + */ + public function collection(Collection $rows) + { + $this->validate($rows); + + $aclPermissionAdminRepository = app(IACLPermissionRepository::class); + + foreach ($rows as $row) { + + $aclPermissionAdminRepository->rule($row[0]); + } + } +}; diff --git a/stubs/app/Imports/ACLs/RoleImport.php b/stubs/app/Imports/ACLs/RoleImport.php new file mode 100644 index 0000000..3c49949 --- /dev/null +++ b/stubs/app/Imports/ACLs/RoleImport.php @@ -0,0 +1,52 @@ +rules(); + + Validator::make($rows->toArray(), [ + + "*.0" => $validator["role"], + + ])->validate(); + } + + /** + * @param \Illuminate\Support\Collection $rows + * @return void + */ + public function collection(Collection $rows) + { + $this->validate($rows); + + $aclRoleAdminRepository = app(IACLRoleRepository::class); + + foreach ($rows as $row) { + + $aclRoleAdminRepository->rule($row[0]); + } + } +}; diff --git a/stubs/routes/admin/acl.php b/stubs/routes/admin/acl.php index fb5f051..db7fcc6 100644 --- a/stubs/routes/admin/acl.php +++ b/stubs/routes/admin/acl.php @@ -14,8 +14,14 @@ Route::prefix("acls")->group(function () { Route::apiResource("users", UserAdminController::class)->only("show")->parameters([ "users" => "user", ]); + Route::apiResource("roles", RoleAdminController::class)->except("update")->parameters([ "roles" => "role", ]); + Route::post("roles-import", [ RoleAdminController::class, "import", ]); + Route::get("roles-export", [ RoleAdminController::class, "export", ]); + Route::apiResource("permissions", PermissionAdminController::class)->except("update")->parameters([ "permissions" => "permission", ]); + Route::post("permissions-import", [ PermissionAdminController::class, "import", ]); + Route::get("permissions-export", [ PermissionAdminController::class, "export", ]); Route::put("/{context}/{object}", [ ACLAdminController::class, "rule", ]); });