|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Uxmp\Core\Api\User\SubSonic; |
|
6
|
|
|
|
|
7
|
|
|
use PH7\Generator\Password; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Uxmp\Core\Api\AbstractApiApplication; |
|
11
|
|
|
use Uxmp\Core\Api\Lib\Message\JsonEnabledResponseInterface; |
|
12
|
|
|
use Uxmp\Core\Api\Lib\Middleware\SessionValidatorMiddleware; |
|
13
|
|
|
use Uxmp\Core\Component\Authentication\AccessKey\AccessKeyTypeEnum; |
|
14
|
|
|
use Uxmp\Core\Component\SubSonic\AuthenticationProvider; |
|
15
|
|
|
use Uxmp\Core\Orm\Model\UserInterface; |
|
16
|
|
|
use Uxmp\Core\Orm\Repository\AccessKeyRepositoryInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Creates and delivers the users subsonic api settings |
|
20
|
|
|
*/ |
|
21
|
|
|
final class SubSonicSettingsCreateApplication extends AbstractApiApplication |
|
22
|
|
|
{ |
|
23
|
2 |
|
public function __construct( |
|
24
|
|
|
private readonly AccessKeyRepositoryInterface $accessKeyRepository, |
|
25
|
|
|
) { |
|
26
|
2 |
|
} |
|
27
|
|
|
|
|
28
|
2 |
|
protected function run( |
|
29
|
|
|
ServerRequestInterface $request, |
|
30
|
|
|
JsonEnabledResponseInterface $response, |
|
31
|
|
|
array $args |
|
32
|
|
|
): ResponseInterface { |
|
33
|
|
|
/** @var UserInterface $user */ |
|
34
|
2 |
|
$user = $request->getAttribute(SessionValidatorMiddleware::USER); |
|
35
|
|
|
|
|
36
|
2 |
|
$accessKey = $this->accessKeyRepository->findOneBy([ |
|
37
|
2 |
|
'user' => $user, |
|
38
|
2 |
|
'type_id' => AccessKeyTypeEnum::SUBSONIC, |
|
39
|
2 |
|
]); |
|
40
|
|
|
|
|
41
|
2 |
|
if ($accessKey === null) { |
|
42
|
1 |
|
$accessKey = $this->accessKeyRepository->prototype() |
|
43
|
1 |
|
->setUser($user) |
|
44
|
1 |
|
->setActive(true) |
|
45
|
1 |
|
->setTypeId(AccessKeyTypeEnum::SUBSONIC) |
|
46
|
1 |
|
->setConfig([ |
|
47
|
1 |
|
AuthenticationProvider::CONFIG_KEY_TOKEN => Password::generate( |
|
48
|
1 |
|
AuthenticationProvider::SUBSONIC_KEY_LENGTH |
|
49
|
1 |
|
), |
|
50
|
1 |
|
]); |
|
51
|
|
|
|
|
52
|
1 |
|
$this->accessKeyRepository->save($accessKey); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
return $response->withJson([ |
|
56
|
2 |
|
'accessToken' => $accessKey->getConfig()[AuthenticationProvider::CONFIG_KEY_TOKEN] ?? null, |
|
57
|
2 |
|
]); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|