I am trying to write a module that will change a user's permissions once a node form is inserted into the database. I already have a role assigned when a user registers, but i want that role to be changed once they create a 'company' profile in this case which is when they fill out a cck form of 'company_post' type. My code is below...


<?php
	function role_change_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
		global $user;
		
  		switch ($op) {
      		case 'insert':
      		
      		if ($node->type == 'company_post') {
          
                }

?>

im not sure what to put in the if statement because I don't really know how to reference the users roles or how to change them.
?
So my question is what code can I use to change the users current role to a new role? (Both roles are already created in drupal and have seperate permissions)

Comments

ludo1960’s picture

Not sure if this helps, but one way to do it would be to use:
http://api.drupal.org/api/function/drupal_execute/6

marques2386’s picture

i dont see how that helps...where does it explain the role changing?

ludo1960’s picture

Isn't the role assigned on the user form? Example here http://drupal.org/node/115522

marques2386’s picture

im not really sure and im still new to drupal so clearly I am missing something...can you explain further what you mean?

ludo1960’s picture

Use drupal_execute() on the user form to change the role as in the links I sent

marques2386’s picture

that is for the default registration form though right? this is an additional form

ludo1960’s picture

Use whatever form that sets the role, I don't have drupal in front of me at the moment

marques2386’s picture

here's what i have now but its still not working...


<?php
	function role_change_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
		global $user;
		
  		switch ($op) {
      		case 'insert':
      		
      		if ($node->type == 'company_post') {
				global $user;

				$roles = $user->roles;
				$roles['Company'] = 'Company';
				user_save($user, $roles);
     	
			}
   		}
	}
?>

ludo1960’s picture

You need to get User_id and call whatever form has the roles on it then assign the new role and drupal_execute() will save the form, or use http://api.drupal.org/api/function/user_save/6

sagar ramgade’s picture

Cant we have rules module to do this job which is simple and less complicated.
http://www.drupal.org/project/rules
I am sure it can do this.

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form

dreadfulcode’s picture

Rules totally rules. Use rules to achieve this.

You do not need to create a module. This process can be done in 5 minutes with rules.

Here's how you do it:

Add a new rules trigger and name it "facebook registration event". Whatever.

Then choose

ON event User account has been created

add a conditional

"if user has roles"

be sure to select "registered user"

and choose authenticated role -- if your facebook connect app is working correctly, your users will already be authenticated. The authenticated role is excellent for the rules module, because it's a springboard for user registration events!

Now the condition is made. time to create an event.

Scroll down to "Do"

Select action "Add user role"

again, be sure to select "registered user"

and select the role you want to add.

Done.

Note: Do not unselect "authenticated user"

keep the user authenticated and permissions accordingly.

You add the new role to extend the permissions of the authenticated user.

And that's it.

Rules rules.

dreadfulcode’s picture

Whoops. Thought this was a facebook connect question-- too many windows open...

Anyhow, you would choose the event,

On event "Node is created", or "Field has changed" event...

Everything else the same, except the name of the rules trigger and event.

the conditions and actions are the same.