|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Api\Manage; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Models\User; |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use Illuminate\Auth\Events\Registered; |
| 9 | + |
| 10 | +class UserController extends Controller |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Display a listing of the resource. |
| 14 | + * |
| 15 | + * @return \Illuminate\Http\Response |
| 16 | + */ |
| 17 | + public function index() |
| 18 | + { |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Store a newly created resource in storage. |
| 23 | + * |
| 24 | + * @param \Illuminate\Http\Request $request |
| 25 | + * |
| 26 | + * @return \Illuminate\Http\Response |
| 27 | + */ |
| 28 | + public function store(Request $request) |
| 29 | + { |
| 30 | + $this->validate($request, [ |
| 31 | + 'name' => 'required|string|max:255', |
| 32 | + 'email' => 'required|string|email|max:255|unique:users', |
| 33 | + 'password' => 'required|string|min:6|confirmed', |
| 34 | + ]); |
| 35 | + $data = $request->only('name', 'email', 'password'); |
| 36 | + $user = User::create([ |
| 37 | + 'name' => $data['name'], |
| 38 | + 'email' => $data['email'], |
| 39 | + 'password' => bcrypt($data['password']), |
| 40 | + ]); |
| 41 | + event(new Registered($user)); |
| 42 | + $user->syncRoles([$request->role]); |
| 43 | + return response()->api([], __('User successfully stored.'), true, 201); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Display the specified resource. |
| 48 | + * |
| 49 | + * @param string $id |
| 50 | + * |
| 51 | + * @return \Illuminate\Http\Response |
| 52 | + */ |
| 53 | + public function show($id) |
| 54 | + { |
| 55 | + $user = User::details()->findByHashSlug($id); |
| 56 | + |
| 57 | + /** |
| 58 | + * @todo should have a transformer to do this. |
| 59 | + */ |
| 60 | + $user = collect($user->only('name', 'email', 'roles_to_string', 'roles')); |
| 61 | + $roles = $user->get('roles')->mapWithKeys(function($role){ |
| 62 | + return [$role->id => $role->name]; |
| 63 | + }); |
| 64 | + $user->put('roles', $roles); |
| 65 | + |
| 66 | + return response()->api($user); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Update the specified resource in storage. |
| 71 | + * |
| 72 | + * @param \Illuminate\Http\Request $request |
| 73 | + * @param string $id |
| 74 | + * |
| 75 | + * @return \Illuminate\Http\Response |
| 76 | + */ |
| 77 | + public function update(Request $request, $id) |
| 78 | + { |
| 79 | + $this->validate($request, [ |
| 80 | + 'name' => 'required|string|max:255', |
| 81 | + ]); |
| 82 | + |
| 83 | + $fields = $request->only('name'); |
| 84 | + |
| 85 | + if(!empty($request->input('password'))) { |
| 86 | + $this->validate($request, [ |
| 87 | + 'password' => 'required|string|min:6|confirmed', |
| 88 | + ]); |
| 89 | + $fields['password'] = bcrypt($request->input('password')); |
| 90 | + } |
| 91 | + |
| 92 | + $user = User::findByHashSlug($id); |
| 93 | + $user->update($fields); |
| 94 | + $user->syncRoles($request->input('roles')); |
| 95 | + |
| 96 | + return response()->api([], __('User successfully updated.'), true, 201); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Remove the specified resource from storage. |
| 101 | + * |
| 102 | + * @param string $id |
| 103 | + * |
| 104 | + * @return \Illuminate\Http\Response |
| 105 | + */ |
| 106 | + public function destroy($id) |
| 107 | + { |
| 108 | + if ($id == user()->hashslug) { |
| 109 | + return response()->api([], __('You cannot delete yourself!'), false, 401); |
| 110 | + } |
| 111 | + $user = User::findByHashSlug($id); |
| 112 | + if ($user->hasRole('developer')) { |
| 113 | + return response()->api([], __('Trust me, don\'t kill your developer!'), false, 401); |
| 114 | + } |
| 115 | + $user->delete(); |
| 116 | + |
| 117 | + return response()->api([], __('You have successfully delete a user.')); |
| 118 | + } |
| 119 | +} |
0 commit comments