Skip to content

Commit

Permalink
authx - Support Joomla users+sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed Feb 17, 2021
1 parent c8f2cd0 commit 3621dd3
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions ext/authx/Civi/Authx/Joomla.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

namespace Civi\Authx;

class Joomla implements AuthxInterface {

/**
* Joomla constructor.
*/
public function __construct() {
jimport('joomla.application.component.helper');
jimport('joomla.database.table');
jimport('joomla.user.helper');
}

/**
* @inheritDoc
*/
public function checkPassword(string $username, string $password) {
$JUserTable = \JTable::getInstance('User', 'JTable');

$db = $JUserTable->getDbo();
$query = $db->getQuery(TRUE);
$query->select('id, name, username, email, password');
$query->from($JUserTable->getTableName());
$query->where('(LOWER(username) = LOWER(' . $db->quote($username) . ')) AND (block = 0)');
$db->setQuery($query, 0, 0);
$users = $db->loadObjectList();

if (!empty($users)) {
$user = array_shift($users);
if (is_callable(['JUserHelper', 'verifyPassword'])) {
$verified = \JUserHelper::verifyPassword($password, $user->password, $user->id);
return $verified ? $user->id : NULL;
}
else {
throw new \CRM_Core_Exception("This version of Joomla does not support verifyPassword().");
}
}

return NULL;
}

/**
* @inheritDoc
*/
public function loginSession($userId) {
$user = new \JUser($userId);
$session = \JFactory::getSession();
$session->set('user', $user);
}

/**
* @inheritDoc
*/
public function logoutSession() {
\JFactory::getSession()->destroy();
}

/**
* @inheritDoc
*/
public function loginStateless($userId) {
\JFactory::getSession()->setHandler(new \CRM_Utils_FakeJoomlaSession('CIVISCRIPT'));
$user = new \JUser($userId);
$session = \JFactory::getSession();
$session->set('user', $user);
}

/**
* @inheritDoc
*/
public function getCurrentUserId() {
$user = \JFactory::getUser();
return ($user->guest) ? NULL : $user->id;
}

}

0 comments on commit 3621dd3

Please sign in to comment.