Skip to content

Commit

Permalink
Merge pull request #1285 from proditis/master
Browse files Browse the repository at this point in the history
tokens and api work
  • Loading branch information
proditis authored Nov 4, 2024
2 parents b8e194a + 6f472f9 commit 1562ac4
Show file tree
Hide file tree
Showing 69 changed files with 2,983 additions and 833 deletions.
10 changes: 4 additions & 6 deletions backend/commands/PlayerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use app\modules\frontend\models\Profile;
use app\modules\frontend\models\Team;
use app\modules\frontend\models\TeamPlayer;
use app\modules\frontend\models\PlayerIp;
use app\modules\frontend\models\PlayerSsl;
use yii\helpers\ArrayHelper;
use yii\console\widgets\Table;
Expand Down Expand Up @@ -212,15 +211,14 @@ public function actionRegister($username, $email, $fullname, $password = false,

$player->active = intval($active);
$player->status = 10;
if (!$player->active) {
$player->verification_token = str_replace('_', '-', Yii::$app->security->generateRandomString() . '-' . (time()));
$player->status = 9;
}

$player->auth_key = Yii::$app->security->generateRandomString();

if (!$player->saveWithSsl()) {
print_r($player->getErrors());
if (!$player->active) {
$player->generateEmailVerificationToken();
$player->status = 9;
}
throw new ConsoleException('Failed to save player:' . $player->username, "\n");
}

Expand Down
1 change: 0 additions & 1 deletion backend/commands/SslController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use app\modules\frontend\models\Player;
use app\modules\frontend\models\Crl;
use app\modules\frontend\models\PlayerSsl;
use app\modules\frontend\models\PlayerIp;
use app\modules\gameplay\models\Target;
use app\modules\settings\models\Sysconfig;
use yii\console\Exception as ConsoleException;
Expand Down
1 change: 0 additions & 1 deletion backend/commands/SysconfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use yii\console\Controller;
use yii\console\ExitCode;
use app\modules\frontend\models\Player;
use app\modules\frontend\models\PlayerIp;
use app\modules\gameplay\models\Target;
use app\modules\settings\models\Sysconfig;
use yii\console\Exception as ConsoleException;
Expand Down
33 changes: 33 additions & 0 deletions backend/migrations-init/m241103_021658_target_api_url_routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use yii\db\Migration;

/**
* Class m241103_021658_target_api_url_routes
*/
class m241103_021658_target_api_url_routes extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->upsert('url_route',['source'=>'profile/generate-token','destination'=>'profile/generate-token','weight'=>339]);
//$this->upsert('url_route',['source'=>'api/targets','destination'=>'api/target/index','weight'=>642]);
$this->upsert('url_route',['source'=>'api/target/claim','destination'=>'api/target/claim','weight'=>643]);
$this->upsert('url_route',['source'=>'api/target/instances','destination'=>'api/target/instances','weight'=>643]);
$this->upsert('url_route',['source'=>'api/target/<id:\d+>','destination'=>'api/target/view','weight'=>644]);
$this->upsert('url_route',['source'=>'api/target/<id:\d+>/spin','destination'=>'api/target/spin','weight'=>645]);
$this->upsert('url_route',['source'=>'api/target/<id:\d+>/spawn','destination'=>'api/target/spawn','weight'=>646]);
$this->upsert('url_route',['source'=>'api/target/<id:\d+>/shut','destination'=>'api/target/shut','weight'=>647]);
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m241103_021658_target_api_url_routes cannot be reverted.\n";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use yii\db\Migration;

/**
* Class m241104_211706_add_default_token_validities
*/
class m241104_211706_add_default_token_validities extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->upsert('sysconfig',['id'=>'password_reset_token_validity','val'=>'24 hour'],true);
$this->upsert('sysconfig',['id'=>'mail_verification_token_validity','val'=>'10 day'],true);
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m241104_211706_add_default_token_validities cannot be reverted.\n";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class m241030_064326_update_tau_player_add_deleted_status extends Migration
ELSEIF NEW.status=10 AND OLD.status=0 THEN
INSERT INTO stream SELECT * FROM archived_stream WHERE player_id=NEW.id;
DELETE FROM archived_stream WHERE player_id=NEW.id;
ELSEIF NEW.status=10 AND (OLD.status=9 OR OLD.status=8) THEN
DELETE FROM player_token WHERE player_id=NEW.id AND `type`='email_verification';
END IF;
END";

Expand Down
34 changes: 34 additions & 0 deletions backend/migrations/m241103_000427_create_player_token_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use yii\db\Migration;

/**
* Handles the creation of table `{{%player_token}}`.
*/
class m241103_000427_create_player_token_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->createTable('{{%player_token}}', [
'player_id' => $this->integer()->unsigned()->notNull(),
'type' => $this->string(32)->notNull()->defaultValue('API'),
'token' => $this->string(128)->notNull()->unique(),
'description' => $this->text()->notNull()->defaultValue(""),
'expires_at' => $this->dateTime(),
'created_at' => $this->timestamp(),
]);
$this->addPrimaryKey('player_token-pk', 'player_token', ['player_id', 'type']);
$this->addForeignKey('fk-player_token-player_id-player', '{{%player_token}}', 'player_id', 'player', 'id', 'CASCADE', 'CASCADE');
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropTable('{{%player_token}}');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use yii\db\Migration;

/**
* Handles the creation of table `{{%player_token_history}}`.
*/
class m241103_105725_create_player_token_history_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->createTable('{{%player_token_history}}', [
'id'=>$this->primaryKey(),
'player_id' => $this->integer()->unsigned()->notNull(),
'type' => $this->string(32)->notNull()->defaultValue('API'),
'token' => $this->string(128)->notNull(),
'description' => $this->text()->notNull()->defaultValue(''),
'expires_at' => $this->dateTime(),
'created_at' => $this->timestamp(),
'ts' => $this->timestamp(),
]);
$this->addForeignKey('fk-player_token_history-player_id-player', '{{%player_token_history}}', 'player_id', 'player', 'id', 'CASCADE', 'CASCADE');
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropTable('{{%player_token}}');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use yii\db\Migration;

/**
* Class m241103_105924_create_tai_player_token_trigger
*/
class m241103_105924_create_tai_player_token_trigger extends Migration
{
public $DROP_SQL="DROP TRIGGER IF EXISTS {{%tai_player_token}}";
public $CREATE_SQL="CREATE TRIGGER {{%tai_player_token}} AFTER INSERT ON {{%player_token}} FOR EACH ROW
thisBegin:BEGIN
IF (@TRIGGER_CHECKS = FALSE) THEN
LEAVE thisBegin;
END IF;
INSERT INTO player_token_history (player_id,`type`,token,`description`,expires_at,created_at,ts) VALUES (NEW.player_id,NEW.type,NEW.token,NEW.description,NEW.expires_at,NEW.created_at,NOW());
END";

public function up()
{
$this->db->createCommand($this->DROP_SQL)->execute();
$this->db->createCommand($this->CREATE_SQL)->execute();
}

public function down()
{
$this->db->createCommand($this->DROP_SQL)->execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use yii\db\Migration;

/**
* Class m241103_105954_create_tau_player_token_trigger
*/
class m241103_105954_create_tau_player_token_trigger extends Migration
{
public $DROP_SQL="DROP TRIGGER IF EXISTS {{%tau_player_token}}";
public $CREATE_SQL="CREATE TRIGGER {{%tau_player_token}} AFTER UPDATE ON {{%player_token}} FOR EACH ROW
thisBegin:BEGIN
IF (@TRIGGER_CHECKS = FALSE) THEN
LEAVE thisBegin;
END IF;
IF (NEW.token != OLD.token) THEN
INSERT INTO player_token_history (player_id,`type`,token,description,expires_at,created_at,ts) VALUES (NEW.player_id,NEW.type,NEW.token,NEW.description,NEW.expires_at,NEW.created_at,NOW());
END IF;
END";

public function up()
{
$this->db->createCommand($this->DROP_SQL)->execute();
$this->db->createCommand($this->CREATE_SQL)->execute();
}

public function down()
{
$this->db->createCommand($this->DROP_SQL)->execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use yii\db\Migration;

/**
* Class m241103_110652_create_tad_player_token_trigger
*/
class m241103_110652_create_tad_player_token_trigger extends Migration
{
public $DROP_SQL="DROP TRIGGER IF EXISTS {{%tad_player_token}}";
public $CREATE_SQL="CREATE TRIGGER {{%tad_player_token}} AFTER DELETE ON {{%player_token}} FOR EACH ROW
thisBegin:BEGIN
IF (@TRIGGER_CHECKS = FALSE) THEN
LEAVE thisBegin;
END IF;
INSERT INTO player_token_history (player_id,`type`,token,description,expires_at,created_at,ts) VALUES (OLD.player_id,OLD.type,OLD.token,OLD.description,OLD.expires_at,OLD.created_at,NOW());
END";

public function up()
{
$this->db->createCommand($this->DROP_SQL)->execute();
$this->db->createCommand($this->CREATE_SQL)->execute();
}

public function down()
{
$this->db->createCommand($this->DROP_SQL)->execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use yii\db\Migration;

/**
* Class m241103_121625_create_expire_player_tokens_procedure
*/
class m241103_121625_create_expire_player_tokens_procedure extends Migration
{
public $DROP_SQL = "DROP PROCEDURE IF EXISTS {{%expire_player_tokens}}";
public $CREATE_SQL = "CREATE PROCEDURE {{%expire_player_tokens}} ()
BEGIN
DECLARE tnow TIMESTAMP;
SET tnow=NOW();
IF (SELECT COUNT(*) FROM player_token WHERE expires_at<tnow and `type`='API')>0 THEN
START TRANSACTION;
INSERT INTO notification (player_id,category,title,body,archived,created_at,updated_at) SELECT player_id,'info','Token expiration',CONCAT(type,' Token [',description,'] expired at ',expires_at),0,tnow,tnow FROM player_token WHERE expires_at<tnow and `type`='API';
DELETE FROM player_token WHERE expires_at<tnow and `type`='API';
COMMIT;
END IF;
IF (SELECT COUNT(*) FROM player_token WHERE expires_at<tnow)>0 THEN
DELETE FROM player_token WHERE expires_at<tnow;
END IF;
END";

public function up()
{
$this->db->createCommand($this->DROP_SQL)->execute();
$this->db->createCommand($this->CREATE_SQL)->execute();
}

public function down()
{
$this->db->createCommand($this->DROP_SQL)->execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use yii\db\Migration;

/**
* Class m241103_121633_create_ev_player_token_expiration_event
*/
class m241103_121633_create_ev_player_token_expiration_event extends Migration
{
public $DROP_SQL = "DROP EVENT IF EXISTS {{%ev_player_token_expiration}}";
public $CREATE_SQL = "CREATE EVENT {{%ev_player_token_expiration}} ON SCHEDULE EVERY 10 SECOND ON COMPLETION PRESERVE ENABLE DO
BEGIN
ALTER EVENT {{%ev_player_token_expiration}} DISABLE;
call expire_player_tokens();
ALTER EVENT {{%ev_player_token_expiration}} ENABLE;
END";

public function up()
{
$this->db->createCommand($this->DROP_SQL)->execute();
$this->db->createCommand($this->CREATE_SQL)->execute();
}

public function down()
{
$this->db->createCommand($this->DROP_SQL)->execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use yii\db\Migration;

/**
* Handles dropping columns from table `{{%player}}`.
*/
class m241104_201004_drop_token_columns_from_player_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->db->createCommand("INSERT INTO player_token (player_id,type,token,expires_at,created_at) SELECT id,'password_reset',substr(password_reset_token,1,30),now()+ INTERVAL 24 HOUR,now() FROM player WHERE password_reset_token is not null and password_reset_token!=''")->execute();
$this->db->createCommand("INSERT INTO player_token (player_id,type,token,expires_at,created_at) SELECT id,'email_verification',substr(verification_token,1,30),now()+ INTERVAL 24 HOUR,now() FROM player WHERE verification_token is not null and verification_token!=''")->execute();
$this->dropColumn('player', 'password_reset_token');
$this->dropColumn('player', 'verification_token');
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->addColumn('player', 'password_reset_token', $this->string());
$this->addColumn('player', 'verification_token', $this->string());
}
}
21 changes: 21 additions & 0 deletions backend/migrations/m241104_231037_drop_tbu_player_trigger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use yii\db\Migration;

/**
* Class m241104_231037_drop_tbu_player_trigger
*/
class m241104_231037_drop_tbu_player_trigger extends Migration
{
public $DROP_SQL = "DROP TRIGGER IF EXISTS {{%tbu_player}}";

public function up()
{
$this->db->createCommand($this->DROP_SQL)->execute();
}

public function down()
{
echo "Nothing to reverse...";
}
}
Loading

0 comments on commit 1562ac4

Please sign in to comment.