芝麻web文件管理V1.00
编辑当前文件:/home/mgatv524/public_html/mctv/lib/Entity/Application.php
. */ namespace Xibo\Entity; use League\OAuth2\Server\Entities\ClientEntityInterface; use Xibo\Factory\ApplicationRedirectUriFactory; use Xibo\Factory\ApplicationScopeFactory; use Xibo\Helper\Random; use Xibo\OAuth\ScopeEntity; use Xibo\Service\LogServiceInterface; use Xibo\Storage\StorageServiceInterface; /** * Class Application * @package Xibo\Entity * * @SWG\Definition */ class Application implements \JsonSerializable, ClientEntityInterface { use EntityTrait; /** * @SWG\Property( * description="Application Key" * ) * @var string */ public $key; /** * @SWG\Property( * description="Private Secret Key" * ) * @var string */ public $secret; /** * @SWG\Property( * description="Application Name" * ) * @var string */ public $name; /** * @SWG\Property( * description="Application Owner" * ) * @var string */ public $owner; /** * @SWG\Property( * description="Application Session Expiry" * ) * @var int */ public $expires; /** * @SWG\Property( * description="The Owner of this Application" * ) * @var int */ public $userId; /** * @SWG\Property(description="Flag indicating whether to allow the authorizationCode Grant Type") * @var int */ public $authCode = 0; /** * @SWG\Property(description="Flag indicating whether to allow the clientCredentials Grant Type") * @var int */ public $clientCredentials = 0; /** * @SWG\Property(description="Flag indicating whether this Application will be confidential or not (can it keep a secret?)") * @var int */ public $isConfidential = 1; /** * @var ApplicationRedirectUri[] */ public $redirectUris = []; /** * @var ApplicationScope[] */ public $scopes = []; /** @var ApplicationRedirectUriFactory */ private $applicationRedirectUriFactory; /** @var ApplicationScopeFactory */ private $applicationScopeFactory; /** * Entity constructor. * @param StorageServiceInterface $store * @param LogServiceInterface $log * @param ApplicationRedirectUriFactory $applicationRedirectUriFactory * @param ApplicationScopeFactory $applicationScopeFactory */ public function __construct($store, $log, $applicationRedirectUriFactory, $applicationScopeFactory) { $this->setCommonDependencies($store, $log); $this->applicationRedirectUriFactory = $applicationRedirectUriFactory; $this->applicationScopeFactory = $applicationScopeFactory; } /** * @param ApplicationRedirectUri $redirectUri */ public function assignRedirectUri($redirectUri) { $this->load(); // Assert client id $redirectUri->clientId = $this->key; if (!in_array($redirectUri, $this->redirectUris)) { $this->redirectUris[] = $redirectUri; } } /** * Unassign RedirectUri * @param ApplicationRedirectUri $redirectUri */ public function unassignRedirectUri($redirectUri) { $this->load(); $this->redirectUris = array_udiff($this->redirectUris, [$redirectUri], function($a, $b) { /** * @var ApplicationRedirectUri $a * @var ApplicationRedirectUri $b */ return $a->getId() - $b->getId(); }); } /** * @param ApplicationScope $scope */ public function assignScope($scope) { $this->load(); if (!in_array($scope, $this->scopes)) { $this->scopes[] = $scope; } } /** * @param ApplicationScope $scope */ public function unassignScope($scope) { $this->load(); $this->scopes = array_udiff($this->scopes, [$scope], function($a, $b) { /** * @var ApplicationScope $a * @var ApplicationScope $b */ return $a->getId() !== $b->getId(); }); } /** * Get the hash for password verify * @return string */ public function getHash() { return password_hash($this->secret, PASSWORD_DEFAULT); } /** * Load * @return $this */ public function load() { if ($this->loaded || empty($this->key)) { return $this; } // Redirects $this->redirectUris = $this->applicationRedirectUriFactory->getByClientId($this->key); // Get scopes $this->scopes = $this->applicationScopeFactory->getByClientId($this->key); $this->loaded = true; return $this; } /** * @return $this */ public function save() { if ($this->key == null || $this->key == '') { // Make a new secret. $this->resetSecret(); // Add $this->add(); } else { // Edit $this->edit(); } $this->getLog()->debug('Saving redirect uris: ' . json_encode($this->redirectUris)); foreach ($this->redirectUris as $redirectUri) { $redirectUri->save(); } $this->manageScopeAssignments(); return $this; } /** * Delete */ public function delete() { $this->load(); foreach ($this->redirectUris as $redirectUri) { $redirectUri->delete(); } // Clear out everything owned by this client $this->getStore()->update('DELETE FROM `oauth_client_scopes` WHERE `clientId` = :id', ['id' => $this->key]); $this->getStore()->update('DELETE FROM `oauth_clients` WHERE `id` = :id', ['id' => $this->key]); } /** * Reset Secret */ public function resetSecret() { $this->secret = Random::generateString(254); } private function add() { // Make an ID $this->key = Random::generateString(40); // Simple Insert for now $this->getStore()->insert(' INSERT INTO `oauth_clients` (`id`, `secret`, `name`, `userId`, `authCode`, `clientCredentials`, `isConfidential`) VALUES (:id, :secret, :name, :userId, :authCode, :clientCredentials, :isConfidential) ', [ 'id' => $this->key, 'secret' => $this->secret, 'name' => $this->name, 'userId' => $this->userId, 'authCode' => $this->authCode, 'clientCredentials' => $this->clientCredentials, 'isConfidential' => $this->isConfidential ]); } private function edit() { $this->getStore()->update(' UPDATE `oauth_clients` SET `id` = :id, `secret` = :secret, `name` = :name, `userId` = :userId, `authCode` = :authCode, `clientCredentials` = :clientCredentials, `isConfidential` = :isConfidential WHERE `id` = :id ', [ 'id' => $this->key, 'secret' => $this->secret, 'name' => $this->name, 'userId' => $this->userId, 'authCode' => $this->authCode, 'clientCredentials' => $this->clientCredentials, 'isConfidential' => $this->isConfidential ]); } /** * Compare the original assignments with the current assignments and delete any that are missing, add any new ones */ private function manageScopeAssignments() { $i = 0; $params = ['clientId' => $this->key]; $unassignIn = ''; foreach ($this->scopes as $link) { $this->getStore()->update(' INSERT INTO `oauth_client_scopes` (clientId, scopeId) VALUES (:clientId, :scopeId) ON DUPLICATE KEY UPDATE scopeId = scopeId', [ 'clientId' => $this->key, 'scopeId' => $link->id ]); $i++; $unassignIn .= ',:scopeId' . $i; $params['scopeId' . $i] = $link->id; } // Unlink any NOT in the collection $sql = 'DELETE FROM `oauth_client_scopes` WHERE clientId = :clientId AND scopeId NOT IN (\'0\'' . $unassignIn . ')'; $this->getStore()->update($sql, $params); } /** @inheritDoc */ public function getIdentifier() { return $this->key; } /** @inheritDoc */ public function getName() { return $this->name; } /** @inheritDoc */ public function getRedirectUri() { $count = count($this->redirectUris); if ($count <= 0) { return null; } else if (count($this->redirectUris) == 1) { return $this->redirectUris[0]->redirectUri; } else { return array_map(function($el) { return $el->redirectUri; }, $this->redirectUris); } } /** * @return \League\OAuth2\Server\Entities\ScopeEntityInterface[] */ public function getScopes() { $scopes = []; foreach ($this->scopes as $applicationScope) { $scope = new ScopeEntity(); $scope->setIdentifier($applicationScope->getId()); $scopes[] = $scope; } return $scopes; } /** @inheritDoc */ public function isConfidential() { return $this->isConfidential === 1; } }