1 | <?php |
||
62 | class User extends ActiveRecord implements IdentityInterface |
||
63 | { |
||
64 | use ModuleAwareTrait; |
||
65 | use ContainerAwareTrait; |
||
66 | |||
67 | // following constants are used on secured email changing process |
||
68 | const OLD_EMAIL_CONFIRMED = 0b01; |
||
69 | const NEW_EMAIL_CONFIRMED = 0b10; |
||
70 | |||
71 | /** |
||
72 | * @var string Plain password. Used for model validation |
||
73 | */ |
||
74 | public $password; |
||
75 | /** |
||
76 | * @var array connected account list |
||
77 | */ |
||
78 | protected $connectedAccounts; |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | * |
||
83 | * @throws InvalidParamException |
||
84 | * @throws InvalidConfigException |
||
85 | * @throws Exception |
||
86 | */ |
||
87 | 16 | public static function tableName() |
|
88 | { |
||
89 | 16 | return '{{%user}}'; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 3 | public static function findIdentity($id) |
|
96 | { |
||
97 | 3 | return static::findOne($id); |
|
|
|||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return UserQuery |
||
102 | */ |
||
103 | 16 | public static function find() |
|
104 | { |
||
105 | 16 | return new UserQuery(static::class); |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | * |
||
111 | * @throws NotSupportedException |
||
112 | */ |
||
113 | public static function findIdentityByAccessToken($token, $type = null) |
||
114 | { |
||
115 | throw new NotSupportedException('Method "' . __CLASS__ . '::' . __METHOD__ . '" is not implemented.'); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 10 | public function beforeSave($insert) |
|
122 | { |
||
123 | /** @var SecurityHelper $security */ |
||
124 | 10 | $security = $this->make(SecurityHelper::class); |
|
125 | 10 | if ($insert) { |
|
126 | 7 | $this->setAttribute('auth_key', $security->generateRandomString()); |
|
127 | 7 | if (Yii::$app instanceof Application) { |
|
128 | 7 | $this->setAttribute('registration_ip', $this->module->disableIpLogging ? '127.0.0.1' : Yii::$app->request->getUserIP()); |
|
129 | } |
||
130 | } |
||
131 | |||
132 | 10 | if (!empty($this->password)) { |
|
133 | 10 | $this->setAttribute( |
|
134 | 10 | 'password_hash', |
|
135 | 10 | $security->generatePasswordHash($this->password, $this->getModule()->blowfishCost) |
|
136 | ); |
||
137 | 10 | $this->password_changed_at = time(); |
|
138 | } |
||
139 | |||
140 | 10 | return parent::beforeSave($insert); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * @inheritdoc |
||
145 | * |
||
146 | * @throws InvalidConfigException |
||
147 | */ |
||
148 | 10 | public function afterSave($insert, $changedAttributes) |
|
149 | { |
||
150 | 10 | parent::afterSave($insert, $changedAttributes); |
|
151 | |||
152 | 10 | if ($insert && $this->profile === null) { |
|
153 | 7 | $profile = $this->make(Profile::class); |
|
154 | 7 | $profile->link('user', $this); |
|
155 | } |
||
156 | 10 | } |
|
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 16 | public function behaviors() |
|
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | */ |
||
181 | 2 | public function attributeLabels() |
|
182 | { |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | 9 | public function scenarios() |
|
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | 9 | public function rules() |
|
254 | |||
255 | /** |
||
256 | * {@inheritdoc} |
||
257 | */ |
||
258 | public function validateAuthKey($authKey) |
||
262 | |||
263 | /** |
||
264 | * {@inheritdoc} |
||
265 | */ |
||
266 | 9 | public function getId() |
|
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | */ |
||
274 | 9 | public function getAuthKey() |
|
278 | |||
279 | /** |
||
280 | * @return bool whether is blocked or not |
||
281 | */ |
||
282 | 4 | public function getIsBlocked() |
|
286 | |||
287 | /** |
||
288 | * @throws InvalidConfigException |
||
289 | * @return bool whether the user is an admin or not |
||
290 | */ |
||
291 | 2 | public function getIsAdmin() |
|
295 | |||
296 | /** |
||
297 | * Returns whether user account has been confirmed or not. |
||
298 | * @return bool whether user account has been confirmed or not |
||
299 | */ |
||
300 | 5 | public function getIsConfirmed() |
|
304 | |||
305 | /** |
||
306 | * Checks whether a user has a specific role. |
||
307 | * |
||
308 | * @param string $role |
||
309 | * |
||
310 | * @return bool |
||
311 | */ |
||
312 | public function hasRole($role) |
||
316 | |||
317 | /** |
||
318 | * @throws InvalidConfigException |
||
319 | * @throws InvalidParamException |
||
320 | * @return \yii\db\ActiveQuery |
||
321 | */ |
||
322 | 10 | public function getProfile() |
|
326 | |||
327 | /** |
||
328 | * @throws \Exception |
||
329 | * @return SocialNetworkAccount[] social connected accounts [ 'providerName' => socialAccountModel ] |
||
330 | * |
||
331 | */ |
||
332 | public function getSocialNetworkAccounts() |
||
350 | |||
351 | /** |
||
352 | * Returns password age in days |
||
353 | * @return integer |
||
354 | */ |
||
355 | public function getPassword_age() |
||
364 | } |
||
365 |
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.