@@ -78,7 +78,6 @@ protected function configure() {
7878 protected function execute (InputInterface $ input , OutputInterface $ output ) {
7979 $ uid = $ input ->getArgument ('ocName ' );
8080 $ this ->isAllowed ($ input ->getOption ('force ' ));
81- //$this->confirmUserIsMapped($uid);
8281
8382 $ mappedData = $ this ->getMappedUUIDAndDN ($ uid );
8483 if ($ mappedData ['mappedDN ' ] === false || $ mappedData ['mappedUUID ' ] === false ) {
@@ -93,7 +92,6 @@ protected function execute(InputInterface $input, OutputInterface $output) {
9392 $ table1 ->render ();
9493
9594 $ entries = $ this ->backend ->findUsername ($ uid );
96- $ entryCount = \count ($ entries );
9795
9896 $ output ->writeln ('' );
9997 $ output ->writeln ('Candidates found in LDAP: ' );
@@ -104,25 +102,13 @@ protected function execute(InputInterface $input, OutputInterface $output) {
104102 }
105103 $ table2 ->render ();
106104
107- if ($ entryCount > 1 ) {
108- $ output ->writeln ('<error>Found too many candidates in LDAP for the target user, remapping isn \'t possible</error> ' );
109- return 1 ;
110- } elseif ($ entryCount < 1 ) {
111- $ output ->writeln ('<error>User not found in LDAP. Consider removing the ownCloud \'s account</error> ' );
112- return 2 ;
113- }
114-
115- if ($ mappedData ['mappedDN ' ] === $ entries [0 ]['dn ' ] && $ mappedData ['mappedUUID ' ] === $ entries [0 ]['directory_uuid ' ]) {
116- $ output ->writeln ('The same user is already mapped. Nothing to do ' );
117- return 0 ; // just show a message and return a success code
118- }
119-
120- $ result = $ this ->mapping ->replaceUUIDAndDN ($ uid , $ entries [0 ]['dn ' ], $ entries [0 ]['directory_uuid ' ]);
121- if ($ result === false ) {
122- $ output ->writeln ("<error>Failed to replace mapping data for user {$ uid }</error> " );
123- return 3 ;
105+ try {
106+ $ message = $ this ->remapUser ($ uid , $ mappedData , $ entries );
107+ $ output ->writeln ($ message );
108+ } catch (\UnexpectedValueException $ e ) {
109+ $ output ->writeln ("<error> {$ e ->getMessage ()}</error> " );
110+ return $ e ->getCode ();
124111 }
125- $ output ->writeln ('Mapping data replaced ' );
126112 }
127113
128114 private function getMappedUUIDAndDN ($ username ) {
@@ -134,21 +120,6 @@ private function getMappedUUIDAndDN($username) {
134120 ];
135121 }
136122
137- /**
138- * checks whether a user is actually mapped
139- * @param string $ocName the username as used in ownCloud
140- * @throws \Exception
141- * @return true
142- */
143- private function confirmUserIsMapped ($ ocName ) {
144- $ dn = $ this ->mapping ->getDNByName ($ ocName );
145- if ($ dn === false ) {
146- throw new \Exception ('The given user is not a recognized LDAP user. ' );
147- }
148-
149- return true ;
150- }
151-
152123 /**
153124 * checks whether the setup allows reliable checking of LDAP user existence
154125 * @throws \Exception
@@ -162,4 +133,23 @@ private function isAllowed($force) {
162133
163134 return true ;
164135 }
136+
137+ private function remapUser ($ uid , $ mappedData , $ entries ) {
138+ $ entryCount = \count ($ entries );
139+ if ($ entryCount > 1 ) {
140+ throw new \UnexpectedValueException ('Found too many candidates in LDAP for the target user, remapping isn \'t possible ' , 1 );
141+ } elseif ($ entryCount < 1 ) {
142+ throw new \UnexpectedValueException ('User not found in LDAP. Consider removing the ownCloud \'s account ' , 2 );
143+ }
144+
145+ if ($ mappedData ['mappedDN ' ] === $ entries [0 ]['dn ' ] && $ mappedData ['mappedUUID ' ] === $ entries [0 ]['directory_uuid ' ]) {
146+ return 'The same user is already mapped. Nothing to do ' ;
147+ }
148+
149+ $ result = $ this ->mapping ->replaceUUIDAndDN ($ uid , $ entries [0 ]['dn ' ], $ entries [0 ]['directory_uuid ' ]);
150+ if ($ result === false ) {
151+ throw new \UnexpectedValueException ("Failed to replace mapping data for user {$ uid }" , 3 );
152+ }
153+ return 'Mapping data replaced ' ;
154+ }
165155}
0 commit comments