Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 42
AuthorityController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 9
306.00
0.00% covered (danger)
0.00%
0 / 42
 index
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 3
 create
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 3
 store
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 18
 validatorCreate
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 validatorUpdate
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 show
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 edit
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 3
 update
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 6
 destroy
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 4
<?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];
    }
}