RoleController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Permission;
  6. use App\Models\Role;
  7. use DB;
  8. use Artisan;
  9. class RoleController extends Controller
  10. {
  11. /**
  12. * Create a new controller instance.
  13. *
  14. * @return void
  15. */
  16. public function __construct()
  17. {
  18. $this->middleware('admin');
  19. }
  20. /**
  21. * Display a listing of the resource.
  22. *
  23. * @return \Illuminate\Http\Response
  24. */
  25. public function index()
  26. {
  27. $roles = Role::where('guard_name','admin')->get();
  28. return view('admin.roles.list',compact('roles'));
  29. }
  30. /**
  31. * Show the form for creating a new resource.
  32. *
  33. * @return \Illuminate\Http\Response
  34. */
  35. public function create()
  36. {
  37. $permissions = array();
  38. $parent_permissions = Permission::where('guard_name', 'admin')->where('parent', 0)->get();
  39. foreach ($parent_permissions as $key => $parent) {
  40. $childs = Permission::where('guard_name', 'admin')->where('parent', $parent->id)->get();
  41. $permissions[] = array(
  42. 'parent' => $parent,
  43. 'childs' => $childs
  44. );
  45. }
  46. return view('admin.roles.add',compact('permissions'));
  47. }
  48. /**
  49. * Store a newly created resource in storage.
  50. *
  51. * @param \Illuminate\Http\Request $request
  52. * @return \Illuminate\Http\Response
  53. */
  54. public function store(Request $request)
  55. {
  56. $this->validate($request,[
  57. 'name' => 'required|unique:roles,name',
  58. 'permissions' => 'required'
  59. ]);
  60. $role = new Role;
  61. $role->name = $request->name;
  62. $role->guard_name = 'admin';
  63. $role->save();
  64. foreach ($request->input('permissions') as $key => $value) {
  65. $role->givePermissionTo($value);
  66. }
  67. return redirect()->action('Admin\RoleController@index')->with('success','Successfully Created!');
  68. }
  69. /**
  70. * Display the specified resource.
  71. *
  72. * @param int $id
  73. * @return \Illuminate\Http\Response
  74. */
  75. public function show($id)
  76. {
  77. $role = Role::find($id);
  78. $rolePermissions = DB::table("role_has_permissions")->where("role_has_permissions.role_id",$id)
  79. ->pluck('role_has_permissions.permission_id')->toArray();
  80. $permissions = array();
  81. $parent_permissions = Permission::where('guard_name', 'admin')->where('parent', 0)->whereIn('id',$rolePermissions)->get();
  82. foreach ($parent_permissions as $key => $parent) {
  83. $childs = Permission::where('guard_name', 'admin')->where('parent', $parent->id)->whereIn('id',$rolePermissions)->get();
  84. $permissions[] = array(
  85. 'parent' => $parent,
  86. 'childs' => $childs,
  87. );
  88. }
  89. return view('admin.roles.view',compact('role','permissions'));
  90. }
  91. /**
  92. * Show the form for editing the specified resource.
  93. *
  94. * @param int $id
  95. * @return \Illuminate\Http\Response
  96. */
  97. public function edit($id)
  98. {
  99. $role = Role::find($id);
  100. $permissions = array();
  101. $parent_permissions = Permission::where('guard_name', 'admin')->where('parent', 0)->get();
  102. foreach ($parent_permissions as $key => $parent) {
  103. $childs = Permission::where('guard_name', 'admin')->where('parent', $parent->id)->get();
  104. $permissions[] = array(
  105. 'parent' => $parent,
  106. 'childs' => $childs
  107. );
  108. }
  109. $rolePermissions = DB::table("role_has_permissions")->where("role_has_permissions.role_id",$id)
  110. ->pluck('role_has_permissions.permission_id','role_has_permissions.permission_id')->toArray();
  111. return view('admin.roles.edit',compact('role','permissions','rolePermissions'));
  112. }
  113. /**
  114. * Update the specified resource in storage.
  115. *
  116. * @param \Illuminate\Http\Request $request
  117. * @param int $id
  118. * @return \Illuminate\Http\Response
  119. */
  120. public function update(Request $request, $id)
  121. {
  122. $this->validate($request,[
  123. 'name' => 'required',
  124. 'permissions' => 'required'
  125. ]);
  126. $role = Role::find($id);
  127. $role->name = $request->name;
  128. $role->update();
  129. DB::table("role_has_permissions")->where("role_has_permissions.role_id",$id)->delete();
  130. foreach ($request->input('permissions') as $key => $value) {
  131. $role->givePermissionTo($value);
  132. }
  133. return redirect()->action('Admin\RoleController@index')->with('success','Successfully Updated!');
  134. }
  135. /**
  136. * Remove the specified resource from storage.
  137. *
  138. * @param int $id
  139. * @return \Illuminate\Http\Response
  140. */
  141. public function destroy($id)
  142. {
  143. DB::beginTransaction();
  144. try {
  145. Role::whereId($id)->delete();
  146. DB::table("role_has_permissions")->where("role_has_permissions.role_id",$id)->delete();
  147. DB::commit();
  148. return redirect()->action('Admin\RoleController@index')->with('success','Successfully Deleted!');
  149. }catch (\Exception $e) {
  150. DB::rollBack();
  151. return redirect()->action('Admin\RoleController@index')->with('error', 'Something went wrong!');
  152. }
  153. }
  154. }