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

67 lines
1.5 KiB
PHP
Raw Normal View History

2016-05-30 05:50:59 +00:00
<?php
namespace App\Http\Controllers\API;
use App\Http\Requests\API\ProfileUpdateRequest;
2018-08-22 19:46:36 +00:00
use Illuminate\Contracts\Hashing\Hasher as Hash;
2017-06-04 01:30:45 +00:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
2017-06-04 01:30:45 +00:00
use RuntimeException;
2016-05-30 05:50:59 +00:00
/**
* @group 7. User management
*/
2016-05-30 05:50:59 +00:00
class ProfileController extends Controller
{
2018-08-22 19:46:36 +00:00
private $hash;
public function __construct(Hash $hash)
{
$this->hash = $hash;
}
/**
* Get current user's profile
*
* @response {
* "id": 42,
* "name": "John Doe",
* "email": "john@doe.com"
* }
*
* @return JsonResponse
*/
public function show(Request $request)
{
return response()->json($request->user());
}
2016-05-30 05:50:59 +00:00
/**
* Update current user's profile
*
* @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)
*
* @response []
2016-05-30 05:50:59 +00:00
*
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
2016-05-30 05:50:59 +00:00
*/
public function update(ProfileUpdateRequest $request)
{
if (config('koel.misc.demo')) {
return response()->json();
}
2016-05-30 05:50:59 +00:00
$data = $request->only('name', 'email');
2017-04-29 02:55:41 +00:00
if ($request->password) {
2018-08-22 19:46:36 +00:00
$data['password'] = $this->hash->make($request->password);
2016-05-30 05:50:59 +00:00
}
2017-05-02 03:23:10 +00:00
return response()->json($request->user()->update($data));
2016-05-30 05:50:59 +00:00
}
}