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

76 lines
1.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
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.
*
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),
2015-12-13 04:42:28 +00:00
]));
}
/**
* Update a user.
*
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
{
$data = $request->only('name', 'email');
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);
return response()->json();
2015-12-13 04:42:28 +00:00
}
/**
* Delete a user.
*
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
}
}