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

58 lines
1.6 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;
2020-09-13 22:04:07 +00:00
use App\Models\User;
use App\Services\TokenManager;
2020-09-13 22:04:07 +00:00
use Illuminate\Contracts\Auth\Authenticatable;
2018-08-22 19:46:36 +00:00
use Illuminate\Contracts\Hashing\Hasher as Hash;
use Illuminate\Validation\ValidationException;
2016-05-30 05:50:59 +00:00
class ProfileController extends Controller
{
2021-06-05 10:47:56 +00:00
private Hash $hash;
private TokenManager $tokenManager;
2018-08-22 19:46:36 +00:00
2020-09-13 22:04:07 +00:00
/** @var User */
2021-06-05 10:47:56 +00:00
private ?Authenticatable $currentUser;
2020-09-13 22:04:07 +00:00
public function __construct(Hash $hash, TokenManager $tokenManager, ?Authenticatable $currentUser)
2018-08-22 19:46:36 +00:00
{
$this->hash = $hash;
$this->tokenManager = $tokenManager;
2020-09-13 22:04:07 +00:00
$this->currentUser = $currentUser;
2018-08-22 19:46:36 +00:00
}
2020-09-13 22:04:07 +00:00
public function show()
{
2020-09-13 22:04:07 +00:00
return response()->json($this->currentUser);
}
2016-05-30 05:50:59 +00:00
public function update(ProfileUpdateRequest $request)
{
if (config('koel.misc.demo')) {
return response()->json();
}
throw_unless(
$this->hash->check($request->current_password, $this->currentUser->password),
ValidationException::withMessages(['current_password' => 'Invalid current password'])
);
2016-05-30 05:50:59 +00:00
$data = $request->only('name', 'email');
if ($request->new_password) {
$data['password'] = $this->hash->make($request->new_password);
2016-05-30 05:50:59 +00:00
}
2020-09-13 22:04:07 +00:00
$this->currentUser->update($data);
$responseData = $request->new_password
? ['token' => $this->tokenManager->refreshToken($this->currentUser)->plainTextToken]
: [];
return response()->json($responseData);
2016-05-30 05:50:59 +00:00
}
}