/**
  * @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 auth()
 {
     if (!$this->validate()) {
         return false;
     }
     $client = new Client();
     $client->setUser($this->_user);
     $client->generateToken();
     if (!$client->save()) {
         $this->addError('password', 'Could not create Client');
         return false;
     }
     return ['user' => $this->_user, 'client' => $client];
 }
Esempio n. 3
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getClients()
 {
     return $this->hasMany(Client::className(), ['user_id' => 'id']);
 }
 public function withToken($token)
 {
     $client = Client::find()->andWhere(['token' => $token])->limit(1)->one();
     return $this->andWhere(['id' => isset($client->user_id) ? $client->user_id : 0]);
 }