joomla-oauth-server/com_oauthserver/administrator/src/Table/RevokedTableTrait.php

165 lines
4.0 KiB
PHP
Raw Normal View History

<?php
2024-03-09 18:57:05 +03:00
/**
* @package Joomla.Administrator
* @subpackage com_oauthserver
*
* @copyright (c) 2024. Webmasterskaya. <https://webmasterskaya.xyz>
* @license MIT; see LICENSE.txt
**/
namespace Webmasterskaya\Component\OauthServer\Administrator\Table;
use Joomla\CMS\Event\AbstractEvent;
use Joomla\CMS\Language\Text;
2024-03-09 18:57:05 +03:00
use Joomla\Database\DatabaseDriver;
\defined('_JEXEC') or die;
trait RevokedTableTrait
{
/**
* @var string
2024-03-09 18:57:05 +03:00
* @since version
* @noinspection PhpMissingFieldTypeInspection
*/
protected $_tbl = '';
/**
* @var string
2024-03-09 18:57:05 +03:00
* @since version
* @noinspection PhpMissingFieldTypeInspection
*/
protected $_tbl_key = '';
/**
* @var array
2024-03-09 18:57:05 +03:00
* @since version
* @noinspection PhpMissingFieldTypeInspection
*/
protected $_tbl_keys = [];
/**
2024-03-09 18:57:05 +03:00
* @var DatabaseDriver
* @since version
* @noinspection PhpMissingFieldTypeInspection
*/
protected $_db;
abstract public function getDbo();
2024-03-07 03:59:12 +03:00
public function revoke($pks = null): bool
{
// Pre-processing by observers
$event = AbstractEvent::create(
'onTableBeforeRevoke',
[
'subject' => $this,
2024-03-09 18:57:05 +03:00
'pks' => $pks,
]
);
$this->getDispatcher()->dispatch('onTableBeforeRevoke', $event);
2024-03-09 18:57:05 +03:00
if (!\is_null($pks))
{
if (!\is_array($pks))
{
$pks = [$pks];
}
2024-03-09 18:57:05 +03:00
foreach ($pks as $key => $pk)
{
if (!\is_array($pk))
{
$pks[$key] = [$this->_tbl_key => $pk];
}
}
}
// If there are no primary keys set check to see if the instance key is set.
2024-03-09 18:57:05 +03:00
if (empty($pks))
{
$pk = [];
2024-03-09 18:57:05 +03:00
foreach ($this->_tbl_keys as $key)
{
if ($this->$key)
{
$pk[$key] = $this->$key;
2024-03-09 18:57:05 +03:00
}
else
{
// We don't have a full primary key - return false
$this->setError(Text::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
return false;
}
}
$pks = [$pk];
}
$revokedField = $this->getColumnAlias('revoked');
2024-03-09 18:57:05 +03:00
foreach ($pks as $pk)
{
$query = $this->_db->getQuery(true)
->update($this->_db->quoteName($this->_tbl))
->set($this->_db->quoteName($revokedField) . ' = 0');
// Build the WHERE clause for the primary keys.
$this->appendPrimaryKeys($query, $pk);
$this->_db->setQuery($query);
2024-03-09 18:57:05 +03:00
try
{
$this->_db->execute();
2024-03-09 18:57:05 +03:00
}
catch (\RuntimeException $e)
{
$this->setError($e->getMessage());
return false;
}
// If the Table instance value is in the list of primary keys that were set, set the instance.
$ours = true;
2024-03-09 18:57:05 +03:00
foreach ($this->_tbl_keys as $key)
{
if ($this->$key != $pk[$key])
{
$ours = false;
}
}
2024-03-09 18:57:05 +03:00
if ($ours)
{
$this->$revokedField = 0;
}
}
$this->setError('');
// Pre-processing by observers
$event = AbstractEvent::create(
'onTableAfterRevoke',
[
'subject' => $this,
2024-03-09 18:57:05 +03:00
'pks' => $pks
]
);
$this->getDispatcher()->dispatch('onTableAfterRevoke', $event);
return true;
}
2024-03-09 18:57:05 +03:00
abstract public function getDispatcher();
abstract public function setError($error);
abstract public function getColumnAlias($column);
abstract public function appendPrimaryKeys($query, $pk = null);
}