2016-09-06 15:26:39 +05:30
|
|
|
<?php
|
2019-12-05 03:45:45 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-09-06 15:26:39 +05:30
|
|
|
namespace api\modules\session\models;
|
|
|
|
|
|
|
|
use api\modules\session\exceptions\ForbiddenOperationException;
|
|
|
|
use api\modules\session\exceptions\IllegalArgumentException;
|
|
|
|
use api\modules\session\models\protocols\HasJoinedInterface;
|
|
|
|
use api\modules\session\Module as Session;
|
|
|
|
use common\models\Account;
|
2019-12-05 03:45:45 +05:30
|
|
|
use Webmozart\Assert\Assert;
|
2017-11-19 18:06:51 +05:30
|
|
|
use Yii;
|
2016-09-06 15:26:39 +05:30
|
|
|
use yii\base\Model;
|
|
|
|
|
|
|
|
class HasJoinedForm extends Model {
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function __construct(
|
|
|
|
private readonly HasJoinedInterface $protocol,
|
|
|
|
array $config = [],
|
|
|
|
) {
|
2016-09-06 15:26:39 +05:30
|
|
|
parent::__construct($config);
|
|
|
|
}
|
|
|
|
|
2019-12-05 03:45:45 +05:30
|
|
|
/**
|
|
|
|
* @return Account
|
|
|
|
* @throws ForbiddenOperationException
|
|
|
|
* @throws IllegalArgumentException
|
|
|
|
*/
|
2017-09-19 22:36:16 +05:30
|
|
|
public function hasJoined(): Account {
|
2017-11-19 18:06:51 +05:30
|
|
|
Yii::$app->statsd->inc('sessionserver.hasJoined.attempt');
|
2016-09-06 15:26:39 +05:30
|
|
|
if (!$this->protocol->validate()) {
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$serverId = $this->protocol->getServerId();
|
|
|
|
$username = $this->protocol->getUsername();
|
|
|
|
|
2017-11-19 18:06:51 +05:30
|
|
|
Session::info("Server with server_id = '{$serverId}' trying to verify has joined user with username = '{$username}'.");
|
2016-09-06 15:26:39 +05:30
|
|
|
|
|
|
|
$joinModel = SessionModel::find($username, $serverId);
|
|
|
|
if ($joinModel === null) {
|
|
|
|
Session::error("Not found join operation for username = '{$username}'.");
|
2017-11-19 18:06:51 +05:30
|
|
|
Yii::$app->statsd->inc('sessionserver.hasJoined.fail_no_join');
|
2016-09-06 15:26:39 +05:30
|
|
|
throw new ForbiddenOperationException('Invalid token.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$joinModel->delete();
|
2019-12-05 03:45:45 +05:30
|
|
|
/** @var Account $account */
|
2016-09-06 15:26:39 +05:30
|
|
|
$account = $joinModel->getAccount();
|
2019-12-05 03:45:45 +05:30
|
|
|
Assert::notNull($account);
|
2016-09-06 15:26:39 +05:30
|
|
|
|
2017-11-19 18:06:51 +05:30
|
|
|
Session::info("User with username = '{$username}' successfully verified by server with server_id = '{$serverId}'.");
|
|
|
|
Yii::$app->statsd->inc('sessionserver.hasJoined.success');
|
2016-09-06 15:26:39 +05:30
|
|
|
|
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|