1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/patron/license |
6
|
|
|
* @link https://www.flipboxfactory.com/patron/domains/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\patron\db\traits; |
10
|
|
|
|
11
|
|
|
use craft\helpers\Db; |
12
|
|
|
use flipbox\patron\records\Token; |
13
|
|
|
use flipbox\patron\records\TokenEnvironment; |
14
|
|
|
use yii\db\Expression; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
trait TokenAttributes |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var bool|null The enabled state |
24
|
|
|
*/ |
25
|
|
|
public $enabled = true; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int|int[]|false|null The model ID(s). Prefix IDs with "not " to exclude them. |
29
|
|
|
*/ |
30
|
|
|
public $providerId; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string|string[]|null The access token(s). Prefix IDs with "not " to exclude them. |
34
|
|
|
*/ |
35
|
|
|
public $accessToken; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string|string[]|null The refresh token(s). Prefix IDs with "not " to exclude them. |
39
|
|
|
*/ |
40
|
|
|
public $refreshToken; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string|string[]|null The environment(s). Prefix with "not " to exclude them. |
44
|
|
|
*/ |
45
|
|
|
public $environment; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Adds an additional WHERE condition to the existing one. |
49
|
|
|
* The new condition and the existing one will be joined using the `AND` operator. |
50
|
|
|
* @param string|array|Expression $condition the new WHERE condition. Please refer to [[where()]] |
51
|
|
|
* on how to specify this parameter. |
52
|
|
|
* @param array $params the parameters (name => value) to be bound to the query. |
53
|
|
|
* @return $this the query object itself |
54
|
|
|
* @see where() |
55
|
|
|
* @see orWhere() |
56
|
|
|
*/ |
57
|
|
|
abstract public function andWhere($condition, $params = []); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Appends a LEFT OUTER JOIN part to the query. |
61
|
|
|
* @param string|array $table the table to be joined. |
62
|
|
|
* |
63
|
|
|
* Use a string to represent the name of the table to be joined. |
64
|
|
|
* The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). |
65
|
|
|
* The method will automatically quote the table name unless it contains some parenthesis |
66
|
|
|
* (which means the table is given as a sub-query or DB expression). |
67
|
|
|
* |
68
|
|
|
* Use an array to represent joining with a sub-query. The array must contain only one element. |
69
|
|
|
* The value must be a [[Query]] object representing the sub-query while the corresponding key |
70
|
|
|
* represents the alias for the sub-query. |
71
|
|
|
* |
72
|
|
|
* @param string|array $on the join condition that should appear in the ON part. |
73
|
|
|
* Please refer to [[join()]] on how to specify this parameter. |
74
|
|
|
* @param array $params the parameters (name => value) to be bound to the query |
75
|
|
|
* @return $this the query object itself |
76
|
|
|
*/ |
77
|
|
|
abstract public function leftJoin($table, $on = '', $params = []); |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $enabled |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function enabled($enabled) |
85
|
|
|
{ |
86
|
|
|
$this->enabled = $enabled; |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $id |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function providerId($id) |
95
|
|
|
{ |
96
|
|
|
$this->providerId = $id; |
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $accessToken |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function accessToken($accessToken) |
105
|
|
|
{ |
106
|
|
|
$this->accessToken = $accessToken; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param $refreshToken |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function refreshToken($refreshToken) |
115
|
|
|
{ |
116
|
|
|
$this->refreshToken = $refreshToken; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $environment |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function environment($environment) |
125
|
|
|
{ |
126
|
|
|
$this->environment = $environment; |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
*/ |
133
|
|
|
protected function applyConditions() |
134
|
|
|
{ |
135
|
|
|
if ($this->enabled !== null) { |
136
|
|
|
$this->andWhere(Db::parseParam('enabled', $this->enabled)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$attributes = ['providerId', 'accessToken', 'refreshToken']; |
140
|
|
|
|
141
|
|
|
foreach ($attributes as $attribute) { |
142
|
|
|
if (($value = $this->{$attribute}) !== null) { |
143
|
|
|
$this->andWhere(Db::parseParam($attribute, $value)); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->applyEnvironmentParam(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Apply environment params |
152
|
|
|
*/ |
153
|
|
|
protected function applyEnvironmentParam() |
154
|
|
|
{ |
155
|
|
|
if (empty($this->environment)) { |
156
|
|
|
return; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$alias = TokenEnvironment::tableAlias(); |
160
|
|
|
|
161
|
|
|
$this->leftJoin( |
162
|
|
|
TokenEnvironment::tableName() . ' ' . $alias, |
163
|
|
|
'[[' . $alias . '.tokenId]] = [[' . Token::tableAlias() . '.id]]' |
164
|
|
|
); |
165
|
|
|
$this->andWhere( |
166
|
|
|
Db::parseParam($alias . '.environment', $this->environment) |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|