Esempio n. 1
0
 /**
  * Login
  *
  * <b>Request Type</b>: POST<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/site/login<br/><br/>
  * <b>Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used for the users to login.
  * <br/><br/>
  *
  * <b>Request Params</b>:<br/>
  *     email: string, the user email, required<br/>
  *     password: string, the user password, required<br/>
  *     <br/><br/>
  *
  * <b>Response Params:</b><br/>
  *     ack: integer, mark the create result, 0 means create successfully, 1 means create fail<br/>
  *     msg: string, if create fail, it contains the error message<br/>
  *     data: array, json array to describe the users detail information<br/>
  *     <br/><br/>
  *
  * <b>Request Example:</b><br/>
  * <pre>
  * {
  *     "email" : "*****@*****.**",
  *     "password" : "aaaaaaaaaaaaaaaaaaaaaaaaa"
  * }
  * </pre>
  * <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * <pre>
  * {
  *    'ack' : 1,
  *    'data': {"userInfo": {name:"Devin Jin", avatar:"path/to/avatar", enabledModules:['a', 'b', 'c']}}
  * }
  * </pre>
  */
 public function actionLogin()
 {
     $params = $this->getParams();
     if (empty($params['email']) || empty($params['password'])) {
         throw new BadRequestHttpException(Yii::t('common', 'parameters_missing'));
     }
     $user = User::getByEmail(mb_strtolower($params['email']));
     if (empty($user)) {
         throw new InvalidParameterException(['email' => Yii::t('common', 'incorrect_userid')]);
     }
     $account = Account::findByPk($user->accountId);
     if (empty($account) || $account->status !== Account::STATUS_ACTIVATED) {
         throw new BadRequestHttpException(Yii::t('common', 'account_is_unactivated'));
     }
     if (!$user->isActivated) {
         throw new InvalidParameterException(['email' => Yii::t('common', 'user_not_activate')]);
     }
     if ($user->validatePassword($params['password'])) {
         $accessToken = Token::create($user);
         $userInfo = ['name' => $user->name, 'email' => $user->email, 'language' => $user->language, 'avatar' => empty($user->avatar) ? '' : $user->avatar, 'enabledModules' => $accessToken['enabledMods'], 'role' => $user->role, 'id' => $user->_id . '', 'accountId' => (string) $user->accountId, 'company' => $account->company];
         $this->setAccessToken($accessToken['accessToken']);
         return ['userInfo' => $userInfo];
     } else {
         throw new InvalidParameterException(['password' => Yii::t('common', 'password_error')]);
     }
 }