diff --git a/com_oauthserver/administrator/src/Model/ClientsModel.php b/com_oauthserver/administrator/src/Model/ClientsModel.php index a14f5e9..0c8603b 100644 --- a/com_oauthserver/administrator/src/Model/ClientsModel.php +++ b/com_oauthserver/administrator/src/Model/ClientsModel.php @@ -1,4 +1,11 @@ + * @license MIT; see LICENSE.txt + **/ namespace Webmasterskaya\Component\OauthServer\Administrator\Model; @@ -6,6 +13,9 @@ use Joomla\CMS\Factory; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; use Joomla\CMS\MVC\Model\ListModel; use Joomla\Database\ParameterType; +use Joomla\Database\QueryInterface; + +\defined('_JEXEC') or die; class ClientsModel extends ListModel { @@ -21,8 +31,8 @@ class ClientsModel extends ListModel /** * Constructor. * - * @param array $config An optional associative array of configuration settings. - * @param MVCFactoryInterface|null $factory The factory. + * @param array $config An optional associative array of configuration settings. + * @param MVCFactoryInterface|null $factory The factory. * * @throws \Exception * @@ -31,7 +41,8 @@ class ClientsModel extends ListModel public function __construct($config = [], MVCFactoryInterface $factory = null) { // Add the ordering filtering fields whitelist - if (empty($config['filter_fields'])) { + if (empty($config['filter_fields'])) + { $config['filter_fields'] = [ 'id', 'client.id' ]; @@ -43,8 +54,8 @@ class ClientsModel extends ListModel /** * Method to auto-populate the model state. * - * @param string $ordering An optional ordering field. - * @param string $direction An optional direction (asc|desc). + * @param string $ordering An optional ordering field. + * @param string $direction An optional direction (asc|desc). * * @throws \Exception * @@ -55,7 +66,8 @@ class ClientsModel extends ListModel $app = Factory::getApplication(); // Adjust the context to support modal layouts. - if ($layout = $app->input->get('layout')) { + if ($layout = $app->input->get('layout')) + { $this->context .= '.' . $layout; } @@ -68,7 +80,7 @@ class ClientsModel extends ListModel /** * Method to get a store id based on model configuration state. * - * @param string $id A prefix for the store id. + * @param string $id A prefix for the store id. * * @return string A store id. * @@ -84,13 +96,13 @@ class ClientsModel extends ListModel /** * Method to get a DatabaseQuery object for retrieving the data set from a database. * - * @return \Joomla\Database\QueryInterface A QueryInterface object to retrieve the data set. + * @return QueryInterface A QueryInterface object to retrieve the data set. * * @throws \Exception * * @since 1.0.0 */ - protected function getListQuery(): \Joomla\Database\QueryInterface + protected function getListQuery(): QueryInterface { $db = $this->getDatabase(); @@ -101,16 +113,17 @@ class ClientsModel extends ListModel // Filter by search state $search = $this->getState('filter.search'); - if (!empty($search)) { + if (!empty($search)) + { $query->where('client.name LIKE :search') ->bind(':search', $search, ParameterType::STRING); } // Add the list ordering clause - $ordering = $this->state->get('list.ordering', 'client.id'); + $ordering = $this->state->get('list.ordering', 'client.id'); $direction = $this->state->get('list.direction', 'desc'); $query->order($db->escape($ordering) . ' ' . $db->escape($direction)); return $query; } -} \ No newline at end of file +} diff --git a/com_oauthserver/administrator/src/Model/GetItemByIdentifierTrait.php b/com_oauthserver/administrator/src/Model/GetItemByIdentifierTrait.php index fcddff1..f22abe6 100644 --- a/com_oauthserver/administrator/src/Model/GetItemByIdentifierTrait.php +++ b/com_oauthserver/administrator/src/Model/GetItemByIdentifierTrait.php @@ -1,30 +1,37 @@ + * @license MIT; see LICENSE.txt + **/ namespace Webmasterskaya\Component\OauthServer\Administrator\Model; use Joomla\CMS\Language\Text; use Joomla\CMS\Object\CMSObject; +use Joomla\CMS\Table\Table; use Joomla\Utilities\ArrayHelper; +\defined('_JEXEC') or die; + trait GetItemByIdentifierTrait { - abstract public function getState($property = null, $default = null); - - abstract public function getName(); - - abstract public function getTable($name = '', $prefix = '', $options = []); - public function getItemByIdentifier($identifier = null): object { - $identifier = (!empty($identifier)) ? $identifier : (int)$this->getState($this->getName() . '.identifier'); - /** @var \Joomla\CMS\Table\Table $table */ + $identifier = (!empty($identifier)) ? $identifier : (int) $this->getState($this->getName() . '.identifier'); + /** @var Table $table */ $table = $this->getTable(); - if (!empty($identifier)) { + if (!empty($identifier)) + { $return = $table->load(['identifier' => $identifier]); - if ($return === false) { - if (method_exists($table, 'getError') && $table->getError()) { + if ($return === false) + { + if (method_exists($table, 'getError') && $table->getError()) + { throw new \RuntimeException($table->getError()); } throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_NOT_EXIST')); @@ -32,12 +39,15 @@ trait GetItemByIdentifierTrait } // Convert to the CMSObject before adding other data. - $properties = $table->getProperties(true); + $properties = $table->getProperties(true); $all_properties = $table->getProperties(false); - if (!empty($all_properties['_jsonEncode'])) { - foreach ($all_properties['_jsonEncode'] as $prop) { - if (array_key_exists($prop, $properties) && is_string($properties[$prop])) { + if (!empty($all_properties['_jsonEncode'])) + { + foreach ($all_properties['_jsonEncode'] as $prop) + { + if (array_key_exists($prop, $properties) && is_string($properties[$prop])) + { $properties[$prop] = json_decode($properties[$prop]); } } @@ -45,4 +55,10 @@ trait GetItemByIdentifierTrait return ArrayHelper::toObject($properties, CMSObject::class, true); } -} \ No newline at end of file + + abstract public function getState($property = null, $default = null); + + abstract public function getName(); + + abstract public function getTable($name = '', $prefix = '', $options = []); +} diff --git a/com_oauthserver/administrator/src/Model/RefreshTokenModel.php b/com_oauthserver/administrator/src/Model/RefreshTokenModel.php index 9d5fca8..5b4fc6c 100644 --- a/com_oauthserver/administrator/src/Model/RefreshTokenModel.php +++ b/com_oauthserver/administrator/src/Model/RefreshTokenModel.php @@ -1,10 +1,20 @@ + * @license MIT; see LICENSE.txt + **/ namespace Webmasterskaya\Component\OauthServer\Administrator\Model; use Joomla\CMS\Factory; use Joomla\CMS\Form\Form; use Joomla\CMS\MVC\Model\AdminModel; +use Webmasterskaya\Component\OauthServer\Administrator\Table\RefreshTokenTable; + +\defined('_JEXEC') or die; class RefreshTokenModel extends AdminModel implements RevokedModelInterface { @@ -15,19 +25,26 @@ class RefreshTokenModel extends AdminModel implements RevokedModelInterface { $form = $this->loadForm('com_oauthserver.refresh_token', 'refresh_token', ['control' => 'jform', 'load_data' => $loadData]); - if (empty($form)) { + if (empty($form)) + { return false; } return $form; } + public function getTable($name = 'RefreshToken', $prefix = 'Administrator', $options = []) + { + return parent::getTable($name, $prefix, $options); + } + protected function loadFormData(): mixed { // Check the session for previously entered form data. $data = Factory::getApplication()->getUserState('com_oauthserver.edit.refresh_token.data', []); - if (empty($data)) { + if (empty($data)) + { $data = $this->getItem(); } @@ -37,19 +54,16 @@ class RefreshTokenModel extends AdminModel implements RevokedModelInterface } /** - * @param \Webmasterskaya\Component\OauthServer\Administrator\Table\RefreshTokenTable $table + * @param RefreshTokenTable $table + * * @return void * @since version */ protected function prepareTable($table) { - if ($table->expiry instanceof \DateTime || $table->expiry instanceof \DateTimeImmutable) { + if ($table->expiry instanceof \DateTime || $table->expiry instanceof \DateTimeImmutable) + { $table->expiry = $table->expiry->format($table->getDbo()->getDateFormat()); } } - - public function getTable($name = 'RefreshToken', $prefix = 'Administrator', $options = []) - { - return parent::getTable($name, $prefix, $options); - } -} \ No newline at end of file +} diff --git a/com_oauthserver/administrator/src/Model/RevokedModelInterface.php b/com_oauthserver/administrator/src/Model/RevokedModelInterface.php index 5e77469..171d141 100644 --- a/com_oauthserver/administrator/src/Model/RevokedModelInterface.php +++ b/com_oauthserver/administrator/src/Model/RevokedModelInterface.php @@ -1,8 +1,17 @@ + * @license MIT; see LICENSE.txt + **/ namespace Webmasterskaya\Component\OauthServer\Administrator\Model; +\defined('_JEXEC') or die; + interface RevokedModelInterface { public function revoke(&$identifiers): bool; -} \ No newline at end of file +} diff --git a/com_oauthserver/administrator/src/Model/RevokedModelTrait.php b/com_oauthserver/administrator/src/Model/RevokedModelTrait.php index 8a6ed65..b063d35 100644 --- a/com_oauthserver/administrator/src/Model/RevokedModelTrait.php +++ b/com_oauthserver/administrator/src/Model/RevokedModelTrait.php @@ -1,99 +1,109 @@ + * @license MIT; see LICENSE.txt + **/ namespace Webmasterskaya\Component\OauthServer\Administrator\Model; use Joomla\CMS\Factory; use Joomla\CMS\Plugin\PluginHelper; +use Joomla\CMS\Table\Table; use Joomla\CMS\User\User; +\defined('_JEXEC') or die; + trait RevokedModelTrait { /** * @var string - * @since version + * @since version * @noinspection PhpMissingFieldTypeInspection */ protected $option; /** * @var string - * @since version + * @since version * @noinspection PhpMissingFieldTypeInspection */ protected $name; /** * @var array - * @since version + * @since version * @noinspection PhpMissingFieldTypeInspection */ protected $events_map; /** * @var string - * @since version + * @since version * @noinspection PhpMissingFieldTypeInspection */ protected $event_before_change_state; /** * @var string - * @since version + * @since version * @noinspection PhpMissingFieldTypeInspection */ protected $event_change_state; - abstract public function getTable($name = '', $prefix = '', $options = []); - - abstract protected function getCurrentUser(): User; - - abstract public function setError($error); - - abstract protected function cleanCache($group = null); - public function revoke(&$identifiers): bool { $user = $this->getCurrentUser(); - /** @var \Joomla\CMS\Table\Table $table */ - $table = $this->getTable(); - $identifiers = (array)$identifiers; - $pks = []; + /** @var Table $table */ + $table = $this->getTable(); + $identifiers = (array) $identifiers; + $pks = []; $context = $this->option . '.' . $this->name; // Include the plugins for the change of state event. PluginHelper::importPlugin($this->events_map['change_state']); - foreach ($identifiers as $i => $identifier) { + foreach ($identifiers as $i => $identifier) + { $table->reset(); - if ($table->load(['identifier' => $identifier])) { + if ($table->load(['identifier' => $identifier])) + { $revokedColumnName = $table->getColumnAlias('revoked'); - if (property_exists($table, $revokedColumnName) && $table->get($revokedColumnName, 1) == 0) { + if (property_exists($table, $revokedColumnName) && $table->get($revokedColumnName, 1) == 0) + { unset($identifiers[$i]); - } else { + } + else + { $pks[] = $table->get('id'); } } } // Check if there are items to change - if (!\count($pks)) { + if (!\count($pks)) + { return true; } // Trigger the before change state event. $result = Factory::getApplication()->triggerEvent($this->event_before_change_state, [$context, $pks, 0]); - if (\in_array(false, $result, true)) { + if (\in_array(false, $result, true)) + { $this->setError($table->getError()); return false; } // Attempt to change the state of the records. - if (!$table->revoke($pks, $user->id)) { + if (!$table->revoke($pks, $user->id)) + { $this->setError($table->getError()); return false; @@ -102,7 +112,8 @@ trait RevokedModelTrait // Trigger the change state event. $result = Factory::getApplication()->triggerEvent($this->event_change_state, [$context, $pks, 0]); - if (\in_array(false, $result, true)) { + if (\in_array(false, $result, true)) + { $this->setError($table->getError()); return false; @@ -113,4 +124,12 @@ trait RevokedModelTrait return true; } -} \ No newline at end of file + + abstract protected function getCurrentUser(): User; + + abstract public function getTable($name = '', $prefix = '', $options = []); + + abstract public function setError($error); + + abstract protected function cleanCache($group = null); +} diff --git a/com_oauthserver/administrator/src/Model/ScopeModel.php b/com_oauthserver/administrator/src/Model/ScopeModel.php index d5bac94..e4c55ac 100644 --- a/com_oauthserver/administrator/src/Model/ScopeModel.php +++ b/com_oauthserver/administrator/src/Model/ScopeModel.php @@ -1,4 +1,11 @@ + * @license MIT; see LICENSE.txt + **/ namespace Webmasterskaya\Component\OauthServer\Administrator\Model; @@ -6,11 +13,12 @@ use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\MVC\Model\BaseModel; use Joomla\CMS\MVC\Model\ItemModelInterface; +\defined('_JEXEC') or die; + class ScopeModel extends BaseModel implements ItemModelInterface { - private static array $_storage; - private const PREDEFINED_SCOPES = ['userinfo', 'email']; + private static array $_storage; public function getItem($pk = null) { @@ -23,4 +31,4 @@ class ScopeModel extends BaseModel implements ItemModelInterface } -} \ No newline at end of file +}