Skip to content

Commit 33b1ef1

Browse files
committed
edit user model
1 parent bddedaa commit 33b1ef1

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

api/modules/v1/controllers/UserController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,6 @@ public function actionRegister(){
7070

7171
}
7272

73+
74+
7375
}

api/modules/v1/models/User.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Yii;
66
use yii\base\Model;
77
use base\db\ActiveRecord;
8+
use yii\behaviors\AttributeBehavior;
89
use yii\web\IdentityInterface;
910

1011
/**
@@ -52,13 +53,30 @@ public static function tableName()
5253
return '{{%user}}';
5354
}
5455

56+
public function behaviors()
57+
{
58+
return [
59+
[
60+
'class' => AttributeBehavior::className(),
61+
'attributes' => [
62+
ActiveRecord::EVENT_BEFORE_INSERT => ['created_by', 'updated_by'],
63+
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_by'
64+
65+
],
66+
'value' => function ($event) {
67+
return Yii::$app->user->id;
68+
},
69+
],
70+
];
71+
}
72+
5573
public function rules()
5674
{
5775
return [
5876
// username and password are both required
5977
[['username', 'password_hash'], 'required'],
6078
[['username', 'password_hash'], 'safe'],
61-
[['username', 'password_hash'], 'string'],
79+
[['username', 'password_hash', 'created_by', 'updated_by'], 'string'],
6280
// rememberMe must be a boolean value
6381
['rememberMe', 'boolean'],
6482
// password is validated by validatePassword()
@@ -265,7 +283,7 @@ public function login()
265283
public function register($data){
266284

267285
$user = new User();
268-
$user->id= null;
286+
$user->id= Yii::$app->security->generateRandomString(36);
269287
$user->email= $data->username;
270288
$user->username = $data->username;
271289
$user->setPassword($data->password);
@@ -275,6 +293,7 @@ public function register($data){
275293
$user->generateRefreshToken();
276294
$user->created_at = time() /1000;
277295
$user->updated_at = time() / 1000;
296+
$user->created_by = 'test';
278297

279298
$user->isNewRecord = true;
280299
if(!$user->save()) {
@@ -286,4 +305,5 @@ public function register($data){
286305
}
287306
}
288307

308+
289309
}

0 commit comments

Comments
 (0)