Index: js/lastuser.js =================================================================== --- js/lastuser.js (revision 0) +++ js/lastuser.js (revision 0) @@ -0,0 +1,33 @@ +/* + * Reads the user cookie and populates the login block with the + * first name of the last logged in user. If a username is found, + * add the question 'Not you?' to prompt for a login. + * + * Note that the user first name in the cookie is its second element + */ + +Drupal.behaviors.myModuleBehavior = function(context){ + var userdetails = readCookie("known_user_role_details"); + if (userdetails != null) { + // Note that "%7C" corresponds to "|" + // The cookie text is URI encoded, and needs to be decoded so that characters outside the + // standard English alphabet can be displayed. Spaces aer rendered as a '+' and these need + // to be changed in the text too. + var firstname = decodeURI(userdetails.split("%7C")[1]).replace('+', ' '); + $("strong.welcome").append(firstname); + $("strong.notyou").append("Not you?"); + } +} + +// Given the name of a cookie, return the associated value +function readCookie(name) { + var nameEQ = name + "="; + var ca = document.cookie.split(';'); + for(var i=0;i < ca.length;i++) { + var c = ca[i]; + while (c.charAt(0)==' ') c = c.substring(1,c.length); + if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); + } + return null; +} + Index: known_user_role.module =================================================================== --- known_user_role.module (revision 1431) +++ known_user_role.module (working copy) @@ -1,277 +1,312 @@ - 0, so this module - * will not work. Node module is fine though. - * - * @author: Greg Harvey - */ - -/** - * Implementation of hook_perm(). - */ -function known_user_role_perm() { - return array('administer known user role'); -} - -/** - * Implementation of hook_theme(). - */ -function known_user_role_theme() { - return array( - 'known_user_welcome' => array( - 'arguments' => array('known_user' => NULL), - ), - 'known_user_login_block' => array( - 'arguments' => array(), - ), - 'known_user_login_block_links' => array( - 'arguments' => array(), - ), - 'known_user_logged_in_block_links' => array( - 'arguments' => array(), - ), - ); -} - -/** - * Implementation of hook_boot(). - */ -function known_user_role_boot() { - global $user; - if (!$user->uid && (isset($_COOKIE['known_user_role_uid']) && $_COOKIE['known_user_role_uid'])) { - // load the known user role - $known_user_role = variable_get('known_user_role', array('1' => 'anonymous user')); - // if the user is known, set the role - if (!$user->roles[key($known_user_role)]) { - $user->roles[key($known_user_role)] = $known_user_role[key($known_user_role)]; - } - } -} - -/** - * Clears the cookie. - */ -function known_user_role_clear() { - setcookie('known_user_role_uid', '', time()-1000000, '/'); - cache_clear_all(); - drupal_goto('user'); -} - -/** - * Loads the user object of the known user to access other info, - * e.g. profile data - * - * @return - * User object (if available) or username set to 'Guest' - */ -function known_user_role_load_user() { - global $user; - if ($user->uid) { - // load profile data - $known_user = user_load($user->uid); - } - elseif (isset($_COOKIE['known_user_role_uid']) && $_COOKIE['known_user_role_uid']) { - $known_user = user_load($_COOKIE['known_user_role_uid']); - } - else { - $known_user->name = t('Guest'); - } - return $known_user; -} - -/** - * Implementation of hook_block(). - */ -function known_user_role_block($op = 'list', $delta = 0, $edit = array()) { - global $user; - if ($op == 'list') { - $blocks[0]['info'] = t('Known user login'); - $blocks[0]['cache'] = BLOCK_NO_CACHE; - return $blocks; - } - else if ($op == 'view') { - $block = array(); - switch ($delta) { - case 0: - //if the cookie is there or we're logged in, we need to show the welcome message - if ($user->uid || (isset($_COOKIE['known_user_role_uid']) && $_COOKIE['known_user_role_uid'])) { - $block['subject'] = 'Welcome'; - //see theme_known_user_welcome() - //developer can override the welcome message to known users - $block['content'] = theme('known_user_welcome', known_user_role_load_user()); - //see theme_known_user_logged_in_block_links() - //developer can provide alternative/additional links in the block - $block['links'] = theme('known_user_logged_in_block_links'); - } - elseif ((!$user->uid || $user->uid == 0) && !(arg(0) == 'user' && !is_numeric(arg(1)))) { - $block['subject'] = t('User login'); - //see theme_known_user_login_block() - //developer can use alternative module login block e.g. LoginToboggan - $block['content'] = theme('known_user_login_block'); - //see theme_known_user_login_block_links() - //developer can provide alternative/additional links in the block - $block['links'] = theme('known_user_login_block_links'); - } - return $block; - break; - } - } -} - -/** - * welcome message presented in a theme function so it can - * be easily overridden - * - * @param $known_user - * Object: standard Drupal user object from a user_load() - * - * @return - * HTML string - */ -function theme_known_user_welcome($known_user) { - global $user; - - $output = '
'.t('Hello %user.', array('%user' => $known_user->name)).'
'; - if (!$user->uid || $user->uid == 0) { - $output .= '
'.l('Not you? Click here to login.', 'known-user/reset').'
'; - } else { - $output .= l(t('Log out'), 'logout'); - } - return $output; -} - -/** - * login block form presented in a theme function so it can - * be easily overridden - * - * @return - * HTML string - */ -function theme_known_user_login_block() { - //the standard user login block from the user module by default: - return drupal_get_form('user_login_block'); -} - -/** - * optional login block form links presented in a theme function - * so they can be easily overridden - empty by default because - * default user login block has links incorporated in the form - * - * @return - * HTML string - */ -function theme_known_user_login_block_links() { - $items = array(); - return $items; -} - -/** - * optional LOGGED IN block form links presented in a theme function - * so they can be easily overridden - empty by default because - * default user login block has links incorporated in the form - * - * @return - * HTML string - */ -function theme_known_user_logged_in_block_links() { - $items = array(); - return $items; -} - -/** - * Implementation of hook_menu(). - */ -function known_user_role_menu() { - $items = array(); - $items['admin/user/known-user-role'] = array( - 'title' => 'Known user role', - 'description' => 'Select the role that known users receive.', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('known_user_role_select_role_form'), - 'access arguments' => array('administer known user role'), - ); - $items['known-user/reset'] = array( - 'title' => 'Reset known user cookie', - 'page callback' => 'known_user_role_clear', - 'access arguments' => array('access content'), - 'type' => MENU_CALLBACK, - ); - return $items; -} - -/** - * Admin form for module allowing users to select the role - * to be used for known users. - */ -function known_user_role_select_role_form(&$form_state) { - $form = array(); - $known_user_role = variable_get('known_user_role', array('1' => 'anonymous user')); - $form['role'] = array( - '#type' => 'select', - '#title' => t('Available roles'), - '#default_value' => key($known_user_role), - '#description' => t('Select a role to be used by known users, prior to logging in.'), - ); - foreach (user_roles() as $rid => $role) { - $form['role']['#options'][$rid] = $role; - } - $form['submit'] = array( - '#type' => 'submit', - '#value' => 'Save', - ); - return $form; -} - -/** - * Submit function saving selected known role in to a variable - * for use in hook_boot(). - */ -function known_user_role_select_role_form_submit($form, &$form_state) { - $roles = user_roles(); - variable_set('known_user_role', array($form_state['values']['role'] => $roles[$form_state['values']['role']])); - drupal_set_message(t('Settings saved.')); -} - -/** - * Implementation of hook_user(). - * - * Sets user cookie on login, so we remember this user. - * Has the handy advantage of over-writing the last cookie, if someone - * else was here first. - */ -function known_user_role_user($op, &$edit, &$account, $category = NULL) { - switch ($op) { - case 'login': - //set cookie, expires after 30 days - setcookie('known_user_role_uid', $account->uid, time()+2592000, '/'); - cache_clear_all(); - break; - } -} - -/** - * Implementation of hook_form_alter(). - * - * This function ensures that "known" users get recognised when they - * submit comments and nodes. - */ -function known_user_role_form_alter(&$form, $form_state, $form_id) { - switch ($form_id) { - case 'comment_form': - if (isset($_COOKIE['known_user_role_uid']) && $_COOKIE['known_user_role_uid']) { - $form['uid']['#value'] = $_COOKIE['known_user_role_uid']; - } - break; - } - - if ($form['#id'] == 'node-form' && (isset($_COOKIE['known_user_role_uid']) && $_COOKIE['known_user_role_uid'])) { - $form['uid']['#value'] = $_COOKIE['known_user_role_uid']; - } -} + 0, so this module + * will not work. Node module is fine though. + * + * @author: Greg Harvey + */ + +/** + * Implementation of hook_perm(). + */ +function known_user_role_perm() { + return array('administer known user role'); +} + +/** + * Implementation of hook_theme(). + */ +function known_user_role_theme() { + return array( + 'known_user_welcome' => array( + 'arguments' => array('known_user' => NULL), + ), + 'known_user_login_block' => array( + 'arguments' => array(), + ), + 'known_user_login_block_links' => array( + 'arguments' => array(), + ), + 'known_user_logged_in_block_links' => array( + 'arguments' => array(), + ), + ); +} + +/** + * Implementation of hook_boot(). + */ +function known_user_role_boot() { + global $user; + if (!$user->uid && (isset($_COOKIE['known_user_role_details']) && $_COOKIE['known_user_role_details'])) { + // load the known user role + $known_user_role = variable_get('known_user_role', array('1' => 'anonymous user')); + // if the user is known, set the role + //if (!$user->roles[key($known_user_role)]) { + $user->roles[key($known_user_role)] = $known_user_role[key($known_user_role)]; + //} + } +} + +/** + * Clear the cookies. + */ +function known_user_role_clear() { + $cookiedomain = variable_get('known_user_role_cookie_domain',''); + if($cookiedomain!='') + setcookie('known_user_role_details', '', time()-1000000, '/', $cookiedomain); + else + setcookie('known_user_role_details', '', time()-1000000, '/'); + cache_clear_all(); + drupal_goto('user'); +} + +/** + * Loads the user object of the known user to access other info, + * e.g. profile data + * + * @return + * User object (if available) or username set to 'Guest' + */ +function known_user_role_load_user() { + global $user; + if ($user->uid) { + // load profile data + $known_user = user_load($user->uid); + } + elseif (isset($_COOKIE['known_user_role_details']) && $_COOKIE['known_user_role_details']) { + $known_user = user_load(explode('|',$_COOKIE['known_user_role_details'],1)); + } + else { + $known_user->name = t('Guest'); + } + return $known_user; +} + +/** + * Implementation of hook_block(). + */ +function known_user_role_block($op = 'list', $delta = 0, $edit = array()) { + global $user; + + if ($op == 'list') { + $blocks[0]['info'] = t('Known user login'); + $blocks[0]['cache'] = BLOCK_NO_CACHE; + return $blocks; + } + else if ($op == 'view') { + $block = array(); + switch ($delta) { + case 0: + $base = drupal_get_path('module', 'known_user_role'); + drupal_add_js($base . '/js/lastuser.js'); + //if the cookie is there or we're logged in, we need to show the welcome message + if ($user->uid || (isset($_COOKIE['known_user_role_details']) && $_COOKIE['known_user_role_details'])) { + $block['subject'] = 'Welcome'; + //see theme_known_user_welcome() + //developer can override the welcome message to known users + $block['content'] = theme('known_user_welcome', known_user_role_load_user()); + //see theme_known_user_logged_in_block_links() + //developer can provide alternative/additional links in the block + $block['links'] = theme('known_user_logged_in_block_links'); + } + elseif ((!$user->uid || $user->uid == 0) && !(arg(0) == 'user' && !is_numeric(arg(1)))) { + $block['subject'] = t('User login'); + //see theme_known_user_login_block() + //developer can use alternative module login block e.g. LoginToboggan + $block['content'] = theme('known_user_login_block'); + //see theme_known_user_login_block_links() + //developer can provide alternative/additional links in the block + $block['links'] = theme('known_user_login_block_links'); + } + return $block; + break; + } + } +} + +/** + * welcome message presented in a theme function so it can + * be easily overridden + * + * @param $known_user + * Object: standard Drupal user object from a user_load() + * + * @return + * HTML string + */ +function theme_known_user_welcome($known_user) { + global $user; + + $output = '
'.t('Hello %user.', array('%user' => $known_user->name)).'
'; + if (!$user->uid || $user->uid == 0) { + $output .= '
'.l('Not you? Click here to login.', 'known-user/reset').'
'; + } else { + $output .= l(t('Log out'), 'logout'); + } + return $output; +} + +/** + * login block form presented in a theme function so it can + * be easily overridden + * + * @return + * HTML string + */ +function theme_known_user_login_block() { + //the standard user login block from the user module by default: + return drupal_get_form('user_login_block'); +} + +/** + * optional login block form links presented in a theme function + * so they can be easily overridden - empty by default because + * default user login block has links incorporated in the form + * + * @return + * HTML string + */ +function theme_known_user_login_block_links() { + $items = array(); + return $items; +} + +/** + * optional LOGGED IN block form links presented in a theme function + * so they can be easily overridden - empty by default because + * default user login block has links incorporated in the form + * + * @return + * HTML string + */ +function theme_known_user_logged_in_block_links() { + $items = array(); + return $items; +} + +/** + * Implementation of hook_menu(). + */ +function known_user_role_menu() { + $items = array(); + $items['admin/user/known-user-role'] = array( + 'title' => 'Known user role', + 'description' => 'Select the role that known users receive.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('known_user_role_select_role_form'), + 'access arguments' => array('administer known user role'), + ); + $items['known-user/reset'] = array( + 'title' => 'Reset known user cookie', + 'page callback' => 'known_user_role_clear', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + return $items; +} + +/** + * Admin form for module allowing users to select the role + * to be used for known users. + */ +function known_user_role_select_role_form(&$form_state) { + $form = array(); + $known_user_role = variable_get('known_user_role', array('1' => 'anonymous user')); + $form['role'] = array( + '#type' => 'select', + '#title' => t('Available roles'), + '#default_value' => key($known_user_role), + '#description' => t('Select a role to be used by known users, prior to logging in.'), + ); + foreach (user_roles() as $rid => $role) { + $form['role']['#options'][$rid] = $role; + } + $form['cookie_domain'] = array( + '#type' => 'textfield', + '#title' => t('Known User Cookie Domain'), + '#description' => t('Ensure that this text is used across all sites. The domains to which the cookie applies.'. + 'The usual format will be ".domain.com", and this will cover xxx.domain.com, as well as domain.com.' . + 'Note that there could be security issues if the domain is too broad. .com or .www will allow the cookie '. + 'to be accessed from nearly every domain.'), + '#default_value' => variable_get('known_user_role_cookie_domain',''), + '#size' => 100, + '#required' => TRUE, + ); + $lifetimeoptions = array(7,10,30,365); + $form['cookie_lifetime'] = array( + '#type' => 'select', + '#title' => t('Known User Cookie Lifetime (days)'), + '#options' => $lifetimeoptions, + '#default_value' => variable_get('known_user_role_cookie_lifetime',2), + '#description' => t('Select the lifetime in days for the known user cookie.'), + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Save', + ); + return $form; +} + +/** + * Submit function saving selected known role in to a variable + * for use in hook_boot(). + */ +function known_user_role_select_role_form_submit($form, &$form_state) { + $roles = user_roles(); + variable_set('known_user_role', array($form_state['values']['role'] => $roles[$form_state['values']['role']])); + variable_set('known_user_role_cookie_domain',$form_state['values']['cookie_domain']); + variable_set('known_user_role_cookie_lifetime', trim($form_state['values']['cookie_lifetime'])); + drupal_set_message(t('Settings saved.')); +} + +/** + * Implementation of hook_user(). + * + * Sets user cookie on login, so we remember this user. + * Has the handy advantage of over-writing the last cookie, if someone + * else was here first. + */ +function known_user_role_user($op, &$edit, &$account, $category = NULL) { + switch ($op) { + case 'login': + // Create the login cookie, setting its lifetime in seconds + // (This could be cleaned up a little) + $lifetimeoptions = array(7,10,30,365); + $cookielifetime = (60*60*24) * $lifetimeoptions[variable_get('known_user_role_cookie_lifetime',2)]; + $cookiedomain = variable_get('known_user_role_cookie_domain',''); + if($cookiedomain!='') + setcookie('known_user_role_details', $account->uid.'|'.$account->profile_first_name, time() + $cookielifetime, '/', $cookiedomain); + else + setcookie('known_user_role_details', $account->uid.'|'.$account->profile_first_name, time() + $cookielifetime, '/'); + cache_clear_all(); + break; + } +} + +/** + * Implementation of hook_form_alter(). + * + * This function ensures that "known" users get recognised when they + * submit comments and nodes. + */ +function known_user_role_form_alter(&$form, $form_state, $form_id) { + switch ($form_id) { + case 'comment_form': + if (isset($_COOKIE['known_user_role_details']) && $_COOKIE['known_user_role_details']) { + $form['uid']['#value'] = explode('|',$_COOKIE['known_user_role_details'],1); + } + break; + } + + if ($form['#id'] == 'node-form' && (isset($_COOKIE['known_user_role_details']) && $_COOKIE['known_user_role_details'])) { + $form['uid']['#value'] = explode('|',$_COOKIE['known_user_role_details'],1); + } +} Index: README.txt =================================================================== --- README.txt (revision 1431) +++ README.txt (working copy) @@ -14,8 +14,15 @@ Known user role) ... it is advised you create a role specifically for the purpose, if required, and use with care! Default is Anonymous (e.g. for safety it does nothing except a fancy welcome message if you do not -set a role). +set a role). +Apply the know user cookie settings in the same path (User management -> +Known user role). Here, the cookie lifetime can be set, and the domain +that is applied to the cookie can be set here too. '.mysite.com' is the +format that is required, and allows known user cookies to be shared between +'part1.mysite.com' and 'part2.mysite.com' for example. Note that this cookie +domain needs to be consistent across all sites. + In the block admin page (Site building -> Blocks) enable the "Known user login" block in the region you usually show your login block. This block has three states: @@ -97,3 +104,40 @@ } } + +Note, The 'known_user_role_details' cookie is created by this module when +a user logs in, and it will persist for a month, storing the user's id and +firstname. With Caching turned on in Drupal, there can be problems with the +names being displayed in the login block, and the problem is fixed by using +a script to apply the last login name to the home page. For the script in +this module to work, certain elements need to be applied to the theme +template.php file. Namely '' and ''. + +For example... +/** + * override the welcome message provided by the known_user_role + * module here + */ +function phptemplate_known_user_welcome($known_user) { + global $user; + + $output =''; + + $output .= '
'; + $output .= ''; + $output .= t('Welcome '); + + if (!$user->uid || $user->uid == 0) { + $output .= '
'; + $output .= ' '; + $output .= l('Click here to login.', 'known-user/reset'); + $output .= '
'; + } else { + $output .= ''; + $output .= '
'; + $output .= l(''.t('Logout').'', 'logout',array('attributes' => array('class' => t('logout-btn'), 'title' => t('Logout')), 'html' => TRUE)); + $output .= '
'; + } + $output .= '
'; + return $output; +}