From 2629a16a4570698af539fba06d6bf659df9bce45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=AE=8F=E4=BC=9F?= <524415250@qq.com> Date: Fri, 22 Sep 2017 17:13:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 ++ .../m140501_075311_add_oauth2_server.php | 152 ------------------ .../m140602_111327_create_menu_table.php | 3 +- migrations/schema-mssql.sql | 33 ++++ migrations/schema-mysql.sql | 33 ++++ migrations/schema-oci.sql | 33 ++++ migrations/schema-pgsql.sql | 33 ++++ migrations/schema-sqlite.sql | 33 ++++ 8 files changed, 175 insertions(+), 154 deletions(-) delete mode 100644 migrations/m140501_075311_add_oauth2_server.php create mode 100644 migrations/schema-mssql.sql create mode 100644 migrations/schema-mysql.sql create mode 100644 migrations/schema-oci.sql create mode 100644 migrations/schema-pgsql.sql create mode 100644 migrations/schema-sqlite.sql diff --git a/README.md b/README.md index b98f8ac..f1dde43 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,15 @@ ] ], ``` +* **创建所需要的表** +``` +//用户表user和菜单表menu +yii migrate --migrationPath=@vendor/windhoney/yii2-rest-rbac/migrations +//rbac相关权限表 +yii migrate --migrationPath=@yii/rbac/migrations/ +//oauth2相关表 +yii migrate --migrationPath=@vendor/filsh/yii2-oauth2-server/migrations +``` * **添加路由配置** diff --git a/migrations/m140501_075311_add_oauth2_server.php b/migrations/m140501_075311_add_oauth2_server.php deleted file mode 100644 index ecabd05..0000000 --- a/migrations/m140501_075311_add_oauth2_server.php +++ /dev/null @@ -1,152 +0,0 @@ -db->driverName === 'mysql' ? $yes : $no; - } - - public function primaryKey($columns) { - return 'PRIMARY KEY (' . $this->db->getQueryBuilder()->buildColumns($columns) . ')'; - } - - public function foreignKey($columns,$refTable,$refColumns,$onDelete = null,$onUpdate = null) { - $builder = $this->db->getQueryBuilder(); - $sql = ' FOREIGN KEY (' . $builder->buildColumns($columns) . ')' - . ' REFERENCES ' . $this->db->quoteTableName($refTable) - . ' (' . $builder->buildColumns($refColumns) . ')'; - if ($onDelete !== null) { - $sql .= ' ON DELETE ' . $onDelete; - } - if ($onUpdate !== null) { - $sql .= ' ON UPDATE ' . $onUpdate; - } - return $sql; - } - - public function up() - { - $tableOptions = null; - if ($this->db->driverName === 'mysql') { - $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'; - } - - $now = $this->mysql('CURRENT_TIMESTAMP',"'now'"); - $on_update_now = $this->mysql("ON UPDATE $now"); - - $transaction = $this->db->beginTransaction(); - try { - $this->createTable('{{%oauth_clients}}', [ - 'client_id' => Schema::TYPE_STRING . '(32) NOT NULL', - 'client_secret' => Schema::TYPE_STRING . '(32) DEFAULT NULL', - 'redirect_uri' => Schema::TYPE_STRING . '(1000) NOT NULL', - 'grant_types' => Schema::TYPE_STRING . '(100) NOT NULL', - 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', - 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL', - $this->primaryKey('client_id'), - ], $tableOptions); - - $this->createTable('{{%oauth_access_tokens}}', [ - 'access_token' => Schema::TYPE_STRING . '(40) NOT NULL', - 'client_id' => Schema::TYPE_STRING . '(32) NOT NULL', - 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL', - 'expires' => Schema::TYPE_TIMESTAMP . " NOT NULL DEFAULT $now $on_update_now", - 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', - $this->primaryKey('access_token'), - $this->foreignKey('client_id','{{%oauth_clients}}','client_id','CASCADE','CASCADE'), - ], $tableOptions); - - $this->createTable('{{%oauth_refresh_tokens}}', [ - 'refresh_token' => Schema::TYPE_STRING . '(40) NOT NULL', - 'client_id' => Schema::TYPE_STRING . '(32) NOT NULL', - 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL', - 'expires' => Schema::TYPE_TIMESTAMP . " NOT NULL DEFAULT $now $on_update_now", - 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', - $this->primaryKey('refresh_token'), - $this->foreignKey('client_id','{{%oauth_clients}}','client_id','CASCADE','CASCADE'), - ], $tableOptions); - - $this->createTable('{{%oauth_authorization_codes}}', [ - 'authorization_code' => Schema::TYPE_STRING . '(40) NOT NULL', - 'client_id' => Schema::TYPE_STRING . '(32) NOT NULL', - 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL', - 'redirect_uri' => Schema::TYPE_STRING . '(1000) NOT NULL', - 'expires' => Schema::TYPE_TIMESTAMP . " NOT NULL DEFAULT $now $on_update_now", - 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', - $this->primaryKey('authorization_code'), - $this->foreignKey('client_id','{{%oauth_clients}}','client_id','CASCADE','CASCADE'), - ], $tableOptions); - - $this->createTable('{{%oauth_scopes}}', [ - 'scope' => Schema::TYPE_STRING . '(2000) NOT NULL', - 'is_default' => Schema::TYPE_BOOLEAN . ' NOT NULL', - ], $tableOptions); - - $this->createTable('{{%oauth_jwt}}', [ - 'client_id' => Schema::TYPE_STRING . '(32) NOT NULL', - 'subject' => Schema::TYPE_STRING . '(80) DEFAULT NULL', - 'public_key' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', - $this->primaryKey('client_id'), - ], $tableOptions); - - $this->createTable('{{%oauth_users}}', [ - 'username' => Schema::TYPE_STRING . '(255) NOT NULL', - 'password' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', - 'first_name' => Schema::TYPE_STRING . '(255) DEFAULT NULL', - 'last_name' => Schema::TYPE_STRING . '(255) DEFAULT NULL', - $this->primaryKey('username'), - ], $tableOptions); - - $this->createTable('{{%oauth_public_keys}}', [ - 'client_id' => Schema::TYPE_STRING . '(255) NOT NULL', - 'public_key' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', - 'private_key' => Schema::TYPE_STRING . '(2000) DEFAULT NULL', - 'encryption_algorithm' => Schema::TYPE_STRING . '(100) DEFAULT \'RS256\'', - ], $tableOptions); - - // insert client data - $this->batchInsert('{{%oauth_clients}}', ['client_id', 'client_secret', 'redirect_uri', 'grant_types'], [ - ['testclient', 'testpass', 'http://fake/', 'client_credentials authorization_code password implicit'], - ]); - - $transaction->commit(); - } catch (Exception $e) { - echo 'Exception: ' . $e->getMessage() . '\n'; - $transaction->rollback(); - - return false; - } - - return true; - } - - public function down() - { - $transaction = $this->db->beginTransaction(); - try { - $this->dropTable('{{%oauth_users}}'); - $this->dropTable('{{%oauth_jwt}}'); - $this->dropTable('{{%oauth_scopes}}'); - $this->dropTable('{{%oauth_authorization_codes}}'); - $this->dropTable('{{%oauth_refresh_tokens}}'); - $this->dropTable('{{%oauth_access_tokens}}'); - $this->dropTable('{{%oauth_public_keys}}'); - $this->dropTable('{{%oauth_clients}}'); - - $transaction->commit(); - } catch (Exception $e) { - $transaction->rollback(); - echo $e->getMessage(); - echo "\n"; - echo get_called_class() . ' cannot be reverted.'; - echo "\n"; - - return false; - } - - return true; - } -} diff --git a/migrations/m140602_111327_create_menu_table.php b/migrations/m140602_111327_create_menu_table.php index d158b2c..fb416db 100644 --- a/migrations/m140602_111327_create_menu_table.php +++ b/migrations/m140602_111327_create_menu_table.php @@ -1,6 +1,5 @@ * @since 1.0 */ -class m140602_111327_create_menu_table extends Migration +class m140602_111327_create_menu_table extends \yii\db\Migration { /** diff --git a/migrations/schema-mssql.sql b/migrations/schema-mssql.sql new file mode 100644 index 0000000..5017958 --- /dev/null +++ b/migrations/schema-mssql.sql @@ -0,0 +1,33 @@ +/** + * Database schema required by yii2-admin. + * + * @author Misbahul D Munir + * @since 2.5 + */ + +drop table if exists [menu]; +drop table if exists [user]; + +create table [menu] +( + [id] int IDENTITY PRIMARY KEY, + [name] varchar(128), + [parent] int(11), + [route] varchar(256), + [order] int(11), + [data] text, + foreign key (parent) references [menu]([id]) ON DELETE SET NULL ON UPDATE CASCADE +); + +create table [user] +( + [id] int IDENTITY PRIMARY KEY, + [username] varchar(32) NOT NULL, + [auth_key] varchar(32) NOT NULL, + [password_hash] varchar(256) NOT NULL, + [password_reset_token] varchar(256), + [email] varchar(256) NOT NULL, + [status] integer not null default 10, + [created_at] integer not null, + [updated_at] integer not null +); diff --git a/migrations/schema-mysql.sql b/migrations/schema-mysql.sql new file mode 100644 index 0000000..d8a2dd6 --- /dev/null +++ b/migrations/schema-mysql.sql @@ -0,0 +1,33 @@ +/** + * Database schema required by yii2-admin. + * + * @author Misbahul D Munir + * @since 2.5 + */ + +drop table if exists `menu`; +drop table if exists `user` cascade; + +create table `menu` +( + `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, + `name` varchar(128), + `parent` int(11), + `route` varchar(256), + `order` int(11), + `data` blob, + foreign key (`parent`) references `menu`(`id`) ON DELETE SET NULL ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +create table `user` +( + `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, + `username` varchar(32) NOT NULL, + `auth_key` varchar(32) NOT NULL, + `password_hash` varchar(256) NOT NULL, + `password_reset_token` varchar(256), + `email` varchar(256) NOT NULL, + `status` integer not null default 10, + `created_at` integer not null, + `updated_at` integer not null +)ENGINE=InnoDB DEFAULT CHARSET=utf8; diff --git a/migrations/schema-oci.sql b/migrations/schema-oci.sql new file mode 100644 index 0000000..3072ed7 --- /dev/null +++ b/migrations/schema-oci.sql @@ -0,0 +1,33 @@ +/** + * Database schema required by yii2-admin. + * + * @author Misbahul D Munir + * @since 2.5 + */ + +drop table if exists "menu"; +drop table if exists "user"; + +create table "menu" +( + "id" NUMBER(10) NOT NULL PRIMARY KEY, + "name" varchar(128), + "parent" number(10), + "route" varchar(256), + "order" number(10), + "data" BYTEA, + foreign key (parent) references "menu"("id") ON DELETE SET NULL ON UPDATE CASCADE +); + +create table "user" +( + "id" NUMBER(10) NOT NULL PRIMARY KEY, + "username" varchar(32) NOT NULL, + "auth_key" varchar(32) NOT NULL, + "password_hash" varchar(256) NOT NULL, + "password_reset_token" varchar(256), + "email" varchar(256) NOT NULL, + "status" integer not null default 10, + "created_at" number(10) not null, + "updated_at" number(10) not null +); diff --git a/migrations/schema-pgsql.sql b/migrations/schema-pgsql.sql new file mode 100644 index 0000000..334a932 --- /dev/null +++ b/migrations/schema-pgsql.sql @@ -0,0 +1,33 @@ +/** + * Database schema required by yii2-admin. + * + * @author Misbahul D Munir + * @since 2.5 + */ + +drop table if exists "menu"; +drop table if exists "user"; + +create table "menu" +( + "id" serial NOT NULL PRIMARY KEY, + "name" varchar(128), + "parent" integer, + "route" varchar(256), + "order" integer, + "data" bytea, + foreign key ("parent") references "menu"("id") ON DELETE SET NULL ON UPDATE CASCADE +); + +create table "user" +( + "id" serial NOT NULL PRIMARY KEY, + "username" varchar(32) NOT NULL, + "auth_key" varchar(32) NOT NULL, + "password_hash" varchar(256) NOT NULL, + "password_reset_token" varchar(256), + "email" varchar(256) NOT NULL, + "status" integer not null default 10, + "created_at" integer not null, + "updated_at" integer not null +); diff --git a/migrations/schema-sqlite.sql b/migrations/schema-sqlite.sql new file mode 100644 index 0000000..771d442 --- /dev/null +++ b/migrations/schema-sqlite.sql @@ -0,0 +1,33 @@ +/** + * Database schema required by yii2-admin. + * + * @author Misbahul D Munir + * @since 2.5 + */ + +drop table if exists "menu"; +drop table if exists "user"; + +create table "menu" +( + "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, + "name" varchar(128), + "parent" int(11), + "route" varchar(256), + "order" int(11), + "data" LONGBLOB, + foreign key ("parent") references "menu"("id") ON DELETE SET NULL ON UPDATE CASCADE +); + +create table "user" +( + "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, + "username" varchar(32) NOT NULL, + "auth_key" varchar(32) NOT NULL, + "password_hash" varchar(256) NOT NULL, + "password_reset_token" varchar(256), + "email" varchar(256) NOT NULL, + "status" integer not null default 10, + "created_at" integer not null, + "updated_at" integer not null +);