|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Admin; |
| 4 | + |
| 5 | +use App\Events\permChangeEvent; |
| 6 | +use Illuminate\Http\Request; |
| 7 | + |
| 8 | +use App\Http\Requests; |
| 9 | +use App\Http\Requests\PermissionCreateRequest; |
| 10 | +use App\Http\Requests\PermissionUpdateRequest; |
| 11 | +use App\Http\Controllers\Controller; |
| 12 | +use App\Models\Permission; |
| 13 | +use Cache, Event; |
| 14 | + |
| 15 | +class PermissionController extends Controller |
| 16 | +{ |
| 17 | + protected $fields = [ |
| 18 | + 'name' => '', |
| 19 | + 'label' => '', |
| 20 | + 'description' => '', |
| 21 | + 'cid' => 0, |
| 22 | + 'icon' => '', |
| 23 | + ]; |
| 24 | + |
| 25 | + |
| 26 | + /** |
| 27 | + * Display a listing of the resource. |
| 28 | + * |
| 29 | + * @return \Illuminate\Http\Response |
| 30 | + */ |
| 31 | + public function index(Request $request, $cid = 0) |
| 32 | + { |
| 33 | + $cid = (int)$cid; |
| 34 | + if ($request->ajax()) { |
| 35 | + $data = array(); |
| 36 | + $data['draw'] = $request->get('draw'); |
| 37 | + $start = $request->get('start'); |
| 38 | + $length = $request->get('length'); |
| 39 | + $order = $request->get('order'); |
| 40 | + $columns = $request->get('columns'); |
| 41 | + $search = $request->get('search'); |
| 42 | + $cid = $request->get('cid', 0); |
| 43 | + $data['recordsTotal'] = Permission::where('cid', $cid)->count(); |
| 44 | + if (strlen($search['value']) > 0) { |
| 45 | + $data['recordsFiltered'] = Permission::where('cid', $cid)->where(function ($query) use ($search) { |
| 46 | + $query |
| 47 | + ->where('name', 'LIKE', '%' . $search['value'] . '%') |
| 48 | + ->orWhere('description', 'like', '%' . $search['value'] . '%'); |
| 49 | + })->count(); |
| 50 | + $data['data'] = Permission::where('cid', $cid)->where(function ($query) use ($search) { |
| 51 | + $query->where('name', 'LIKE', '%' . $search['value'] . '%') |
| 52 | + ->orWhere('description', 'like', '%' . $search['value'] . '%'); |
| 53 | + }) |
| 54 | + ->skip($start)->take($length) |
| 55 | + ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir']) |
| 56 | + ->get(); |
| 57 | + } else { |
| 58 | + $data['recordsFiltered'] = Permission::where('cid', $cid)->count(); |
| 59 | + $data['data'] = Permission::where('cid', $cid)-> |
| 60 | + skip($start)->take($length) |
| 61 | + ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir']) |
| 62 | + ->get(); |
| 63 | + } |
| 64 | + return response()->json($data); |
| 65 | + } |
| 66 | + $datas['cid'] = $cid; |
| 67 | + if ($cid > 0) { |
| 68 | + $datas['data'] = Permission::find($cid); |
| 69 | + } |
| 70 | + return view('admin.permission.index', $datas); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Show the form for creating a new resource. |
| 75 | + * |
| 76 | + * @return \Illuminate\Http\Response |
| 77 | + */ |
| 78 | + public function create(int $cid) |
| 79 | + { |
| 80 | + $data = []; |
| 81 | + foreach ($this->fields as $field => $default) { |
| 82 | + $data[$field] = old($field, $default); |
| 83 | + } |
| 84 | + $data['cid'] = $cid; |
| 85 | + return view('admin.permission.create', $data); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Store a newly created resource in storage. |
| 90 | + * |
| 91 | + * @param PremissionCreateRequest|Request $request |
| 92 | + * @return \Illuminate\Http\Response |
| 93 | + */ |
| 94 | + public function store(PermissionCreateRequest $request) |
| 95 | + { |
| 96 | + |
| 97 | + $permission = new Permission(); |
| 98 | + foreach (array_keys($this->fields) as $field) { |
| 99 | + $permission->$field = $request->get($field); |
| 100 | + } |
| 101 | + $permission->save(); |
| 102 | + Event::fire(new permChangeEvent()); |
| 103 | + event(new \App\Events\userActionEvent('\App\Models\Permission', $permission->id, 1, '添加了权限:' . $permission->name . '(' . $permission->label . ')')); |
| 104 | + return redirect('/admin/permission/' . $permission->cid)->withSuccess('添加成功!'); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Display the specified resource. |
| 109 | + * |
| 110 | + * @param int $id |
| 111 | + * @return \Illuminate\Http\Response |
| 112 | + */ |
| 113 | + public function show($id) |
| 114 | + { |
| 115 | + // |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Show the form for editing the specified resource. |
| 120 | + * |
| 121 | + * @param int $id |
| 122 | + * @return \Illuminate\Http\Response |
| 123 | + */ |
| 124 | + public function edit($id) |
| 125 | + { |
| 126 | + $permission = Permission::find((int)$id); |
| 127 | + if (!$permission) return redirect('/admin/permission')->withErrors("找不到该权限!"); |
| 128 | + $data = ['id' => (int)$id]; |
| 129 | + foreach (array_keys($this->fields) as $field) { |
| 130 | + $data[$field] = old($field, $permission->$field); |
| 131 | + } |
| 132 | + //dd($data); |
| 133 | + return view('admin.permission.edit', $data); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Update the specified resource in storage. |
| 138 | + * |
| 139 | + * @param PermissionUpdateRequest|Request $request |
| 140 | + * @param int $id |
| 141 | + * @return \Illuminate\Http\Response |
| 142 | + */ |
| 143 | + public function update(PermissionUpdateRequest $request, $id) |
| 144 | + { |
| 145 | + $permission = Permission::find((int)$id); |
| 146 | + foreach (array_keys($this->fields) as $field) { |
| 147 | + $permission->$field = $request->get($field); |
| 148 | + } |
| 149 | + $permission->save(); |
| 150 | + Event::fire(new permChangeEvent()); |
| 151 | + event(new \App\Events\userActionEvent('\App\Models\Permission', $permission->id, 3, '修改了权限:' . $permission->name . '(' . $permission->label . ')')); |
| 152 | + return redirect('admin/permission/' . $permission->cid)->withSuccess('修改成功!'); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Remove the specified resource from storage. |
| 157 | + * |
| 158 | + * @param int $id |
| 159 | + * @return \Illuminate\Http\Response |
| 160 | + */ |
| 161 | + public function destroy($id) |
| 162 | + { |
| 163 | + $child = Permission::where('cid', $id)->first(); |
| 164 | + |
| 165 | + if ($child) { |
| 166 | + return redirect()->back() |
| 167 | + ->withErrors("请先将该权限的子权限删除后再做删除操作!"); |
| 168 | + } |
| 169 | + $tag = Permission::find((int)$id); |
| 170 | + if ($tag) { |
| 171 | + $tag->delete(); |
| 172 | + } else { |
| 173 | + return redirect()->back() |
| 174 | + ->withErrors("删除失败"); |
| 175 | + } |
| 176 | + Event::fire(new permChangeEvent()); |
| 177 | + event(new \App\Events\userActionEvent('\App\Models\Permission', $tag->id, 2, '删除了权限:' . $tag->name . '(' . $tag->label . ')')); |
| 178 | + return redirect()->back() |
| 179 | + ->withSuccess("删除成功"); |
| 180 | + } |
| 181 | +} |
0 commit comments