PermissionController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: dingran
  5. * Date: 2019/3/10
  6. * Time: 下午12:36
  7. */
  8. namespace app\platform\modules\user\controllers;
  9. use app\common\events\UserActionEvent;
  10. use app\platform\controllers\BaseController;
  11. use app\platform\modules\user\models\Permission;
  12. use app\platform\modules\user\requests\PermissionCreateRequest;
  13. use app\platform\modules\user\requests\PermissionUpdateRequest;
  14. use Illuminate\Http\Request;
  15. class PermissionController extends BaseController
  16. {
  17. protected $fields = [
  18. 'name' => '',
  19. 'label' => '',
  20. 'description' => '',
  21. 'parent_id' => 0,
  22. 'icon' => '',
  23. ];
  24. /**
  25. * Display a listing of the resource.
  26. *
  27. * @return \Illuminate\Http\Response
  28. */
  29. public function index(Request $request, $parentId = 0)
  30. {
  31. $parentId = (int)$parentId;
  32. $datas['parentId'] = $parentId;
  33. $datas['data'] = Permission::where('parent_id', $parentId)->get();
  34. return view('admin.permission.index', $datas);
  35. }
  36. /**
  37. * Show the form for creating a new resource.
  38. *
  39. * @return \Illuminate\Http\Response
  40. */
  41. public function create($parentId)
  42. {
  43. $data = [];
  44. foreach ($this->fields as $field => $default) {
  45. $data[$field] = old($field, $default);
  46. }
  47. $data['parent_id'] = $parentId;
  48. return view('admin.permission.create', $data);
  49. }
  50. /**
  51. * Store a newly created resource in storage.
  52. *
  53. * @param PremissionCreateRequest|Request $request
  54. *
  55. * @return \Illuminate\Http\Response
  56. */
  57. public function store(PermissionCreateRequest $request)
  58. {
  59. $permission = new Permission();
  60. foreach (array_keys($this->fields) as $field) {
  61. $permission->$field = $request->get($field, $this->fields[$field]);
  62. }
  63. $permission->save();
  64. event(new UserActionEvent(Permission::class, $permission->id, 1,
  65. '添加了权限:' . $permission->name . '(' . $permission->label . ')'));
  66. return $this->successJson('添加成功', []);
  67. }
  68. /**
  69. * Display the specified resource.
  70. *
  71. * @param int $id
  72. *
  73. * @return \Illuminate\Http\Response
  74. */
  75. public function show($id)
  76. {
  77. //
  78. }
  79. /**
  80. * Show the form for editing the specified resource.
  81. *
  82. * @param int $id
  83. *
  84. * @return \Illuminate\Http\Response
  85. */
  86. public function edit($id)
  87. {
  88. $permission = Permission::find((int)$id);
  89. if (!$permission) {
  90. return redirect('/admin/permission')->withErrors("找不到该权限!");
  91. }
  92. $data = ['id' => (int)$id];
  93. foreach (array_keys($this->fields) as $field) {
  94. $data[$field] = old($field, $permission->$field);
  95. }
  96. //dd($data);
  97. return view('admin.permission.edit', $data);
  98. }
  99. /**
  100. * Update the specified resource in storage.
  101. *
  102. * @param PermissionUpdateRequest|Request $request
  103. * @param int $id
  104. *
  105. * @return \Illuminate\Http\Response
  106. */
  107. public function update(PermissionUpdateRequest $request, $id)
  108. {
  109. $permission = Permission::find((int)$id);
  110. foreach (array_keys($this->fields) as $field) {
  111. $permission->$field = $request->get($field, $this->fields[$field]);
  112. }
  113. $permission->save();
  114. event(new UserActionEvent(Permission::class, $permission->id, 3,
  115. '修改了权限:' . $permission->name . '(' . $permission->label . ')'));
  116. return $this->successJson('添加成功', []);
  117. }
  118. /**
  119. * Remove the specified resource from storage.
  120. *
  121. * @param int $id
  122. *
  123. * @return \Illuminate\Http\Response
  124. */
  125. public function destroy($id)
  126. {
  127. $child = Permission::where('parent_id', $id)->first();
  128. if ($child) {
  129. return redirect()->back()
  130. ->withErrors("请先将该权限的子权限删除后再做删除操作!");
  131. }
  132. $tag = Permission::find((int)$id);
  133. foreach ($tag->roles as $v) {
  134. $tag->roles()->detach($v->id);
  135. }
  136. if ($tag) {
  137. $tag->delete();
  138. } else {
  139. return redirect()->back()
  140. ->withErrors("删除失败");
  141. }
  142. event(new UserActionEvent(Permission::class, $tag->id, 2, '删除了权限:' . $tag->name . '(' . $tag->label . ')'));
  143. return $this->successJson('成功', []);
  144. }
  145. }