Because the roles name 'authenticated user' and 'anonymous' will be localized, so there use user_roles to get the roles, and compare with the settings, it caused a bug, I used this code to resolve it, please check.

# It uses platform neutral UTF-8 encoding and \n newlines.
--- 24060
+++ 24061
@@ -364,6 +364,17 @@
$account = $user;
}

+ /**
+ * fixed the bug
+ * when the anonymous or authenticated role name were translated, so the comparsion will be fail
+ */
+ foreach ($account->roles as $rid => $rname) {
+ if ( $rid == DRUPAL_ANONYMOUS_RID
+ ||$rid == DRUPAL_AUTHENTICATED_RID ) {
+ $account->roles[$rid] = t($rname);
+ }
+ }
+
foreach (user_roles(TRUE) as $role) {
switch (_autologout_local_settings($role)) {
case 0: // Enforce for all in this role
@@ -394,6 +405,17 @@
$account = $user;
}

+ /**
+ * fixed the bug
+ * when the anonymous or authenticated role name were translated, so the comparsion will be fail
+ */
+ foreach ($account->roles as $rid => $rname) {
+ if ( $rid == DRUPAL_ANONYMOUS_RID
+ || $rid == DRUPAL_AUTHENTICATED_RID ) {
+ $account->roles[$rid] = t($rname);
+ }
+ }
+
foreach (user_roles(TRUE) as $role) {
if (_autologout_local_settings($role) == 2 && in_array($role, array_values($account->roles))) {
return TRUE;

CommentFileSizeAuthor
#1 autologout.patch1.2 KBrobbin.zhao

Comments

robbin.zhao’s picture

StatusFileSize
new1.2 KB

The code was messed up, and I uploaded a patch file, please check.

robbin.zhao’s picture

By default, if a user has one role was excluded, so this user won't be logout automatically.
Replace the follow 2 functions and add 1 function if you want to set the user logout automatically if who has only one autologout role

function _autologout_exclude_by_role($account = NULL) {
  global $user;
  if ($account == NULL) {
    $account = $user;
  }

  /**
   * fixed the bug 
   * when the anonymous or authenticated role name were translated, so the comparsion will be fail
   */
  foreach ($account->roles as $rid => $rname) {
    if (  $rid == DRUPAL_ANONYMOUS_RID 
        ||$rid == DRUPAL_AUTHENTICATED_RID ) {
      $account->roles[$rid] = t($rname);
    }
  }
  
  foreach (user_roles(TRUE) as $role) {
    $r = _autologout_exclude_by_role_is($account, $role);
    if (FALSE === $r) {
      return FALSE;
    }
  }

  return TRUE;
}

function _autologout_exclude_by_role_is($account, $role) {
  switch (_autologout_local_settings($role)) {
    case 0: // Enforce for all in this role
      break;
    case 1: // Exclude all users in this role
      if (in_array($role, array_values($account->roles))) {
        return TRUE;
      }
      break;
    case 2: // Exclude user if user set to disable
      if (in_array($role, array_values($account->roles))) {
        if ($account->autologout == 1) {
          return TRUE;
        }
      }
      break;
  }
  
  return FALSE;
}

/* }}} */

/* {{{ _autologout_user_in_by_user_role() */
function _autologout_user_in_by_user_role($account = NULL) {
  global $user;
  if ($account == NULL) {
    $account = $user;
  }

 /**
  * fixed the bug 
  * when the anonymous or authenticated role name were translated, so the comparsion will be fail
  */
  foreach ($account->roles as $rid => $rname) {
    if (   $rid == DRUPAL_ANONYMOUS_RID 
        || $rid == DRUPAL_AUTHENTICATED_RID ) {
      $account->roles[$rid] = t($rname);
    }
  }
  
  foreach (user_roles(TRUE) as $role) {
    if (_autologout_local_settings($role) == 2 && in_array($role, array_values($account->roles))) {
      return TRUE;
    }
  }

  return FALSE;
}
johnennew’s picture

Status: Active » Closed (won't fix)

Closing old issue. The supported 6.x-4.x branch uses role rid so should not have this issue.