This commit is contained in:
2020-10-06 14:27:47 +07:00
commit 586be80cf6
16613 changed files with 3274099 additions and 0 deletions

View File

@@ -0,0 +1,169 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
use yii\base\InvalidConfigException;
use yii\rbac\DbManager;
/**
* Initializes RBAC tables.
*
* @author Alexander Kochetov <creocoder@gmail.com>
* @since 2.0
*/
class m140506_102106_rbac_init extends \yii\db\Migration
{
/**
* @throws yii\base\InvalidConfigException
* @return DbManager
*/
protected function getAuthManager()
{
$authManager = Yii::$app->getAuthManager();
if (!$authManager instanceof DbManager) {
throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.');
}
return $authManager;
}
/**
* @return bool
*/
protected function isMSSQL()
{
return $this->db->driverName === 'mssql' || $this->db->driverName === 'sqlsrv' || $this->db->driverName === 'dblib';
}
protected function isOracle()
{
return $this->db->driverName === 'oci';
}
/**
* {@inheritdoc}
*/
public function up()
{
$authManager = $this->getAuthManager();
$this->db = $authManager->db;
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
$this->createTable($authManager->ruleTable, [
'name' => $this->string(64)->notNull(),
'data' => $this->binary(),
'created_at' => $this->integer(),
'updated_at' => $this->integer(),
'PRIMARY KEY ([[name]])',
], $tableOptions);
$this->createTable($authManager->itemTable, [
'name' => $this->string(64)->notNull(),
'type' => $this->smallInteger()->notNull(),
'description' => $this->text(),
'rule_name' => $this->string(64),
'data' => $this->binary(),
'created_at' => $this->integer(),
'updated_at' => $this->integer(),
'PRIMARY KEY ([[name]])',
'FOREIGN KEY ([[rule_name]]) REFERENCES ' . $authManager->ruleTable . ' ([[name]])' .
$this->buildFkClause('ON DELETE SET NULL', 'ON UPDATE CASCADE'),
], $tableOptions);
$this->createIndex('idx-auth_item-type', $authManager->itemTable, 'type');
$this->createTable($authManager->itemChildTable, [
'parent' => $this->string(64)->notNull(),
'child' => $this->string(64)->notNull(),
'PRIMARY KEY ([[parent]], [[child]])',
'FOREIGN KEY ([[parent]]) REFERENCES ' . $authManager->itemTable . ' ([[name]])' .
$this->buildFkClause('ON DELETE CASCADE', 'ON UPDATE CASCADE'),
'FOREIGN KEY ([[child]]) REFERENCES ' . $authManager->itemTable . ' ([[name]])' .
$this->buildFkClause('ON DELETE CASCADE', 'ON UPDATE CASCADE'),
], $tableOptions);
$this->createTable($authManager->assignmentTable, [
'item_name' => $this->string(64)->notNull(),
'user_id' => $this->string(64)->notNull(),
'created_at' => $this->integer(),
'PRIMARY KEY ([[item_name]], [[user_id]])',
'FOREIGN KEY ([[item_name]]) REFERENCES ' . $authManager->itemTable . ' ([[name]])' .
$this->buildFkClause('ON DELETE CASCADE', 'ON UPDATE CASCADE'),
], $tableOptions);
if ($this->isMSSQL()) {
$this->execute("CREATE TRIGGER dbo.trigger_auth_item_child
ON dbo.{$authManager->itemTable}
INSTEAD OF DELETE, UPDATE
AS
DECLARE @old_name VARCHAR (64) = (SELECT name FROM deleted)
DECLARE @new_name VARCHAR (64) = (SELECT name FROM inserted)
BEGIN
IF COLUMNS_UPDATED() > 0
BEGIN
IF @old_name <> @new_name
BEGIN
ALTER TABLE {$authManager->itemChildTable} NOCHECK CONSTRAINT FK__auth_item__child;
UPDATE {$authManager->itemChildTable} SET child = @new_name WHERE child = @old_name;
END
UPDATE {$authManager->itemTable}
SET name = (SELECT name FROM inserted),
type = (SELECT type FROM inserted),
description = (SELECT description FROM inserted),
rule_name = (SELECT rule_name FROM inserted),
data = (SELECT data FROM inserted),
created_at = (SELECT created_at FROM inserted),
updated_at = (SELECT updated_at FROM inserted)
WHERE name IN (SELECT name FROM deleted)
IF @old_name <> @new_name
BEGIN
ALTER TABLE {$authManager->itemChildTable} CHECK CONSTRAINT FK__auth_item__child;
END
END
ELSE
BEGIN
DELETE FROM dbo.{$authManager->itemChildTable} WHERE parent IN (SELECT name FROM deleted) OR child IN (SELECT name FROM deleted);
DELETE FROM dbo.{$authManager->itemTable} WHERE name IN (SELECT name FROM deleted);
END
END;");
}
}
/**
* {@inheritdoc}
*/
public function down()
{
$authManager = $this->getAuthManager();
$this->db = $authManager->db;
if ($this->isMSSQL()) {
$this->execute('DROP TRIGGER dbo.trigger_auth_item_child;');
}
$this->dropTable($authManager->assignmentTable);
$this->dropTable($authManager->itemChildTable);
$this->dropTable($authManager->itemTable);
$this->dropTable($authManager->ruleTable);
}
protected function buildFkClause($delete = '', $update = '')
{
if ($this->isMSSQL()) {
return '';
}
if ($this->isOracle()) {
return ' ' . $delete;
}
return implode(' ', ['', $delete, $update]);
}
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
use yii\base\InvalidConfigException;
use yii\db\Migration;
use yii\rbac\DbManager;
/**
* Adds index on `user_id` column in `auth_assignment` table for performance reasons.
*
* @see https://github.com/yiisoft/yii2/pull/14765
*
* @author Ivan Buttinoni <ivan.buttinoni@cibi.it>
* @since 2.0.13
*/
class m170907_052038_rbac_add_index_on_auth_assignment_user_id extends Migration
{
public $column = 'user_id';
public $index = 'auth_assignment_user_id_idx';
/**
* @throws yii\base\InvalidConfigException
* @return DbManager
*/
protected function getAuthManager()
{
$authManager = Yii::$app->getAuthManager();
if (!$authManager instanceof DbManager) {
throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.');
}
return $authManager;
}
/**
* {@inheritdoc}
*/
public function up()
{
$authManager = $this->getAuthManager();
$this->createIndex($this->index, $authManager->assignmentTable, $this->column);
}
/**
* {@inheritdoc}
*/
public function down()
{
$authManager = $this->getAuthManager();
$this->dropIndex($this->index, $authManager->assignmentTable);
}
}

View File

@@ -0,0 +1,101 @@
/**
* Database schema required by \yii\rbac\DbManager.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alexander Kochetov <creocoder@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @since 2.0
*/
if object_id('[auth_assignment]', 'U') is not null
drop table [auth_assignment];
if object_id('[auth_item_child]', 'U') is not null
drop table [auth_item_child];
if object_id('[auth_item]', 'U') is not null
drop table [auth_item];
if object_id('[auth_rule]', 'U') is not null
drop table [auth_rule];
create table [auth_rule]
(
[name] varchar(64) not null,
[data] blob,
[created_at] integer,
[updated_at] integer,
primary key ([name])
);
create table [auth_item]
(
[name] varchar(64) not null,
[type] smallint not null,
[description] text,
[rule_name] varchar(64),
[data] blob,
[created_at] integer,
[updated_at] integer,
primary key ([name]),
foreign key ([rule_name]) references [auth_rule] ([name])
);
create index [idx-auth_item-type] on [auth_item] ([type]);
create table [auth_item_child]
(
[parent] varchar(64) not null,
[child] varchar(64) not null,
primary key ([parent],[child]),
foreign key ([parent]) references [auth_item] ([name]),
foreign key ([child]) references [auth_item] ([name])
);
create table [auth_assignment]
(
[item_name] varchar(64) not null,
[user_id] varchar(64) not null,
[created_at] integer,
primary key ([item_name], [user_id]),
foreign key ([item_name]) references [auth_item] ([name]) on delete cascade on update cascade
);
create index [auth_assignment_user_id_idx] on [auth_assignment] ([user_id]);
CREATE TRIGGER dbo.trigger_auth_item_child
ON dbo.[auth_item]
INSTEAD OF DELETE, UPDATE
AS
DECLARE @old_name VARCHAR (64) = (SELECT name FROM deleted)
DECLARE @new_name VARCHAR (64) = (SELECT name FROM inserted)
BEGIN
IF COLUMNS_UPDATED() > 0
BEGIN
IF @old_name <> @new_name
BEGIN
ALTER TABLE auth_item_child NOCHECK CONSTRAINT FK__auth_item__child;
UPDATE auth_item_child SET child = @new_name WHERE child = @old_name;
END
UPDATE auth_item
SET name = (SELECT name FROM inserted),
type = (SELECT type FROM inserted),
description = (SELECT description FROM inserted),
rule_name = (SELECT rule_name FROM inserted),
data = (SELECT data FROM inserted),
created_at = (SELECT created_at FROM inserted),
updated_at = (SELECT updated_at FROM inserted)
WHERE name IN (SELECT name FROM deleted)
IF @old_name <> @new_name
BEGIN
ALTER TABLE auth_item_child CHECK CONSTRAINT FK__auth_item__child;
END
END
ELSE
BEGIN
DELETE FROM dbo.[auth_item_child] WHERE parent IN (SELECT name FROM deleted) OR child IN (SELECT name FROM deleted);
DELETE FROM dbo.[auth_item] WHERE name IN (SELECT name FROM deleted);
END
END;

View File

@@ -0,0 +1,57 @@
/**
* Database schema required by \yii\rbac\DbManager.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alexander Kochetov <creocoder@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @since 2.0
*/
drop table if exists `auth_assignment`;
drop table if exists `auth_item_child`;
drop table if exists `auth_item`;
drop table if exists `auth_rule`;
create table `auth_rule`
(
`name` varchar(64) not null,
`data` blob,
`created_at` integer,
`updated_at` integer,
primary key (`name`)
) engine InnoDB;
create table `auth_item`
(
`name` varchar(64) not null,
`type` smallint not null,
`description` text,
`rule_name` varchar(64),
`data` blob,
`created_at` integer,
`updated_at` integer,
primary key (`name`),
foreign key (`rule_name`) references `auth_rule` (`name`) on delete set null on update cascade,
key `type` (`type`)
) engine InnoDB;
create table `auth_item_child`
(
`parent` varchar(64) not null,
`child` varchar(64) not null,
primary key (`parent`, `child`),
foreign key (`parent`) references `auth_item` (`name`) on delete cascade on update cascade,
foreign key (`child`) references `auth_item` (`name`) on delete cascade on update cascade
) engine InnoDB;
create table `auth_assignment`
(
`item_name` varchar(64) not null,
`user_id` varchar(64) not null,
`created_at` integer,
primary key (`item_name`, `user_id`),
foreign key (`item_name`) references `auth_item` (`name`) on delete cascade on update cascade,
key `auth_assignment_user_id_idx` (`user_id`)
) engine InnoDB;

View File

@@ -0,0 +1,60 @@
/**
* Database schema required by \yii\rbac\DbManager.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alexander Kochetov <creocoder@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @since 2.0
*/
drop table "auth_assignment";
drop table "auth_item_child";
drop table "auth_item";
drop table "auth_rule";
-- create new auth_rule table
create table "auth_rule"
(
"name" varchar(64) not null,
"data" BYTEA,
"created_at" integer,
"updated_at" integer,
primary key ("name")
);
-- create auth_item table
create table "auth_item"
(
"name" varchar(64) not null,
"type" smallint not null,
"description" varchar(1000),
"rule_name" varchar(64),
"data" BYTEA,
"updated_at" integer,
foreign key ("rule_name") references "auth_rule"("name") on delete set null,
primary key ("name")
);
-- adds oracle specific index to auth_item
CREATE INDEX auth_type_index ON "auth_item"("type");
create table "auth_item_child"
(
"parent" varchar(64) not null,
"child" varchar(64) not null,
primary key ("parent","child"),
foreign key ("parent") references "auth_item"("name") on delete cascade,
foreign key ("child") references "auth_item"("name") on delete cascade
);
create table "auth_assignment"
(
"item_name" varchar(64) not null,
"user_id" varchar(64) not null,
"created_at" integer,
primary key ("item_name","user_id"),
foreign key ("item_name") references "auth_item" ("name") on delete cascade
);
CREATE INDEX auth_assignment_user_id_idx ON "auth_assignment" ("user_id");

View File

@@ -0,0 +1,59 @@
/**
* Database schema required by \yii\rbac\DbManager.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alexander Kochetov <creocoder@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @since 2.0
*/
drop table if exists "auth_assignment";
drop table if exists "auth_item_child";
drop table if exists "auth_item";
drop table if exists "auth_rule";
create table "auth_rule"
(
"name" varchar(64) not null,
"data" bytea,
"created_at" integer,
"updated_at" integer,
primary key ("name")
);
create table "auth_item"
(
"name" varchar(64) not null,
"type" smallint not null,
"description" text,
"rule_name" varchar(64),
"data" bytea,
"created_at" integer,
"updated_at" integer,
primary key ("name"),
foreign key ("rule_name") references "auth_rule" ("name") on delete set null on update cascade
);
create index auth_item_type_idx on "auth_item" ("type");
create table "auth_item_child"
(
"parent" varchar(64) not null,
"child" varchar(64) not null,
primary key ("parent","child"),
foreign key ("parent") references "auth_item" ("name") on delete cascade on update cascade,
foreign key ("child") references "auth_item" ("name") on delete cascade on update cascade
);
create table "auth_assignment"
(
"item_name" varchar(64) not null,
"user_id" varchar(64) not null,
"created_at" integer,
primary key ("item_name","user_id"),
foreign key ("item_name") references "auth_item" ("name") on delete cascade on update cascade
);
create index auth_assignment_user_id_idx on "auth_assignment" ("user_id");

View File

@@ -0,0 +1,59 @@
/**
* Database schema required by \yii\rbac\DbManager.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alexander Kochetov <creocoder@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @since 2.0
*/
drop table if exists "auth_assignment";
drop table if exists "auth_item_child";
drop table if exists "auth_item";
drop table if exists "auth_rule";
create table "auth_rule"
(
"name" varchar(64) not null,
"data" blob,
"created_at" integer,
"updated_at" integer,
primary key ("name")
);
create table "auth_item"
(
"name" varchar(64) not null,
"type" smallint not null,
"description" text,
"rule_name" varchar(64),
"data" blob,
"created_at" integer,
"updated_at" integer,
primary key ("name"),
foreign key ("rule_name") references "auth_rule" ("name") on delete set null on update cascade
);
create index "auth_item_type_idx" on "auth_item" ("type");
create table "auth_item_child"
(
"parent" varchar(64) not null,
"child" varchar(64) not null,
primary key ("parent","child"),
foreign key ("parent") references "auth_item" ("name") on delete cascade on update cascade,
foreign key ("child") references "auth_item" ("name") on delete cascade on update cascade
);
create table "auth_assignment"
(
"item_name" varchar(64) not null,
"user_id" varchar(64) not null,
"created_at" integer,
primary key ("item_name","user_id"),
foreign key ("item_name") references "auth_item" ("name") on delete cascade on update cascade
);
create index "auth_assignment_user_id_idx" on "auth_assignment" ("user_id");