44 * 
55 * @author Arthur Schiwon <blizzz@arthur-schiwon.de> 
66 * @author Christoph Wurst <christoph@winzerhof-wurst.at> 
7+  * @author Côme Chilliet <come.chilliet@nextcloud.com> 
78 * @author Joas Schilling <coding@schilljs.com> 
89 * @author Morris Jobke <hey@morrisjobke.de> 
910 * @author Roeland Jago Douma <roeland@famdouma.nl> 
@@ -48,12 +49,6 @@ class CheckUser extends Command {
4849	/** @var UserMapping */ 
4950	protected  $ mapping
5051
51- 	/** 
52- 	 * @param User_Proxy $uBackend 
53- 	 * @param Helper $helper 
54- 	 * @param DeletedUsersIndex $dui 
55- 	 * @param UserMapping $mapping 
56- 	 */ 
5752	public  function  __construct (User_Proxy $ uBackendHelper $ helperDeletedUsersIndex $ duiUserMapping $ mapping
5853		$ this backend  = $ uBackend
5954		$ this helper  = $ helper
@@ -62,14 +57,14 @@ public function __construct(User_Proxy $uBackend, Helper $helper, DeletedUsersIn
6257		parent ::__construct ();
6358	}
6459
65- 	protected  function  configure () {
60+ 	protected  function  configure ():  void  {
6661		$ this 
6762			->setName ('ldap:check-user ' )
6863			->setDescription ('checks whether a user exists on LDAP. ' )
6964			->addArgument (
7065					'ocName ' ,
7166					InputArgument::REQUIRED ,
72- 					'the user name as used in Nextcloud ' 
67+ 					'the user name as used in Nextcloud, or the LDAP DN  ' 
7368					 )
7469			->addOption (
7570					'force ' ,
@@ -88,23 +83,31 @@ protected function configure() {
8883
8984	protected  function  execute (InputInterface $ inputOutputInterface $ outputint  {
9085		try  {
86+ 			$ this assertAllowed ($ inputgetOption ('force ' ));
9187			$ uid$ inputgetArgument ('ocName ' );
92- 			$ this isAllowed ($ inputgetOption ('force ' ));
93- 			$ this confirmUserIsMapped ($ uid
88+ 			if  ($ this backend ->getLDAPAccess ($ uidstringResemblesDN ($ uid
89+ 				$ username$ this backend ->dn2UserName ($ uid
90+ 				if  ($ usernamefalse ) {
91+ 					$ uid$ username
92+ 				}
93+ 			}
94+ 			$ wasMapped$ this userWasMapped ($ uid
9495			$ exists$ this backend ->userExistsOnLDAP ($ uidtrue );
9596			if  ($ existstrue ) {
9697				$ outputwriteln ('The user is still available on LDAP. ' );
9798				if  ($ inputgetOption ('update ' )) {
9899					$ this updateUser ($ uid$ output
99100				}
100101				return  0 ;
102+ 			} elseif  ($ wasMapped
103+ 				$ this dui ->markUser ($ uid
104+ 				$ outputwriteln ('The user does not exists on LDAP anymore. ' );
105+ 				$ outputwriteln ('Clean up the user \'s remnants by: ./occ user:delete " ' 
106+ 					. $ uid'" ' );
107+ 				return  0 ;
108+ 			} else  {
109+ 				throw  new  \Exception ('The given user is not a recognized LDAP user. ' );
101110			}
102- 
103- 			$ this dui ->markUser ($ uid
104- 			$ outputwriteln ('The user does not exists on LDAP anymore. ' );
105- 			$ outputwriteln ('Clean up the user \'s remnants by: ./occ user:delete " ' 
106- 				. $ uid'" ' );
107- 			return  0 ;
108111		} catch  (\Exception   $ e
109112			$ outputwriteln ('<error> '  . $ egetMessage (). '</error> ' );
110113			return  1 ;
@@ -114,24 +117,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
114117	/** 
115118	 * checks whether a user is actually mapped 
116119	 * @param string $ocName the username as used in Nextcloud 
117- 	 * @throws \Exception 
118- 	 * @return true 
119120	 */ 
120- 	protected  function  confirmUserIsMapped ( $ ocName
121+ 	protected  function  userWasMapped ( string   $ ocName:  bool  {
121122		$ dn$ this mapping ->getDNByName ($ ocName
122- 		if  ($ dnfalse ) {
123- 			throw  new  \Exception ('The given user is not a recognized LDAP user. ' );
124- 		}
125- 
126- 		return  true ;
123+ 		return  $ dnfalse ;
127124	}
128125
129126	/** 
130127	 * checks whether the setup allows reliable checking of LDAP user existence 
131128	 * @throws \Exception 
132- 	 * @return true 
133129	 */ 
134- 	protected  function  isAllowed ( $ force
130+ 	protected  function  assertAllowed ( bool   $ force:  void  {
135131		if  ($ this helper ->haveDisabledConfigurations () && !$ force
136132			throw  new  \Exception ('Cannot check user existence, because  ' 
137133				. 'disabled LDAP configurations are present. ' );
@@ -140,8 +136,6 @@ protected function isAllowed($force) {
140136		// we don't check ldapUserCleanupInterval from config.php because this 
141137		// action is triggered manually, while the setting only controls the 
142138		// background job. 
143- 
144- 		return  true ;
145139	}
146140
147141	private  function  updateUser (string  $ uidOutputInterface $ outputvoid  {
0 commit comments