| 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 / 8 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 32
UserController | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 8 | 210.00 | 0.00% covered (danger) | 0.00% | 0 / 32
index | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 5
create | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 3
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 / 4
update | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 11
destroy | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 4

1 | <?php
2 |
3 | namespace App\Http\Controllers;
4 |
5 | use App\Exports\UserExport;
6 | use App\Role;
7 | use App\User;
8 | use Exception;
9 | use Illuminate\Http\Request;
10 | use Illuminate\Http\Response;
11 | use Illuminate\Support\Facades\Auth;
12 | use Illuminate\Support\Facades\Gate;
13 | use Illuminate\Support\Facades\Validator;
14 | use Maatwebsite\Excel\Facades\Excel;
15 |
16 | class UserController extends Controller
17 | {
18 | /**
19 | * Display a listing of the resource.
20 | *
21 | * @return Response
22 | */
23 | public function index()
24 | {
25 | if(!Gate::allows('用户-查询')){ return redirect(url('/')); }
26 | $users=User::with('roles')->orderBy('id','desc')->paginate(35);
27 | $users->each(function (User $user){
28 | $user['isSuperAdmin']=$user->isSuperAdmin();
29 | });
30 | return view('maintenance.user.index',['users'=>$users]);
31 | }
32 |
33 | /**
34 | * Show the form for creating a new resource.
35 | *
36 | * @return Response
37 | */
38 | public function create()
39 | {
40 | if(!Gate::allows('用户-录入')){ return redirect(url('/')); }
41 | $roles=Role::all();
42 | return view('maintenance.user.create',['rolesAll'=>$roles]);
43 | }
44 |
45 | // /**
46 | // * Store a newly created resource in storage.
47 | // *
48 | // * @param Request $request
49 | // * @return Response
50 | // */
51 | // public function store(Request $request)
52 | // {
53 | // $this->validatorCreate($request->all())->validate();
54 | // $user=new User($request->all());
55 | // $user->save();
56 | //
57 | // return redirect('maintenance/user/create')->with('successTip',"成功录入用户“{$request->input('name')}”");
58 | // }
59 | protected function validatorCreate(array $data)
60 | {
61 | return Validator::make($data, [
62 | 'name' => ['required', 'string', 'max:50', 'unique:users'],
63 | ]);
64 | }
65 | protected function validatorUpdate(array $data)
66 | {
67 | return Validator::make($data, [
68 | 'name' => ['required', 'string', 'max:50'],
69 | ]);
70 | }
71 | /**
72 | * Display the specified resource.
73 | *
74 | * @param User $user
75 | * @return Response
76 | */
77 | public function show(User $user)
78 | {
79 | //
80 | }
81 |
82 | /**
83 | * Show the form for editing the specified resource.
84 | *
85 | * @param User $user
86 | * @return Response
87 | */
88 | public function edit(User $user)
89 | {
90 | if(!Gate::allows('用户-编辑')){ return redirect(url('/')); }
91 | $rolesAll=Role::all();
92 | $roles=$user->roles()->get();
93 | return view('maintenance.user.edit',compact('user','rolesAll','roles'));
94 | }
95 |
96 | /**
97 | * Update the specified resource in storage.
98 | *
99 | * @param Request $request
100 | * @param User $user
101 | * @return Response
102 | */
103 | public function update(Request $request, User $user)
104 | {
105 | if(!Gate::allows('用户-编辑')){ return redirect(url('/')); }
106 | $this->validatorUpdate($request->all())->validate();
107 | $user->fill($request->all());
108 | $user->update();
109 | $rolesIds=$request->input('role')??'';
110 | if($rolesIds){
111 | $roleIdArr=explode(',',$rolesIds);
112 | $user->roles()->sync($roleIdArr);
113 | }else{
114 | $user->roles()->sync([]);
115 | }
116 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
117 | return redirect('maintenance/user/')->with('successTip',"成功修改用户“{$user['name']}”！");
118 | }
119 |
120 | /**
121 | * Remove the specified resource from storage.
122 | *
123 | * @param User $user
124 | * @return array|Response
125 | * @throws Exception
126 | */
127 | public function destroy(User $user)
128 | {
129 | if(!Gate::allows('用户-删除')){ return redirect(url('/')); }
130 | $this->log(__METHOD__,__FUNCTION__,$user->toJson(),Auth::user()['id']);
131 | $re=$user->delete();
132 | return ['success'=>$re];
133 | }
134 | }