9

I'm trying to add a user role to a user who has become a member of a group. The workflow is like this:

  • User requests to join group.
  • Request is approved by group administrator.
  • User is a member of the group.
  • User gets an additional role.

I've tried to make a rule which would be able to do this, but although all the settings seem correct after joining the group the user doesn't get the additional role. See below a screenshot of my rule settings:

Screenshot of my rule

  • I have exactly the same issue, so started a bounty on it. – Green Black Dec 13 '12 at 21:58
  • @John Have you solved your problems? – kalabro Dec 15 '12 at 18:55
2

Now your Rule runs after user requests membership. But you should run it after OG membership was approved.
I prepared Rule which adds role “member” after membership was approved or if it was added by admin (auto-approved). Here is export (4 is id of my drupal role “member”):

{ "rules_after_add_user_to_group" : {
    "LABEL" : "After add user to group",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "test" ],
    "REQUIRES" : [ "rules", "og" ],
    "ON" : [ "og_user_insert", "og_user_approved" ],
    "IF" : [ { "data_is" : { "data" : [ "og-membership:state" ], "value" : "1" } } ],
    "DO" : [
      { "user_add_role" : { "account" : [ "account" ], "roles" : { "value" : { "4" : "4" } } } }
    ]
  }
}

Screenshot: enter image description here

Tip: you can enable Rules debug (page admin/config/workflow/rules/settings) to see when your rules are executed.

  • Hi Kalabro. Thank you for your awnser! I set it up that way, but the user does not get the role. I was able to fix it by adding a function to a custom module and add the role using custom php code. Ill post the dirty solution here. – Green Black Dec 17 '12 at 22:16
  • PS. I asked my question here, more detailed than this question: drupal.org/node/1866278 If you debug $account, you see the role is added, but simply not saved, or overwritten by something. – Green Black Dec 17 '12 at 22:31
  • @John I have commented your question on Drupal.org. Please, look at this: #1 – kalabro Dec 19 '12 at 17:38
2

I have the same issue (that why I started a bounty).

I solved it this way.

I added a function to a custom module. You can simple create your own module by making a new folder in /sites/all/modules with two files:

yourname_module.info ->>

name = "Custom Functions"
description = "Allows execution of custom code for the website"
core = 7.x
package = "yourname_customs"
version = 7.x-1.x

yourname.module

<?php 
function yourname_add_role_to_user($uid, $role_name) {

    $user = user_load($uid);

    if ($user === false || !isset($user->uid) || !is_array($user->roles)) {
        //Display an ugly error when user is not set correctly
        exit('$user is not set correctly <pre>' . print_r($user, true) . "</pre>");
    }

    //Get the user roles
    $roles = user_roles(TRUE);
    $rid = array_search($role_name, $roles);

    if ($rid != FALSE) {
        $new_role[$rid] = $role_name;

        // Add new role to existing roles.
        $all_roles = $user->roles + $new_role;

        //Delete all user roles from DB
        db_delete('users_roles')
                ->condition('uid', $user->uid)
                ->execute();

        //Insert all user roles in DB
        $query = db_insert('users_roles')->fields(array('uid', 'rid'));
        foreach (array_keys($all_roles) as $rid) {
            if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
                $query->values(array(
                    'uid' => $user->uid,
                    'rid' => $rid,
                ));
            }
        }
        $query->execute();
    } else {

        //Display an ugly error wen role not found
        exit("Could not find role " . htmlspecialchars($role_name) . "<br/>
              Vald roles: <pre>" . print_r($roles, true) . "</pre>");
    }
}

Then goto your modules and enable "Custom Functions".

Make sure you have module custom php code enabled.

Then instead of action add user to role in rules, add: run custom php code and enter:

yourname_add_role_to_user($account->uid, "Members");
header("Location: /admin/people");
exit;

This adds the user to the role, and stops the script. If you don't stop the script, the role is not saved. And I needed to add a module because user_save does not work when executed in custom php code.

So, I know it is very ugly, but it works for me.

0

Which version of OG do you run ?

This is a know bug that have been fixed with Organic Group 2.4+ (see bug page).

If you are already running a 2.3 or lower version of OG the updgrate should be straightforward. If you are running OG 1.x, you can find more details on the update process from OG 1.5 to 2.4+

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.