1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use yii\db\Schema; |
4
|
|
|
|
5
|
|
|
class m160501_075311_add_oauth2_server extends \yii\db\Migration |
6
|
|
|
{ |
7
|
|
|
public function mysql($yes, $no = '') |
8
|
|
|
{ |
9
|
|
|
return $this->db->driverName === 'mysql' ? $yes : $no; |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
public function primaryKey($columns = '') |
13
|
|
|
{ |
14
|
|
|
return 'PRIMARY KEY (' . $this->db->getQueryBuilder()->buildColumns($columns) . ')'; |
|
|
|
|
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function foreignKey($columns, $refTable, $refColumns, $onDelete = null, $onUpdate = null) |
18
|
|
|
{ |
19
|
|
|
$builder = $this->db->getQueryBuilder(); |
20
|
|
|
$sql = ' FOREIGN KEY (' . $builder->buildColumns($columns) . ')' |
21
|
|
|
. ' REFERENCES ' . $this->db->quoteTableName($refTable) |
22
|
|
|
. ' (' . $builder->buildColumns($refColumns) . ')'; |
23
|
|
|
if ($onDelete !== null) { |
24
|
|
|
$sql .= ' ON DELETE ' . $onDelete; |
25
|
|
|
} |
26
|
|
|
if ($onUpdate !== null) { |
27
|
|
|
$sql .= ' ON UPDATE ' . $onUpdate; |
28
|
|
|
} |
29
|
|
|
return $sql; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function up() |
33
|
|
|
{ |
34
|
|
|
$tableOptions = null; |
35
|
|
|
if ($this->db->driverName === 'mysql') { |
36
|
|
|
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$now = $this->mysql('CURRENT_TIMESTAMP', "'now'"); |
40
|
|
|
$on_update_now = $this->mysql("ON UPDATE $now"); |
41
|
|
|
|
42
|
|
|
$transaction = $this->db->beginTransaction(); |
43
|
|
|
try { |
44
|
|
|
$this->createTable('{{%oauth_clients}}', [ |
45
|
|
|
'client_name' => Schema::TYPE_STRING . '(36) NOT NULL COMMENT "客户端名称"', |
46
|
|
|
'client_icon' => $this->string()->comment('图标'), |
47
|
|
|
'client_id' => Schema::TYPE_STRING . '(36) NOT NULL COMMENT "客户端ID"', |
48
|
|
|
'client_secret' => Schema::TYPE_STRING . '(32) DEFAULT NULL COMMENT "客户端密钥"', |
49
|
|
|
'redirect_uri' => Schema::TYPE_STRING . '(1000) NOT NULL DEFAULT "" COMMENT "回跳地址"', |
50
|
|
|
'grant_types' => Schema::TYPE_STRING . '(100) NOT NULL COMMENT "授权类型"', |
51
|
|
|
'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL COMMENT "权限范围"', |
52
|
|
|
'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL COMMENT "用户"', |
53
|
|
|
$this->primaryKey('client_id'), |
54
|
|
|
], $tableOptions); |
55
|
|
|
|
56
|
|
|
$this->createTable('{{%oauth_access_tokens}}', [ |
57
|
|
|
'access_token' => Schema::TYPE_STRING . '(40) NOT NULL COMMENT "访问令牌"', |
58
|
|
|
'client_id' => Schema::TYPE_STRING . '(36) NOT NULL COMMENT "客户端ID"', |
59
|
|
|
'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL COMMENT "用户"', |
60
|
|
|
'expires' => Schema::TYPE_TIMESTAMP . " NOT NULL DEFAULT $now $on_update_now COMMENT '期限'", |
61
|
|
|
'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL COMMENT "权限范围"', |
62
|
|
|
$this->primaryKey('access_token'), |
63
|
|
|
$this->foreignKey('client_id', '{{%oauth_clients}}', 'client_id', 'CASCADE', 'CASCADE'), |
64
|
|
|
], $tableOptions); |
65
|
|
|
|
66
|
|
|
$this->createTable('{{%oauth_refresh_tokens}}', [ |
67
|
|
|
'refresh_token' => Schema::TYPE_STRING . '(40) NOT NULL COMMENT "刷新令牌"', |
68
|
|
|
'client_id' => Schema::TYPE_STRING . '(36) NOT NULL COMMENT "客户端ID"', |
69
|
|
|
'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL COMMENT "用户"', |
70
|
|
|
'expires' => Schema::TYPE_TIMESTAMP . " NOT NULL DEFAULT $now $on_update_now COMMENT '期限'", |
71
|
|
|
'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL COMMENT "权限范围"', |
72
|
|
|
$this->primaryKey('refresh_token'), |
73
|
|
|
$this->foreignKey('client_id', '{{%oauth_clients}}', 'client_id', 'CASCADE', 'CASCADE'), |
74
|
|
|
], $tableOptions); |
75
|
|
|
|
76
|
|
|
$this->createTable('{{%oauth_authorization_codes}}', [ |
77
|
|
|
'authorization_code' => Schema::TYPE_STRING . '(40) NOT NULL COMMENT "授权码"', |
78
|
|
|
'client_id' => Schema::TYPE_STRING . '(36) NOT NULL COMMENT "客户端ID"', |
79
|
|
|
'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL COMMENT "用户"', |
80
|
|
|
'redirect_uri' => Schema::TYPE_STRING . '(1000) NOT NULL COMMENT "回跳地址"', |
81
|
|
|
'expires' => Schema::TYPE_TIMESTAMP . " NOT NULL DEFAULT $now $on_update_now COMMENT '期限'", |
82
|
|
|
'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL COMMENT "权限范围"', |
83
|
|
|
$this->primaryKey('authorization_code'), |
84
|
|
|
$this->foreignKey('client_id', '{{%oauth_clients}}', 'client_id', 'CASCADE', 'CASCADE'), |
85
|
|
|
], $tableOptions); |
86
|
|
|
|
87
|
|
|
$this->createTable('{{%oauth_scopes}}', [ |
88
|
|
|
'scope' => Schema::TYPE_STRING . '(2000) NOT NULL COMMENT "权限范围"', |
89
|
|
|
'is_default' => Schema::TYPE_BOOLEAN . ' NOT NULL COMMENT "默认"', |
90
|
|
|
], $tableOptions); |
91
|
|
|
|
92
|
|
|
$this->createTable('{{%oauth_jwt}}', [ |
93
|
|
|
'client_id' => Schema::TYPE_STRING . '(36) NOT NULL COMMENT "客户端ID"', |
94
|
|
|
'subject' => Schema::TYPE_STRING . '(80) DEFAULT NULL COMMENT "接收者"', |
95
|
|
|
'public_key' => Schema::TYPE_STRING . '(2000) DEFAULT NULL COMMENT "公钥"', |
96
|
|
|
$this->primaryKey('client_id'), |
97
|
|
|
], $tableOptions); |
98
|
|
|
|
99
|
|
|
$this->createTable('{{%oauth_public_keys}}', [ |
100
|
|
|
'client_id' => Schema::TYPE_STRING . '(255) NOT NULL COMMENT "客户端ID"', |
101
|
|
|
'public_key' => Schema::TYPE_STRING . '(2000) DEFAULT NULL COMMENT "公钥"', |
102
|
|
|
'private_key' => Schema::TYPE_STRING . '(2000) DEFAULT NULL COMMENT "私钥"', |
103
|
|
|
'encryption_algorithm' => Schema::TYPE_STRING . '(100) DEFAULT \'RS256\' COMMENT "加密算法"', |
104
|
|
|
], $tableOptions); |
105
|
|
|
|
106
|
|
|
$transaction->commit(); |
107
|
|
|
} catch (Exception $e) { |
108
|
|
|
echo 'Exception: ' . $e->getMessage() . '\n'; |
109
|
|
|
$transaction->rollback(); |
110
|
|
|
|
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return true; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function down() |
118
|
|
|
{ |
119
|
|
|
$transaction = $this->db->beginTransaction(); |
120
|
|
|
try { |
121
|
|
|
$this->dropTable('{{%oauth_jwt}}'); |
122
|
|
|
$this->dropTable('{{%oauth_scopes}}'); |
123
|
|
|
$this->dropTable('{{%oauth_authorization_codes}}'); |
124
|
|
|
$this->dropTable('{{%oauth_refresh_tokens}}'); |
125
|
|
|
$this->dropTable('{{%oauth_access_tokens}}'); |
126
|
|
|
$this->dropTable('{{%oauth_public_keys}}'); |
127
|
|
|
$this->dropTable('{{%oauth_clients}}'); |
128
|
|
|
|
129
|
|
|
$transaction->commit(); |
130
|
|
|
} catch (Exception $e) { |
131
|
|
|
$transaction->rollback(); |
132
|
|
|
echo $e->getMessage(); |
133
|
|
|
echo "\n"; |
134
|
|
|
echo get_called_class() . ' cannot be reverted.'; |
135
|
|
|
echo "\n"; |
136
|
|
|
|
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return true; |
|
|
|
|
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.