|
| 1 | +<?php |
| 2 | + |
| 3 | +use yii\db\Schema; |
| 4 | + |
| 5 | +class m140501_075311_add_oauth2_server extends \yii\db\Migration |
| 6 | +{ |
| 7 | + public function up() |
| 8 | + { |
| 9 | + $tableOptions = null; |
| 10 | + if ($this->db->driverName === 'mysql') { |
| 11 | + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'; |
| 12 | + } |
| 13 | + |
| 14 | + $transaction = $this->db->beginTransaction(); |
| 15 | + try { |
| 16 | + $this->createTable('{{%oauth_clients}}', [ |
| 17 | + 'client_id' => Schema::TYPE_STRING . '(32) NOT NULL', |
| 18 | + 'client_secret' => Schema::TYPE_STRING . '(32) NOT NULL', |
| 19 | + 'redirect_uri' => Schema::TYPE_STRING . '(1000) NOT NULL', |
| 20 | + 'grant_types' => Schema::TYPE_STRING . '(100) NOT NULL', |
| 21 | + 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', |
| 22 | + 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL', |
| 23 | + 'PRIMARY KEY (`client_id`)' |
| 24 | + ], $tableOptions); |
| 25 | + |
| 26 | + $this->createTable('{{%oauth_access_tokens}}', [ |
| 27 | + 'access_token' => Schema::TYPE_STRING . '(40) NOT NULL', |
| 28 | + 'client_id' => Schema::TYPE_STRING . '(32) NOT NULL', |
| 29 | + 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL', |
| 30 | + 'expires' => Schema::TYPE_TIMESTAMP . ' NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', |
| 31 | + 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', |
| 32 | + 'PRIMARY KEY (`access_token`)', |
| 33 | + 'FOREIGN KEY (`client_id`) REFERENCES {{%oauth_clients}} (`client_id`) ON DELETE CASCADE ON UPDATE CASCADE', |
| 34 | + ], $tableOptions); |
| 35 | + |
| 36 | + $this->createTable('{{%oauth_refresh_tokens}}', [ |
| 37 | + 'refresh_token' => Schema::TYPE_STRING . '(40) NOT NULL', |
| 38 | + 'client_id' => Schema::TYPE_STRING . '(32) NOT NULL', |
| 39 | + 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL', |
| 40 | + 'expires' => Schema::TYPE_TIMESTAMP . ' NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', |
| 41 | + 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', |
| 42 | + 'PRIMARY KEY (`refresh_token`)', |
| 43 | + 'FOREIGN KEY (`client_id`) REFERENCES {{%oauth_clients}} (`client_id`) ON DELETE CASCADE ON UPDATE CASCADE', |
| 44 | + ], $tableOptions); |
| 45 | + |
| 46 | + $this->createTable('{{%oauth_authorization_codes}}', [ |
| 47 | + 'authorization_code' => Schema::TYPE_STRING . '(40) NOT NULL', |
| 48 | + 'client_id' => Schema::TYPE_STRING . '(32) NOT NULL', |
| 49 | + 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL', |
| 50 | + 'redirect_uri' => Schema::TYPE_STRING . '(1000) NOT NULL', |
| 51 | + 'expires' => Schema::TYPE_TIMESTAMP . ' NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', |
| 52 | + 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', |
| 53 | + 'PRIMARY KEY (`authorization_code`)', |
| 54 | + 'FOREIGN KEY (`client_id`) REFERENCES {{%oauth_clients}} (`client_id`) ON DELETE CASCADE ON UPDATE CASCADE', |
| 55 | + ], $tableOptions); |
| 56 | + |
| 57 | + $this->createTable('{{%oauth_scopes}}', [ |
| 58 | + 'scope' => Schema::TYPE_STRING . '(2000) NOT NULL', |
| 59 | + 'is_default' => Schema::TYPE_BOOLEAN . ' NOT NULL', |
| 60 | + ], $tableOptions); |
| 61 | + |
| 62 | + $this->createTable('{{%oauth_jwt}}', [ |
| 63 | + 'client_id' => Schema::TYPE_STRING . '(32) NOT NULL', |
| 64 | + 'subject' => Schema::TYPE_STRING . '(80) DEFAULT NULL', |
| 65 | + 'public_key' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', |
| 66 | + 'PRIMARY KEY (`client_id`)', |
| 67 | + ], $tableOptions); |
| 68 | + |
| 69 | + $this->createTable('{{%oauth_users}}', [ |
| 70 | + 'username' => Schema::TYPE_STRING . '(255) NOT NULL', |
| 71 | + 'password' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', |
| 72 | + 'first_name' => Schema::TYPE_STRING . '(255) DEFAULT NULL', |
| 73 | + 'last_name' => Schema::TYPE_STRING . '(255) DEFAULT NULL', |
| 74 | + 'PRIMARY KEY (`username`)', |
| 75 | + ], $tableOptions); |
| 76 | + |
| 77 | + // insert client data |
| 78 | + $this->batchInsert('{{%oauth_clients}}', ['client_id', 'client_secret', 'redirect_uri', 'grant_types'], [ |
| 79 | + ['testclient', 'testpass', 'http://fake/', 'client_credentials password'], |
| 80 | + ]); |
| 81 | + |
| 82 | + $transaction->commit(); |
| 83 | + } catch (Exception $e) { |
| 84 | + echo 'Exception: ' . $e->getMessage() . '\n'; |
| 85 | + $transaction->rollback(); |
| 86 | + |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + public function down() |
| 94 | + { |
| 95 | + $transaction = $this->db->beginTransaction(); |
| 96 | + try { |
| 97 | + $this->dropTable('{{%oauth_users}}'); |
| 98 | + $this->dropTable('{{%oauth_jwt}}'); |
| 99 | + $this->dropTable('{{%oauth_scopes}}'); |
| 100 | + $this->dropTable('{{%oauth_authorization_codes}}'); |
| 101 | + $this->dropTable('{{%oauth_refresh_tokens}}'); |
| 102 | + $this->dropTable('{{%oauth_access_tokens}}'); |
| 103 | + $this->dropTable('{{%oauth_clients}}'); |
| 104 | + |
| 105 | + $transaction->commit(); |
| 106 | + } catch (Exception $e) { |
| 107 | + $transaction->rollback(); |
| 108 | + echo $e->getMessage(); |
| 109 | + echo "\n"; |
| 110 | + echo get_called_class() . ' cannot be reverted.'; |
| 111 | + echo "\n"; |
| 112 | + |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + return true; |
| 117 | + } |
| 118 | +} |
0 commit comments