PermissionController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Admin;
  7. use Auth;
  8. use Session;
  9. class PermissionController extends Controller
  10. {
  11. public function __construct()
  12. {
  13. $this->middleware('admin');
  14. }
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return \Illuminate\Http\Response
  19. */
  20. public function index()
  21. {
  22. $permissions = Permission::where('guard_name','admin')->get();
  23. return view('admin.permissions.list',compact('permissions'));
  24. }
  25. /**
  26. * Show the form for creating a new resource.
  27. *
  28. * @return \Illuminate\Http\Response
  29. */
  30. public function create()
  31. {
  32. $parents = Permission::where('guard_name', 'admin')->where('parent',0)->orderBy('name','ASC')->get();
  33. return view('admin.permissions.add', compact('parents'));
  34. }
  35. /**
  36. * Store a newly created resource in storage.
  37. *
  38. * @param \Illuminate\Http\Request $request
  39. * @return \Illuminate\Http\Response
  40. */
  41. public function store(Request $request)
  42. {
  43. $this->validate($request,[
  44. 'name' => 'required|unique:permissions,name'
  45. ]);
  46. $permission = new Permission;
  47. $permission->name = str_replace(' ', '_', strtolower(trim($request->name)));
  48. $permission->display_name = $request->name;
  49. if ($request->parent !="") {
  50. $permission->parent = $request->parent;
  51. }
  52. $permission->guard_name = 'admin';
  53. $permission->save();
  54. return redirect()->action('Admin\PermissionController@index')->with('success','Successfully Created!');
  55. }
  56. /**
  57. * Display the specified resource.
  58. *
  59. * @param int $id
  60. * @return \Illuminate\Http\Response
  61. */
  62. public function show($id)
  63. {
  64. $permission = Permission::find($id);
  65. return view('admin.permissions.view',compact('permission'));
  66. }
  67. /**
  68. * Show the form for editing the specified resource.
  69. *
  70. * @param int $id
  71. * @return \Illuminate\Http\Response
  72. */
  73. public function edit($id)
  74. {
  75. $permission = Permission::find($id);
  76. $parents = Permission::where('guard_name', 'admin')->where('parent',0)->orderBy('name','ASC')->get();
  77. return view('admin.permissions.edit',compact('permission','parents'));
  78. }
  79. /**
  80. * Update the specified resource in storage.
  81. *
  82. * @param \Illuminate\Http\Request $request
  83. * @param int $id
  84. * @return \Illuminate\Http\Response
  85. */
  86. public function update(Request $request, $id)
  87. {
  88. $this->validate($request,[
  89. 'name' => 'required'
  90. ]);
  91. $permission = Permission::find($id);
  92. $permission->name = str_replace(' ', '_', strtolower(trim($request->name)));
  93. $permission->display_name = $request->name;
  94. if ($request->parent !="") {
  95. $permission->parent = $request->parent;
  96. }
  97. $permission->update();
  98. return redirect()->action('Admin\PermissionController@index')->with('success','Successfully Updated!');
  99. }
  100. /**
  101. * Remove the specified resource from storage.
  102. *
  103. * @param int $id
  104. * @return \Illuminate\Http\Response
  105. */
  106. public function destroy($id)
  107. {
  108. $permission = Permission::find($id);
  109. Permission::whereId($id)->delete();
  110. return redirect()->action('Admin\PermissionController@index')->with('success','Successfully Deleted!');
  111. }
  112. }