2018-02-28 03:57:35 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace api\modules\oauth\models;
|
|
|
|
|
|
|
|
use api\modules\oauth\exceptions\InvalidOauthClientState;
|
|
|
|
use common\models\OauthClient;
|
|
|
|
use common\tasks\ClearOauthSessions;
|
2019-12-13 23:18:13 +05:30
|
|
|
use Webmozart\Assert\Assert;
|
2018-02-28 03:57:35 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\helpers\Inflector;
|
|
|
|
|
|
|
|
class OauthClientForm {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var OauthClient
|
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
|
|
|
|
public function __construct(OauthClient $client) {
|
|
|
|
if ($client->type === null) {
|
|
|
|
throw new InvalidOauthClientState('client\'s type field must be set');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->client = $client;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClient(): OauthClient {
|
|
|
|
return $this->client;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save(OauthClientTypeForm $form): bool {
|
|
|
|
if (!$form->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$client = $this->getClient();
|
|
|
|
$form->applyToClient($client);
|
|
|
|
|
|
|
|
if ($client->isNewRecord) {
|
|
|
|
$baseId = $id = substr(Inflector::slug($client->name), 0, 250);
|
|
|
|
$i = 0;
|
|
|
|
while ($this->isClientExists($id)) {
|
|
|
|
$id = $baseId . ++$i;
|
|
|
|
}
|
|
|
|
|
|
|
|
$client->id = $id;
|
|
|
|
$client->generateSecret();
|
|
|
|
}
|
|
|
|
|
2019-12-13 23:18:13 +05:30
|
|
|
Assert::true($client->save(), 'Cannot save oauth client');
|
2018-02-28 03:57:35 +05:30
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete(): bool {
|
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
|
|
|
|
2019-12-14 02:46:05 +05:30
|
|
|
$client = $this->getClient();
|
2018-02-28 03:57:35 +05:30
|
|
|
$client->is_deleted = true;
|
2019-12-13 23:18:13 +05:30
|
|
|
Assert::true($client->save(), 'Cannot update oauth client');
|
2018-02-28 03:57:35 +05:30
|
|
|
|
|
|
|
Yii::$app->queue->push(ClearOauthSessions::createFromOauthClient($client));
|
|
|
|
|
|
|
|
$transaction->commit();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function reset(bool $regenerateSecret = false): bool {
|
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
|
|
|
|
2019-12-14 02:46:05 +05:30
|
|
|
$client = $this->getClient();
|
2018-02-28 03:57:35 +05:30
|
|
|
if ($regenerateSecret) {
|
|
|
|
$client->generateSecret();
|
2019-12-13 23:18:13 +05:30
|
|
|
Assert::true($client->save(), 'Cannot update oauth client');
|
2018-02-28 03:57:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
Yii::$app->queue->push(ClearOauthSessions::createFromOauthClient($client, time()));
|
|
|
|
|
|
|
|
$transaction->commit();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function isClientExists(string $id): bool {
|
2018-11-11 16:18:23 +05:30
|
|
|
return OauthClient::find()
|
|
|
|
->includeDeleted()
|
|
|
|
->andWhere(['id' => $id])
|
|
|
|
->exists();
|
2018-02-28 03:57:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|