0
votes

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...

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)

3
update to the code...here is the direction i went but its still not working! <code> <?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); } } } ?> </code> - Marques

3 Answers

1
votes

To delete actualy there are 3 queries:

db_query('DELETE FROM {role} WHERE rid = %d', $form_state['values']['rid']);

db_query('DELETE FROM {permission} WHERE rid = %d', $form_state['values']['rid']);

// Update the users who have this role set:

db_query('DELETE FROM {users_roles} WHERE rid = %d', $form_state['values']['rid']);
0
votes

You can't make generic code, as the what Drupal use is the role id (rid) of a role, which is a serial.

A users roles, is in the user_roles table containing uid and rid, so to remove a role you would do:

global $user;
$rid = x; // x = the id of the role to remove
db_query("DELETE FROM {user_roles} WHERE uid = %d AND rid = %d", $user->uid, $rid);

To give the new role you could do:

$record = array(
  'uid' => $user->uid,
  'rid' => y, // y = the id of the role to give.
);
drupal_write_record('user_roles', $record);
0
votes

There's a much better way to accomplish this...
In response to your edit, if you use the global $user object, then whoever updates the node next time will have the roles applied to them also (if you decided to add case 'update': to the function or whatever).
Instead, since the node will have $node->uid assigned to it, use that.

<?php
function role_change_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { 
 switch ($op) {
  case 'insert':
  if ($node->type == 'company_post') {
   // Assuming the role id you want to assign is "3"...
   $role_id = 3;
   $role_name = db_result(db_query('SELECT name FROM {role} WHERE rid = %d',$role_id));
   $author = user_load(array('uid' => $node->uid));
   if ($author !== FALSE && !isset($author->roles[$role_id])) {
    $new_roles_combined_with_old_ones = $author->roles + array($role_id => $role_name);
    user_save($author, array('roles' => $new_roles_combined_with_old_ones));
   }
  }
  break;
 }
}
?>

Or, if you want to accomplish the same thing in a situation that may not have a $node->uid, global $user will work fine too. But remember that when you use global $user, the changes will be applied to the account of the person who is currently logged in and completing whatever the action may be.

<?php
global $user;
// Assuming the role id you want to assign is "5"...
$role_id = 5;
$role_name = db_result(db_query('SELECT name FROM {role} WHERE rid = %d', $role_id));
// Make sure user is valid, has a uid, and does't already have role 5 assigned to them.
if ($user !== FALSE && $user->uid && !isset($user->roles[$role_id])) {
  $new_roles_combined_with_old_ones = $user->roles + array($role_id => $role_name);
  user_save($user, array('roles' => $new_roles_combined_with_old_ones));
}
?>

Voila. I'm here all night. ;)