diff --git a/lib/classes/API/OAuth2/Account.class.php b/lib/classes/API/OAuth2/Account.class.php index 01ccb12b53..94ce99501f 100644 --- a/lib/classes/API/OAuth2/Account.class.php +++ b/lib/classes/API/OAuth2/Account.class.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +use Alchemy\Phrasea\Application; + /** * * @package OAuth2 Connector @@ -23,9 +25,9 @@ class API_OAuth2_Account { /** * - * @var appbox + * @var Application */ - protected $appbox; + protected $app; /** * @@ -82,22 +84,22 @@ class API_OAuth2_Account * @param int $account_id * @return API_OAuth2_Account */ - public function __construct(appbox &$appbox, $account_id) + public function __construct(Application $app, $account_id) { - $this->appbox = $appbox; + $this->app = $app; $this->id = (int) $account_id; $sql = 'SELECT api_account_id, usr_id, api_version, revoked , application_id, created FROM api_accounts WHERE api_account_id = :api_account_id'; - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array(':api_account_id' => $this->id)); $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); $this->application_id = (int) $row['application_id']; - $this->user = User_Adapter::getInstance($row['usr_id'], $this->appbox); + $this->user = User_Adapter::getInstance($row['usr_id'], $app); $this->api_version = $row['api_version']; $this->revoked = ! ! $row['revoked']; @@ -159,7 +161,7 @@ class API_OAuth2_Account , 'account_id' => $this->id ); - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); @@ -183,9 +185,9 @@ class API_OAuth2_Account { if ( ! $this->token) { try { - $this->token = new API_OAuth2_Token($this->appbox, $this); + $this->token = new API_OAuth2_Token($this->app['phraseanet.appbox'], $this); } catch (Exception_NotFound $e) { - $this->token = API_OAuth2_Token::create($this->appbox, $this); + $this->token = API_OAuth2_Token::create($this->app['phraseanet.appbox'], $this); } } @@ -199,7 +201,7 @@ class API_OAuth2_Account public function get_application() { if ( ! $this->application) - $this->application = new API_OAuth2_Application($this->appbox, $this->application_id); + $this->application = new API_OAuth2_Application($this->app, $this->application_id); return $this->application; } @@ -212,16 +214,16 @@ class API_OAuth2_Account { $this->get_token()->delete(); - foreach (API_OAuth2_AuthCode::load_codes_by_account($this->appbox, $this) as $code) { + foreach (API_OAuth2_AuthCode::load_codes_by_account($this->app, $this) as $code) { $code->delete(); } - foreach (API_OAuth2_RefreshToken::load_by_account($this->appbox, $this) as $token) { + foreach (API_OAuth2_RefreshToken::load_by_account($this->app, $this) as $token) { $token->delete(); } $sql = 'DELETE FROM api_accounts WHERE api_account_id = :account_id'; - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array('account_id' => $this->id)); $stmt->closeCursor(); @@ -235,7 +237,7 @@ class API_OAuth2_Account * @param API_OAuth2_Application $application * @return API_OAuth2_Account */ - public static function create(appbox &$appbox, User_Adapter $user, API_OAuth2_Application $application) + public static function create(Application $app, User_Adapter $user, API_OAuth2_Application $application) { $sql = 'INSERT INTO api_accounts (api_account_id, usr_id, revoked, api_version, application_id, created) @@ -250,13 +252,13 @@ class API_OAuth2_Account , ':created' => $datetime->format("Y-m-d H:i:s") ); - $stmt = $appbox->get_connection()->prepare($sql); + $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); - $account_id = $appbox->get_connection()->lastInsertId(); + $account_id = $app['phraseanet.appbox']->get_connection()->lastInsertId(); - return new self($appbox, $account_id); + return new self($app, $account_id); } /** @@ -266,7 +268,7 @@ class API_OAuth2_Account * @param User_Adapter $user * @return API_OAuth2_Account */ - public static function load_with_user(appbox &$appbox, API_OAuth2_Application $application, User_Adapter $user) + public static function load_with_user(Application $app, API_OAuth2_Application $application, User_Adapter $user) { $sql = 'SELECT api_account_id FROM api_accounts WHERE usr_id = :usr_id AND application_id = :application_id'; @@ -276,7 +278,7 @@ class API_OAuth2_Account ":application_id" => $application->get_id() ); - $stmt = $appbox->get_connection()->prepare($sql); + $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); @@ -285,6 +287,6 @@ class API_OAuth2_Account throw new Exception_NotFound(); } - return new self($appbox, $row['api_account_id']); + return new self($app, $row['api_account_id']); } } diff --git a/lib/classes/API/OAuth2/Adapter.class.php b/lib/classes/API/OAuth2/Adapter.class.php index 9f190e25bf..8f122c3c02 100644 --- a/lib/classes/API/OAuth2/Adapter.class.php +++ b/lib/classes/API/OAuth2/Adapter.class.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +use Alchemy\Phrasea\Application; + /** * * @@ -35,9 +37,9 @@ class API_OAuth2_Adapter extends OAuth2 /** * - * @var appbox + * @var Application */ - protected $appbox; + protected $app; /** * request parameter @@ -83,14 +85,14 @@ class API_OAuth2_Adapter extends OAuth2 /** * - * @param appbox $appbox + * @param Application $app * @return API_OAuth2_Adapter */ - public function __construct(appbox $appbox) + public function __construct(Application $app) { parent::__construct(); $this->params = array(); - $this->appbox = $appbox; + $this->app = $app; return $this; } @@ -179,7 +181,7 @@ class API_OAuth2_Adapter extends OAuth2 protected function checkClientCredentials($client_id, $client_secret = NULL) { try { - $application = API_OAuth2_Application::load_from_client_id($this->appbox, $client_id); + $application = API_OAuth2_Application::load_from_client_id($this->app, $client_id); if ($client_secret === NULL) { return true; @@ -202,7 +204,7 @@ class API_OAuth2_Adapter extends OAuth2 */ protected function getRedirectUri($client_id) { - $application = API_OAuth2_Application::load_from_client_id($this->appbox, $client_id); + $application = API_OAuth2_Application::load_from_client_id($this->app, $client_id); return $application->get_redirect_uri(); } @@ -219,7 +221,7 @@ class API_OAuth2_Adapter extends OAuth2 $result = null; try { - $token = API_OAuth2_Token::load_by_oauth_token($this->appbox, $oauth_token); + $token = API_OAuth2_Token::load_by_oauth_token($this->app, $oauth_token); $result = array( 'scope' => $token->get_scope() @@ -251,8 +253,8 @@ class API_OAuth2_Adapter extends OAuth2 */ protected function setAccessToken($oauth_token, $account_id, $expires, $scope = NULL) { - $account = new API_OAuth2_Account($this->appbox, $account_id); - $token = API_OAuth2_Token::create($this->appbox, $account, $scope); + $account = new API_OAuth2_Account($this->app, $account_id); + $token = API_OAuth2_Token::create($this->app, $account, $scope); $token->set_value($oauth_token)->set_expires($expires); return $this; @@ -292,7 +294,7 @@ class API_OAuth2_Adapter extends OAuth2 protected function getAuthCode($code) { try { - $code = new API_OAuth2_AuthCode($this->appbox, $code); + $code = new API_OAuth2_AuthCode($this->app, $code); return array( 'redirect_uri' => $code->get_redirect_uri() @@ -320,8 +322,8 @@ class API_OAuth2_Adapter extends OAuth2 */ protected function setAuthCode($code, $account_id, $redirect_uri, $expires, $scope = NULL) { - $account = new API_OAuth2_Account($this->appbox, $account_id); - $code = API_OAuth2_AuthCode::create($this->appbox, $account, $code, $expires); + $account = new API_OAuth2_Account($this->app, $account_id); + $code = API_OAuth2_AuthCode::create($this->app, $account, $code, $expires); $code->set_redirect_uri($redirect_uri)->set_scope($scope); return $this; @@ -332,8 +334,8 @@ class API_OAuth2_Adapter extends OAuth2 */ protected function setRefreshToken($refresh_token, $account_id, $expires, $scope = NULL) { - $account = new API_OAuth2_Account($this->appbox, $account_id); - API_OAuth2_RefreshToken::create($this->appbox, $account, $expires, $refresh_token, $scope); + $account = new API_OAuth2_Account($this->app, $account_id); + API_OAuth2_RefreshToken::create($this->app, $account, $expires, $refresh_token, $scope); return $this; } @@ -344,7 +346,7 @@ class API_OAuth2_Adapter extends OAuth2 protected function getRefreshToken($refresh_token) { try { - $token = new API_OAuth2_RefreshToken($this->appbox, $refresh_token); + $token = new API_OAuth2_RefreshToken($this->app, $refresh_token); return array( 'token' => $token->get_value() @@ -363,7 +365,7 @@ class API_OAuth2_Adapter extends OAuth2 */ protected function unsetRefreshToken($refresh_token) { - $token = new API_OAuth2_RefreshToken($this->appbox, $refresh_token); + $token = new API_OAuth2_RefreshToken($this->app, $refresh_token); $token->delete(); return $this; @@ -511,8 +513,8 @@ class API_OAuth2_Adapter extends OAuth2 throw new logicalException("Client property must be set before update an account"); try { - $user = User_Adapter::getInstance($usr_id, $this->appbox); - $account = API_OAuth2_Account::load_with_user($this->appbox, $this->client, $user); + $user = User_Adapter::getInstance($usr_id, $this->app); + $account = API_OAuth2_Account::load_with_user($this->app, $this->client, $user); } catch (Exception $e) { $account = $this->createAccount($usr_id); } @@ -527,9 +529,9 @@ class API_OAuth2_Adapter extends OAuth2 */ private function createAccount($usr_id) { - $user = User_Adapter::getInstance($usr_id, $this->appbox); + $user = User_Adapter::getInstance($usr_id, $this->app); - return API_OAuth2_Account::create($this->appbox, $user, $this->client); + return API_OAuth2_Account::create($this->app, $user, $this->client); } /** @@ -576,7 +578,7 @@ class API_OAuth2_Adapter extends OAuth2 public function remember_this_ses_id($ses_id) { try { - $token = API_OAuth2_Token::load_by_oauth_token($this->appbox, $this->token); + $token = API_OAuth2_Token::load_by_oauth_token($this->app, $this->token); $token->set_session_id($ses_id); return true; @@ -702,7 +704,7 @@ class API_OAuth2_Adapter extends OAuth2 $this->errorJsonResponse(OAUTH2_HTTP_BAD_REQUEST, OAUTH2_ERROR_EXPIRED_TOKEN); break; case OAUTH2_GRANT_TYPE_USER_CREDENTIALS: - $application = API_OAuth2_Application::load_from_client_id($this->appbox, $client[0]); + $application = API_OAuth2_Application::load_from_client_id($this->app, $client[0]); if ( ! $application->is_password_granted()) { $this->errorJsonResponse(OAUTH2_HTTP_BAD_REQUEST, OAUTH2_ERROR_UNSUPPORTED_GRANT_TYPE, 'Password grant type is not enable for your client'); @@ -792,15 +794,13 @@ class API_OAuth2_Adapter extends OAuth2 protected function checkUserCredentials($client_id, $username, $password) { try { - $appbox = appbox::get_instance(\bootstrap::getCore()); + $application = API_OAuth2_Application::load_from_client_id($this->app, $client_id); - $application = API_OAuth2_Application::load_from_client_id($appbox, $client_id); - - $auth = new \Session_Authentication_Native($appbox, $username, $password); + $auth = new \Session_Authentication_Native($this->app, $username, $password); $auth->challenge_password(); - $account = API_OAuth2_Account::load_with_user($appbox, $application, $auth->get_user()); + $account = API_OAuth2_Account::load_with_user($this->app, $application, $auth->get_user()); return array( 'redirect_uri' => $application->get_redirect_uri() diff --git a/lib/classes/API/OAuth2/Application.class.php b/lib/classes/API/OAuth2/Application.class.php index b756786293..ed02c8c6fd 100644 --- a/lib/classes/API/OAuth2/Application.class.php +++ b/lib/classes/API/OAuth2/Application.class.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +use Alchemy\Phrasea\Application; + /** * * @package OAuth2 Connector @@ -36,9 +38,9 @@ class API_OAuth2_Application /** * - * @var appbox + * @var Application */ - protected $appbox; + protected $app; /** * @@ -126,13 +128,13 @@ class API_OAuth2_Application /** * - * @param appbox $appbox + * @param Application $app * @param int $application_id * @return API_OAuth2_Application */ - public function __construct(appbox &$appbox, $application_id) + public function __construct(Application $app, $application_id) { - $this->appbox = $appbox; + $this->app = $app; $this->id = (int) $application_id; $sql = ' @@ -143,7 +145,7 @@ class API_OAuth2_Application FROM api_applications WHERE application_id = :application_id'; - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array(':application_id' => $this->id)); if (0 === $stmt->rowCount()) { @@ -152,7 +154,7 @@ class API_OAuth2_Application $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); - $this->creator = ! $row['creator'] ? null : User_Adapter::getInstance($row['creator'], $this->appbox); + $this->creator = ! $row['creator'] ? null : User_Adapter::getInstance($row['creator'], $this->app); $this->type = $row['type']; $this->name = $row['name']; $this->description = $row['description']; @@ -228,7 +230,7 @@ class API_OAuth2_Application , ':application_id' => $this->id ); - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); @@ -261,7 +263,7 @@ class API_OAuth2_Application , ':application_id' => $this->id ); - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); @@ -295,7 +297,7 @@ class API_OAuth2_Application , ':application_id' => $this->id ); - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); @@ -329,7 +331,7 @@ class API_OAuth2_Application , ':application_id' => $this->id ); - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); @@ -363,7 +365,7 @@ class API_OAuth2_Application , ':application_id' => $this->id ); - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); @@ -397,7 +399,7 @@ class API_OAuth2_Application , ':application_id' => $this->id ); - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); @@ -449,7 +451,7 @@ class API_OAuth2_Application , ':application_id' => $this->id ); - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); @@ -483,7 +485,7 @@ class API_OAuth2_Application , ':application_id' => $this->id ); - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); @@ -516,7 +518,7 @@ class API_OAuth2_Application , ':application_id' => $this->id ); - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); @@ -538,7 +540,7 @@ class API_OAuth2_Application , ':id' => $this->id ); - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); @@ -546,7 +548,7 @@ class API_OAuth2_Application if ( ! $row) throw new Exception_NotFound(); - return new API_OAuth2_Account($this->appbox, $row['api_account_id']); + return new API_OAuth2_Account($this->app, $row['api_account_id']); } /** @@ -562,7 +564,7 @@ class API_OAuth2_Application $sql = 'DELETE FROM api_applications WHERE application_id = :application_id'; - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array(':application_id' => $this->get_id())); $stmt->closeCursor(); @@ -578,7 +580,7 @@ class API_OAuth2_Application $sql = 'SELECT api_account_id FROM api_accounts WHERE application_id = :application_id'; - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array(':application_id' => $this->get_id())); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt->closeCursor(); @@ -586,7 +588,7 @@ class API_OAuth2_Application $accounts = array(); foreach ($rs as $row) { - $accounts[] = new API_OAuth2_Account($this->appbox, $row['api_account_id']); + $accounts[] = new API_OAuth2_Account($this->app, $row['api_account_id']); } return $accounts; @@ -594,12 +596,12 @@ class API_OAuth2_Application /** * - * @param appbox $appbox + * @param Application $app * @param User_Adapter $user * @param type $name * @return API_OAuth2_Application */ - public static function create(appbox &$appbox, User_Adapter $user = null, $name) + public static function create(Application $app, User_Adapter $user = null, $name) { $sql = ' INSERT INTO api_applications ( @@ -625,16 +627,16 @@ class API_OAuth2_Application ':grant_password' => 0 ); - $stmt = $appbox->get_connection()->prepare($sql); + $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); - $application_id = $appbox->get_connection()->lastInsertId(); + $application_id = $app['phraseanet.appbox']->get_connection()->lastInsertId(); - $application = new self($appbox, $application_id); + $application = new self($app, $application_id); if ($user) { - API_OAuth2_Account::create($appbox, $user, $application); + API_OAuth2_Account::create($app, $user, $application); } return $application; @@ -642,16 +644,16 @@ class API_OAuth2_Application /** * - * @param appbox $appbox + * @param Application $app * @param type $client_id * @return API_OAuth2_Application */ - public static function load_from_client_id(appbox &$appbox, $client_id) + public static function load_from_client_id(Application $app, $client_id) { $sql = 'SELECT application_id FROM api_applications WHERE client_id = :client_id'; - $stmt = $appbox->get_connection()->prepare($sql); + $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array(':client_id' => $client_id)); $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); @@ -659,7 +661,7 @@ class API_OAuth2_Application if ( ! $row) throw new Exception_NotFound(); - return new self($appbox, $row['application_id']); + return new self($app, $row['application_id']); } /** @@ -668,20 +670,20 @@ class API_OAuth2_Application * @param User_Adapter $user * @return array */ - public static function load_dev_app_by_user(appbox &$appbox, User_Adapter $user) + public static function load_dev_app_by_user(Application $app, User_Adapter $user) { $sql = 'SELECT a.application_id FROM api_applications a, api_accounts b WHERE a.creator = :usr_id AND a.application_id = b.application_id'; - $stmt = $appbox->get_connection()->prepare($sql); + $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array(':usr_id' => $user->get_id())); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt->closeCursor(); $apps = array(); foreach ($rs as $row) { - $apps[] = new API_OAuth2_Application($appbox, $row['application_id']); + $apps[] = new API_OAuth2_Application($app, $row['application_id']); } return $apps; @@ -693,26 +695,26 @@ class API_OAuth2_Application * @param user_adapter $user * @return API_OAuth2_Application */ - public static function load_app_by_user(appbox $appbox, user_adapter $user) + public static function load_app_by_user(Application $app, user_adapter $user) { $sql = 'SELECT a.application_id FROM api_accounts a, api_applications c WHERE usr_id = :usr_id AND c.application_id = a.application_id'; - $stmt = $appbox->get_connection()->prepare($sql); + $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array(':usr_id' => $user->get_id())); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt->closeCursor(); $apps = array(); foreach ($rs as $row) { - $apps[] = new API_OAuth2_Application($appbox, $row['application_id']); + $apps[] = new API_OAuth2_Application($app, $row['application_id']); } return $apps; } - public static function load_authorized_app_by_user(appbox $appbox, user_adapter $user) + public static function load_authorized_app_by_user(Application $app, user_adapter $user) { $sql = ' SELECT a.application_id @@ -720,14 +722,14 @@ class API_OAuth2_Application WHERE usr_id = :usr_id AND c.application_id = a.application_id AND revoked = 0'; - $stmt = $appbox->get_connection()->prepare($sql); + $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array(':usr_id' => $user->get_id())); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt->closeCursor(); $apps = array(); foreach ($rs as $row) { - $apps[] = new API_OAuth2_Application($appbox, $row['application_id']); + $apps[] = new API_OAuth2_Application($app, $row['application_id']); } return $apps; diff --git a/lib/classes/API/OAuth2/AuthCode.class.php b/lib/classes/API/OAuth2/AuthCode.class.php index 673c1cfbba..9e58781286 100644 --- a/lib/classes/API/OAuth2/AuthCode.class.php +++ b/lib/classes/API/OAuth2/AuthCode.class.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +use Alchemy\Phrasea\Application; + /** * * @package OAuth2 Connector @@ -21,7 +23,7 @@ */ class API_OAuth2_AuthCode { - protected $appbox; + protected $app; protected $code; protected $account; protected $account_id; @@ -29,14 +31,14 @@ class API_OAuth2_AuthCode protected $expires; protected $scope; - public function __construct(appbox &$appbox, $code) + public function __construct(Application $app, $code) { - $this->appbox = $appbox; + $this->app = $app; $this->code = $code; $sql = 'SELECT code, api_account_id, redirect_uri, UNIX_TIMESTAMP(expires) AS expires, scope FROM api_oauth_codes WHERE code = :code'; - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array(':code' => $this->code)); $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); @@ -64,7 +66,7 @@ class API_OAuth2_AuthCode public function get_account() { if ( ! $this->account) - $this->account = new API_OAuth2_Account($this->appbox, $this->account_id); + $this->account = new API_OAuth2_Account($this->app['phraseanet.appbox'], $this->account_id); return $this->account; } @@ -81,7 +83,7 @@ class API_OAuth2_AuthCode $params = array(':redirect_uri' => $redirect_uri, ':code' => $this->code); - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); @@ -111,7 +113,7 @@ class API_OAuth2_AuthCode $params = array(':scope' => $scope, ':code' => $this->code); - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); @@ -124,7 +126,7 @@ class API_OAuth2_AuthCode { $sql = 'DELETE FROM api_oauth_codes WHERE code = :code'; - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array(':code' => $this->code)); $stmt->closeCursor(); @@ -133,16 +135,16 @@ class API_OAuth2_AuthCode /** * - * @param appbox $appbox + * @param Application $app * @param API_OAuth2_Account $account * @return array */ - public static function load_codes_by_account(appbox &$appbox, API_OAuth2_Account $account) + public static function load_codes_by_account(Application $app, API_OAuth2_Account $account) { $sql = 'SELECT code FROM api_oauth_codes WHERE api_account_id = :account_id'; - $stmt = $appbox->get_connection()->prepare($sql); + $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); $params = array(":account_id" => $account->get_id()); $stmt->execute($params); @@ -152,7 +154,7 @@ class API_OAuth2_AuthCode $codes = array(); foreach ($rs as $row) { - $codes[] = new API_OAuth2_AuthCode($appbox, $row['code']); + $codes[] = new API_OAuth2_AuthCode($app, $row['code']); } return $codes; @@ -160,19 +162,19 @@ class API_OAuth2_AuthCode /** * - * @param appbox $appbox + * @param Application $app * @param API_OAuth2_Account $account * @param type $code * @param int $expires * @return API_OAuth2_AuthCode */ - public static function create(appbox &$appbox, API_OAuth2_Account $account, $code, $expires) + public static function create(Application $app, API_OAuth2_Account $account, $code, $expires) { $sql = 'INSERT INTO api_oauth_codes (code, api_account_id, expires) VALUES (:code, :account_id, FROM_UNIXTIME(:expires))'; - $stmt = $appbox->get_connection()->prepare($sql); + $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); $params = array( ":code" => $code, @@ -182,6 +184,6 @@ class API_OAuth2_AuthCode $stmt->execute($params); $stmt->closeCursor(); - return new self($appbox, $code); + return new self($app, $code); } } diff --git a/lib/classes/API/OAuth2/RefreshToken.class.php b/lib/classes/API/OAuth2/RefreshToken.class.php index ddc368035b..a13a57bd0e 100644 --- a/lib/classes/API/OAuth2/RefreshToken.class.php +++ b/lib/classes/API/OAuth2/RefreshToken.class.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +use Alchemy\Phrasea\Application; + /** * * @package OAuth2 Connector @@ -21,22 +23,22 @@ */ class API_OAuth2_RefreshToken { - protected $appbox; + protected $app; protected $token; protected $account_id; protected $account; protected $expires; protected $scope; - public function __construct(appbox &$appbox, $token) + public function __construct(Application $app, $token) { - $this->appbox = $appbox; + $this->app = $app; $this->token = $token; $sql = 'SELECT api_account_id, UNIX_TIMESTAMP(expires) AS expires, scope FROM api_oauth_refresh_tokens WHERE refresh_token = :token'; - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array(':token' => $this->token)); $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); @@ -59,8 +61,9 @@ class API_OAuth2_RefreshToken */ public function get_account() { - if ( ! $this->account) - $this->account = new API_OAuth2_Account($this->appbox, $this->account_id); + if ( ! $this->account) { + $this->account = new API_OAuth2_Account($this->app, $this->account_id); + } return $this->account; } @@ -84,7 +87,7 @@ class API_OAuth2_RefreshToken $sql = 'DELETE FROM api_oauth_refresh_tokens WHERE refresh_token = :refresh_token'; - $stmt = $this->appbox->get_connection()->prepare($sql); + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array(":refresh_token" => $this->token)); $stmt->closeCursor(); @@ -93,16 +96,16 @@ class API_OAuth2_RefreshToken /** * - * @param appbox $appbox + * @param Application $app * @param API_OAuth2_Account $account * @return array */ - public static function load_by_account(appbox &$appbox, API_OAuth2_Account $account) + public static function load_by_account(Application $app, API_OAuth2_Account $account) { $sql = 'SELECT refresh_token FROM api_oauth_refresh_tokens WHERE api_account_id = :account_id'; - $stmt = $appbox->get_connection()->prepare($sql); + $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array(':account_id' => $account->get_id())); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt->closeCursor(); @@ -110,7 +113,7 @@ class API_OAuth2_RefreshToken $tokens = array(); foreach ($rs as $row) { - $tokens[] = new API_OAuth2_RefreshToken($appbox, $row['refresh_token']); + $tokens[] = new API_OAuth2_RefreshToken($app, $row['refresh_token']); } return $tokens; @@ -118,20 +121,20 @@ class API_OAuth2_RefreshToken /** * - * @param appbox $appbox + * @param Application $app * @param API_OAuth2_Account $account * @param int $expires * @param type $refresh_token * @param type $scope * @return API_OAuth2_RefreshToken */ - public static function create(appbox &$appbox, API_OAuth2_Account $account, $expires, $refresh_token, $scope) + public static function create(Application $app, API_OAuth2_Account $account, $expires, $refresh_token, $scope) { $sql = 'INSERT INTO api_oauth_refresh_tokens (refresh_token, api_account_id, expires, scope) VALUES (:refresh_token, :account_id, :expires, :scope)'; - $stmt = $appbox->get_connection()->prepare($sql); + $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); $params = array( ":refresh_token" => $refresh_token, ":account_id" => $account->get_id(), @@ -141,6 +144,6 @@ class API_OAuth2_RefreshToken $stmt->execute($params); $stmt->closeCursor(); - return new self($appbox, $refresh_token); + return new self($app, $refresh_token); } } diff --git a/lib/classes/API/OAuth2/Token.class.php b/lib/classes/API/OAuth2/Token.class.php index 142ca62c3f..6dc3e0856b 100644 --- a/lib/classes/API/OAuth2/Token.class.php +++ b/lib/classes/API/OAuth2/Token.class.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +use Alchemy\Phrasea\Application; + /** * * @package OAuth2 Connector @@ -270,14 +272,14 @@ class API_OAuth2_Token * @param type $oauth_token * @return API_OAuth2_Token */ - public static function load_by_oauth_token(appbox &$appbox, $oauth_token) + public static function load_by_oauth_token(Application $app, $oauth_token) { $sql = 'SELECT a.api_account_id FROM api_oauth_tokens a, api_accounts b WHERE a.oauth_token = :oauth_token AND a.api_account_id = b.api_account_id'; - $stmt = $appbox->get_connection()->prepare($sql); + $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); $params = array(":oauth_token" => $oauth_token); $stmt->execute($params); $row = $stmt->fetch(PDO::FETCH_ASSOC); @@ -286,9 +288,9 @@ class API_OAuth2_Token if ( ! $row) throw new Exception_NotFound(); - $account = new API_OAuth2_Account($appbox, $row['api_account_id']); + $account = new API_OAuth2_Account($app, $row['api_account_id']); - return new self($appbox, $account); + return new self($app['phraseanet.appbox'], $account); } /**