Skip to content

Commit

Permalink
Namespace models
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Babker committed Apr 14, 2019
1 parent 39a3c90 commit 848cf7b
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 115 deletions.
Expand Up @@ -7,32 +7,47 @@
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Component\Privacy\Site\Model;

defined('_JEXEC') or die;

use Joomla\CMS\Date\Date;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\AdminModel;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\String\PunycodeHelper;
use Joomla\CMS\Table\Table;
use Joomla\CMS\User\UserHelper;
use Joomla\Component\Messages\Administrator\Model\MessageModel;
use Joomla\Component\Privacy\Administrator\Table\RequestTable;
use Joomla\Database\Exception\ExecutionFailureException;

/**
* Request confirmation model class.
*
* @since 3.9.0
*/
class PrivacyModelConfirm extends JModelAdmin
class ConfirmModel extends AdminModel
{
/**
* Confirms the information request.
*
* @param array $data The data expected for the form.
*
* @return mixed Exception | JException | boolean
* @return mixed Exception | boolean
*
* @since 3.9.0
*/
public function confirmRequest($data)
{
// Get the form.
$form = $this->getForm();
$data['email'] = JStringPunycode::emailToPunycode($data['email']);
$data['email'] = PunycodeHelper::emailToPunycode($data['email']);

// Check for an error.
if ($form instanceof Exception)
if ($form instanceof \Exception)
{
return $form;
}
Expand All @@ -42,7 +57,7 @@ public function confirmRequest($data)
$return = $form->validate($data);

// Check for an error.
if ($return instanceof Exception)
if ($return instanceof \Exception)
{
return $return;
}
Expand All @@ -60,29 +75,29 @@ public function confirmRequest($data)
}

// Search for the information request
/** @var PrivacyTableRequest $table */
/** @var RequestTable $table */
$table = $this->getTable();

if (!$table->load(array('email' => $data['email'], 'status' => 0)))
if (!$table->load(['email' => $data['email'], 'status' => 0]))
{
$this->setError(JText::_('COM_PRIVACY_ERROR_NO_PENDING_REQUESTS'));
$this->setError(Text::_('COM_PRIVACY_ERROR_NO_PENDING_REQUESTS'));

return false;
}

// A request can only be confirmed if it is in a pending status and has a confirmation token
if ($table->status != '0' || !$table->confirm_token)
{
$this->setError(JText::_('COM_PRIVACY_ERROR_NO_PENDING_REQUESTS'));
$this->setError(Text::_('COM_PRIVACY_ERROR_NO_PENDING_REQUESTS'));

return false;
}

// A request can only be confirmed if the token is less than 24 hours old
$confirmTokenCreatedAt = new JDate($table->confirm_token_created_at);
$confirmTokenCreatedAt = new Date($table->confirm_token_created_at);
$confirmTokenCreatedAt->add(new DateInterval('P1D'));

$now = new JDate('now');
$now = new Date('now');

if ($now > $confirmTokenCreatedAt)
{
Expand All @@ -94,31 +109,31 @@ public function confirmRequest($data)
{
$table->store();
}
catch (JDatabaseException $exception)
catch (ExecutionFailureException $exception)
{
// The error will be logged in the database API, we just need to catch it here to not let things fatal out
}

$this->setError(JText::_('COM_PRIVACY_ERROR_CONFIRM_TOKEN_EXPIRED'));
$this->setError(Text::_('COM_PRIVACY_ERROR_CONFIRM_TOKEN_EXPIRED'));

return false;
}

// Verify the token
if (!JUserHelper::verifyPassword($data['confirm_token'], $table->confirm_token))
if (!UserHelper::verifyPassword($data['confirm_token'], $table->confirm_token))
{
$this->setError(JText::_('COM_PRIVACY_ERROR_NO_PENDING_REQUESTS'));
$this->setError(Text::_('COM_PRIVACY_ERROR_NO_PENDING_REQUESTS'));

return false;
}

// Everything is good to go, transition the request to confirmed
$saved = $this->save(
array(
'id' => $table->id,
'status' => 1,
[
'id' => $table->id,
'status' => 1,
'confirm_token' => '',
)
]
);

if (!$saved)
Expand All @@ -128,29 +143,22 @@ public function confirmRequest($data)
}

// Push a notification to the site's super users, deliberately ignoring if this process fails so the below message goes out
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_messages/models', 'MessagesModel');
JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_messages/tables');

/** @var MessagesModelMessage $messageModel */
$messageModel = JModelLegacy::getInstance('Message', 'MessagesModel');
/** @var MessageModel $messageModel */
$messageModel = Factory::getApplication()->bootComponent('com_messages')->getMVCFactory()->createModel('Message', 'Administrator');

$messageModel->notifySuperUsers(
JText::_('COM_PRIVACY_ADMIN_NOTIFICATION_USER_CONFIRMED_REQUEST_SUBJECT'),
JText::sprintf('COM_PRIVACY_ADMIN_NOTIFICATION_USER_CONFIRMED_REQUEST_MESSAGE', $table->email)
Text::_('COM_PRIVACY_ADMIN_NOTIFICATION_USER_CONFIRMED_REQUEST_SUBJECT'),
Text::sprintf('COM_PRIVACY_ADMIN_NOTIFICATION_USER_CONFIRMED_REQUEST_MESSAGE', $table->email)
);

JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_actionlogs/models', 'ActionlogsModel');

$message = array(
$message = [
'action' => 'request-confirmed',
'subjectemail' => $table->email,
'id' => $table->id,
'itemlink' => 'index.php?option=com_privacy&view=request&id=' . $table->id,
);
];

/** @var ActionlogsModelActionlog $model */
$model = JModelLegacy::getInstance('Actionlog', 'ActionlogsModel');
$model->addLog(array($message), 'COM_PRIVACY_ACTION_LOG_CONFIRMED_REQUEST', 'com_privacy.request');
$this->getActionlogModel()->addLog([$message], 'COM_PRIVACY_ACTION_LOG_CONFIRMED_REQUEST', 'com_privacy.request');

return true;
}
Expand All @@ -161,21 +169,21 @@ public function confirmRequest($data)
* @param array $data Data for the form.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
*
* @return JForm|boolean A JForm object on success, false on failure
* @return Form|boolean A Form object on success, false on failure
*
* @since 3.9.0
*/
public function getForm($data = array(), $loadData = true)
public function getForm($data = [], $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_privacy.confirm', 'confirm', array('control' => 'jform'));
$form = $this->loadForm('com_privacy.confirm', 'confirm', ['control' => 'jform']);

if (empty($form))
{
return false;
}

$input = JFactory::getApplication()->input;
$input = Factory::getApplication()->input;

if ($input->getMethod() === 'GET')
{
Expand All @@ -192,12 +200,12 @@ public function getForm($data = array(), $loadData = true)
* @param string $prefix The class prefix. Optional.
* @param array $options Configuration array for model. Optional.
*
* @return JTable A JTable object
* @return Table A Table object
*
* @since 3.9.0
* @throws \Exception
*/
public function getTable($name = 'Request', $prefix = 'PrivacyTable', $options = array())
public function getTable($name = 'Request', $prefix = 'Administrator', $options = [])
{
return parent::getTable($name, $prefix, $options);
}
Expand All @@ -214,9 +222,23 @@ public function getTable($name = 'Request', $prefix = 'PrivacyTable', $options =
protected function populateState()
{
// Get the application object.
$params = JFactory::getApplication()->getParams('com_privacy');
$params = Factory::getApplication()->getParams('com_privacy');

// Load the parameters.
$this->setState('params', $params);
}

/**
* Method to fetch an instance of the action log model.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
private function getActionlogModel(): \ActionlogsModelActionlog
{
BaseDatabaseModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_actionlogs/models', 'ActionlogsModel');

return BaseDatabaseModel::getInstance('Actionlog', 'ActionlogsModel');
}
}
Expand Up @@ -7,14 +7,26 @@
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Component\Privacy\Site\Model;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\AdminModel;
use Joomla\CMS\String\PunycodeHelper;
use Joomla\CMS\Table\Table;
use Joomla\CMS\User\UserHelper;
use Joomla\Component\Privacy\Administrator\Table\ConsentTable;
use Joomla\Database\Exception\ExecutionFailureException;

/**
* Remind confirmation model class.
*
* @since 3.9.0
*/
class PrivacyModelRemind extends JModelAdmin
class RemindModel extends AdminModel
{
/**
* Confirms the remind request.
Expand All @@ -29,10 +41,10 @@ public function remindRequest($data)
{
// Get the form.
$form = $this->getForm();
$data['email'] = JStringPunycode::emailToPunycode($data['email']);
$data['email'] = PunycodeHelper::emailToPunycode($data['email']);

// Check for an error.
if ($form instanceof Exception)
if ($form instanceof \Exception)
{
return $form;
}
Expand All @@ -42,7 +54,7 @@ public function remindRequest($data)
$return = $form->validate($data);

// Check for an error.
if ($return instanceof Exception)
if ($return instanceof \Exception)
{
return $return;
}
Expand All @@ -59,7 +71,7 @@ public function remindRequest($data)
return false;
}

/** @var PrivacyTableConsent $table */
/** @var ConsentTable $table */
$table = $this->getTable();

$db = $this->getDbo();
Expand All @@ -75,36 +87,36 @@ public function remindRequest($data)
{
$remind = $db->loadObject();
}
catch (RuntimeException $e)
catch (ExecutionFailureException $e)
{
$this->setError(JText::_('COM_PRIVACY_ERROR_NO_PENDING_REMIND'));
$this->setError(Text::_('COM_PRIVACY_ERROR_NO_PENDING_REMIND'));

return false;
}

if (!$remind)
{
$this->setError(JText::_('COM_PRIVACY_ERROR_NO_PENDING_REMIND'));
$this->setError(Text::_('COM_PRIVACY_ERROR_NO_PENDING_REMIND'));

return false;
}

// Verify the token
if (!JUserHelper::verifyPassword($data['remind_token'], $remind->token))
if (!UserHelper::verifyPassword($data['remind_token'], $remind->token))
{
$this->setError(JText::_('COM_PRIVACY_ERROR_NO_REMIND_REQUESTS'));
$this->setError(Text::_('COM_PRIVACY_ERROR_NO_REMIND_REQUESTS'));

return false;
}

// Everything is good to go, transition the request to extended
$saved = $this->save(
array(
[
'id' => $remind->id,
'remind' => 0,
'token' => '',
'created' => JFactory::getDate()->toSql(),
)
'created' => Factory::getDate()->toSql(),
]
);

if (!$saved)
Expand All @@ -122,21 +134,21 @@ public function remindRequest($data)
* @param array $data Data for the form.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
*
* @return JForm|boolean A JForm object on success, false on failure
* @return Form|boolean A Form object on success, false on failure
*
* @since 3.9.0
*/
public function getForm($data = array(), $loadData = true)
public function getForm($data = [], $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_privacy.remind', 'remind', array('control' => 'jform'));
$form = $this->loadForm('com_privacy.remind', 'remind', ['control' => 'jform']);

if (empty($form))
{
return false;
}

$input = JFactory::getApplication()->input;
$input = Factory::getApplication()->input;

if ($input->getMethod() === 'GET')
{
Expand All @@ -153,12 +165,12 @@ public function getForm($data = array(), $loadData = true)
* @param string $prefix The class prefix. Optional.
* @param array $options Configuration array for model. Optional.
*
* @return JTable A JTable object
* @return Table A JTable object
*
* @since 3.9.0
* @throws \Exception
*/
public function getTable($name = 'Consent', $prefix = 'PrivacyTable', $options = array())
public function getTable($name = 'Consent', $prefix = 'Administrator', $options = [])
{
return parent::getTable($name, $prefix, $options);
}
Expand All @@ -175,7 +187,7 @@ public function getTable($name = 'Consent', $prefix = 'PrivacyTable', $options =
protected function populateState()
{
// Get the application object.
$params = JFactory::getApplication()->getParams('com_privacy');
$params = Factory::getApplication()->getParams('com_privacy');

// Load the parameters.
$this->setState('params', $params);
Expand Down

0 comments on commit 848cf7b

Please sign in to comment.