| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace App\Http\Controllers;
- use App\Authority;
- use App\Owner;
- use App\User;
- 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 AuthorityController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return string
- */
- public function index()
- {
- if(!Gate::allows('权限-查询')){ return redirect(url('/')); }
- $authorities=Authority::orderBy('id','asc')->paginate(100);
- return view('maintenance.authority.index',['authorities'=>$authorities]);
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return string
- */
- public function create()
- {
- if(!Gate::allows('权限-录入')){ return redirect(url('/')); }
- $owners=Owner::all();
- return view('maintenance.authority.create',compact('owners'));
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param Request $request
- * @return string
- */
- public function store(Request $request)
- {
- if(!Gate::allows('权限-录入')){ return redirect(url('/')); }
- $inputs=$request->all();
- $inputs['combinedName']=$request->input('name').'_'.$request->input('id_owner');
- $this->validatorCreate($inputs)->validate();
- $successName= $request->input('name')??'';
- $inputs['name']=$inputs['combinedName'];
- if($request->input('id_owner')??''){
- $owner=Owner::find($inputs['id_owner']);
- if(isset($inputs['remark'])){
- $inputs['remark'].="(key: {$inputs['combinedName']})";
- }else{
- $inputs['remark']="(key: {$inputs['combinedName']})";
- }
- $inputs['alias_name']=$request->input('name')."_(货主:$owner->name)";
- $successName.="(货主:$owner->name)";
- }else{
- $inputs['alias_name']=$request->input('name');
- }
- $authority=new Authority($inputs);
- $authority->save();
- $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
- return redirect('maintenance/authority/create')->with('successTip',"成功录入权限“{$successName}”");
- }
- protected function validatorCreate(array $data)
- {
- return Validator::make($data, [
- 'name' => ['max:50', 'required_without:id_owner'],
- 'id_owner' => ['max:11', 'required_without:name'],
- 'combinedName' => ['max:50', 'unique:authorities,name'],
- ]);
- }
- protected function validatorUpdate(array $data)
- {
- return Validator::make($data, [
- 'name' => ['required', 'string', 'max:50'],
- ]);
- }
- /**
- * Display the specified resource.
- *
- * @param Authority $authority
- * @return Response
- */
- public function show(Authority $authority)
- {
- //
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param \App\Http\Controllers\Authority $authority
- * @return Response
- */
- public function edit(Authority $authority)
- {
- if(!Gate::allows('权限-编辑')){ return redirect(url('/')); }
- $owners=Owner::all();
- return view('maintenance.authority.edit',compact('owners','authority'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param Request $request
- * @param \App\Http\Controllers\Authority $authority
- * @return Response
- */
- public function update(Request $request, Authority $authority)
- {
- if(!Gate::allows('权限-编辑')){ return redirect(url('/')); }
- $this->validatorUpdate($request->all())->validate();
- $authority->fill($request->all());
- $authority->update();
- $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
- return redirect('maintenance/authority/')->with('successTip',"成功修改权限“{$authority['name']}”!");
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param \App\Http\Controllers\Authority $authority
- * @return array|Response
- * @throws Exception
- */
- public function destroy(Authority $authority)
- {
- if(!Gate::allows('权限-删除')){ return redirect(url('/')); }
- $this->log(__METHOD__,__FUNCTION__,$authority->toJson(),Auth::user()['id']);
- $re=$authority->delete();
- return ['success'=>$re];
- }
- }
|