Action, if a specific user is logged in

Permalink 1 user found helpful
Hi!
I'm familiar with the condition if a registered user is logged in:

<?php
$u = new User();
if($u->isLoggedIn()) {
// do this
}


But what if i want to have a condition if a specific user is logged in, e.g. the user with id=23. How would i do that?

Would it also be possible to create conditions for specific user groups?

Thank you,
Michael

okapi
 
enlil replied on at Permalink Reply
enlil
$u = uID();

Something like that. I'm not at my desk right now and can't remember it off hand...
okapi replied on at Permalink Reply
okapi
Thank you,

but where does the id of the user in my example go?
enlil replied on at Permalink Reply
enlil
just run another IF statement inside the first, like so...

<?php
$u = new User();
if($u->isLoggedIn()) {
   if($u == 1) {
      echo t("User ID is 1");
      }
   }
?>
JohntheFish replied on at Permalink Reply
JohntheFish
Just clarifying a difference between a user object and a user id

$u = new User(); // user object for current user
$uID = $u->getUserID(); // then from that get the user id for current user
enlil replied on at Permalink Best Answer Reply
enlil
Excellent, thank you john, In my haste writing and testing this from memory (finally getting this stuff!!), I only tested it against "1" for super user and didn't realize I missed that line...

this works perfect now!!

<?php
$u = new User();
$uID = $u->getUserID();
if($u->isLoggedIn()) {
   if($uID == 1) {
      echo t("User ID is 1");
   }
   if($uID == 2) {
      echo t("User ID is 2");
   }
}
?>
enlil replied on at Permalink Reply
enlil
And this will check if the user is in the "Administrators" group and then filter the personalized code by a set of user id's...

<?php
$u = new User();
$uID = $u->getUserID();
$groups = $u->getUserGroups();
$grp = Group::getByName('Administrators');
if($u->isLoggedIn()) {
   if(!$u->inGroup($grp)) {
      if($uID == 1) {
         echo t("Logged as User ID 1, administrator");
      }
      if($uID == 2) {
         echo t("Logged as User ID 2, administrator");
      }
   }
}
okapi replied on at Permalink Reply
okapi
Cool! Thank you so much!

Cheers,
Michael