updated login-destination, libraries

This commit is contained in:
Bachir Soussi Chiadmi
2016-11-05 17:05:09 +01:00
parent d1963312a6
commit 2b028cf376
20 changed files with 1347 additions and 299 deletions

View File

@@ -11,7 +11,6 @@
* Shows list of all login destination.
*/
function login_destination_overview_form($form, &$form_state) {
// Get all login destination rules from the database.
$result = db_select('login_destination', 'l')
->fields('l', array(
@@ -23,8 +22,7 @@ function login_destination_overview_form($form, &$form_state) {
'destination',
'weight',
'enabled',
)
)
))
->orderBy('weight')
->execute()
->fetchAll();
@@ -33,7 +31,6 @@ function login_destination_overview_form($form, &$form_state) {
// Loop through the categories and add them to the table.
foreach ($result as $data) {
$triggers = array_map('check_plain', unserialize($data->triggers));
if (empty($triggers)) {
$triggers = array();
@@ -46,7 +43,10 @@ function login_destination_overview_form($form, &$form_state) {
$form[$data->id]['destination']['#markup'] = theme('login_destination_destination', array('destination' => $data->destination));
$form[$data->id]['triggers']['#markup'] = theme('login_destination_triggers', array('items' => $triggers));
$form[$data->id]['pages']['#markup'] = theme('login_destination_pages', array('pages' => $data->pages, 'pages_type' => $data->pages_type));
$form[$data->id]['pages']['#markup'] = theme('login_destination_pages', array(
'pages' => $data->pages,
'pages_type' => $data->pages_type,
));
$form[$data->id]['roles']['#markup'] = theme('login_destination_roles', array('items' => $roles));
$form[$data->id]['weight'] = array(
@@ -77,9 +77,7 @@ function login_destination_overview_form($form, &$form_state) {
'#title' => t('delete'),
'#href' => 'admin/config/people/login-destination/delete/' . $data->id,
);
$form[$data->id]['operations'] = $operations;
}
if (element_children($form)) {
@@ -104,6 +102,8 @@ function login_destination_overview_form($form, &$form_state) {
* - form: A render element representing the form.
*
* @ingroup themeable
*
* @return string
*/
function theme_login_destination_overview_form($variables) {
$form = $variables['form'];
@@ -122,38 +122,42 @@ function theme_login_destination_overview_form($variables) {
$rows = array();
foreach (element_children($form) as $ldid) {
if (isset($form[$ldid]['enabled'])) {
$element = &$form[$ldid];
$operations = array();
foreach (element_children($element['operations']) as $op) {
$operations[] = array('data' => drupal_render($element['operations'][$op]), 'class' => array('login-destination-operations'));
}
while (count($operations) < 2) {
$operations[] = '';
}
$row = array();
$row[] = drupal_render($element['destination']);
$row[] = drupal_render($element['triggers']);
$row[] = drupal_render($element['pages']);
$row[] = drupal_render($element['roles']);
$row[] = array(
'data' => drupal_render($element['enabled']),
'class' => array(
'checkbox', 'login-destination-enabled',
),
);
$form[$ldid]['weight']['#attributes']['class'] = array('login-destination-weight');
$row[] = drupal_render($element['weight']);
$row = array_merge($row, $operations);
$row = array_merge(array('data' => $row), array());
$row['class'][] = 'draggable';
$rows[] = $row;
if (!isset($form[$ldid]['enabled'])) {
continue;
}
$element = &$form[$ldid];
$operations = array();
foreach (element_children($element['operations']) as $op) {
$operations[] = array(
'data' => drupal_render($element['operations'][$op]),
'class' => array('login-destination-operations'),
);
}
while (count($operations) < 2) {
$operations[] = '';
}
$row = array();
$row[] = drupal_render($element['destination']);
$row[] = drupal_render($element['triggers']);
$row[] = drupal_render($element['pages']);
$row[] = drupal_render($element['roles']);
$row[] = array(
'data' => drupal_render($element['enabled']),
'class' => array(
'checkbox',
'login-destination-enabled',
),
);
$form[$ldid]['weight']['#attributes']['class'] = array('login-destination-weight');
$row[] = drupal_render($element['weight']);
$row = array_merge($row, $operations);
$row = array_merge(array('data' => $row), array());
$row['class'][] = 'draggable';
$rows[] = $row;
}
$output = '';
@@ -175,7 +179,6 @@ function theme_login_destination_overview_form($variables) {
);
$output .= theme('table', $table_arguments);
$output .= drupal_render_children($form);
return $output;
@@ -220,15 +223,20 @@ function login_destination_overview_form_submit($form, &$form_state) {
*/
function _login_destination_update_rules($login_destination_rule) {
if (!(isset($login_destination_rule['enabled']) && isset($login_destination_rule['weight']) && isset($login_destination_rule['ldid']))) {
if (!(isset($login_destination_rule['enabled']) &&
isset($login_destination_rule['weight']) &&
isset($login_destination_rule['ldid']))
) {
return FALSE;
}
if ($login_destination_rule['enabled'] != 0 && $login_destination_rule['enabled'] != 1) {
if ($login_destination_rule['enabled'] != 0 &&
$login_destination_rule['enabled'] != 1
) {
return FALSE;
}
$login_destination_rule['weight'] = intval($login_destination_rule['weight']);
$login_destination_rule['weight'] = (int) $login_destination_rule['weight'];
if (!is_int($login_destination_rule['weight'])) {
return FALSE;
@@ -367,21 +375,20 @@ function login_destination_edit_form($form, &$form_state, array $rule = array())
LOGIN_DESTINATION_STATIC => t('Internal page or external URL'),
);
$description = t("Specify page by using its path. Example path is %blog for the blog page. %front is the front page. %current is the current page. Precede with http:// for an external URL. Leave empty to redirect to a default page.", array(
'%blog' => 'blog',
'%front' => '<front>',
'%current' => '<current>',
)
);
'%blog' => 'blog',
'%front' => '<front>',
'%current' => '<current>',
));
if (module_exists('php') && $access) {
if ($access && module_exists('php')) {
$options += array(LOGIN_DESTINATION_SNIPPET => t('Page returned by this PHP code (experts only)'));
$description .= ' ' . t('If the PHP option is chosen, enter PHP code between %php. It should return either a string value or an array of params that the %function function will understand, for example. %example. For more information, see the online API entry for <a href="@url">url function</a>. Note that executing incorrect PHP code can break your Drupal site.', array(
$description .= ' ' .
t('If the PHP option is chosen, enter PHP code between %php. It should return either a string value or an array of params that the %function function will understand, for example. %example. For more information, see the online API entry for <a href="@url">url function</a>. Note that executing incorrect PHP code can break your Drupal site.', array(
'%php' => '<?php ?>',
'%function' => 'url($path = \'\', array $options = array())',
'%example' => '<?php return array(\'blog\', array(\'fragment\' => \'overlay=admin/config\', ), ); ?>',
'@url' => 'http://api.drupal.org/api/drupal/includes--common.inc/function/url/7',
)
);
));
}
$form['destination_type'] = array(
@@ -405,7 +412,10 @@ function login_destination_edit_form($form, &$form_state, array $rule = array())
$form['triggers'] = array(
'#type' => 'checkboxes',
'#title' => t('Redirect upon triggers'),
'#options' => array('login' => t('Login, registration, one-time login link'), 'logout' => t('Logout')),
'#options' => array(
'login' => t('Login, registration, one-time login link'),
'logout' => t('Logout'),
),
'#default_value' => $triggers,
'#description' => t('Redirect only upon selected trigger(s). If you select no triggers, all of them will be used.'),
);
@@ -428,18 +438,18 @@ function login_destination_edit_form($form, &$form_state, array $rule = array())
LOGIN_DESTINATION_REDIRECT_LISTED => t('Only the listed pages'),
);
$description = t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page. %login is the login form. %register is the registration form. %reset is the one-time login (e-mail validation).", array(
'%blog' => 'blog',
'%blog-wildcard' => 'blog/*',
'%front' => '<front>',
'%login' => 'user',
'%register' => 'user/register',
'%reset' => 'user/*/edit',
)
);
'%blog' => 'blog',
'%blog-wildcard' => 'blog/*',
'%front' => '<front>',
'%login' => 'user',
'%register' => 'user/register',
'%reset' => 'user/*/edit',
));
if (module_exists('php') && $access) {
if ($access && module_exists('php')) {
$options += array(LOGIN_DESTINATION_REDIRECT_PHP => t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'));
$description .= ' ' . t('If the PHP option is chosen, enter PHP code between %php. Note that executing incorrect PHP code can break your Drupal site.', array('%php' => '<?php ?>'));
$description .= ' ' .
t('If the PHP option is chosen, enter PHP code between %php. Note that executing incorrect PHP code can break your Drupal site.', array('%php' => '<?php ?>'));
}
$form['pages_type'] = array(
@@ -489,16 +499,25 @@ function login_destination_edit_form($form, &$form_state, array $rule = array())
*/
function login_destination_edit_form_validate($form, &$form_state) {
$destination = $form_state['values']['destination'];
$destination_type = $form_state['values']['destination_type'];
$destination_type = $form_state['values']['destination_type'];
// Check user has enter any path.
if (!empty($destination) && $destination_type == 0) {
$destination = preg_replace("/\?.+/", "", $destination);
if (!drupal_valid_path($destination)) {
form_set_error('destination', t('Incorrect path, Please Enter valid Path'));
}
$available_urls = array('<current>', '<front>');
if (empty($destination) || $destination_type != 0 || in_array($destination, $available_urls)) {
return;
}
$destination = preg_replace("/\?.+/", "", $destination);
if (url_is_external($destination)) {
return;
}
// Get source path if an alias entered.
$source_path = drupal_lookup_path('source', $destination);
if (!empty($source_path)) {
$destination = $source_path;
}
if (!drupal_valid_path($destination)) {
form_set_error('destination', t('Incorrect path, please enter a valid path.'));
}
}
/**
@@ -529,14 +548,7 @@ function login_destination_delete_form($form, &$form_state, array $rule) {
'#value' => $rule,
);
return confirm_form(
$form,
t('Are you sure you want to delete the login destination %destination ?', array('%destination' => $rule['destination'])),
'admin/config/people/login-destination',
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
return confirm_form($form, t('Are you sure you want to delete the login destination %destination ?', array('%destination' => $rule['destination'])), 'admin/config/people/login-destination', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}
/**

View File

@@ -3,9 +3,9 @@ description = Customize the destination that the user is redirected to after log
core = 7.x
configure = admin/config/people/login-destination
; Information added by Drupal.org packaging script on 2015-05-11
version = "7.x-1.1+6-dev"
; Information added by Drupal.org packaging script on 2016-01-22
version = "7.x-1.4"
core = "7.x"
project = "login_destination"
datestamp = "1431374284"
datestamp = "1453451940"

View File

@@ -81,7 +81,11 @@ function login_destination_schema() {
function login_destination_install() {
// Update the alter option of 'user/logout' to TRUE,
// (menu_save invokes necessary hooks).
$result = db_query("SELECT mlid, menu_name FROM {menu_links} WHERE link_path = 'user/logout' OR link_path = 'user/login' OR link_path = 'user' ORDER BY mlid ASC");
$result = db_query("
SELECT mlid, menu_name
FROM {menu_links}
WHERE link_path = 'user/logout' OR link_path = 'user/login' OR link_path = 'user'
ORDER BY mlid ASC");
foreach ($result as $res) {
$item = menu_link_load($res->mlid);
$item['options']['alter'] = TRUE;
@@ -163,3 +167,30 @@ function login_destination_update_7001() {
db_add_field('login_destination', 'enabled', $spec);
}
/**
* Clear hooks cache.
*/
function login_destination_update_7002() {
cache_clear_all('hook_info', 'cache_bootstrap');
}
/**
* Automatically give all roles with permission "Administer Users" the new dedicated permission
* "Administer Login Destination settings".
*/
function login_destination_update_7003() {
drupal_set_message(t('The Login Destination module has just been updated.<br>
A new permission called "Administer Login Destination settings" has now been
added.<br>Previously the access to the Login Destination\'s settings page was
managed by the "Administer Users" permission.<br>That\'s why all roles with
that old permission have been just automatically given the new dedicated
"Administer Login Destination settings" permission.<br>If you want to
duoble-check things, you can go to the
<a href="/admin/people/permissions" title="Permissions page" >Permissions page</a> now.'));
$roles = user_roles(TRUE, 'administer users');
foreach ($roles as $rid => $role_name) {
user_role_grant_permissions($rid, array('administer login destination settings'));
}
}

View File

@@ -22,14 +22,29 @@ function login_destination_help($path, $arg) {
case 'admin/help#login_destination':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Login Destination module allows you to customize the destination that the user is redirected to after logging in, registering to the site, using a one-time login link or logging out. The destination can be an internal page or an external URL. You may specify certain conditions like pages or user roles and make the destination depend upon them. You may also use a PHP snippets to provide custom conditions and destinations. Note that PHP Filter module has to be enabled and you have to be granted the "Use PHP for settings" permissions to be able to enter PHP code.') . '</p>';
$output .= '<p>' .
t('The Login Destination module allows you to customize the destination that the user is redirected to after logging in, registering to the site, using a one-time login link or logging out. The destination can be an internal page or an external URL. You may specify certain conditions like pages or user roles and make the destination depend upon them. You may also use a PHP snippets to provide custom conditions and destinations. Note that PHP Filter module has to be enabled and you have to be granted the "Use PHP for settings" permissions to be able to enter PHP code.') .
'</p>';
return $output;
case 'admin/config/people/login-destination':
return '<p>' . t('Login destination rules are evaluated each time a user logs in, registers to the site, uses a one-time login link or logs out. Each rule consists of the destination, path conditions and user roles conditions. First matching rule gets executed.') . '</p>';
return '<p>' .
t('Login destination rules are evaluated each time a user logs in, registers to the site, uses a one-time login link or logs out. Each rule consists of the destination, path conditions and user roles conditions. First matching rule gets executed.') .
'</p>';
}
}
/**
* Implements hook_permission().
*/
function login_destination_permission() {
return array(
'administer login destination settings' => array(
'title' => t('Administer Login Destination settings'),
),
);
}
/**
* Implements hook_menu().
*/
@@ -39,7 +54,7 @@ function login_destination_menu() {
'description' => 'Customize the destination that the user is redirected to after login.',
'page callback' => 'drupal_get_form',
'page arguments' => array('login_destination_overview_form'),
'access arguments' => array('administer users'),
'access arguments' => array('administer login destination settings'),
'file' => 'login_destination.admin.inc',
'weight' => 10,
);
@@ -47,7 +62,7 @@ function login_destination_menu() {
'title' => 'Add login destination rule',
'page callback' => 'drupal_get_form',
'page arguments' => array('login_destination_edit_form'),
'access arguments' => array('administer users'),
'access arguments' => array('administer login destination settings'),
'type' => MENU_LOCAL_ACTION,
'weight' => 1,
'file' => 'login_destination.admin.inc',
@@ -56,14 +71,14 @@ function login_destination_menu() {
'title' => 'Edit login destination rule',
'page callback' => 'drupal_get_form',
'page arguments' => array('login_destination_edit_form', 5),
'access arguments' => array('administer users'),
'access arguments' => array('administer login destination settings'),
'file' => 'login_destination.admin.inc',
);
$items['admin/config/people/login-destination/delete/%login_destination'] = array(
'title' => 'Delete login destination rule',
'page callback' => 'drupal_get_form',
'page arguments' => array('login_destination_delete_form', 5),
'access arguments' => array('administer users'),
'access arguments' => array('administer login destination settings'),
'file' => 'login_destination.admin.inc',
);
$items['admin/config/people/login-destination/list'] = array(
@@ -76,7 +91,7 @@ function login_destination_menu() {
'description' => 'Change Login Destination settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('login_destination_settings'),
'access arguments' => array('administer users'),
'access arguments' => array('administer login destination settings'),
'type' => MENU_LOCAL_TASK,
'file' => 'login_destination.admin.inc',
'weight' => 10,
@@ -141,14 +156,14 @@ function login_destination_theme() {
*/
function login_destination_form_alter(&$form, &$form_state, $form_id) {
// We redirect by using the drupal_goto_alter hook. If we simply
// call drupal_goto() it may break compability with other modules. If we set
// call drupal_goto() it may break compatibility with other modules. If we set
// the $_GET['destination'] variable we will loose the possibility to redirect
// to an external URL.
// Please note the the system_goto_action() calls drupal_goto()
// More on this issue http://drupal.org/node/732542.
// If we add the $form_state['redirect'] here it will be overriden by the
// If we add the $form_state['redirect'] here it will be overridden by the
// user_login_submit(). So we add a submit handler instead and will set the
// redirect later. Our submit handler will be executed after the execution
// of user_login_submit(). This is because form_submit() functions are
@@ -203,7 +218,6 @@ function login_destination_validate($form, &$form_state) {
$_GET['current'] = $_GET['destination'];
}
}
}
/**
@@ -230,11 +244,16 @@ function login_destination_menu_link_alter(&$item) {
* Implements hook_translated_menu_link_alter().
*/
function login_destination_translated_menu_link_alter(&$item, $map) {
global $user;
$paths = array('user/login', 'user');
// Append the current path to URL.
if ($item['link_path'] == 'user/logout' || (in_array($item['link_path'], $paths) && user_is_anonymous())) {
$item['localized_options']['query'] = array('current' => $_GET['q']);
if ($item['link_path'] == 'user/logout' ||
(in_array($item['link_path'], $paths) && user_is_anonymous())
) {
$current = $_GET['q'];
if ($current == '<front>') {
$current = '';
}
$item['localized_options']['query'] = array('current' => $current);
}
}
@@ -244,7 +263,7 @@ function login_destination_translated_menu_link_alter(&$item, $map) {
function login_destination_page_alter(&$page) {
// Substitute toolbar's pre_render function to change links.
if (isset($page['page_top']['toolbar']['#pre_render'])) {
$page['page_top']['toolbar']['#pre_render'][0] = 'login_destination_toolbar_pre_render';
$page['page_top']['toolbar']['#pre_render'][] = 'login_destination_toolbar_pre_render';
}
}
@@ -252,9 +271,9 @@ function login_destination_page_alter(&$page) {
* Helper function to change toolbar's links.
*/
function login_destination_toolbar_pre_render($toolbar) {
$toolbar = toolbar_pre_render($toolbar);
// Add current param to be able to evaluate previous page.
$toolbar['toolbar_user']['#links']['logout']['query'] = array('current' => $_GET['q']);
return $toolbar;
}
@@ -262,7 +281,11 @@ function login_destination_toolbar_pre_render($toolbar) {
* Implements hook_user_login().
*/
function login_destination_user_login(&$edit, $account) {
if (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset' || variable_get('login_destination_immediate_redirect', FALSE)) {
$form_exception = 'user_pass_reset';
if (module_exists('change_pwd_page')) {
$form_exception = 'change_pwd_page_user_pass_reset';
}
if (!isset($_POST['form_id']) || $_POST['form_id'] != $form_exception || variable_get('login_destination_immediate_redirect', FALSE)) {
login_destination_perform_redirect('login');
}
}
@@ -291,23 +314,22 @@ function login_destination_user_logout($account) {
* Implements hook_drupal_goto_alter().
*/
function login_destination_drupal_goto_alter(&$path, &$options, &$http_response_code) {
// Note that this functionality cannot be backported do 6.x as Drupal 6 does
// Note that this functionality cannot be backported to 6.x as Drupal 6 does
// not call drupal_alter for drupal_goto.
// This actually may be used also by templates.
if (isset($GLOBALS['destination'])) {
$destination = $GLOBALS['destination'];
if (!isset($GLOBALS['destination'])) {
return;
}
$destination = $GLOBALS['destination'];
$path = $destination;
// Alter drupal_goto.
if (is_array($destination)) {
$path = $destination[0];
$options = array();
if (count($destination) > 1) {
$options = $destination[1];
}
}
else {
$path = $destination;
// Alter drupal_goto.
if (is_array($destination)) {
$path = $destination[0];
$options = array();
if (count($destination) > 1) {
$options = $destination[1];
}
}
}
@@ -317,7 +339,9 @@ function login_destination_drupal_goto_alter(&$path, &$options, &$http_response_
*/
function login_destination_prepare_goto($destination) {
// Check if $_GET['destination'] should overwrite us.
if (!isset($_GET['destination']) || !variable_get('login_destination_preserve_destination', FALSE)) {
if (!isset($_GET['destination']) ||
!variable_get('login_destination_preserve_destination', FALSE)
) {
$GLOBALS['destination'] = $destination;
}
}
@@ -357,14 +381,13 @@ function login_destination_get_destination($trigger = '', $current = NULL) {
'destination_type',
'destination',
'enabled',
)
)
))
->orderBy('weight')
->execute()
->fetchAll();
if ($current == NULL) {
$current = $_GET['q'];
if ($current === NULL) {
$current = _login_destination_get_current($trigger);
}
// Examine path matches.
@@ -402,6 +425,11 @@ function _login_destination_role_options() {
/**
* Get the current path (before trigger was invoked).
*
* @param string $trigger
* Trigger.
*
* @return string
*/
function _login_destination_get_current($trigger = '') {
if (isset($_GET['current'])) {