Skip to content

Commit

Permalink
Plugin: adapt userremoteservice plugin to enable redirect and adding …
Browse files Browse the repository at this point in the history
…an option to hide links from main menu -refs BT#20171
  • Loading branch information
NicoDucou committed Sep 28, 2022
1 parent bb1fa6f commit 873a81b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 4 deletions.
41 changes: 41 additions & 0 deletions plugin/userremoteservice/Entity/UserRemoteService.php
Expand Up @@ -96,6 +96,15 @@ public function setURL($url)

return $this;
}
/**
* @return string
*/
public function getAccessURL($pluginName)
{
$accessUrl = api_get_path(WEB_PLUGIN_PATH) . $pluginName . "/redirect.php?serviceId=" . $this->getId();
return $accessUrl;
}


/**
* Returns a user-specific URL, with two extra query string parameters : 'username' and 'hash'.
Expand Down Expand Up @@ -128,4 +137,36 @@ public function getCustomUserURL($username, $userId, $salt)
)
);
}


/**
* Returns a user-specific URL, with two extra query string parameters : 'uid' and 'hash'.
* 'hash' is generated using $salt and $userId.
*
* @param string $userId the user identifier, to build the hash and to include for the uid parameter
* @param string $salt the salt, to build the hash
*
* @throws Exception on hash generation failure
*
* @return string the custom user redirect URL
*/
public function getCustomUserRedirectURL($userId, $salt)
{
$hash = password_hash($salt.$userId, PASSWORD_BCRYPT);
if (false === $hash) {
throw new Exception('hash generation failed');
}

return sprintf(
'%s%s%s',
$this->url,
false === strpos($this->url, '?') ? '?' : '&',
http_build_query(
[
'uid' => $userId,
'hash' => $hash,
]
)
);
}
}
3 changes: 2 additions & 1 deletion plugin/userremoteservice/README.md
Expand Up @@ -2,8 +2,9 @@ User Remote Service Plugin
==========================

Appends site-specific iframe-targetted user-identifying links to the menu bar.
You can also hide the link from the menu and use the redirect link to use it as a link in a course.

After activation, configure a __salt__ then select region __menu_administrator__.

In the __Administration__ menu, in block __Plugins__,
you will find link __User Remote Services__ to manage the services to be shown in the main menu (title and URL).
you will find link __User Remote Services__ to manage the services to be shown in the main menu (title and URL) and also get the redirect URL to use in a course.
6 changes: 4 additions & 2 deletions plugin/userremoteservice/index.php
Expand Up @@ -3,6 +3,8 @@

require_once __DIR__.'/config.php';

foreach (UserRemoteServicePlugin::create()->getNavigationMenu() as $key => $menu) {
$template->params['menu'][$key] = $menu;
if ('true' !== UserRemoteServicePlugin::create()->get_hide_link_from_navigation_menu()) {
foreach (UserRemoteServicePlugin::create()->getNavigationMenu() as $key => $menu) {
$template->params['menu'][$key] = $menu;
}
}
4 changes: 3 additions & 1 deletion plugin/userremoteservice/lang/english.php
Expand Up @@ -3,7 +3,7 @@

$strings['plugin_title'] = 'User Remote Services';
$strings['plugin_comment'] = 'Appends site-specific iframe-targetted user-identifying links to the menu bar.';

$strings['salt'] = 'Salt';
$strings['salt_help'] =
'Secret character string, used to generate the <em>hash</em> URL parameter. The longest, the best.
Expand All @@ -13,10 +13,12 @@
<br/><code>$salt</code> is this input value,
<br/><code>$userId</code> is the number of the user referenced by the <em>username</em> URL parameter value and
<br/><code>$hash</code> contains the <em>hash</em> URL parameter value.';
$strings['hide_link_from_navigation_menu'] = 'hide links from the menu';

// Please keep alphabetically sorted
$strings['CreateService'] = 'Add service to menu bar';
$strings['DeleteServices'] = 'Remove services from menu bar';
$strings['ServicesToDelete'] = 'Services to remove from menu bar';
$strings['ServiceTitle'] = 'Service title';
$strings['ServiceURL'] = 'Service web site location (URL)';
$strings['RedirectAccessURL'] = "URL to use in Chamilo to redirect user to the service (URL)";
2 changes: 2 additions & 0 deletions plugin/userremoteservice/lang/french.php
Expand Up @@ -15,10 +15,12 @@
<br/><code>$salt</code> est la valeur saisie ici,
<br/><code>$userId</code> est le numéro de l\'utilisateur auquel fait référence le paramètre d\'URL <em>username</em> et
<br/><code>$hash</code> représente la valeur du paramètre d\'URL <em>hash</em>.';
$strings['hide_link_from_navigation_menu'] = 'Masquer les liens dans le menu';

// Please keep alphabetically sorted
$strings['CreateService'] = "Ajouter le service au menu";
$strings['DeleteServices'] = "Retirer les services du menu";
$strings['ServicesToDelete'] = "Services à retirer du menu";
$strings['ServiceTitle'] = "Titre du service";
$strings['ServiceURL'] = "Adresse web du service (URL)";
$strings['RedirectAccessURL'] = "Adresse à utiliser pour rediriger l'utilisateur au service (URL)";
27 changes: 27 additions & 0 deletions plugin/userremoteservice/src/user_remote_service_plugin.class.php
Expand Up @@ -18,6 +18,7 @@ protected function __construct()
'Sébastien Ducoulombier',
[
'salt' => 'text',
'hide_link_from_navigation_menu' => 'boolean',
]
);
$this->isAdminPlugin = true;
Expand Down Expand Up @@ -68,6 +69,14 @@ public function salt()
return $this->get('salt');
}

/**
* @return boolean the value of hide_link_from_navigation_menu setting
*/
public function get_hide_link_from_navigation_menu()
{
return $this->get('hide_link_from_navigation_menu');
}

/**
* Retrieves the list of all services.
*
Expand Down Expand Up @@ -229,6 +238,7 @@ public function getServiceHTMLTable()
[
get_lang('ServiceTitle'),
get_lang('ServiceURL'),
get_lang('RedirectAccessURL'),
],
null,
'th'
Expand All @@ -237,6 +247,7 @@ public function getServiceHTMLTable()
$table->addRow([
$service->getTitle(),
$service->getURL(),
$service->getAccessURL($this->get_name()),
]);
}
$html = $table->toHtml();
Expand Down Expand Up @@ -279,4 +290,20 @@ public function getIFrame()
)->getCustomUserURL($userInfo['username'], $userInfo['id'], $this->salt())
);
}

/**
* Generates the redirect user specific URL for redirection.
*
* @throws Exception on hash generation failure
*
* @return string the specific user redirect URL
*/
public function getActiveServiceSpecificUserUrl()
{
$userInfo = api_get_user_info();

return $this->getService(
$this->getActiveServiceId()
)->getCustomUserRedirectURL($userInfo['id'], $this->salt());
}
}

0 comments on commit 873a81b

Please sign in to comment.