Skip to content

Commit

Permalink
- Username can be an e-mail
Browse files Browse the repository at this point in the history
- SSO: both systems log out at the same time
- code refactoring for SSO
  • Loading branch information
sergeytolkachyov committed Apr 9, 2024
1 parent e80fa78 commit 294f6d6
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 60 deletions.
2 changes: 1 addition & 1 deletion language/en-GB/plg_user_wtjmoodleusersync.sys.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ PLG_WTJMOODLEUSERSYNC_AFTER_DISCOVER_INSTALL = "Thank you for intalling extensio
PLG_WTJMOODLEUSERSYNC_AFTER_UPDATE = "Thank you for updating extension"
PLG_WTJMOODLEUSERSYNC_AFTER_UNINSTALL = "We are sorry that you deleted the extension"
PLG_WTJMOODLEUSERSYNC_MAYBE_INTERESTING="<h4>It may be interesting</h4><ul class=\"nav nav-tabs nav-stacked list-group\"><li class=\"list-group-item\"><a href=\"https://web-tolk.ru/en/dev/web-assets\" target=\"_blank\" title=\"Go to https://web-tolk.ru\">Joomla 4 & Joomla 5 Web Assets.</a></li><li class=\"list-group-item\"><a href=\"https://web-tolk.ru/en/dev/joomshopping\" target=\"_blank\" title=\"Go to https://web-tolk.ru\">JoomShopping extensions</a></li><li class=\"list-group-item\"><a href=\"https://web-tolk.ru/en/dev/joomla-plugins\" target=\"_blank\" title=\"Go to https://web-tolk.ru\">Joomla plugins</a></li><li class=\"list-group-item\"><a href=\"https://web-tolk.ru/en/dev/joomla-modules\" target=\"_blank\" title=\"Go to https://web-tolk.ru\">Joomla modules</a></li><li class=\"list-group-item\"><a href=\"https://web-tolk.ru/en/dev/components\" target=\"_blank\" title=\"Go to https://web-tolk.ru\">Joomla components</a></li><li class=\"list-group-item\"><a href=\"https://web-tolk.ru/en/dev/joomla-libraries\" target=\"_blank\" title=\"Go to https://web-tolk.ru\">Joomla libraries for developers.</a></li></ul>"
PLG_WTJMOODLEUSERSYNC_WHATS_NEW="<h3>v.1.0.1 What's new?</h3><h4>First and last name</h4><p>Added separation of first and last names by a space, if it is detected.</p><h4>The minimum version of PHP is 8</h4><p>More details in <a href=\"https://web-tolk.ru/en/dev/joomla-plugins/wt-jmoodle-user-sync\" target=\"_blank\">documentation</a>.</p>"
PLG_WTJMOODLEUSERSYNC_WHATS_NEW="<h3>v.1.0.2 What's new?</h3><h4>Username can be an e-mail</h4><p>It has become possible to use the user email as username when using SSO. On the Joomla side, third-party plugins are usually used for this. On the Moodle side, the parameter must be used <strong>Allow log in via email</strong> <code>authloginviaemail</code>.</p><h4>SSO: log out</h4><p>When logging out of Joomla, the user logs out of Moodle at the same time. To do this, install the version of the authentication plugin <code>jmoodle 1.1.0 (20240409)</code>.</p><h4>Code refactoring</h4><p>The code has become simpler and more reliable.</p><h4>Documentation</h4><p><a href=\"https://web-tolk.ru/en/dev/joomla-plugins/wt-jmoodle-user-sync\" target=\"_blank\">Read the documentation</a>.</p>"
PLG_WTJMOODLEUSERSYNC_JOOMLARU_TELEGRAM_CHAT="Joomla community in Telegram"
2 changes: 1 addition & 1 deletion language/ru-RU/plg_user_wtjmoodleusersync.sys.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ PLG_WTJMOODLEUSERSYNC_AFTER_DISCOVER_INSTALL = "Спасибо за устано
PLG_WTJMOODLEUSERSYNC_AFTER_UPDATE = "Спасибо за обновление расширения"
PLG_WTJMOODLEUSERSYNC_AFTER_UNINSTALL = "Сожалеем, что Вы удалили расширение"
PLG_WTJMOODLEUSERSYNC_MAYBE_INTERESTING="<h4>Может быть интересно</h4><ul class=\"nav nav-tabs nav-stacked list-group\"><li class=\"list-group-item\"><a href=\"https://web-tolk.ru/dev/web-assets\" target=\"_blank\" title=\"Перейти на https://web-tolk.ru\">Joomla 4 & Joomla 5 Web Assets</a></li><li class=\"list-group-item\"><a href=\"https://web-tolk.ru/dev/joomshopping\" target=\"_blank\" title=\"Перейти на https://web-tolk.ru\">Расширения для JoomShopping</a></li><li class=\"list-group-item\"><a href=\"https://web-tolk.ru/dev/joomla-plugins\" target=\"_blank\" title=\"Перейти на https://web-tolk.ru\">Плагины Joomla</a></li><li class=\"list-group-item\"><a href=\"https://web-tolk.ru/dev/joomla-modules\" target=\"_blank\" title=\"Перейти на https://web-tolk.ru\">Модули Joomla</a></li><li class=\"list-group-item\"><a href=\"https://web-tolk.ru/dev/components\" target=\"_blank\" title=\"Перейти на https://web-tolk.ru\">Компоненты Joomla</a></li><li class=\"list-group-item\"><a href=\"https://web-tolk.ru/dev/biblioteki\" target=\"_blank\" title=\"Перейти на https://web-tolk.ru\">Библиотеки Joomla для разработчиков</a></li></ul>"
PLG_WTJMOODLEUSERSYNC_WHATS_NEW="<h3>v.1.0.1 Что нового?</h3><h4>Имя и фамилия</h4><p>Добавлено разделение имени и фамилии по пробелу, если он обнаружен.</p><h4>Минимальная версия PHP - 8</h4><p>Подробнее в <a href=\"https://web-tolk.ru/dev/joomla-plugins/wt-jmoodle-user-sync\" target=\"_blank\">документации</a>.</p>"
PLG_WTJMOODLEUSERSYNC_WHATS_NEW="<h3>v.1.1.0 Что нового?</h3><h4>Username может быть e-mail</h4><p>Стало возможно использовать email пользователя как username при использовании SSO. На стороне Joomla обычно для этого используются сторонние плагины. На стороне Moodle необходимо использовать параметр <strong>Разрешить использовать адрес электронной почты в качестве логина</strong> <code>authloginviaemail</code>.</p><h4>SSO: log out</h4><p>При выходе из Joomla пользователь одновременно выходит из Moodle. Для этого установите версию плагина аутентификации <code>jmoodle 1.1.0 (20240409)</code>.</p><h4>Рефакторинг кода</h4><p>Код стал проще и надёжнее.</p><h4>Документация</h4><p><a href=\"https://web-tolk.ru/dev/joomla-plugins/wt-jmoodle-user-sync\" target=\"_blank\">Читать документацию</a>.</p>"
PLG_WTJMOODLEUSERSYNC_JOOMLARU_TELEGRAM_CHAT="Joomla сообщество в Телеграм"
5 changes: 2 additions & 3 deletions script.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* @package WT JMoodle user sync
* @version 1.0.1
* @version 1.1.0
* @Author Sergey Tolkachyov, https://web-tolk.ru
* @сopyright (c) March 2024 Sergey Tolkachyov. All rights reserved.
* @сopyright (c) April 2024 Sergey Tolkachyov. All rights reserved.
* @license GNU/GPL3 http://www.gnu.org/licenses/gpl-3.0.html
* @since 1.0.0
*/
Expand Down Expand Up @@ -193,7 +193,6 @@ public function postflight(string $type, InstallerAdapter $adapter): bool
}
}


$html .= $wt_jmoodle_library_install_status.'</div>
<div class="col-12 col-md-4 p-0 d-flex flex-column justify-content-start">
<img width="180" src="https://web-tolk.ru/web_tolk_logo_wide.png">
Expand Down
4 changes: 2 additions & 2 deletions services/provider.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* @package WT JMoodle user sync
* @version 1.0.1
* @version 1.1.0
* @Author Sergey Tolkachyov, https://web-tolk.ru
* @сopyright (c) March 2024 Sergey Tolkachyov. All rights reserved.
* @сopyright (c) April 2024 Sergey Tolkachyov. All rights reserved.
* @license GNU/GPL3 http://www.gnu.org/licenses/gpl-3.0.html
* @since 1.0.0
*/
Expand Down
94 changes: 47 additions & 47 deletions src/Extension/Wtjmoodleusersync.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* @package WT JMoodle user sync
* @version 1.0.1
* @version 1.1.0
* @Author Sergey Tolkachyov, https://web-tolk.ru
* @сopyright (c) March 2024 Sergey Tolkachyov. All rights reserved.
* @сopyright (c) April 2024 Sergey Tolkachyov. All rights reserved.
* @license GNU/GPL3 http://www.gnu.org/licenses/gpl-3.0.html
* @since 1.0.0
*/
Expand Down Expand Up @@ -43,6 +43,7 @@ public static function getSubscribedEvents(): array
'onUserAfterLogin' => 'onUserAfterLogin',
'onUserLoginFailure' => 'onUserLoginFailure',
'onUserLogout' => 'onUserLogout',
'onUserAfterLogout' => 'onUserAfterLogout',
'onUserAfterResetRequest' => 'onUserAfterResetRequest',
'onUserAfterResetComplete' => 'onUserAfterResetComplete',
'onUserBeforeSave' => 'onUserBeforeSave',
Expand Down Expand Up @@ -280,61 +281,33 @@ public function onUserAfterLogin($event): void
*/
[$options, $subject] = array_values($event->getArguments());
$user = $options['user'];

$moodle = new JMoodle();

$data = [
'username' => strtolower($user->username)
];

$config = Factory::getContainer()->get('config');
$temppath = $config->get('tmp_path');
$file = $temppath . '/' . UserHelper::genRandomPassword() . '.txt';
// First make sure we can write to file
touch($file);
if (!file_exists($file))
{
$moodle::saveToLog('Plugin WT JMoodle user sync can\'t create temporary file for cookie SSO', 'error');
$moodle = new JMoodle();

}
$config = Factory::getContainer()->get('config');

$curl_options = [
CURLOPT_COOKIEJAR => $file,
$curl_options = [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 1,
CURLOPT_SSL_VERIFYPEER => false
];

$moodle_reposnse = $moodle->customRequest('/auth/jmoodle/jmoodle_login.php', $data, 'POST', $curl_options);

/**
* Thanks to Joomdle for next lines.
* Deprecated.
* It will be replaced by working with the Set-Cookie header.
*/
$f = fopen($file, 'ro');
$cookies = $moodle_reposnse->getHeader('Set-Cookie');

if (!$f)
foreach ($cookies as $cookie)
{
$moodle::saveToLog('COM_JOOMDLE_ERROR_CANT_OPEN_CURL_FILE', 'error');
}
$parts = explode(';', $cookie);
$part = explode('=', $parts[0]);
$name = $part[0];
$value = !empty($part[1]) ? $part[1] : true;

while (!feof($f))
{
$line = fgets($f);
if (($line == '\n') || ((is_array($line)) && ($line[0] == '#')))
{
continue;
}
$parts = explode("\t", $line);
if (array_key_exists(5, $parts))
{
$name = $parts[5];
$value = trim($parts[6]);
// SET cookie_domain in Joomla settings with leading DOT
setcookie($name, $value, 0, $config->get('cookie_path', '/'), $config->get('cookie_domain', ''));
}
setcookie($name, $value, 0, $config->get('cookie_path', '/'), $config->get('cookie_domain', ''));
}
unlink($file);
}

/**
Expand Down Expand Up @@ -373,11 +346,38 @@ public function onUserLogout($event): void
*/
[$user, $options] = array_values($event->getArguments());

// $loggedOutUser = $this->getUserFactory()->loadUserById($user['id']);
//
// if ($loggedOutUser->block) {
// return;
// }
}

/**
* Method to log user's logout action
*
* @param $event Event
*
* @return void
*
* @since 3.9.0
*/
public function onUserAfterLogout($event): void
{
if (!$this->params->get('use_sso'))
{
// SSO is disabled
return;
}

/**
* @var array $user Holds the user data
* @var array $options Array holding options (remember, autoregister, group)
*/
[$user, $options] = array_values($event->getArguments());
$moodle = new JMoodle();

$data = [
'username' => strtolower($options['username'])
];

$moodle->customRequest('/auth/jmoodle/jmoodle_logout.php', $data, 'POST');

}

/**
Expand Down Expand Up @@ -435,7 +435,7 @@ public function onAjaxWtjmoodleusersync($event): void
// Check action
if ($action == 'check_joomla_user_session' && $this->params->get('use_sso'))
{
$username = $app->getInput()->json->getCmd('username');
$username = $app->getInput()->json->get('username', '', 'raw');
$user = Factory::getContainer()->get(UserFactoryInterface::class)->loadUserByUsername($username);

$db = Factory::getContainer()->get('DatabaseDriver');
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/CheckusermethodsField.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* @package WT JMoodle user sync
* @version 1.0.1
* @version 1.1.0
* @Author Sergey Tolkachyov, https://web-tolk.ru
* @сopyright (c) March 2024 Sergey Tolkachyov. All rights reserved.
* @сopyright (c) April 2024 Sergey Tolkachyov. All rights reserved.
* @license GNU/GPL3 http://www.gnu.org/licenses/gpl-3.0.html
* @since 1.0.0
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/PlugininfoField.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* @package WT JMoodle user sync
* @version 1.0.1
* @version 1.1.0
* @Author Sergey Tolkachyov, https://web-tolk.ru
* @сopyright (c) March 2024 Sergey Tolkachyov. All rights reserved.
* @сopyright (c) April 2024 Sergey Tolkachyov. All rights reserved.
* @license GNU/GPL3 http://www.gnu.org/licenses/gpl-3.0.html
* @since 1.0.0
*/
Expand Down
4 changes: 2 additions & 2 deletions wtjmoodleusersync.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<author>Sergey Tolkachyov</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>https://web-tolk.ru/</authorUrl>
<creationDate>March 2024</creationDate>
<creationDate>April 2024</creationDate>
<copyright>2023 Sergey Tolkachyov</copyright>
<license>GNU General Public License v3.0</license>
<version>1.0.1</version>
<version>1.1.0</version>
<description>PLG_WTJMOODLEUSERSYNC_DESC</description>
<namespace path="src">Joomla\Plugin\User\Wtjmoodleusersync</namespace>
<scriptfile>script.php</scriptfile>
Expand Down

0 comments on commit 294f6d6

Please sign in to comment.