Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
Added ability to use a custom user import scope
Browse files Browse the repository at this point in the history
Closes #699
  • Loading branch information
stevebauman committed Mar 30, 2019
1 parent 6ca2c46 commit d41c4a4
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/Commands/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

class Import
{
/**
* The scope to utilize for locating the LDAP user to synchronize.
*
* @var string
*/
public static $scope = UserImportScope::class;

/**
* The LDAP user that is being imported.
*
Expand All @@ -28,6 +35,16 @@ class Import
*/
protected $model;

/**
* Sets the scope to use for locating LDAP users.
*
* @param $scope
*/
public static function useScope($scope)
{
static::$scope = $scope;
}

/**
* Constructor.
*
Expand Down Expand Up @@ -83,17 +100,15 @@ protected function findUser()
$query->withTrashed();
}

// We'll try to locate the user by their object guid,
// otherwise we'll locate them by their username.
return $query->where(
Resolver::getDatabaseIdColumn(),
'=',
$this->getUserObjectGuid()
)->orWhere(
Resolver::getDatabaseUsernameColumn(),
'=',
/** @var \Illuminate\Database\Eloquent\Scope $scope */
$scope = new static::$scope(
$this->getUserObjectGuid(),
$this->getUserUsername()
)->first();
);

$scope->apply($query, $this->model);

return $query->first();
}

/**
Expand Down
74 changes: 74 additions & 0 deletions src/Commands/UserImportScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Adldap\Laravel\Commands;

use Adldap\Laravel\Facades\Resolver;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class UserImportScope implements Scope
{
/**
* The LDAP users object guid.
*
* @var string
*/
protected $guid;

/**
* The LDAP users username.
*
* @var string
*/
protected $username;

/**
* Constructor.
*
* @param string $guid
* @param string $username
*/
public function __construct($guid, $username)
{
$this->guid = $guid;
$this->username = $username;
}

/**
* Apply the scope to a given Eloquent query builder.
*
* @param Builder $query
* @param Model $model
*
* @return void
*/
public function apply(Builder $query, Model $model)
{
// We'll try to locate the user by their object guid,
// otherwise we'll locate them by their username.
$query
->where(Resolver::getDatabaseIdColumn(), '=', $this->getGuid())
->orWhere(Resolver::getDatabaseUsernameColumn(), '=', $this->getUsername());
}

/**
* Returns the users object guid.
*
* @return string
*/
protected function getGuid()
{
return $this->guid;
}

/**
* Returns the LDAP users username.
*
* @return string
*/
protected function getUsername()
{
return $this->username;
}
}

0 comments on commit d41c4a4

Please sign in to comment.