diff --git a/common/models/OauthAccessToken.php b/common/models/OauthAccessToken.php deleted file mode 100644 index 7364a79..0000000 --- a/common/models/OauthAccessToken.php +++ /dev/null @@ -1,48 +0,0 @@ -hasOne(OauthSession::class, ['id' => 'session_id']); - } - - public function getScopes() { - return new Set(static::getDb()->getSchema()->getRawTableName(static::tableName()), $this->access_token, 'scopes'); - } - - public function beforeDelete() { - if (!$result = parent::beforeDelete()) { - return $result; - } - - $this->getScopes()->delete(); - - return true; - } - - public function isExpired() : bool { - return time() > $this->expire_time; - } - -} diff --git a/console/controllers/CleanupController.php b/console/controllers/CleanupController.php index f08985b..0f8cb83 100644 --- a/console/controllers/CleanupController.php +++ b/console/controllers/CleanupController.php @@ -1,22 +1,8 @@ andWhere(['<', 'expire_time', time()]) - ->each(1000); - - foreach($accessTokens as $token) { - /** @var OauthAccessToken $token */ - $token->delete(); - } - - return self::EXIT_CODE_NORMAL; - } - } diff --git a/console/migrations/m161222_222520_remove_oauth_access_tokens.php b/console/migrations/m161222_222520_remove_oauth_access_tokens.php new file mode 100644 index 0000000..ebc228d --- /dev/null +++ b/console/migrations/m161222_222520_remove_oauth_access_tokens.php @@ -0,0 +1,31 @@ +dropForeignKey('FK_oauth_access_toke_to_oauth_session', '{{%oauth_access_tokens}}'); + $this->dropTable('{{%oauth_access_tokens}}'); + } + + public function safeDown() { + $this->createTable('{{%oauth_access_tokens}}', [ + 'access_token' => $this->string(64), + 'session_id' => $this->getDb()->getTableSchema('{{%oauth_sessions}}')->getColumn('id')->dbType, + 'expire_time' => $this->integer()->notNull(), + $this->primary('access_token'), + ], $this->tableOptions); + + $this->addForeignKey( + 'FK_oauth_access_toke_to_oauth_session', + '{{%oauth_access_tokens}}', + 'session_id', + '{{%oauth_sessions}}', + 'id', + 'CASCADE', + 'SET NULL' + ); + } + +} diff --git a/tests/codeception/common/fixtures/OauthAccessTokenFixture.php b/tests/codeception/common/fixtures/OauthAccessTokenFixture.php deleted file mode 100644 index d51f14f..0000000 --- a/tests/codeception/common/fixtures/OauthAccessTokenFixture.php +++ /dev/null @@ -1,17 +0,0 @@ - [ - 'access_token' => '07541285-831e-1e47-e314-b950309a6fca', - 'session_id' => 1, - 'expire_time' => time() + 3600, - ], - 'admin-test1-expired' => [ - 'access_token' => '2977ec21-3022-96f8-544db-2e1df936908', - 'session_id' => 1, - 'expire_time' => time() - 3600, - ], -]; diff --git a/tests/codeception/console/unit/controllers/CleanupControllerTest.php b/tests/codeception/console/unit/controllers/CleanupControllerTest.php index bcb0cef..d13e470 100644 --- a/tests/codeception/console/unit/controllers/CleanupControllerTest.php +++ b/tests/codeception/console/unit/controllers/CleanupControllerTest.php @@ -1,31 +1,8 @@ OauthAccessTokenFixture::class, - ]; - } - - public function testActionAccessTokens() { - /** @var OauthAccessToken $validAccessToken */ - $validAccessToken = $this->tester->grabFixture('accessTokens', 'admin-test1'); - /** @var OauthAccessToken $expiredAccessToken */ - $expiredAccessToken = $this->tester->grabFixture('accessTokens', 'admin-test1-expired'); - - $controller = new CleanupController('cleanup', Yii::$app); - $this->assertEquals(0, $controller->actionAccessTokens()); - - $this->tester->canSeeRecord(OauthAccessToken::class, ['access_token' => $validAccessToken->access_token]); - $this->tester->cantSeeRecord(OauthAccessToken::class, ['access_token' => $expiredAccessToken->access_token]); - } - }