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

Commit d41c4a4

Browse files
committed
Added ability to use a custom user import scope
Closes #699
1 parent 6ca2c46 commit d41c4a4

File tree

2 files changed

+99
-10
lines changed

2 files changed

+99
-10
lines changed

src/Commands/Import.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
class Import
1616
{
17+
/**
18+
* The scope to utilize for locating the LDAP user to synchronize.
19+
*
20+
* @var string
21+
*/
22+
public static $scope = UserImportScope::class;
23+
1724
/**
1825
* The LDAP user that is being imported.
1926
*
@@ -28,6 +35,16 @@ class Import
2835
*/
2936
protected $model;
3037

38+
/**
39+
* Sets the scope to use for locating LDAP users.
40+
*
41+
* @param $scope
42+
*/
43+
public static function useScope($scope)
44+
{
45+
static::$scope = $scope;
46+
}
47+
3148
/**
3249
* Constructor.
3350
*
@@ -83,17 +100,15 @@ protected function findUser()
83100
$query->withTrashed();
84101
}
85102

86-
// We'll try to locate the user by their object guid,
87-
// otherwise we'll locate them by their username.
88-
return $query->where(
89-
Resolver::getDatabaseIdColumn(),
90-
'=',
91-
$this->getUserObjectGuid()
92-
)->orWhere(
93-
Resolver::getDatabaseUsernameColumn(),
94-
'=',
103+
/** @var \Illuminate\Database\Eloquent\Scope $scope */
104+
$scope = new static::$scope(
105+
$this->getUserObjectGuid(),
95106
$this->getUserUsername()
96-
)->first();
107+
);
108+
109+
$scope->apply($query, $this->model);
110+
111+
return $query->first();
97112
}
98113

99114
/**

src/Commands/UserImportScope.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Commands;
4+
5+
use Adldap\Laravel\Facades\Resolver;
6+
use Illuminate\Database\Eloquent\Scope;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Builder;
9+
10+
class UserImportScope implements Scope
11+
{
12+
/**
13+
* The LDAP users object guid.
14+
*
15+
* @var string
16+
*/
17+
protected $guid;
18+
19+
/**
20+
* The LDAP users username.
21+
*
22+
* @var string
23+
*/
24+
protected $username;
25+
26+
/**
27+
* Constructor.
28+
*
29+
* @param string $guid
30+
* @param string $username
31+
*/
32+
public function __construct($guid, $username)
33+
{
34+
$this->guid = $guid;
35+
$this->username = $username;
36+
}
37+
38+
/**
39+
* Apply the scope to a given Eloquent query builder.
40+
*
41+
* @param Builder $query
42+
* @param Model $model
43+
*
44+
* @return void
45+
*/
46+
public function apply(Builder $query, Model $model)
47+
{
48+
// We'll try to locate the user by their object guid,
49+
// otherwise we'll locate them by their username.
50+
$query
51+
->where(Resolver::getDatabaseIdColumn(), '=', $this->getGuid())
52+
->orWhere(Resolver::getDatabaseUsernameColumn(), '=', $this->getUsername());
53+
}
54+
55+
/**
56+
* Returns the users object guid.
57+
*
58+
* @return string
59+
*/
60+
protected function getGuid()
61+
{
62+
return $this->guid;
63+
}
64+
65+
/**
66+
* Returns the LDAP users username.
67+
*
68+
* @return string
69+
*/
70+
protected function getUsername()
71+
{
72+
return $this->username;
73+
}
74+
}

0 commit comments

Comments
 (0)