rodineiti /
backend-api-frontend-laravel
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Http\Controllers; |
||
| 4 | |||
| 5 | use Illuminate\Http\Request; |
||
| 6 | use App\User; |
||
| 7 | use Session; |
||
| 8 | |||
| 9 | class UsersController extends Controller |
||
| 10 | { |
||
| 11 | private $user; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Create a new controller instance. |
||
| 15 | * |
||
| 16 | * @return void |
||
| 17 | */ |
||
| 18 | public function __construct(User $user) |
||
| 19 | { |
||
| 20 | $this->middleware('auth'); |
||
| 21 | $this->middleware('admin'); |
||
| 22 | $this->user = $user; |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Display a listing of the resource. |
||
| 27 | * |
||
| 28 | * @return \Illuminate\Http\Response |
||
| 29 | */ |
||
| 30 | public function index() |
||
| 31 | { |
||
| 32 | $users = $this->user->whereNotIn('id', [auth()->user()->id, 1])->orderBy('name', 'asc')->paginate(10); |
||
| 33 | return view('admin.users.index', compact('users')); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Show the form for creating a new resource. |
||
| 38 | * |
||
| 39 | * @return \Illuminate\Http\Response |
||
| 40 | */ |
||
| 41 | public function create() |
||
| 42 | { |
||
| 43 | return view('admin.users.create'); |
||
|
0 ignored issues
–
show
|
|||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Store a newly created resource in storage. |
||
| 48 | * |
||
| 49 | * @param \Illuminate\Http\Request $request |
||
| 50 | * @return \Illuminate\Http\Response |
||
| 51 | */ |
||
| 52 | public function store(Request $request) |
||
| 53 | { |
||
| 54 | $this->validate($request, [ |
||
| 55 | 'name' => 'required|string|max:255', |
||
| 56 | 'email' => 'required|string|email|max:255|unique:users', |
||
| 57 | 'password' => 'required|string|min:6|confirmed', |
||
| 58 | ]); |
||
| 59 | |||
| 60 | $data = $request->all(); |
||
| 61 | $data['password'] = bcrypt($data['password']); |
||
| 62 | |||
| 63 | $this->user->create($data); |
||
| 64 | |||
| 65 | Session::flash('success', 'Usuário criado com sucesso'); |
||
| 66 | |||
| 67 | return redirect()->route('users.index'); |
||
|
0 ignored issues
–
show
|
|||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Show the form for editing the specified resource. |
||
| 72 | * |
||
| 73 | * @param int $id |
||
| 74 | * @return \Illuminate\Http\Response |
||
| 75 | */ |
||
| 76 | public function edit($id) |
||
| 77 | { |
||
| 78 | $user = $this->user->whereNotIn('id', [auth()->user()->id, 1])->findOrFail($id); |
||
| 79 | return view('admin.users.edit')->with('user', $user); |
||
|
0 ignored issues
–
show
|
|||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Update the specified resource in storage. |
||
| 84 | * |
||
| 85 | * @param \Illuminate\Http\Request $request |
||
| 86 | * @param int $id |
||
| 87 | * @return \Illuminate\Http\Response |
||
| 88 | */ |
||
| 89 | public function update(Request $request, $id) |
||
| 90 | { |
||
| 91 | $this->validate($request, [ |
||
| 92 | 'name' => 'required|string|max:255', |
||
| 93 | 'email' => 'required|string|email|max:255|unique:users,email,'.$id |
||
| 94 | ]); |
||
| 95 | |||
| 96 | $user = $this->user->whereNotIn('id', [auth()->user()->id, 1])->findOrFail($id); |
||
| 97 | $user->update($request->all()); |
||
| 98 | |||
| 99 | Session::flash('success', 'Usuário atualizado com sucesso'); |
||
| 100 | |||
| 101 | return redirect()->route('users.index'); |
||
|
0 ignored issues
–
show
|
|||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Remove the specified resource from storage. |
||
| 106 | * |
||
| 107 | * @param int $id |
||
| 108 | * @return \Illuminate\Http\Response |
||
| 109 | */ |
||
| 110 | public function destroy($id) |
||
| 111 | { |
||
| 112 | $user = $this->user->whereNotIn('id', [auth()->user()->id, 1])->findOrFail($id); |
||
| 113 | $user->delete(); |
||
| 114 | |||
| 115 | Session::flash('success', 'Usuário deletado com sucesso'); |
||
| 116 | |||
| 117 | return redirect()->back(); |
||
|
0 ignored issues
–
show
|
|||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Update the specified resource in storage. |
||
| 122 | * |
||
| 123 | * @param \Illuminate\Http\Request $request |
||
| 124 | * @return \Illuminate\Http\Response |
||
| 125 | */ |
||
| 126 | public function toggle(Request $request) |
||
| 127 | { |
||
| 128 | $this->validate($request, [ |
||
| 129 | "id" => "exists:users,id" |
||
| 130 | ]); |
||
| 131 | |||
| 132 | $user = $this->user->whereNotIn('id', [auth()->user()->id, 1])->findOrFail($request->id); |
||
| 133 | $user->update(['status' => !$user->status]); |
||
| 134 | |||
| 135 | return response()->json(['status' => 'success', 'message' => 'Usuário atualizado com sucesso']); |
||
|
0 ignored issues
–
show
|
|||
| 136 | } |
||
| 137 | } |
||
| 138 |