Exemple #1
0
 /**
  *
  * @param  int                $usr_id
  * @return API_OAuth2_Account
  */
 private function createAccount($usr_id)
 {
     $user = $this->app['manipulator.user']->getRepository()->find($usr_id);
     return API_OAuth2_Account::create($this->app, $user, $this->client);
 }
 /**
  *
  * @param  Application            $app
  * @param  User                   $user
  * @param  type                   $name
  * @return API_OAuth2_Application
  */
 public static function create(Application $app, User $user = null, $name)
 {
     $sql = '
         INSERT INTO api_applications (
             application_id, creator, created_on, name, last_modified,
             nonce, client_id, client_secret, activated, grant_password
         )
         VALUES (
             null, :usr_id, NOW(), :name, NOW(), :nonce, :client_id,
             :client_secret, :activated, :grant_password
         )';
     $nonce = random::generatePassword(6);
     $client_secret = API_OAuth2_Token::generate_token();
     $client_token = API_OAuth2_Token::generate_token();
     $params = [':usr_id' => $user ? $user->getId() : null, ':name' => $name, ':client_id' => $client_token, ':client_secret' => $client_secret, ':nonce' => $nonce, ':activated' => 1, ':grant_password' => 0];
     $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
     $stmt->execute($params);
     $stmt->closeCursor();
     $application_id = $app['phraseanet.appbox']->get_connection()->lastInsertId();
     $application = new self($app, $application_id);
     if ($user) {
         API_OAuth2_Account::create($app, $user, $application);
     }
     return $application;
 }
 public function testCheckNativeApp()
 {
     $value = self::$DI['app']['conf']->get(['registry', 'api-clients', 'navigator-enabled']);
     self::$DI['app']['conf']->set(['registry', 'api-clients', 'navigator-enabled'], false);
     $fail = null;
     try {
         $nativeApp = \API_OAuth2_Application::load_from_client_id(self::$DI['app'], \API_OAuth2_Application_Navigator::CLIENT_ID);
         $account = \API_OAuth2_Account::create(self::$DI['app'], self::$DI['user'], $nativeApp);
         $token = $account->get_token()->get_value();
         $this->setToken($token);
         self::$DI['client']->request('GET', '/api/v1/databoxes/list/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
         $content = $this->unserialize(self::$DI['client']->getResponse()->getContent());
         if (403 != $content['meta']['http_code']) {
             $fail = new \Exception('Result does not match expected 403, returns ' . $content['meta']['http_code']);
         }
     } catch (\Exception $e) {
         $fail = $e;
     }
     self::$DI['app']['conf']->set(['registry', 'api-clients', 'navigator-enabled'], false);
     if ($fail) {
         throw $fail;
     }
 }