예제 #1
0
 private function simple($require)
 {
     $params = $this->controller->getParams($require);
     $application = $this->controller->getApplication($params['app_id']);
     if (!$application) {
         throw new NotFoundApiException('ApplicationNotFound');
     }
     $form = new SimpleRegisterForm('default', $params);
     if (!$form->validate()) {
         throw new ValidationFailedApiException();
     }
     if (!$form->save()) {
         throw new ApiException($form->errors, 500);
     }
     $form->user->refresh();
     $this->controller->identity = new UserIdentity($params['email'], $params['password']);
     /**
      * @var UserApiToken $token
      */
     $token = UserApiToken::model()->findByPk($this->controller->identity->getId() . $params['app_id']);
     //        if ($token) {
     //            $token->delete();
     //        }
     if (!$token) {
         $token = UserApiToken::model()->create($form->user, $params['app_id']);
     }
     $data = array('user' => $form->user, 'token' => $token);
     $this->controller->data = $data;
 }
예제 #2
0
 public function setUp()
 {
     parent::setUp();
     $this->client = new Client(array('base_uri' => Yii::app()->homeUrl . '/' . ApiModule::CURRENT . '/api/'));
     if (!ApiTest::$userId) {
         $hash = mb_strcut(md5(time()), 0, 6);
         $params = array('email' => $hash . '@' . $hash . '.' . $hash, 'password' => $hash, 'first_name' => $hash);
         $form = new SimpleRegisterForm('default', $params);
         \Yii::app()->setComponent('gearman', array('class' => 'site.common.components.Gearman', 'servers' => array(array('host' => '127.0.0.1', 'port' => 4730))));
         if ($form->validate() && $form->save()) {
             ApiTest::$email = $form->user->email;
             ApiTest::$password = $hash;
             ApiTest::$userId = $form->user->id;
         } else {
             throw new \Exception("User Saving failed In Tests Setup");
         }
     }
 }
예제 #3
0
 /**
  * @param array $require
  * @throws NotFoundApiException
  * @return array
  */
 private function registerNewUser($require)
 {
     $params = $this->controller->getParams($require);
     $application = $this->controller->getApplication($params['app_id']);
     if (!$application) {
         throw new NotFoundApiException('ApplicationNotFound');
     }
     $connector = new SocialApiConnector();
     $method = $this->getService($params['service']);
     /**
      * @var \site\frontend\modules\api\modules\v2_1\components\social\SocialResult $data
      */
     $data = $connector->{$method}($params['token'])->check();
     if (!$data->getStatus()) {
         $this->controller->setError($data->toArray(), 400);
         return false;
     }
     /**
      * @var \site\frontend\modules\api\modules\v2_1\components\social\SocialResult $result
      */
     $result = $connector->{$method}()->userGet($data->getUserId(), $this->serviceUserFields[$this->getService($params['service'])]);
     if ($result->getStatus()) {
         $result->setSocialUser(new SocialUser($params['email'], $this->getService($params['service']), $result->getResult()));
         $form = new SimpleRegisterForm('social', $result->getSocialUser()->toArray());
         if (!$form->validate() || !$form->save()) {
             throw new ApiException($form->errors, 500);
         }
         $this->createUserSocialToken($params['app_id'], $this->serviceNames[$this->getService($params['service'])], $params['token']);
         $this->controller->toAuthArray($form->user);
         return $this->controller->data;
     } else {
         $this->controller->setError($result->getResult(), '500');
     }
 }