Actualización
This commit is contained in:
28
plugin/createdrupaluser/README.md
Normal file
28
plugin/createdrupaluser/README.md
Normal file
@@ -0,0 +1,28 @@
|
||||
Create Drupal user plugin
|
||||
=========================
|
||||
|
||||
This plugin creates a user on a Drupal portal when a user is registered in
|
||||
Chamilo.
|
||||
|
||||
This uses the Hook mechanism available to Chamilo plugins: when enabling this
|
||||
plugin, the HookCreateDrupalUser is automatically added to the hooks stack, and
|
||||
the UserManager::create_user() method calls the HookCreateUser hook and notifies
|
||||
it, resulting in the plugin code to be executed.
|
||||
|
||||
The Drupal portal settings must be configured in the configuration panel for the
|
||||
plugin. A SOAP call is then initiated inside the plugin code, that will use the
|
||||
Drupal's addUser() web service. See src/HookCreateDrupalUser.php for more info
|
||||
on the call parameters.
|
||||
|
||||
After calling the web service and receiving a positive answer, Chamilo stores
|
||||
the remote (Drupal) user ID inside the extra_field_values table, as field name
|
||||
"drupal_user_id". This later serves for updates and other synchronisation
|
||||
purposes.
|
||||
|
||||
Extending
|
||||
---------
|
||||
|
||||
Other plugins could easily be created by copying this one and modifying the
|
||||
class names and web services to call. Simply review every variable coined with
|
||||
the "drupal" name and update according to your own portal.
|
||||
|
||||
10
plugin/createdrupaluser/config.php
Normal file
10
plugin/createdrupaluser/config.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Config the plugin.
|
||||
*
|
||||
* @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
|
||||
*
|
||||
* @package chamilo.plugin.createDrupalUser
|
||||
*/
|
||||
require_once api_get_path(SYS_PATH).'main/inc/global.inc.php';
|
||||
10
plugin/createdrupaluser/index.php
Normal file
10
plugin/createdrupaluser/index.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Config the plugin.
|
||||
*
|
||||
* @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
|
||||
*
|
||||
* @package chamilo.plugin.createDrupalUser
|
||||
*/
|
||||
require_once __DIR__.'/config.php';
|
||||
12
plugin/createdrupaluser/install.php
Normal file
12
plugin/createdrupaluser/install.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Initialization install.
|
||||
*
|
||||
* @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
|
||||
*
|
||||
* @package chamilo.plugin.createDrupalUser
|
||||
*/
|
||||
require_once __DIR__.'/config.php';
|
||||
|
||||
CreateDrupalUser::create()->install();
|
||||
16
plugin/createdrupaluser/lang/english.php
Normal file
16
plugin/createdrupaluser/lang/english.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Strings to english L10n.
|
||||
*
|
||||
* @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
|
||||
*
|
||||
* @package chamilo.plugin.createDrupalUser
|
||||
*/
|
||||
$strings['plugin_title'] = 'Create Drupal users';
|
||||
$strings['plugin_comment'] = 'This plugin creates users in an associated Drupal website when a user is created in Chamilo LMS. This requires the "chamilo" module to be installed and configured on the Drupal side.';
|
||||
|
||||
$strings['drupal_domain'] = 'Drupal website URL';
|
||||
$strings['drupal_domain_help'] = 'The server domain name should be written with a trailing slash and with the protocol, e.g. http://www.example.com/';
|
||||
|
||||
$strings['DruaplUserId'] = 'Drupal user ID';
|
||||
16
plugin/createdrupaluser/lang/spanish.php
Normal file
16
plugin/createdrupaluser/lang/spanish.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Strings to spanish L10n.
|
||||
*
|
||||
* @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
|
||||
*
|
||||
* @package chamilo.plugin.createDrupalUser
|
||||
*/
|
||||
$strings['plugin_title'] = 'Crear usuario Drupal';
|
||||
$strings['plugin_comment'] = 'Este plugin permite crear usuarios en un sitio web Drupal cuando un usuario es creado en Chamilo LMS. Este plugin requiere que el módulo "chamilo" de Drupal esté instalado y configurado en el sitio Drupal.';
|
||||
|
||||
$strings['drupal_domain'] = 'URL del sitio web Drupal';
|
||||
$strings['drupal_domain_help'] = 'La dirección del servidor debe escribirse con el protocolo al comienzo y con la barra al final, por ejemplo http://www.example.com/';
|
||||
|
||||
$strings['DrupalUserId'] = 'ID de usuario Drupal';
|
||||
12
plugin/createdrupaluser/plugin.php
Normal file
12
plugin/createdrupaluser/plugin.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Get the plugin info.
|
||||
*
|
||||
* @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
|
||||
*
|
||||
* @package chamilo.plugin.createDrupalUser
|
||||
*/
|
||||
require_once __DIR__.'/config.php';
|
||||
|
||||
$plugin_info = CreateDrupalUser::create()->get_info();
|
||||
156
plugin/createdrupaluser/src/CreateDrupalUser.php
Normal file
156
plugin/createdrupaluser/src/CreateDrupalUser.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* Create a user in Drupal website when a user is registered in Chamilo LMS.
|
||||
*
|
||||
* @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
|
||||
*
|
||||
* @package chamilo.plugin.createDrupalUser
|
||||
*/
|
||||
class CreateDrupalUser extends Plugin implements HookPluginInterface
|
||||
{
|
||||
public const EXTRAFIELD_VARIABLE_NAME = 'drupal_user_id';
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
$parameters = [
|
||||
'drupal_domain' => 'text',
|
||||
];
|
||||
|
||||
parent::__construct('1.0', 'Angel Fernando Quiroz Campos', $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instance the plugin.
|
||||
*
|
||||
* @staticvar null $result
|
||||
*
|
||||
* @return CreateDrupalUser
|
||||
*/
|
||||
public static function create()
|
||||
{
|
||||
static $result = null;
|
||||
|
||||
return $result ? $result : $result = new self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Install the plugin.
|
||||
*/
|
||||
public function install()
|
||||
{
|
||||
$this->createExtraField();
|
||||
$this->installHook();
|
||||
}
|
||||
|
||||
/**
|
||||
* Uninstall the plugin.
|
||||
*/
|
||||
public function uninstall()
|
||||
{
|
||||
$this->uninstallHook();
|
||||
$this->deleteExtraField();
|
||||
}
|
||||
|
||||
/**
|
||||
* Install the Create User hook.
|
||||
*/
|
||||
public function installHook()
|
||||
{
|
||||
/** @var HookCreateDrupalUser $observer */
|
||||
$observer = HookCreateDrupalUser::create();
|
||||
HookCreateUser::create()->attach($observer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uninstall the Create User hook.
|
||||
*/
|
||||
public function uninstallHook()
|
||||
{
|
||||
/** @var HookCreateDrupalUser $observer */
|
||||
$observer = HookCreateDrupalUser::create();
|
||||
$event = HookCreateUser::create();
|
||||
|
||||
if ($event) {
|
||||
$event->detach($observer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the drupal_user_id extra field information.
|
||||
*
|
||||
* @return array The info
|
||||
*/
|
||||
private function getExtraFieldInfo()
|
||||
{
|
||||
$extraField = new ExtraField('user');
|
||||
$extraFieldHandler = $extraField->get_handler_field_info_by_field_variable(
|
||||
self::EXTRAFIELD_VARIABLE_NAME
|
||||
);
|
||||
|
||||
return $extraFieldHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the drupal_user_id when it not exists.
|
||||
*/
|
||||
private function createExtraField()
|
||||
{
|
||||
$extraFieldExists = $this->getExtraFieldInfo() !== false;
|
||||
|
||||
if (!$extraFieldExists) {
|
||||
$extraField = new ExtraField('user');
|
||||
$extraField->save(
|
||||
[
|
||||
'field_type' => ExtraField::FIELD_TYPE_INTEGER,
|
||||
'variable' => self::EXTRAFIELD_VARIABLE_NAME,
|
||||
'display_text' => get_plugin_lang('DrupalUserId', 'CreateDrupalUser'),
|
||||
'default_value' => null,
|
||||
'field_order' => null,
|
||||
'visible_to_self' => false,
|
||||
'changeable' => false,
|
||||
'filter' => null,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the drupal_user_id and values.
|
||||
*/
|
||||
private function deleteExtraField()
|
||||
{
|
||||
$extraFieldInfo = $this->getExtraFieldInfo();
|
||||
$extraFieldExists = $extraFieldInfo !== false;
|
||||
|
||||
if ($extraFieldExists) {
|
||||
$extraField = new ExtraField('user');
|
||||
$extraField->delete($extraFieldInfo['id']);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
public function notifyDocumentAction(HookDocumentActionEventInterface $hook)
|
||||
{
|
||||
$data = $hook->getEventData();
|
||||
if ($data['type'] === HOOK_EVENT_TYPE_PRE) {
|
||||
$data['actions'][] = Display::return_icon('edit.png');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function notifyDocumentItemAction(HookDocumentItemActionEventInterface $hook)
|
||||
{
|
||||
$data = $hook->getEventData();
|
||||
if ($data['type'] === HOOK_EVENT_TYPE_PRE) {
|
||||
$data['actions'][] = $data['id'].' - '.$data['title'];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}*/
|
||||
}
|
||||
78
plugin/createdrupaluser/src/HookCreateDrupalUser.php
Normal file
78
plugin/createdrupaluser/src/HookCreateDrupalUser.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* Class HookCreateDrupalUser
|
||||
* Hook to create an user in Drupal website.
|
||||
*
|
||||
* @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
|
||||
*
|
||||
* @package chamilo.plugin.createDrupalUser
|
||||
*/
|
||||
class HookCreateDrupalUser extends HookObserver implements HookCreateUserObserverInterface
|
||||
{
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(
|
||||
'plugin/createdrupaluser/src/CreateDrupalUser.php',
|
||||
'drupaluser'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Drupal user when the Chamilo user is registered.
|
||||
*
|
||||
* @param HookCreateUserEventInterface $hook The hook
|
||||
*/
|
||||
public function hookCreateUser(HookCreateUserEventInterface $hook)
|
||||
{
|
||||
$data = $hook->getEventData();
|
||||
|
||||
$drupalDomain = CreateDrupalUser::create()->get('drupal_domain');
|
||||
$drupalDomain = rtrim($drupalDomain, '/').'/';
|
||||
|
||||
if ($data['type'] === HOOK_EVENT_TYPE_POST) {
|
||||
$return = $data['return'];
|
||||
$originalPassword = $data['originalPassword'];
|
||||
|
||||
$userInfo = api_get_user_info($return);
|
||||
$fields = [
|
||||
'name' => $userInfo['username'],
|
||||
'pass' => $originalPassword,
|
||||
'mail' => $userInfo['email'],
|
||||
'status' => 1,
|
||||
'init' => $userInfo['email'],
|
||||
];
|
||||
|
||||
$extraFields = [
|
||||
'first_name' => $userInfo['firstname'],
|
||||
'last_name' => $userInfo['lastname'],
|
||||
];
|
||||
|
||||
$options = [
|
||||
'location' => $drupalDomain.'sites/all/modules/chamilo/soap.php?wsdl',
|
||||
'uri' => $drupalDomain,
|
||||
];
|
||||
|
||||
$client = new SoapClient(null, $options);
|
||||
$drupalUserId = false;
|
||||
|
||||
if (isset($_SESSION['ws_drupal_user_id'])) {
|
||||
$drupalUserId = $_SESSION['ws_drupal_user_id'];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($drupalUserId === false) {
|
||||
$drupalUserId = $client->addUser($fields, $extraFields);
|
||||
}
|
||||
|
||||
if ($drupalUserId !== false) {
|
||||
UserManager::update_extra_field_value($return, 'drupal_user_id', $drupalUserId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
plugin/createdrupaluser/uninstall.php
Normal file
12
plugin/createdrupaluser/uninstall.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Initialization uninstall.
|
||||
*
|
||||
* @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
|
||||
*
|
||||
* @package chamilo.plugin.createDrupalUser
|
||||
*/
|
||||
require_once __DIR__.'/config.php';
|
||||
|
||||
CreateDrupalUser::create()->uninstall();
|
||||
Reference in New Issue
Block a user