1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| <?php
class SignInForm { public $name; public $pwd;
public function rules() { $rules = [ [ [ 'name', 'pwd', ], 'required', 'on' => 'signIn', ], [ ['name'], 'default', 'value' => $this->username, 'on' => 'signIn', ], [ ['pwd'], 'default', 'value' => $this->password_hash, 'on' => 'signIn', ], ];
return $rules; } }
<?php
use Yii; use workerbee\api\controllers\BaseController;
class UserController extends BaseController { public function actionSignIn() { $data['SignInForm'] = Yii::$app->request->post(); $signInModel = new SignInForm(['scenario' => 'signIn']);
if ($signInModel->load($data) && $signInModel->validate()) { $user = SignInForm::findByUsername($signInModel->username); $validator = Yii::$app->getSecurity()->validatePassword($signInModel->pwd, $user->password_hash); if ($user !== null && $validator) { $user->access_token = $signInModel->getNewAccessToken(); if ($user->save()) { return $user; }
throw new ServerErrorHttpException('登陆失败.'); }
throw new BadRequestHttpException('账号名或密码错误.'); }
throw new UnprocessableEntityHttpException('数据验证失败.'); } }
|