Skip to content

Commit

Permalink
Extracted importing for ability to use any model
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Apr 4, 2020
1 parent 4a655c7 commit fe9d707
Show file tree
Hide file tree
Showing 14 changed files with 622 additions and 441 deletions.
66 changes: 3 additions & 63 deletions src/Auth/AuthenticatesWithLdap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,9 @@

namespace LdapRecord\Laravel\Auth;

use LdapRecord\Laravel\ImportableFromLdap;

trait AuthenticatesWithLdap
{
/**
* Get the database column name of the domain.
*
* @return string
*/
public function getLdapDomainColumn()
{
return 'domain';
}

/**
* Get the users LDAP domain.
*
* @return string
*/
public function getLdapDomain()
{
return $this->{$this->getLdapDomainColumn()};
}

/**
* Set the users LDAP domain.
*
* @param string $domain
*
* @return void
*/
public function setLdapDomain($domain)
{
$this->{$this->getLdapDomainColumn()} = $domain;
}

/**
* Get the users LDAP GUID database column name.
*
* @return string
*/
public function getLdapGuidColumn()
{
return 'guid';
}

/**
* Get the users LDAP GUID.
*
* @return string
*/
public function getLdapGuid()
{
return $this->{$this->getLdapGuidColumn()};
}

/**
* Set the users LDAP GUID.
*
* @param string $guid
*
* @return void
*/
public function setLdapGuid($guid)
{
$this->{$this->getLdapGuidColumn()} = $guid;
}
use ImportableFromLdap;
}
2 changes: 1 addition & 1 deletion src/Auth/DatabaseUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function retrieveByCredentials(array $credentials)
if ($user = $this->users->findByCredentials($credentials)) {
$this->setAuthenticatingUser($user);

return $this->importer->run($user, $credentials['password']);
return $this->importer->run($user, $credentials);
}
}

Expand Down
50 changes: 4 additions & 46 deletions src/Auth/LdapAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,9 @@

namespace LdapRecord\Laravel\Auth;

interface LdapAuthenticatable
{
/**
* Get the database column name for the LDAP domain.
*
* @return string
*/
public function getLdapDomainColumn();

/**
* Get the users LDAP domain.
*
* @return string
*/
public function getLdapDomain();

/**
* Set the users LDAP domain.
*
* @param string $domain
*
* @return void
*/
public function setLdapDomain($domain);
use LdapRecord\Laravel\LdapImportable;

/**
* Get the database column name for the LDAP guid.
*
* @return string
*/
public function getLdapGuidColumn();

/**
* Get the users LDAP GUID.
*
* @return string
*/
public function getLdapGuid();

/**
* Set the users LDAP GUID.
*
* @param string $guid
*
* @return void
*/
public function setLdapGuid($guid);
interface LdapAuthenticatable extends LdapImportable
{
//
}
73 changes: 73 additions & 0 deletions src/EloquentHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace LdapRecord\Laravel;

use LdapRecord\Models\Model as LdapModel;
use Illuminate\Database\Eloquent\Model as EloquentModel;

class EloquentHydrator
{
/**
* The configuration to pass to each hydrator.
*
* @var array
*/
protected $config = [];

/**
* Extra data to pass to each hydrator.
*
* @var array
*/
protected $data = [];

/**
* The hydrators to use when importing.
*
* @var array
*/
protected $hydrators = [
Hydrators\GuidHydrator::class,
Hydrators\DomainHydrator::class,
Hydrators\AttributeHydrator::class,
];

/**
* Constructor.
*
* @param array $config
*/
public function __construct(array $config = [])
{
$this->config = $config;
}

/**
* Extra data to pass to each hydrator.
*
* @param array $data
*
* @return $this
*/
public function with(array $data = [])
{
$this->data = $data;

return $this;
}

/**
* Hydrate the database model with the LDAP user.
*
* @param LdapModel $user
* @param EloquentModel $database
*
* @return void
*/
public function hydrate(LdapModel $user, EloquentModel $database)
{
foreach ($this->hydrators as $hydrator) {
$hydrator::with($this->config, $this->data)->hydrate($user, $database);
}
}
}
28 changes: 6 additions & 22 deletions src/EloquentUserHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,17 @@

namespace LdapRecord\Laravel;

use Illuminate\Database\Eloquent\Model as EloquentModel;
use LdapRecord\Models\Model as LdapModel;

class EloquentUserHydrator
class EloquentUserHydrator extends EloquentHydrator
{
/**
* @var Hydrators\AttributeHydrator[]
* The hydrators to use when importing.
*
* @var array
*/
protected static $hydrators = [
protected $hydrators = [
Hydrators\GuidHydrator::class,
Hydrators\DomainHydrator::class,
Hydrators\PasswordHydrator::class,
Hydrators\AttributeHydrator::class,
];

/**
* Hydrate the database model with the LDAP user.
*
* @param LdapModel $user
* @param EloquentModel $database
* @param array $config
*
* @return void
*/
public static function hydrate(LdapModel $user, EloquentModel $database, array $config = [])
{
foreach (static::$hydrators as $hydrator) {
$hydrator::with($config)->hydrate($user, $database);
}
}
}
18 changes: 14 additions & 4 deletions src/Hydrators/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace LdapRecord\Laravel\Hydrators;

use Illuminate\Database\Eloquent\Model as EloquentModel;
use LdapRecord\Models\Model as LdapModel;
use Illuminate\Database\Eloquent\Model as EloquentModel;

abstract class Hydrator
{
Expand All @@ -14,26 +14,36 @@ abstract class Hydrator
*/
protected $config = [];

/**
* Extra data for the hydration process.
*
* @var array
*/
protected $data = [];

/**
* Constructor.
*
* @param array $config
* @param array $data
*/
public function __construct(array $config = [])
public function __construct(array $config = [], array $data = [])
{
$this->config = $config;
$this->data = $data;
}

/**
* Create a new hydrator instance.
*
* @param array $config
* @param array $data
*
* @return static
*/
public static function with(array $config = [])
public static function with(array $config = [], array $data = [])
{
return new static($config);
return new static($config, $data);
}

/**
Expand Down
Loading

0 comments on commit fe9d707

Please sign in to comment.