eddy8 /
LightCMS
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Date: 2019/2/25 Time: 14:49 |
||
| 4 | * |
||
| 5 | * @author Eddy <[email protected]> |
||
| 6 | * @version v1.0.0 |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace App\Http\Controllers\Admin; |
||
| 10 | |||
| 11 | use App\Http\Controllers\Controller; |
||
| 12 | use App\Http\Requests\Admin\RoleRequest; |
||
| 13 | use App\Repository\Admin\MenuRepository; |
||
| 14 | use App\Repository\Admin\RoleRepository; |
||
| 15 | use Illuminate\Database\QueryException; |
||
| 16 | use Illuminate\Http\Request; |
||
| 17 | use Illuminate\Support\Str; |
||
| 18 | use Illuminate\View\View; |
||
| 19 | |||
| 20 | class RoleController extends Controller |
||
| 21 | { |
||
| 22 | protected $formNames = ['name']; |
||
| 23 | |||
| 24 | public function __construct() |
||
| 25 | { |
||
| 26 | parent::__construct(); |
||
| 27 | |||
| 28 | $this->breadcrumb[] = ['title' => '角色列表', 'url' => route('admin::role.index')]; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * 角色管理-角色列表 |
||
| 33 | * |
||
| 34 | */ |
||
| 35 | public function index() |
||
| 36 | { |
||
| 37 | $this->breadcrumb[] = ['title' => '角色列表', 'url' => '']; |
||
| 38 | return view('admin.role.index', ['breadcrumb' => $this->breadcrumb]); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * 角色管理-角色列表数据接口 |
||
| 43 | * |
||
| 44 | * @param Request $request |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | public function list(Request $request) |
||
| 48 | { |
||
| 49 | $perPage = (int) $request->get('limit', 50); |
||
| 50 | $this->formNames[] = 'created_at'; |
||
| 51 | $condition = $request->only($this->formNames); |
||
| 52 | |||
| 53 | $data = RoleRepository::list($perPage, $condition); |
||
| 54 | |||
| 55 | return $data; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * 角色管理-新增角色 |
||
| 60 | * |
||
| 61 | */ |
||
| 62 | public function create() |
||
| 63 | { |
||
| 64 | $this->breadcrumb[] = ['title' => '新增角色', 'url' => '']; |
||
| 65 | return view('admin.role.add', ['breadcrumb' => $this->breadcrumb]); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * 角色管理-保存角色 |
||
| 70 | * |
||
| 71 | * @param RoleRequest $request |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | public function save(RoleRequest $request) |
||
| 75 | { |
||
| 76 | try { |
||
| 77 | $data = $request->only($this->formNames); |
||
| 78 | $data['guard_name'] = 'admin'; |
||
| 79 | RoleRepository::add($data); |
||
| 80 | return [ |
||
| 81 | 'code' => 0, |
||
| 82 | 'msg' => '新增成功', |
||
| 83 | 'redirect' => true |
||
| 84 | ]; |
||
| 85 | } catch (QueryException $e) { |
||
| 86 | return [ |
||
| 87 | 'code' => 1, |
||
| 88 | 'msg' => '新增失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前角色已存在' : '其它错误'), |
||
| 89 | 'redirect' => false |
||
| 90 | ]; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * 角色管理-编辑角色 |
||
| 96 | * |
||
| 97 | * @param int $id |
||
| 98 | * @return View |
||
| 99 | */ |
||
| 100 | public function edit($id) |
||
| 101 | { |
||
| 102 | $this->breadcrumb[] = ['title' => '编辑角色', 'url' => '']; |
||
| 103 | |||
| 104 | $model = RoleRepository::find($id); |
||
| 105 | return view('admin.role.add', ['id' => $id, 'model' => $model, 'breadcrumb' => $this->breadcrumb]); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * 角色管理-更新角色 |
||
| 110 | * |
||
| 111 | * @param RoleRequest $request |
||
| 112 | * @param int $id |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public function update(RoleRequest $request, $id) |
||
| 116 | { |
||
| 117 | $data = $request->only($this->formNames); |
||
| 118 | try { |
||
| 119 | RoleRepository::update($id, $data); |
||
| 120 | return [ |
||
| 121 | 'code' => 0, |
||
| 122 | 'msg' => '编辑成功', |
||
| 123 | 'redirect' => true |
||
| 124 | ]; |
||
| 125 | } catch (QueryException $e) { |
||
| 126 | return [ |
||
| 127 | 'code' => 1, |
||
| 128 | 'msg' => '编辑失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前角色已存在' : '其它错误'), |
||
| 129 | 'redirect' => false |
||
| 130 | ]; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * 角色管理-分配权限 |
||
| 136 | * |
||
| 137 | * @param $id |
||
| 138 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 139 | */ |
||
| 140 | public function permission($id) |
||
| 141 | { |
||
| 142 | $this->breadcrumb[] = ['title' => '分配权限', 'url' => '']; |
||
| 143 | $role = RoleRepository::find($id); |
||
| 144 | return view('admin.role.permission', [ |
||
| 145 | 'id' => $id, |
||
| 146 | 'breadcrumb' => $this->breadcrumb, |
||
| 147 | 'rolePermissions' => $role->permissions, |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 148 | 'role' => $role, |
||
| 149 | ]); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * 角色管理-更新权限 |
||
| 154 | * |
||
| 155 | * @param Request $request |
||
| 156 | * @param $id |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | public function updatePermission(Request $request, $id) |
||
| 160 | { |
||
| 161 | $role = RoleRepository::find($id); |
||
| 162 | $permissions = $request->input('permission'); |
||
| 163 | $role->syncPermissions(MenuRepository::get(array_keys($permissions))); |
||
| 164 | return [ |
||
| 165 | 'code' => 0, |
||
| 166 | 'msg' => '操作成功', |
||
| 167 | 'redirect' => true |
||
| 168 | ]; |
||
| 169 | } |
||
| 170 | } |
||
| 171 |