Add redirection plugin #2176 BT#13461

- Add configuration "plugin_redirection_enabled"
- Redirect a specific user id to a URL after login successful.
- Requires composer update
pull/2487/head
jmontoyaa 8 years ago
parent ea6a9db2ab
commit acb161458f
  1. 21
      main/inc/lib/redirect.class.php
  2. 2
      main/install/configuration.dist.php
  3. 8
      plugin/redirection/README.md
  4. 118
      plugin/redirection/RedirectionPlugin.php
  5. 1
      plugin/redirection/config.php
  6. 85
      plugin/redirection/index.php
  7. 10
      plugin/redirection/install.php
  8. 6
      plugin/redirection/lang/english.php
  9. 1
      plugin/redirection/lang/french.php
  10. 6
      plugin/redirection/lang/spanish.php
  11. 39
      plugin/redirection/lib/PluginRedirection.php
  12. 17
      plugin/redirection/plugin.php
  13. 2
      plugin/redirection/readme.txt
  14. 4
      plugin/redirection/uninstall.php

@ -71,6 +71,26 @@ class Redirect
(isset($_REQUEST['sso_referer']) && !empty($_REQUEST['sso_referer']))
) {
if (isset($user_id)) {
$allow = api_get_configuration_value('plugin_redirection_enabled');
if ($allow) {
// Check redirection plugin
$plugin = new AppPlugin();
$pluginList = $plugin->get_installed_plugins();
$redirectionInstalled = in_array('redirection', $pluginList);
if ($redirectionInstalled) {
$pluginInfo = $plugin->getPluginInfo('redirection');
if (!empty($pluginInfo) && isset($pluginInfo['obj'])) {
/** @var RedirectionPlugin $redirectionPlugin */
$redirectionPlugin = $pluginInfo['obj'];
$record = $redirectionPlugin->getUrlFromUser($user_id);
if (!empty($record) && !empty($record['url'])) {
header('Location: '.$record['url']);
exit;
}
}
}
}
// Make sure we use the appropriate role redirection in case one has been defined
$user_status = api_get_user_status($user_id);
switch ($user_status) {
@ -123,6 +143,7 @@ class Redirect
if (!empty($page_after_login)) {
self::navigate(api_get_path(WEB_PATH).$page_after_login);
}
}
}

@ -660,3 +660,5 @@ $_configuration['gradebook_badge_sidebar'] = [
// Hide email content forcing using to click in a link to visit the portal to check the message
//$_configuration['messages_hide_mail_content'] = false;
// If you install plugin redirection you need to change to true
//$_configuration['plugin_redirection_enabled'] = false;

@ -0,0 +1,8 @@
# chamilo-plugin-redirection
Plugin chamilo para la redirección de usuarios.
Requiere agregar:
<code>
$_configuration['plugin_redirection_enabled'] = true;
</code>

@ -0,0 +1,118 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Config the plugin
* @author Enrique Alcaraz Lopez
* @package chamilo.plugin.redirection
*/
class RedirectionPlugin extends Plugin
{
/**
* Class constructor
*/
protected function __construct()
{
$version = '1.0';
$author = 'Enrique Alcaraz, Julio Montoya';
parent::__construct($version, $author, ['enabled' => 'boolean']);
}
public static function create()
{
static $result = null;
return $result ? $result : $result = new self();
}
/**
* @param int $userId
* @param string $url
* @return false|string
*/
public static function insert($userId, $url)
{
$userId = (int) $userId;
if (empty($userId)) {
return false;
}
$sql = "DELETE FROM plugin_redirection WHERE user_id = $userId";
Database::query($sql);
$userInfo = api_get_user_info($userId);
if (empty($userInfo)) {
return false;
}
return Database::insert(
'plugin_redirection',
[
'user_id' => $userId,
'url' => $url,
]
);
}
/**
* @param $userId
* @return array
* @throws \Doctrine\DBAL\DBALException
*/
public static function getUrlFromUser($userId)
{
$userId = (int) $userId;
$userInfo = api_get_user_info($userId);
if (empty($userInfo)) {
return false;
}
$sql = "SELECT * FROM plugin_redirection WHERE user_id = $userId LIMIT 1";
$result = Database::query($sql);
return Database::fetch_array($result, 'ASSOC');
}
/**
* @param int $id
*/
public static function delete($id)
{
$table = Database::get_main_table('plugin_redirection');
Database::delete(
$table,
array('id = ?' => array($id))
);
}
/**
* @return array
*/
public static function getAll()
{
$table = Database::get_main_table('plugin_redirection');
return Database::select('*', $table);
}
public static function install()
{
$table = Database::get_main_table('plugin_redirection');
$sql = "CREATE TABLE IF NOT EXISTS $table (
id INT unsigned NOT NULL auto_increment PRIMARY KEY,
user_id INT unsigned NOT NULL DEFAULT 0,
url VARCHAR(255) NOT NULL DEFAULT ''
)";
Database::query($sql);
}
public static function uninstall()
{
$table = Database::get_main_table('plugin_redirection');
$sql = "DROP TABLE $table";
Database::query($sql);
}
}

@ -7,4 +7,3 @@
*/
require_once __DIR__.'/../../main/inc/global.inc.php';
require_once __DIR__.'/lib/PluginRedirection.php';

@ -1,56 +1,73 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Config the plugin
* @author Enrique Alcaraz Lopez
* @package chamilo.plugin.redirection
*/
* Config the plugin
* @author Enrique Alcaraz Lopez
* @package chamilo.plugin.redirection
*/
require_once __DIR__.'/config.php';
$redirecciones = PluginRedirection::get();
if (isset($_REQUEST["id"])) {
PluginRedirection::delete($_REQUEST["id"]);
header("Location: index.php");
exit();
} elseif (isset($_POST["submit_button"])) {
PluginRedirection::insert($_POST["user_id"], $_POST["url"]);
header("Location: index.php");
exit();
api_protect_admin_script();
$list = RedirectionPlugin::getAll();
if (isset($_REQUEST['id'])) {
RedirectionPlugin::delete($_REQUEST['id']);
Display::addFlash(Display::return_message(get_lang('Deleted')));
header('Location: index.php');
exit;
} elseif (isset($_POST['submit_button'])) {
$result = RedirectionPlugin::insert($_POST['user_id'], $_POST['url']);
if ($result) {
Display::addFlash(Display::return_message(get_lang('Added')));
} else {
Display::addFlash(Display::return_message(get_lang('Error'), 'warning'));
}
header('Location: index.php');
exit;
}
Display::display_header();
?>
<form action="./index.php" method="post">
<div class="table-responsive well">
<table class="table table-condensed">
<thead>
<td><input type="text" class="form-control" placeholder="User_id" name="user_id" /></td>
<td><input type="text" class="form-control" placeholder="url" name="url" /></td>
<td><input type='submit' value='Agregar' name="submit_button" class='btn btn-primary' /></td>
</thead>
</table>
</div>
<div class="table-responsive well">
<table class="table table-condensed">
<thead>
<td><input type="text" class="form-control" placeholder="User Id" name="user_id"/></td>
<td><input type="text" class="form-control" placeholder="URL" name="url"/></td>
<td><input type='submit' value='Add' name="submit_button" class='btn btn-primary'/></td>
</thead>
</table>
</div>
</form>
<div class="table-responsive">
<table class="table table-bordered table-condensed">
<div class="table-responsive">
<table class="table table-bordered table-condensed">
<tr>
<th>Usuario</th>
<th>Url</th>
<th>User</th>
<th>URL</th>
<th></th>
</tr>
</tr>
<?php
foreach ($redirecciones as $redi) {
foreach ($list as $item) {
$userInfo = api_get_user_info($item['user_id']);
$userName = get_lang('Unknown');
if (!empty($userInfo)) {
$userName = $userInfo['complete_name_with_username'].' - '.$item['user_id'];
}
echo '<tr>';
echo '<td>' . $redi["user_id"] . '</td>';
echo '<td>' . $redi["url"] . '</td>';
echo '<td><a href="index.php?id=' . $redi["id"] . '">Borrar</a></td>';
echo '<td>'.$userName.'</td>';
echo '<td>'.$item['url'].'</td>';
echo '<td><a class="btn btn-danger" href="index.php?id='.$item['id'].'">Delete</a></td>';
echo '</tr>';
}
?>
</table>
</div>
<?php
Display::display_footer();

@ -6,12 +6,4 @@
* @package chamilo.plugin.redirection
*/
$table = Database::get_main_table('plugin_redirection');
$sql = "CREATE TABLE IF NOT EXISTS $table (
id INT unsigned NOT NULL auto_increment PRIMARY KEY,
user_id INT unsigned NOT NULL DEFAULT 0,
url VARCHAR(255) NOT NULL DEFAULT ''
)";
Database::query($sql);
RedirectionPlugin::create()->install();

@ -1,7 +1,5 @@
<?php
/* For licensing terms, see /license.txt */
/**
* @author Enrique Alcaraz Lopez
* @package chamilo.plugin.redirection
*/
$strings['plugin_title'] = "Redirección personalizada";
$strings['plugin_comment'] = "Redirecciona a una url personalizada a un usuario en concreto";

@ -4,4 +4,3 @@
* @author Enrique Alcaraz Lopez
* @package chamilo.plugin.redirection
*/

@ -1,7 +1,5 @@
<?php
/* For licensing terms, see /license.txt */
/**
* @author Enrique Alcaraz Lopez
* @package chamilo.plugin.redirection
*/
$strings['plugin_title'] = "Redirección personalizada";
$strings['plugin_comment'] = "Redirecciona a una url personalizada a un usuario en concreto";

@ -1,39 +0,0 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Config the plugin
* @author Enrique Alcaraz Lopez
* @package chamilo.plugin.redirection
*/
class PluginRedirection
{
# Insertamos la redirección
public static function insert($user_id, $url)
{
return Database::insert(
'plugin_redirection',
[
'user_id' => $user_id,
'url' => $url
]
);
}
# Borramos al redirección
public static function delete($id)
{
$table = Database::get_main_table('plugin_redirection');
Database::delete(
$table,
array('id = ?' => array($id))
);
}
# Devolemos las redirecciones
public static function get()
{
$table = Database::get_main_table('plugin_redirection');
return Database::select('*', $table);
}
}

@ -1,24 +1,11 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Plugin
* @author Enrique Alcaraz Lopez
* @package chamilo.plugin.redirection
*/
require_once __DIR__.'/lib/redireccion.class.php';
/* Plugin config */
//the plugin title
$plugin_info['title'] = 'Redireccion personalizada';
//the comments that go with the plugin
$plugin_info['comment'] = "Redirecciona a una url personalizada a un usuario en concreto";
//the plugin version
$plugin_info['version'] = '1';
//the plugin author
$plugin_info['author'] = 'Enrique Alcaraz';
$form = new FormValidator('redirection_form');
$form->addElement('text', 'user_id', get_lang('user_id'));
$form->addElement('text', 'url', get_lang('url'));
$form->addButtonSave(get_lang('Save'), 'submit_button');
$plugin_info['settings_form'] = $form;
$plugin_info = RedirectionPlugin::create()->get_info();

@ -1,2 +0,0 @@
# chamilo-plugin-redirection
Plugin chamilo para la redirección de usuarios.

@ -4,4 +4,6 @@
* Uninstall the plugin
* @author Enrique Alcaraz Lopez
* @package chamilo.plugin.redirection
*/
*/
RedirectionPlugin::create()->uninstall();

Loading…
Cancel
Save