0

The current state of the site, is configured such that authenticated members, can post specific content types and comment on everything.

What was asked of me, was the restriction of users having tagged (taxonomy) themselves as "private" (their profiles aren't accessible), not being able to perform the above operations.

Apart from assigning a new role, on user update and having one more role to manage, is there another way to do that?

Ideally I'd like to assign to a "private" user the same rights as an anonymous user but without actually making him one, since I am already filtering for authenticated users in views and such.

  • I don't see any comment button, so I just have to post my 'answer'. How about adding a hidden field in the user-profile? But that will give you more work to-do than just simply adding a new role :) – user1973842 Mar 31 '13 at 23:26
1

Well, there is a way to do this, without the need to create an extra role. The code below should give you at least a hint on how to do it. You will need to create a custom module if you don't already have one and modify the function names and code where appropriate accordingly. Be warned though: the code is not tested :)

/**
 * Implements hook_user_load().
 */
function my_module_user_load($users) {
  foreach ($users as &$account) {
    // Check if the user's profile is private
    if (_my_module_user_profile_is_private($account)) {
      static $drupal_static_fast;
      if (!isset($drupal_static_fast)) {
        $drupal_static_fast['perm'] = &drupal_static('user_access');
      }
      $perm = &$drupal_static_fast['perm'];

      if (!isset($perm[$account->uid])) {
        // If you want, you can add here a new "virtual role"
        // $account->roles[ROLE_ID] = 'custom role';

        // Make a copy of the roles array, 
        // because we don't want to make changes on the user object
        $account_roles = $account->roles;

        // Remove the authenticated role, before querying all permissions
        if (isset($account_roles[DRUPAL_AUTHENTICATED_RID])) {
          unset($account_roles[DRUPAL_AUTHENTICATED_RID]);
        }

        // Get all permissions for user's roles
        $role_permissions = user_role_permissions($account_roles);

        $perms = array();
        foreach ($role_permissions as $one_role) {
          $perms += $one_role;
        }

        $perm[$account->uid] = $perms;
      }
    }
  }
}

/**
 * Helper function.
 *
 * Checks if a user's profile is private.
 */
function _my_module_user_profile_is_private($account = NULL) {
  if (!$account) {
    global $user;
    $account = $user;
  }
  // Check if the user's profile is private
  // ... 
  return TRUE; // just for testing
}
  • Seems legit, I'll try it out and return with a checkmark later. – magtak Apr 1 '13 at 10:05
  • Code has some logical errors but the gist is valid. Sadly, it does not meet my needs from the business logic point of view after all but it answers the original question so here you go. – magtak Apr 19 '13 at 10:26
  • Hmm after some further testing this does not seem to throw 403's wherever needed even with permissions manipulated accordingly :/ – magtak Apr 19 '13 at 13:13
  • If it's not a caching issue, then it must be the code. I'm afraid I can't be of more help without seeing the final code you came up with. – ovi Apr 19 '13 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.