By krazykanuk on
I have a module I am trying to create that when a user registers it adds information to a second database. I have this part working. The part that is not working is adding new registers to a specific role (adding there uid and the rid to users_roles table). I tried moving this part that not working at start middle and end with same result. I have dsm() displaying the variables I am trying to write to the database to make sure it the desired information and it is the correct information is displayed.
function freeradius_user($op, &$edit, &$account, $catagory = NULL) {
global $user;
global $db_url;
// Export form data and create a new freeradius account
// Grab the password before it is encrypted so we can use SHA1
switch ($op) {
case 'insert':
$defaultop = variable_get('defaultop', 'defaultop');
$enctype = variable_get('enctype', 'enctype');
$usergroup = db_result(db_query("SELECT DISTINCT usergroup FROM {freeradius}"));
$freeradius_rid = db_result(db_query("SELECT DISTINCT rid FROM {role} WHERE name = '%s'", $usergroup));
foreach ($edit as $key => $value) {
switch ($key) {
case 'pass':
$password = $value;
break;
}
}
switch ($enctype) {
case 'Auth-Type':
break;
case 'Cleartext-Password':
$freeradiuspass = $password;
break;
case 'CHAP-Password':
$freeradiuspass = $password;
break;
case 'ENCRYPT-Password':
$freeradiuspass = shell_exec('ENCRYPT($password)');
break;
case 'MD5-Password':
$tmppass = $password;
$encrypted = md5($tmppass);
$freeradiuspass = $encrypted;
break;
case 'SHA1-Password':
$freeradiuspass = SHA1(strtoupper($account->name).':'.strtoupper($password));
break;
case 'User-Password':
$freeradiuspass = $password;
break;
default;
}
switch($defaultop) {
case '1':
$defaultop = '=';
break;
case '2':
$defaultop = ':=';
break;
case '3':
$defaultop = '==';
break;
case '4':
$defaultop = '+=';
break;
case '5':
$defaultop = '!=';
break;
case '6':
$defaultop = '>';
break;
case '7':
$defaultop = '>=';
break;
case '8':
$defaultop = '<';
break;
case '9':
$defaultop = '<=';
break;
case '10':
$defaultop = '=~';
break;
case '11':
$defaultop = '!~';
break;
case '12':
$defaultop = '=*';
break;
case '13':
$defaultop = '!*';
break;
default;
}
db_set_active('default');
dsm($account->uid);
dsm($freeradius_rid);
db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)",$account->uid, $freeradius_rid);
db_set_active('freeradius');
db_query("INSERT INTO {radcheck} (id, username, attribute, op, value) VALUES (%d, '%s', '%s', '%s', '%s')",$account->uid, $account->name, $enctype, $defaultop, $freeradiuspass);
db_query("INSERT INTO {radusergroup} (username, groupname, priority) VALUES ('%s', '%s', %d)",$account->name, $usergroup, 1);
db_set_active('default');
break;
}
}
Comments
My guess is that the user
My guess is that the user object is (quite obviously) loaded at the time of operation and saved afterwards, overwriting your changes.
Not sure what is "the most correct" way to do this, but I looked at how Auto Assign Role module does this, and come out with the following suggestion:
Instead of
db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)",$account->uid, $freeradius_rid);try
$edit['roles'][$freeradius_rid] = $usergroup;Disclaimer: the documentation for hook_user() contains a recommendation to the contrary – that modules should save their info themselves and "set the saved fields to NULL in $edit". There is some discussion about that though.
I was able to get it working
I was able to get it working with the values I required pretty close to your suggestion:
$edit['roles'][$freeradius_rid] = $roles[$freeradius_rid];