No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PermissionController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace App\Http\Controllers\Admin\User_management;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Http\Request;
  6. use App\Helpers\Helper;
  7. use Validator;
  8. use Hashids;
  9. use Spatie\Permission\Models\Permission;
  10. use Auth;
  11. use Illuminate\Support\Facades\Route;
  12. //load modelmu
  13. use Yajra\Datatables\Datatables;
  14. class PermissionController extends Controller
  15. {
  16. /**
  17. * Untuk CRUD Biar cepat
  18. * Silahkan ganti
  19. * Permission:: => dengan model anda
  20. * $permission_id => ganti dengan id di model anda
  21. */
  22. /**
  23. * Title untuk judul di web
  24. * route digunakan untuk tempat resource (file path) + routing (route/web) diusahain sama ya biar gak ngubah"
  25. */
  26. private $title = 'Admin Usermagement | Permission';
  27. /**jangan lupa diganti*/
  28. private $route = 'admin.user_management.permission.'; //path awal foldernya ajah (misal folder di admin/dashboard) => 'admin.dashboard' | jangan lupa diganti
  29. private $permission = ['list', 'create', 'update', 'delete'];
  30. private $namespace_controller = 'Admin\User_management';
  31. private $url_prefix = 'admin/user-management/permission/';
  32. public function __construct()
  33. {
  34. DB::getQueryLog();
  35. // $this->middleware('permission:permission-list|permission-create|permission-update|permission-delete', ['only' => ['index', 'create', 'update']]);
  36. // $this->middleware('permission:permission-create', ['only' => ['create', 'create_action']]);
  37. // $this->middleware('permission:permission-update', ['only' => ['update', 'update_action']]);
  38. // $this->middleware('permission:permission-delete', ['only' => ['delete']]);
  39. }
  40. /**
  41. * Ini contoh crud yang sudah jalan
  42. * index digunakna untuk tampilan awal dari menu yang akan dibuat
  43. */
  44. public function index()
  45. {
  46. if (session('success')) {
  47. alert()->html('', session('success'), 'success');
  48. }
  49. if (session('error')) {
  50. alert()->html('', session('error'), 'error');
  51. }
  52. $data = [
  53. //bawaan
  54. 'title' => $this->title,
  55. 'route' => $this->route,
  56. ];
  57. // dd($data);
  58. return view($this->route . 'index', $data);
  59. }
  60. public function getData()
  61. {
  62. $query = Permission::query();
  63. return Datatables::of($query)
  64. ->addIndexColumn()
  65. ->addColumn('aksi', function ($query) {
  66. /**cek role */
  67. $aksi = '';
  68. $aksi .= "<a href='" . route($this->route . 'update', ['permission_id' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-primary btn-edit'>Edit</a>";
  69. $aksi .= "<a href='javascript:;' data-route='" . route($this->route . 'delete_action', ['permission_id' => Hashids::encode($query->id)]) . "' class='btn btn-danger btn-sm btn-delete'>Delete</a>";
  70. return $aksi;
  71. })
  72. ->rawColumns(['aksi'])
  73. ->toJson();
  74. }
  75. /**
  76. * create digunakan untuk menampilkan tampilan buat
  77. */
  78. public function create()
  79. {
  80. $data = [
  81. //bawaan
  82. 'title' => $this->title,
  83. 'route' => $this->route,
  84. 'permission' => $this->permission
  85. ];
  86. if (session('success')) {
  87. alert()->html('', session('success'), 'success');
  88. }
  89. if (session('error')) {
  90. alert()->html('', session('error'), 'error');
  91. }
  92. return view($this->route . 'create', $data);
  93. }
  94. /**
  95. * create_action digunakan untuk aksi post
  96. * lengkap dengan validatornya
  97. */
  98. public function create_action(Request $request)
  99. {
  100. /**digunakan untuk set rule validator */
  101. $rules = [
  102. 'name' => 'required|min:2',
  103. 'permission' => 'required',
  104. ];
  105. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  106. $alert = [
  107. 'required' => 'The :attribute is required',
  108. 'min' => ':attribute Min :min Char'
  109. ];
  110. $validator = Validator::make($request->all(), $rules, $alert);
  111. if ($validator->passes()) {
  112. /**menggunakan transaction */
  113. $insert = [];
  114. $name = implode('_', explode(' ', $request['name']));
  115. foreach ($request['permission'] as $key => $value) {
  116. $insert[] = [
  117. 'name' => $name . '-' . $value,
  118. 'guard_name' => 'web',
  119. ];
  120. }
  121. DB::beginTransaction();
  122. $query = Permission::insert($insert);
  123. if ($query) {
  124. DB::commit();
  125. $message = 'Berhasil';
  126. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  127. } else {
  128. DB::rollback();
  129. $message = 'Gagal';
  130. return redirect()->back()->with('error', Helper::parsing_alert($message));
  131. }
  132. }
  133. /**kenapa menggunakan back ? karena baliknya pasti ke halaman sebelumnya */
  134. $message = Helper::parsing_alert($validator->errors()->all());
  135. return redirect()->back()->with('error', $message)->withInput();
  136. }
  137. /** update sama seperti create hanya saja digunakan untuk update (viewnya saja) */
  138. public function update($permission_id)
  139. {
  140. if (session('success')) {
  141. alert()->html('', session('success'), 'success');
  142. }
  143. if (session('error')) {
  144. alert()->html('', session('error'), 'error');
  145. }
  146. $permission_id = Hashids::decode($permission_id);
  147. if (!empty($permission_id)) {
  148. $cek_role = Permission::where('id', $permission_id[0])->first();
  149. if ($cek_role) {
  150. $data = [
  151. //bawaan
  152. 'title' => $this->title,
  153. 'route' => $this->route,
  154. 'data' => $cek_role
  155. ];
  156. return view($this->route . 'update', $data);
  157. }
  158. $message = 'Id role not found or has been deleted';
  159. return redirect()->back()->with('error', $message);
  160. }
  161. $message = 'Id tdak ditemukan';
  162. return redirect()->back()->with('error', $message);
  163. }
  164. /**update_action POST
  165. * Logikanya :
  166. * 1. Cek Hashids::decode dari role_id apalah benar" di hash ?
  167. * -iya
  168. * 2. Cek ID yang didapatkan dari hash apakah benar adanya ?
  169. * -iya
  170. * 3. proses validate
  171. * -iya
  172. * 4. Proses update
  173. * - iya benar
  174. * - tidak salah
  175. * - tidak return error
  176. * - tidak return Id tdak ditemukan
  177. * -tidak skip reutrn id null
  178. */
  179. public function update_action(Request $request)
  180. {
  181. $rules = [
  182. 'name' => 'required|min:2|unique:roles,name',
  183. ];
  184. $alert = [
  185. 'required' => 'The :attribute is required',
  186. 'min' => ':attribute Min 2 Char'
  187. ];
  188. $validator = Validator::make($request->all(), $rules, $alert);
  189. $permission_id = Hashids::decode($request['id']);
  190. if (!empty($permission_id)) {
  191. /**cek apakah role_idnya ada ? */
  192. $cek_role = Permission::where('id', $permission_id[0])->first();
  193. if ($cek_role) {
  194. /**cek apakah id rolenya benar" ada di DB ? */
  195. if ($validator->passes()) {
  196. /**validatornya */
  197. DB::beginTransaction();
  198. $query = $cek_role->update($request->input());
  199. if ($query) {
  200. DB::commit();
  201. $message = 'Berhasil';
  202. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  203. } else {
  204. DB::rollback();
  205. $message = 'Gagal';
  206. return redirect()->back()->with('error', Helper::parsing_alert($message));
  207. }
  208. }
  209. $message = Helper::parsing_alert($validator->errors()->all());
  210. return redirect()->back()->with('error', $message);
  211. } else {
  212. $message = 'Id tdak ditemukan';
  213. return redirect()->back()->with('error', $message);
  214. }
  215. } else {
  216. $message = 'Id tidak boleh kosong';
  217. return redirect()->back()->with('error', $message);
  218. }
  219. }
  220. /**digunakan untuk delete
  221. * Logikanya :
  222. * 1. Cek hash id
  223. * -ya
  224. * 2. Cek id dari hash
  225. * - ya
  226. * 3. Proses delete
  227. * -Ya
  228. * -Tidak
  229. * - tidak return Id tidak ditemukan atau sudah dihapus
  230. * -tidak reutrn Id tdak ditemukan
  231. */
  232. public function delete_action($permission_id)
  233. {
  234. $permission_id = Hashids::decode($permission_id);
  235. /**cek role idnya apakah ada ? */
  236. if (!empty($permission_id)) {
  237. $cek_role = Permission::where('id', $permission_id[0])->first();
  238. if ($cek_role) {
  239. DB::beginTransaction();
  240. $delete = $cek_role->delete();
  241. if ($delete) {
  242. DB::commit();
  243. $message = 'Succesfully';
  244. $response = [
  245. 'message' => $message,
  246. 'status' => true,
  247. ];
  248. return response()->json($response);
  249. } else {
  250. DB::rollback();
  251. $message = 'Gagal';
  252. $response = [
  253. 'message' => $message,
  254. 'status' => false,
  255. ];
  256. return response()->json($response);
  257. }
  258. }
  259. $message = 'Id tidak di temukan atau sudah di hapus';
  260. $response = [
  261. 'message' => $message,
  262. 'status' => false,
  263. ];
  264. return response()->json($response);
  265. }
  266. $message = 'Id tdak ditemukan';
  267. $response = [
  268. 'message' => $message,
  269. 'status' => false,
  270. ];
  271. return response()->json($response);
  272. }
  273. //custom route
  274. public function route()
  275. {
  276. return Route::group([
  277. 'as' => $this->route,
  278. 'prefix' => $this->url_prefix,
  279. 'namespace' => $this->namespace_controller
  280. ], function () {
  281. Route::get('/', ['as' => 'index', 'uses' => 'PermissionController@index']);
  282. Route::get('/create', ['as' => 'create', 'uses' => 'PermissionController@create']);
  283. Route::get('/update/{permission_id}', ['as' => 'update', 'uses' => 'PermissionController@update']);
  284. Route::get('/delete-action/{permission_id}', ['as' => 'delete_action', 'uses' => 'PermissionController@delete_action']);
  285. Route::get('/get-data', ['as' => 'get_data', 'uses' => 'PermissionController@getData']);
  286. Route::post('/create-action', ['as' => 'create_action', 'uses' => 'PermissionController@create_action']);
  287. Route::post('/update-action', ['as' => 'update_action', 'uses' => 'PermissionController@update_action']);
  288. });
  289. }
  290. //end custom route
  291. }