Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 40 |
| RoleController | |
0.00% |
0 / 1 |
|
0.00% |
0 / 9 |
306.00 | |
0.00% |
0 / 40 |
| index | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 3 |
|||
| create | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 3 |
|||
| store | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 10 |
|||
| validatorCreate | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| validatorUpdate | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| show | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| edit | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 4 |
|||
| update | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 11 |
|||
| destroy | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 4 |
|||
| <?php | |
| namespace App\Http\Controllers; | |
| use App\Authority; | |
| use App\Role; | |
| use Exception; | |
| use Illuminate\Http\Request; | |
| use Illuminate\Http\Response; | |
| use Illuminate\Support\Facades\Auth; | |
| use Illuminate\Support\Facades\Gate; | |
| use Illuminate\Support\Facades\Validator; | |
| class RoleController extends Controller | |
| { | |
| /** | |
| * Display a listing of the resource. | |
| * | |
| * @return Response | |
| */ | |
| public function index() | |
| { | |
| if(!Gate::allows('角色-查询')){ return redirect(url('/')); } | |
| $roles=Role::with('authorities')->orderBy('id','desc')->paginate(35); | |
| return view('maintenance.role.index',['roles'=>$roles]); | |
| } | |
| /** | |
| * Show the form for creating a new resource. | |
| * | |
| * @return Response | |
| */ | |
| public function create() | |
| { | |
| if(!Gate::allows('角色-录入')){ return redirect(url('/')); } | |
| $authoritiesAll=Authority::orderBy('alias_name','desc')->get(); | |
| return view('maintenance.role.create',compact('authoritiesAll')); | |
| } | |
| /** | |
| * Store a newly created resource in storage. | |
| * | |
| * @param Request $request | |
| * @return Response | |
| */ | |
| public function store(Request $request) | |
| { | |
| if(!Gate::allows('角色-录入')){ return redirect(url('/')); } | |
| $this->validatorCreate($request->all())->validate(); | |
| $role=new Role($request->all()); | |
| $role->save(); | |
| $authorityIds=$request->input('authority')??''; | |
| if($authorityIds){ | |
| $authorityIdArr=explode(',',$authorityIds); | |
| $role->authorities()->sync($authorityIdArr); | |
| } | |
| $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); | |
| return redirect('maintenance/role/create')->with('successTip',"成功录入角色“{$request->input('name')}”"); | |
| } | |
| protected function validatorCreate(array $data) | |
| { | |
| return Validator::make($data, [ | |
| 'name' => ['required', 'string', 'max:50', 'unique:roles'], | |
| ]); | |
| } | |
| protected function validatorUpdate(array $data) | |
| { | |
| return Validator::make($data, [ | |
| 'name' => ['required', 'string', 'max:50'], | |
| ]); | |
| } | |
| /** | |
| * Display the specified resource. | |
| * | |
| * @param Role $role | |
| * @return Response | |
| */ | |
| public function show(Role $role) | |
| { | |
| // | |
| } | |
| /** | |
| * Show the form for editing the specified resource. | |
| * | |
| * @param Role $role | |
| * @return Response | |
| */ | |
| public function edit(Role $role) | |
| { | |
| if(!Gate::allows('角色-编辑')){ return redirect(url('/')); } | |
| $authoritiesAll=Authority::all(); | |
| $authorities=$role->authorities()->get(); | |
| return view('maintenance.role.edit',compact('role','authorities','authoritiesAll')); | |
| } | |
| /** | |
| * Update the specified resource in storage. | |
| * | |
| * @param Request $request | |
| * @param Role $role | |
| * @return Response | |
| */ | |
| public function update(Request $request, Role $role) | |
| { | |
| if(!Gate::allows('角色-编辑')){ return redirect(url('/')); } | |
| $this->validatorUpdate($request->all())->validate(); | |
| $role->fill($request->all()); | |
| $role->update(); | |
| $authorityIds=$request->input('authority')??''; | |
| if($authorityIds){ | |
| $authorityIdArr=explode(',',$authorityIds); | |
| $role->authorities()->sync($authorityIdArr); | |
| }else{ | |
| $role->authorities()->sync([]); | |
| } | |
| $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); | |
| return redirect('maintenance/role/')->with('successTip',"成功修改角色“{$role['name']}”!"); | |
| } | |
| /** | |
| * Remove the specified resource from storage. | |
| * | |
| * @param Role $role | |
| * @return array|Response | |
| * @throws Exception | |
| */ | |
| public function destroy(Role $role) | |
| { | |
| if(!Gate::allows('角色-删除')){ return redirect(url('/')); } | |
| $this->log(__METHOD__,__FUNCTION__,$role->toJson(),Auth::user()['id']); | |
| $re=$role->delete(); | |
| return ['success'=>$re]; | |
| } | |
| } |