2

I see that triggers page is changed from the version 6, It's pretty wierd.. I can't find When user logs in execute a php code thing...

What I have to do is exactly that.. control some user roles depending on his location (intranet or internet) I already have the php code developed but...can't find how to put it ....

$usr = user_load($uid);

$array_roles=array();
//supose i'm logging in from internet
    $details = array(
        'uid' => $uid,
        'roles' => array(
            21 => 'Internet' 
            )
        );

foreach($usr->roles as $key=>$value) 
{

 if ($key!=20 and $key!=21) // exclude internet or intranet roles
 {

  $details['roles'][$key]=$value;
 }
}

user_save($usr, $details);
2

The best way to execute your custom code after a user logs in is by implementing hook_user_login in a small custom module you build yourself. Turn that php you wrote into a function then call it.

i.e.

function module_name_user_login(&$edit, $account) {
  module_name_user_function($account->uid);
}

function module_name_user_function($uid) {
$usr = user_load($uid);

$array_roles=array();
//supose i'm logging in from internet
$details = array(
    'uid' => $uid,
    'roles' => array(
        21 => 'Internet' 
        )
    );
foreach($usr->roles as $key=>$value) {
  if ($key!=20 and $key!=21) {
  $details['roles'][$key]=$value;
  }
}
user_save($usr, $details);
}

More about custom modules, and modules all around can be found at The Module Developer's Guide

I would not recommend putting custom php in a trigger and action. Keeping custom code in the database is always dangerous for people coming down the line that have to maintain your site in the future.

  • But it can be easier for them to get rid of or change if it's in a trigger and action sort of setup, especially if that setup is defined inside of one module, not trigger and action seperately – BenjaminRH Oct 10 '11 at 21:25
  • Yes, but that means you're keeping functional code in your configuration, outside of any revision control. More times than not, it isn't documented that the code is kept in a rule, or trigger, or view, whatever.... which means that any developer hired to maintain the site after you will have to edit the code at some point and search through all of the maintained code to not find it. It's never "best practice" to keep unrevisioned code in your database. – LSU_JBob Oct 10 '11 at 21:28
  • 1
    Good point. Of course, if the goal is just to get it working, trigger/action is simpler, but in general I concede. bows – BenjaminRH Oct 10 '11 at 21:42
  • 1
    @BenjaminHarris Have a look at drupal.stackexchange.com/questions/2509/…. Also, you probably want to update your code to only call user_save() if you actually had to make changes to avoid executing a number of update queries every time a user logs in. – Berdir Oct 11 '11 at 5:52
1

What you want is the Rules module -- you can do this exact thing. Simply create a new rule, select "user login" as the condition, and add a new action "execute custom PHP".

  • this would help, @benjaminRH, with a few downfall: a) a critical feature of your work would stay away form your code, b) more complicated debug. As upside, rules is quick and easy to implement. – augusto Oct 2 '17 at 10:56

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.