Completed
Push — master ( f68d91...6e47ec )
by vistart
03:59
created

User::attributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
ccs 16
cts 16
cp 1
rs 9.4285
cc 1
eloc 17
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user;
14
15
use rhosocial\base\models\models\BaseUserModel;
16
use rhosocial\base\models\queries\BaseBlameableQuery;
17
use rhosocial\user\models\log\UserLoginTrait;
18
use rhosocial\user\security\UserPasswordHistoryTrait;
19
use Yii;
20
21
/**
22
 * Common User Model.
23
 * This model should be stored in a relational database. You can create a foreign
24
 * key constraint on other models and this model.
25
 *
26
 * If you're using MySQL, we recommend that you create a data table using the following statement:
27
 *
28
 * ```
29
 * CREATE TABLE `user` (
30
 *   `guid` varbinary(16) NOT NULL COMMENT 'GUID',
31
 *   `id` varchar(16) COLLATE utf8_unicode_ci NOT NULL COMMENT 'ID',
32
 *   `pass_hash` varchar(80) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Password Hash',
33
 *   `ip` varbinary(16) NOT NULL DEFAULT '0' COMMENT 'IP',
34
 *   `ip_type` tinyint(3) unsigned NOT NULL DEFAULT '4' COMMENT 'IP Address Type',
35
 *   `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT 'Create Time',
36
 *   `updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT 'Update Time',
37
 *   `auth_key` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT 'Authentication Key',
38
 *   `access_token` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT 'Access Token',
39
 *   `password_reset_token` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT 'Password Reset Token',
40
 *   `status` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT 'Status',
41
 *   `type` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT 'Type',
42
 *   `source` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT 'Source',
43
 *   PRIMARY KEY (`guid`),
44
 *   UNIQUE KEY `user_id_unique` (`id`),
45
 *   KEY `user_auth_key_normal` (`auth_key`),
46
 *   KEY `user_access_token_normal` (`access_token`),
47
 *   KEY `user_password_reset_token` (`password_reset_token`),
48
 *   KEY `user_create_time_normal` (`created_at`)
49
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='User';
50
 * ```
51
 *
52
 * The fields of User table in database are following:
53
 * @property string $guid User's GUID. This property is used to uniquely identify a user.
54
 * This property is automatically generated when the class is created, we do not
55
 * recommend that you modify this property, unless you know the consequences of doing so.
56
 * This property is also regareded as the foreign key target of other models associated
57
 * with this model. If you have to modify this property, the foreign key constraints
58
 * should be updating and deleting on cascade.
59
 * @property string $id User Identifier No. It is a 8-digit number beginning with 4 by default.
60
 * @property string $pass_hash Password Hash.
61
 * We strongly recommend you NOT to change this property directly!
62
 * If you want to set or reset password, please use setPassword() magic property instead.
63
 * @property integer $ip IP address.
64
 * @property integer $ipType
65
 * @property string $createdAt
66
 * @property string $updatedAt
67
 * @property string $auth_key
68
 * @property string $access_token
69
 * @property string $password_reset_token
70
 * @property integer $status
71
 * @property integer $type
72
 * @property string $source
73
 *
74
 * @property-read Profile $profile Profile. This magic property is read-only.
75
 * If you want to modify anyone property of Profile model, please get it first,
76
 * then change and save it, like following:
77
 * ```php
78
 * $profile = $user->profile;
79
 * $profile->nickname = 'vistart';
80
 * $profile->save();
81
 * ```
82
 * If $profileClass is `false`, `null` returned.
83
 * @version 1.0
84
 * @author vistart <[email protected]>
85
 */
86
class User extends BaseUserModel
87
{
88
    use UserPasswordHistoryTrait, UserLoginTrait;
89
    public $searchClass = UserSearch::class;
90
    public function getSearchModel()
91
    {
92
        $class = $this->searchClass;
93
        if (empty($class) || !class_exists($class)) {
94
            return null;
95
        }
96
        return new $class;
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102 1
    public function attributeLabels()
103
    {
104
        return [
105 1
            $this->guidAttribute => Yii::t('user', 'GUID'),
106 1
            $this->idAttribute => Yii::t('user', 'ID'),
107 1
            $this->passwordHashAttribute => Yii::t('user', 'Password Hash'),
108 1
            $this->ipAttribute => Yii::t('user', 'IP Address'),
109 1
            $this->ipTypeAttribute => Yii::t('user', 'IP Address Type'),
110 1
            $this->createdAtAttribute => Yii::t('user', 'Creation Time'),
111 1
            $this->updatedAtAttribute => Yii::t('user', 'Last Updated Time'),
112 1
            $this->authKeyAttribute => Yii::t('user', 'Authentication Key'),
113 1
            $this->accessTokenAttribute => Yii::t('user', 'Access Token'),
114 1
            $this->passwordResetTokenAttribute => Yii::t('user', 'Password Reset Token'),
115 1
            $this->statusAttribute => Yii::t('user', 'Status'),
116 1
            'type' => Yii::t('user', 'Type'),
117 1
            $this->sourceAttribute => Yii::t('user', 'Source'),
118 1
            'createdAt' => Yii::t('user', 'Registration Time'),
119 1
            'updatedAt' => Yii::t('user', 'Last Updated Time'),
120
        ];
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126
    public $idAttributeType = 1;
127
128
    /**
129
     * @inheritdoc
130
     */
131
    public $idAttributeLength = 8;
132
133
    /**
134
     * @inheritdoc
135
     */
136
    public $idAttributePrefix = '4';
137
138
    /**
139
     * @inheritdoc
140
     */
141 41
    public static function tableName()
142
    {
143 41
        return '{{%user}}';
144
    }
145
146
    /**
147
     * @var string|false Profile class name. If you do not need profile model,
148
     * please set it false.
149
     */
150
    public $profileClass = false;
151
    
152 41
    public function init()
153
    {
154 41
        $this->on(static::$eventAfterRegister, [$this, 'onAddPasswordToHistory']);
155 41
        $this->on(static::$eventAfterResetPassword, [$this, 'onAddPasswordToHistory']);
156 41
        parent::init();
157 41
    }
158
159
    /**
160
     * Create profile.
161
     * If profile of this user exists, it will be returned instead of creating it.
162
     * Meanwhile, the $config parameter will be skipped.
163
     * @param array $config Profile configuration. Skipped if it exists.
164
     * @return Profile
165
     */
166 19
    public function createProfile($config = [])
167
    {
168 19
        $profileClass = $this->profileClass;
169 19
        if (empty($profileClass) || !is_string($this->profileClass)) {
170 1
            return null;
171
        }
172 18
        $profile = $profileClass::findOne($this->getGUID());
173 18
        if (!$profile) {
174 18
            $profile = $this->create($profileClass, $config);
175 18
            $profile->setGUID($this->getGUID());
176
        }
177 18
        return $profile;
178
    }
179
180
    /**
181
     * 
182
     * @return boolean
183
     */
184 4
    public function hasProfile()
185
    {
186 4
        if ($this->profileClass === false || !is_string($this->profileClass) || !class_exists($this->profileClass)) {
187 2
            return false;
188
        }
189 2
        return true;
190
    }
191
192
    /**
193
     * Get Profile query.
194
     * If you want to get profile model, please access this method in magic property way,
195
     * like following:
196
     *
197
     * ```php
198
     * $user->profile;
199
     * ```
200
     *
201
     * @return BaseBlameableQuery
202
     */
203 4
    public function getProfile()
204
    {
205 4
        if (!$this->hasProfile()) {
206 2
            return null;
207
        }
208 2
        $profileClass = $this->profileClass;
209 2
        $profileModel = $profileClass::buildNoInitModel();
210 2
        return $this->hasOne($profileClass,
211 2
                [$profileModel->createdByAttribute => $this->guidAttribute])->inverseOf('user');
212
    }
213
}
214