mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-08 18:44:30 +00:00

Conflicts: lib/Alchemy/Phrasea/Command/Developer/JavascriptBuilder.php lib/Alchemy/Phrasea/Controller/Prod/Basket.php lib/Alchemy/Phrasea/Core/Provider/TaskManagerServiceProvider.php lib/classes/Exception/Feed/ItemNotFound.php lib/classes/Exception/Feed/PublisherNotFound.php lib/classes/Feed/Abstract.php lib/classes/Feed/Adapter.php lib/classes/Feed/Aggregate.php lib/classes/Feed/Collection.php lib/classes/Feed/CollectionInterface.php lib/classes/Feed/Entry/Adapter.php lib/classes/Feed/Entry/Collection.php lib/classes/Feed/Entry/Interface.php lib/classes/Feed/Entry/Item.php lib/classes/Feed/Entry/ItemInterface.php lib/classes/Feed/Interface.php lib/classes/Feed/Link.php lib/classes/Feed/LinkInterface.php lib/classes/Feed/Publisher/Adapter.php lib/classes/Feed/Publisher/Interface.php lib/classes/Feed/Token.php lib/classes/Feed/TokenAggregate.php lib/classes/Feed/XML/Abstract.php lib/classes/Feed/XML/Atom.php lib/classes/Feed/XML/Cooliris.php lib/classes/Feed/XML/Interface.php lib/classes/Feed/XML/RSS.php lib/classes/Feed/XML/RSS/ImageInterface.php lib/classes/http/request.php lib/classes/module/console/schedulerStart.php lib/classes/module/console/schedulerState.php lib/classes/module/console/schedulerStop.php lib/classes/module/console/taskState.php lib/classes/module/console/tasklist.php lib/classes/module/console/taskrun.php lib/classes/registry.php lib/classes/registryInterface.php lib/classes/set/order.php lib/classes/system/url.php lib/classes/task/Scheduler.php lib/classes/task/appboxAbstract.php lib/classes/task/databoxAbstract.php lib/classes/task/manager.php lib/classes/task/period/RecordMover.php lib/classes/task/period/apibridge.php lib/classes/task/period/archive.php lib/classes/task/period/cindexer.php lib/classes/task/period/emptyColl.php lib/classes/task/period/ftp.php lib/classes/task/period/ftpPull.php lib/classes/task/period/subdef.php lib/classes/task/period/test.php lib/classes/task/period/writemeta.php lib/conf.d/PhraseaFixture/AbstractWZ.php lib/conf.d/PhraseaFixture/Basket/LoadFiveBaskets.php lib/conf.d/PhraseaFixture/Basket/LoadOneBasket.php lib/conf.d/PhraseaFixture/Basket/LoadOneBasketEnv.php lib/conf.d/PhraseaFixture/Lazaret/LoadOneFile.php lib/conf.d/PhraseaFixture/Story/LoadOneStory.php lib/conf.d/PhraseaFixture/UsrLists/ListAbstract.php lib/conf.d/PhraseaFixture/UsrLists/UsrList.php lib/conf.d/PhraseaFixture/UsrLists/UsrListEntry.php lib/conf.d/PhraseaFixture/UsrLists/UsrListOwner.php lib/conf.d/PhraseaFixture/ValidationParticipant/LoadOneParticipant.php lib/conf.d/PhraseaFixture/ValidationParticipant/LoadParticipantWithSession.php lib/conf.d/PhraseaFixture/ValidationSession/LoadOneValidationSession.php
263 lines
6.0 KiB
PHP
263 lines
6.0 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Phraseanet
|
|
*
|
|
* (c) 2005-2014 Alchemy
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
use Alchemy\Phrasea\Application;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class API_OAuth2_Account
|
|
{
|
|
/**
|
|
*
|
|
* @var Application
|
|
*/
|
|
protected $app;
|
|
|
|
/**
|
|
*
|
|
* @var int
|
|
*/
|
|
protected $id;
|
|
|
|
/**
|
|
*
|
|
* @var User_Adapter
|
|
*/
|
|
protected $user;
|
|
|
|
/**
|
|
*
|
|
* @var API_OAuth2_Application
|
|
*/
|
|
protected $application;
|
|
|
|
/**
|
|
*
|
|
* @var int
|
|
*/
|
|
protected $application_id;
|
|
|
|
/**
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $api_version;
|
|
|
|
/**
|
|
*
|
|
* @var boolean
|
|
*/
|
|
protected $revoked;
|
|
|
|
/**
|
|
*
|
|
* @var DateTime
|
|
*/
|
|
protected $created_on;
|
|
|
|
/**
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $token;
|
|
|
|
public function __construct(Application $app, $account_id)
|
|
{
|
|
$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->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
|
$stmt->execute([':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'], $app);
|
|
|
|
$this->api_version = $row['api_version'];
|
|
$this->revoked = ! ! $row['revoked'];
|
|
$this->created_on = new DateTime($row['created']);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return int
|
|
*/
|
|
public function get_id()
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return User_Adapter
|
|
*/
|
|
public function get_user()
|
|
{
|
|
return $this->user;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return string
|
|
*/
|
|
public function get_api_version()
|
|
{
|
|
return $this->api_version;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function is_revoked()
|
|
{
|
|
return $this->revoked;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param boolean $boolean
|
|
* @return API_OAuth2_Account
|
|
*/
|
|
public function set_revoked($boolean)
|
|
{
|
|
$this->revoked = ! ! $boolean;
|
|
|
|
$sql = 'UPDATE api_accounts SET revoked = :revoked
|
|
WHERE api_account_id = :account_id';
|
|
|
|
$params = [
|
|
':revoked' => ($boolean ? '1' : '0')
|
|
, 'account_id' => $this->id
|
|
];
|
|
|
|
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
|
$stmt->execute($params);
|
|
$stmt->closeCursor();
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return DateTime
|
|
*/
|
|
public function get_created_on()
|
|
{
|
|
return $this->created_on;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return API_OAuth2_Token
|
|
*/
|
|
public function get_token()
|
|
{
|
|
if (! $this->token) {
|
|
try {
|
|
$this->token = new API_OAuth2_Token($this->app['phraseanet.appbox'], $this);
|
|
} catch (NotFoundHttpException $e) {
|
|
$this->token = API_OAuth2_Token::create($this->app['phraseanet.appbox'], $this);
|
|
}
|
|
}
|
|
|
|
return $this->token;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return API_OAuth2_Application
|
|
*/
|
|
public function get_application()
|
|
{
|
|
if ( ! $this->application)
|
|
$this->application = new API_OAuth2_Application($this->app, $this->application_id);
|
|
|
|
return $this->application;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return void
|
|
*/
|
|
public function delete()
|
|
{
|
|
$this->get_token()->delete();
|
|
|
|
foreach (API_OAuth2_AuthCode::load_codes_by_account($this->app, $this) as $code) {
|
|
$code->delete();
|
|
}
|
|
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->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
|
$stmt->execute(['account_id' => $this->id]);
|
|
$stmt->closeCursor();
|
|
|
|
return;
|
|
}
|
|
|
|
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)
|
|
VALUES (null, :usr_id, :revoked, :api_version, :application_id, :created)';
|
|
|
|
$datetime = new Datetime();
|
|
$params = [
|
|
':usr_id' => $user->get_id()
|
|
, ':application_id' => $application->get_id()
|
|
, ':api_version' => API_OAuth2_Adapter::API_VERSION
|
|
, ':revoked' => 0
|
|
, ':created' => $datetime->format("Y-m-d H:i:s")
|
|
];
|
|
|
|
$stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
|
|
$stmt->execute($params);
|
|
$stmt->closeCursor();
|
|
|
|
$account_id = $app['phraseanet.appbox']->get_connection()->lastInsertId();
|
|
|
|
return new self($app, $account_id);
|
|
}
|
|
|
|
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';
|
|
|
|
$params = [
|
|
":usr_id" => $user->get_id(),
|
|
":application_id" => $application->get_id()
|
|
];
|
|
|
|
$stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
|
|
$stmt->execute($params);
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
$stmt->closeCursor();
|
|
|
|
if (! $row) {
|
|
throw new NotFoundHttpException('Account nof found.');
|
|
}
|
|
|
|
return new self($app, $row['api_account_id']);
|
|
}
|
|
}
|