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

Commit 9abc2dd

Browse files
committed
Added delete option to adldap:import command
- The `delete` option will soft-delete users who are disabled in active directory. If the log option is set to true, only newly deleted users will be logged. Previously deleted users will not. - Closes #183
1 parent 057b5fd commit 9abc2dd

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/Commands/Import.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use Adldap\Models\User;
66
use Adldap\Laravel\Traits\ImportsUsers;
77
use Illuminate\Console\Command;
8-
use Symfony\Component\Console\Input\InputArgument;
98
use Symfony\Component\Console\Input\InputOption;
9+
use Symfony\Component\Console\Input\InputArgument;
1010

1111
class Import extends Command
1212
{
@@ -41,6 +41,7 @@ public function handle()
4141
$adldap->connect();
4242
}
4343

44+
// Generate a new user search.
4445
$search = $adldap->search()->users();
4546

4647
if ($filter = $this->getFilter()) {
@@ -70,11 +71,11 @@ public function handle()
7071
* Imports the specified users and returns the total
7172
* number of users successfully imported.
7273
*
73-
* @param mixed $users
74+
* @param array $users
7475
*
7576
* @return int
7677
*/
77-
public function import($users = [])
78+
public function import(array $users = [])
7879
{
7980
$imported = 0;
8081

@@ -93,6 +94,22 @@ public function import($users = [])
9394
logger()->info("Imported user {$user->getCommonName()}");
9495
}
9596
}
97+
98+
if (
99+
$this->isDeleting() &&
100+
method_exists($model, 'trashed') &&
101+
! $model->trashed() &&
102+
$user->isDisabled()
103+
) {
104+
// If deleting is enabled, the model supports soft deletes, the model
105+
// isn't already deleted, and the AD user is disabled, we'll
106+
// go ahead and delete the users model.
107+
$model->delete();
108+
109+
if ($this->isLogging()) {
110+
logger()->info("Soft-deleted user {$user->getCommonName()}. Their AD user account is disabled.");
111+
}
112+
}
96113
} catch (\Exception $e) {
97114
// Log the unsuccessful import.
98115
if ($this->isLogging()) {
@@ -115,6 +132,16 @@ public function isLogging()
115132
return $this->option('log') == 'true';
116133
}
117134

135+
/**
136+
* Returns true / false if users are being deleted if they are disabled in AD.
137+
*
138+
* @return bool
139+
*/
140+
public function isDeleting()
141+
{
142+
return $this->option('delete') == 'true';
143+
}
144+
118145
/**
119146
* Returns the limitation filter for the user query.
120147
*
@@ -150,6 +177,8 @@ public function getOptions()
150177
['log', true, InputOption::VALUE_OPTIONAL, 'Log successful and unsuccessful imported users.'],
151178

152179
['connection', null, InputOption::VALUE_OPTIONAL, 'The LDAP connection to use to import users.'],
180+
181+
['delete', false, InputOption::VALUE_OPTIONAL, 'Soft-delete the users model if the AD user is disabled.'],
153182
];
154183
}
155184

0 commit comments

Comments
 (0)