Support the ongoing development of Laravel.io →
Authentication Database Architecture
Last updated 2 years ago.
0

Hello!

Here's how I did it:

  1. First we need to overwrite getFresh() method of the Illuminate\Database\Query\Builder class. So, I created the following class:
<?php namespace App\Database; // Or use any namespace you think suits you better

use Illuminate\Database\Query\Builder as QueryBuilder;

class LdapQueryBuilder extends QueryBuilder {
	
	public function getFresh($columns = array())
	{
		$users = parent::getFresh($columns);

		foreach($users as &$user)
		{
			// Provide your logic here to fetch data from both sources (DB and LDAP) and add to $user.
			$user->otherField = $otherValue;
		}

		return $users;
	}
}
  1. Overwrite Illuminate\Database\Eloquent\Model::newBaseQueryBuilder() in your model so we can use our custom query builder:
<?php namespace App\Models;

use ...; // Your use statements.
use App\Database\LdapQueryBuilder;

class User extends ... implements ... { // Your extends and implements statements.

	...

	protected function newBaseQueryBuilder()
	{
		$conn = $this->getConnection();

		$grammar = $conn->getQueryGrammar();

		return new LdapQueryBuilder($conn, $grammar, $conn->getPostProcessor());
	}

}

Now, everytime your model returns any result (including in Auth::user()), your returned model will contain attributes from LDAP and from DB, according the logic implemented in your custom query builder.

PS.: I did not tested this code thoroughly, but I didn't had any problems so far.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.