koel/app/Http/Controllers/API/UserController.php

106 lines
2.5 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Http\Controllers\API;
use App\Http\Requests\API\UserStoreRequest;
use App\Http\Requests\API\UserUpdateRequest;
2015-12-14 13:22:39 +00:00
use App\Models\User;
2017-06-04 01:30:45 +00:00
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
2018-08-22 19:40:04 +00:00
use Illuminate\Contracts\Hashing\Hasher as Hash;
2017-06-04 01:30:45 +00:00
use Illuminate\Http\JsonResponse;
use RuntimeException;
2015-12-13 04:42:28 +00:00
/**
* @group 7. User management
*/
2015-12-13 04:42:28 +00:00
class UserController extends Controller
{
2018-08-22 19:40:04 +00:00
private $hash;
public function __construct(Hash $hash)
{
$this->hash = $hash;
}
2015-12-13 04:42:28 +00:00
/**
* Create a new user
2015-12-13 04:42:28 +00:00
*
* @bodyParam name string required User's name. Example: John Doe
* @bodyParam email string required User's email. Example: john@doe.com
* @bodyParam password string required User's password. Example: SoSecureMuchW0w
2020-06-13 15:19:47 +00:00
* @bodyParam is_admin boolean required Whether the user is an admin
*
* @response {
* "id": 42,
* "name": "John Doe",
2020-06-13 15:19:47 +00:00
* "email": "john@doe.com",
* "is_admin": true
* }
*
2017-06-04 01:30:45 +00:00
* @throws RuntimeException
2016-08-03 10:42:39 +00:00
*
2017-06-04 01:30:45 +00:00
* @return JsonResponse
2015-12-13 04:42:28 +00:00
*/
public function store(UserStoreRequest $request)
{
return response()->json(User::create([
2016-08-03 10:42:11 +00:00
'name' => $request->name,
'email' => $request->email,
2018-08-22 19:40:04 +00:00
'password' => $this->hash->make($request->password),
2020-06-13 15:19:47 +00:00
'is_admin' => $request->is_admin,
2015-12-13 04:42:28 +00:00
]));
}
/**
* Update a user
2015-12-13 04:42:28 +00:00
*
* @bodyParam name string required New name. Example: Johny Doe
* @bodyParam email string required New email. Example: johny@doe.com
* @bodyParam password string New password (null/blank for no change)
2020-06-13 15:19:47 +00:00
* @bodyParam is_admin boolean Whether the user is an admin
*
2020-06-13 15:19:47 +00:00
* @response {
* "id": 42,
* "name": "John Doe",
* "email": "john@doe.com",
* "is_admin": true
* }
*
2017-06-04 01:30:45 +00:00
* @throws RuntimeException
2016-08-03 10:42:39 +00:00
*
2017-06-04 01:30:45 +00:00
* @return JsonResponse
2015-12-13 04:42:28 +00:00
*/
2015-12-15 00:45:10 +00:00
public function update(UserUpdateRequest $request, User $user)
2015-12-13 04:42:28 +00:00
{
2020-06-13 15:19:47 +00:00
$data = $request->only('name', 'email', 'is_admin');
2015-12-13 04:42:28 +00:00
2016-08-03 10:42:11 +00:00
if ($request->password) {
2018-08-22 19:40:04 +00:00
$data['password'] = $this->hash->make($request->password);
2015-12-13 04:42:28 +00:00
}
$user->update($data);
2020-06-13 15:19:47 +00:00
return response()->json($user);
2015-12-13 04:42:28 +00:00
}
/**
* Delete a user
2015-12-13 04:42:28 +00:00
*
* @response []
*
2017-06-04 01:30:45 +00:00
* @throws Exception
* @throws AuthorizationException
2016-08-03 10:42:39 +00:00
*
2017-06-04 01:30:45 +00:00
* @return JsonResponse
2015-12-13 04:42:28 +00:00
*/
public function destroy(User $user)
2015-12-13 04:42:28 +00:00
{
2016-09-26 06:30:00 +00:00
$this->authorize('destroy', $user);
2015-12-13 04:42:28 +00:00
$user->delete();
return response()->json();
2015-12-13 04:42:28 +00:00
}
}