Help with filtering groups and results #490
-
Hello, I am trying to write some code that will check if a user is in a group and based on that set a variable to a specific value. use LdapRecord\Models\ActiveDirectory\User;
$user = User::find('cn=John Doe,ou=Users,dc=local,dc=com');
// Get all groups the user is apart of that contain 'Accounting':
$groups = $user->groups()->whereContains('cn', 'Accounting')->get();
foreach ($groups as $group) {
if (stripos($group->getName(), 'accounting')) {
// for testing I echoed out "In group"
$variable = 'Accounting';
} else {
// for testing I echoed out "Not in group"
$variable = 'Not accounting';
}
} Using the above code, if I use my user account as a test and I search for a group that I know I am in, I will get the correct output of "In group" on screen. When I changed the value for whereContains so it will not find any groups, instead of getting the output of "Not in group" I just get a blank page. Part of my problem is I'm not really good with the object oriented aspects of PHP so any OO stuff I deal with is usually built into PHP or through libraries like this, I've never created my own classes and whatnot (so I don't actually always know the proper terms). Trying to do some troubleshooting, I tried the following to see if I could understand what was going on when a group wasn't being found (note this was put after the line from above where I did the "whereContains" portion): var_dump($groups); but I get the following output (which admittedly doesn't make much sense to me):
I then tried putting the following within the foreach statement to see what the value for $group was and was met with a blank screen. var_dump($group); How would I go about being able to basically say, if the group exists echo "In group" (really assigning a variable but for testing purposes that's quicker) else echo "Not in group", because what I'm trying doesn't work. Thanks for any help that can be given with this. -Shark2k |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi @shark2k,
This is because the result of the query is an empty collection, which will not be iterated over. For example, this script will not call $foo = [];
foreach ($foo as $bar) {
die('This will not be reached.');
} If you're looking to check whether a user has at least one group matching the query, you can instead use use LdapRecord\Models\ActiveDirectory\User;
$user = User::find('cn=John Doe,ou=Users,dc=local,dc=com');
if ($user->groups()->whereContains('cn', 'Accounting')->exists()) {
// The user is apart of the "Accounting" group.
} else {
// The user is *not* apart of the "Accounting" group.
} Or, if you want to retrieve the groups matching the result: use LdapRecord\Models\ActiveDirectory\User;
$user = User::find('cn=John Doe,ou=Users,dc=local,dc=com');
$groups = $user->groups()->whereContains('cn', 'Accounting')->get();
if ($groups->isNotEmpty()) {
// The user is apart of the "Accounting" group.
} else {
// The user is *not* apart of the "Accounting" group.
} Hope this helps! Let me know if you have further questions 👍 |
Beta Was this translation helpful? Give feedback.
Hi @shark2k,
This is because the result of the query is an empty collection, which will not be iterated over.
For example, this script will not call
die()
:If you're looking to check whether a user has at least one group matching the query, you can instead use
exists()
: