55use Adldap \Models \User ;
66use Adldap \Laravel \Traits \ImportsUsers ;
77use Illuminate \Console \Command ;
8- use Symfony \Component \Console \Input \InputArgument ;
98use Symfony \Component \Console \Input \InputOption ;
9+ use Symfony \Component \Console \Input \InputArgument ;
1010
1111class 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