diff --git a/lib/Alchemy/Phrasea/Setup/DoctrineMigrations/ApiMigration.php b/lib/Alchemy/Phrasea/Setup/DoctrineMigrations/ApiMigration.php new file mode 100644 index 0000000000..1d1db8cd68 --- /dev/null +++ b/lib/Alchemy/Phrasea/Setup/DoctrineMigrations/ApiMigration.php @@ -0,0 +1,56 @@ +tableExists('ApiApplication'); + } + + public function doUpSql(Schema $schema) + { + $this->addSql("CREATE TABLE ApiLogs (id INT AUTO_INCREMENT NOT NULL, account_id INT NOT NULL, route VARCHAR(128) DEFAULT NULL, method VARCHAR(16) DEFAULT NULL, created DATETIME NOT NULL, status_code INT DEFAULT NULL, format VARCHAR(64) DEFAULT NULL, resource VARCHAR(64) DEFAULT NULL, general VARCHAR(64) DEFAULT NULL, aspect VARCHAR(64) DEFAULT NULL, action VARCHAR(64) DEFAULT NULL, error_code INT DEFAULT NULL, error_message LONGTEXT DEFAULT NULL, INDEX IDX_91E90F309B6B5FBA (account_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB"); + $this->addSql("CREATE TABLE ApiApplications (id INT AUTO_INCREMENT NOT NULL, creator_id INT DEFAULT NULL, type VARCHAR(128) NOT NULL, name VARCHAR(128) NOT NULL, description LONGTEXT NOT NULL, website VARCHAR(128) NOT NULL, created DATETIME NOT NULL, updated DATETIME NOT NULL, client_id VARCHAR(32) NOT NULL, client_secret VARCHAR(32) NOT NULL, nonce VARCHAR(64) NOT NULL, redirect_uri VARCHAR(128) NOT NULL, activated TINYINT(1) NOT NULL, grant_password TINYINT(1) NOT NULL, INDEX IDX_53F7BBE661220EA6 (creator_id), UNIQUE INDEX client_id (client_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB"); + $this->addSql("CREATE TABLE ApiOauthCodes (code VARCHAR(16) NOT NULL, account_id INT NOT NULL, redirect_uri VARCHAR(128) NOT NULL, expires DATETIME DEFAULT NULL, scope VARCHAR(128) DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME NOT NULL, INDEX IDX_BE6B11809B6B5FBA (account_id), PRIMARY KEY(code)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB"); + $this->addSql("CREATE TABLE ApiOauthRefreshTokens (refresh_token VARCHAR(128) NOT NULL, account_id INT NOT NULL, expires DATETIME NOT NULL, scope VARCHAR(128) DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME NOT NULL, INDEX IDX_7DA42A5A9B6B5FBA (account_id), PRIMARY KEY(refresh_token)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB"); + $this->addSql("CREATE TABLE ApiOauthTokens (oauth_token VARCHAR(32) NOT NULL, account_id INT NOT NULL, session_id INT DEFAULT NULL, expires DATETIME DEFAULT NULL, scope VARCHAR(128) DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME NOT NULL, UNIQUE INDEX UNIQ_4FD469539B6B5FBA (account_id), INDEX session_id (session_id), PRIMARY KEY(oauth_token)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB"); + $this->addSql("CREATE TABLE ApiAccounts (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, application_id INT NOT NULL, oauth_token VARCHAR(32) DEFAULT NULL, revoked TINYINT(1) NOT NULL, api_version VARCHAR(16) NOT NULL, created DATETIME NOT NULL, INDEX IDX_2C54E637A76ED395 (user_id), INDEX IDX_2C54E6373E030ACD (application_id), UNIQUE INDEX UNIQ_2C54E637D8344B2A (oauth_token), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB"); + $this->addSql("ALTER TABLE ApiLogs ADD CONSTRAINT FK_91E90F309B6B5FBA FOREIGN KEY (account_id) REFERENCES ApiAccounts (id)"); + $this->addSql("ALTER TABLE ApiApplications ADD CONSTRAINT FK_53F7BBE661220EA6 FOREIGN KEY (creator_id) REFERENCES Users (id)"); + $this->addSql("ALTER TABLE ApiOauthCodes ADD CONSTRAINT FK_BE6B11809B6B5FBA FOREIGN KEY (account_id) REFERENCES ApiAccounts (id)"); + $this->addSql("ALTER TABLE ApiOauthRefreshTokens ADD CONSTRAINT FK_7DA42A5A9B6B5FBA FOREIGN KEY (account_id) REFERENCES ApiAccounts (id)"); + $this->addSql("ALTER TABLE ApiOauthTokens ADD CONSTRAINT FK_4FD469539B6B5FBA FOREIGN KEY (account_id) REFERENCES ApiAccounts (id)"); + $this->addSql("ALTER TABLE ApiAccounts ADD CONSTRAINT FK_2C54E637A76ED395 FOREIGN KEY (user_id) REFERENCES Users (id)"); + $this->addSql("ALTER TABLE ApiAccounts ADD CONSTRAINT FK_2C54E6373E030ACD FOREIGN KEY (application_id) REFERENCES ApiApplications (id)"); + $this->addSql("ALTER TABLE ApiAccounts ADD CONSTRAINT FK_2C54E637D8344B2A FOREIGN KEY (oauth_token) REFERENCES ApiOauthTokens (oauth_token)"); + } + + public function doDownSql(Schema $schema) + { + $this->addSql("ALTER TABLE ApiAccounts DROP FOREIGN KEY FK_2C54E6373E030ACD"); + $this->addSql("ALTER TABLE ApiAccounts DROP FOREIGN KEY FK_2C54E637D8344B2A"); + $this->addSql("ALTER TABLE ApiLogs DROP FOREIGN KEY FK_91E90F309B6B5FBA"); + $this->addSql("ALTER TABLE ApiOauthCodes DROP FOREIGN KEY FK_BE6B11809B6B5FBA"); + $this->addSql("ALTER TABLE ApiOauthRefreshTokens DROP FOREIGN KEY FK_7DA42A5A9B6B5FBA"); + $this->addSql("ALTER TABLE ApiOauthTokens DROP FOREIGN KEY FK_4FD469539B6B5FBA"); + $this->addSql("DROP TABLE ApiLogs"); + $this->addSql("DROP TABLE ApiApplications"); + $this->addSql("DROP TABLE ApiOauthCodes"); + $this->addSql("DROP TABLE ApiOauthRefreshTokens"); + $this->addSql("DROP TABLE ApiOauthTokens"); + $this->addSql("DROP TABLE ApiAccounts"); + } +} diff --git a/lib/classes/patch/370alpha3a.php b/lib/classes/patch/370alpha3a.php index 5a86489c55..094e797f4b 100644 --- a/lib/classes/patch/370alpha3a.php +++ b/lib/classes/patch/370alpha3a.php @@ -43,7 +43,7 @@ class patch_370alpha3a extends patchAbstract */ public function getDoctrineMigrations() { - return []; + return ['api']; } /** diff --git a/lib/classes/patch/390alpha17a.php b/lib/classes/patch/390alpha17a.php new file mode 100644 index 0000000000..8c2c3d5185 --- /dev/null +++ b/lib/classes/patch/390alpha17a.php @@ -0,0 +1,187 @@ +release; + } + + /** + * {@inheritdoc} + */ + public function require_all_upgrades() + { + return false; + } + + /** + * {@inheritdoc} + */ + public function concern() + { + return $this->concern; + } + + /** + * {@inheritdoc} + */ + public function getDoctrineMigrations() + { + return ['api']; + } + + /** + * {@inheritdoc} + */ + public function apply(base $appbox, Application $app) + { + $this->fillApplicationTable($app['EM']); + $this->fillAccountTable($app['EM']); + $this->fillLogTable($app['EM']); + $this->fillCodeTable($app['EM']); + $this->fillRefreshTokenTable($app['EM']); + $this->fillOauthTokenTable($app['EM']); + } + + private function fillApplicationTable(EntityManager $em) + { + $em->getConnection()->executeUpdate( + 'INSERT INTO ApiApplications + ( + id, `type`, `name`, description, website + created, updated, client_id, client_secret, nonce + redirect_uri, activated, grant_password, creator_id + + ) + ( + SELECT + application_id, `type`, `name`, description, website, + created_on, last_modified, client_id, client_secret, nonce, + redirect_uri, activated, grant_password, creator + FROM api_applications + INNER JOIN Users ON (Users.id = api_accounts.usr_id) + )' + ); + } + + private function fillAccountTable(EntityManager $em) + { + $em->getConnection()->executeUpdate( + 'INSERT INTO ApiAccounts + ( + id, user_id, revoked + api_version, created, application_id + + ) + ( + SELECT + api_account_id, usr_id, revoked, + api_version, created, application_id + FROM api_accounts + INNER JOIN Users ON (Users.id = api_accounts.usr_id) + INNER JOIN api_applications ON (api_accounts.application_id = api_applications.application_id) + )' + ); + } + + private function fillLogTable(EntityManager $em) + { + $em->getConnection()->executeUpdate( + 'INSERT INTO ApiLogs + ( + id, account_id, route, error_message + created, status_code, format, resource, + general, aspect, `action`, error_code, + + ) + ( + SELECT + api_log_id, api_account_id, api_log_route, api_log_error_message + api_log_date, api_log_status_code, api_log_format, api_log_resource, + api_log_general, api_log_aspect, api_log_action, api_log_error_code + FROM api_log + INNER JOIN api_accounts ON (api_accounts.api_account_id = api_log.api_account_id) + )' + ); + } + + private function fillCodeTable(EntityManager $em) + { + $em->getConnection()->executeUpdate( + 'INSERT INTO ApiOauthCodes + ( + code, account_id, redirect_uri, expires + scope, created, updated + + ) + ( + SELECT + code, api_account_id, redirect_uri, expires + scope, NOW(), NOW() + FROM api_oauth_codes + INNER JOIN api_accounts ON (api_accounts.api_account_id = api_oauth_codes.api_account_id) + )' + ); + } + + private function fillRefreshTokenTable(EntityManager $em) + { + $em->getConnection()->executeUpdate( + 'INSERT INTO ApiOauthRefreshTokens + ( + refresh_token, account_id, expires + scope, created, updated + + ) + ( + SELECT + refresh_token, api_account_id, expires + scope, NOW(), NOW() + FROM api_oauth_refresh_tokens + INNER JOIN api_accounts ON (api_accounts.api_account_id = api_oauth_refresh_tokens.api_account_id) + )' + ); + } + + + private function fillOauthTokenTable(EntityManager $em) + { + $em->getConnection()->executeUpdate( + 'INSERT INTO ApiOauthTokens + ( + oauth_token, account_id, session_id, expires + scope, created, updated + + ) + ( + SELECT + oauth_token, api_account_id, session_id, expires + scope, NOW(), NOW() + FROM api_oauth_tokens + INNER JOIN api_accounts ON (api_accounts.api_account_id = api_oauth_tokens.api_account_id) + )' + ); + } +} diff --git a/lib/conf.d/migrations.yml b/lib/conf.d/migrations.yml index 2cc0b1cde8..5d58335458 100644 --- a/lib/conf.d/migrations.yml +++ b/lib/conf.d/migrations.yml @@ -60,6 +60,9 @@ migrations: migration19: version: token class: Alchemy\Phrasea\Setup\DoctrineMigrations\TokenMigration + migration20: + version: api + class: Alchemy\Phrasea\Setup\DoctrineMigrations\ApiMigration migration21: version: aggregate-token class: Alchemy\Phrasea\Setup\DoctrineMigrations\AggregateTokenMigration