Code Snippets

use Illuminate\Support\Facades\Auth;
$name=Auth::user()->name;
dd($name);
use Illuminate\Support\Facades\Auth;
use LdapRecord\Models\ActiveDirectory\Group;
 
$user = Auth::user();
$group = Group::find('CN=IVRAdmin,CN=Users,DC=lab2,DC=purplepi,DC=ie');
 
if ($user->ldap->groups()->recursive()->exists($group)) {
	echo (($user->ldap->cn[0]) . " is a member of the group ") . $group -> cn[0];
}
else {
	echo (($user->ldap->cn[0]) . " is NOT a member of the group ") . $group -> cn[0];
}

For the above to work - you must have your User model configured as below - see here for more info.

// app/User.php
 
// ...
use LdapRecord\Laravel\Auth\HasLdapUser;
use LdapRecord\Laravel\Auth\AuthenticatesWithLdap;
use LdapRecord\Laravel\Auth\LdapAuthenticatable;
 
 
class User extends Authenticatable implements LdapAuthenticatable
{
    use Notifiable, AuthenticatesWithLdap, SoftDeletes;
    use HasLdapUser;
 
    // ...
}

Reference

If you need more robust customization of the response returned when a user is authenticated, Laravel provides an empty authenticated(Request $request, $user) method that may be overwritten if desired:

use Illuminate\Http\Request;
 
/**
 * The user has been authenticated.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  mixed  $user
 * @return mixed
 */
protected function authenticated(Request $request, $user)
{
    return response([
        //
    ]);
}
  • code/laravel/code-snippets.txt
  • Last modified: 2021/01/26 18:22
  • by gerardorourke