/**
  * @return array|bool
  * @throws \yii\db\Exception
  */
 public function signUp()
 {
     if (!$this->validate()) {
         return false;
     }
     $dbTransaction = Yii::$app->db->beginTransaction();
     try {
         $user = new User();
         $user->email = $this->email;
         $user->setPassword($this->password);
         if (!$user->save()) {
             throw new ErrorException('Could not create User');
         }
         $this->_user = $user;
         $client = new Client();
         $client->setUser($user);
         $client->generateToken();
         if (!$client->save()) {
             throw new ErrorException('Could not create Client');
         }
     } catch (ErrorException $e) {
         $dbTransaction->rollBack();
         return false;
     }
     $dbTransaction->commit();
     return ['user' => $user, 'client' => $client];
 }
 public function userValidator()
 {
     if (!$this->hasErrors()) {
         $user = User::find()->active()->andWhere(['email' => $this->email])->limit(1)->one();
         /* @var $user \api\v1\models\User */
         if (!$user || !$user->validatePassword($this->password)) {
             $this->addError('password', 'Wrong auth data.');
             return false;
         }
         $this->_user = $user;
         return true;
     }
 }
 public function beforeAction($action)
 {
     Yii::$app->response->format = 'json';
     $token = call_user_func(function () {
         $headers = Yii::$app->request->headers;
         return isset($headers['token']) ? $headers['token'] : null;
     });
     if (!$token) {
         Yii::$app->response->data = ['status' => false, 'error' => 'NO_TOKEN', 'error_code' => 'NO_TOKEN'];
         return false;
     }
     $user = User::find()->active()->withToken($token)->one();
     if (!$user) {
         Yii::$app->response->data = ['status' => false, 'error' => 'WRONG_TOKEN', 'error_code' => 'WRONG_TOKEN'];
         return false;
     }
     $this->_user = $user;
     return parent::beforeAction($action);
 }
 public function getUsers()
 {
     return $this->hasMany(User::className(), ['id' => 'user_id'])->via('userGroups');
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getUser()
 {
     return $this->hasOne(User::className(), ['id' => 'user_id']);
 }