@@ -78,7 +78,6 @@ protected function configure() {
78
78
protected function execute (InputInterface $ input , OutputInterface $ output ) {
79
79
$ uid = $ input ->getArgument ('ocName ' );
80
80
$ this ->isAllowed ($ input ->getOption ('force ' ));
81
- //$this->confirmUserIsMapped($uid);
82
81
83
82
$ mappedData = $ this ->getMappedUUIDAndDN ($ uid );
84
83
if ($ mappedData ['mappedDN ' ] === false || $ mappedData ['mappedUUID ' ] === false ) {
@@ -93,7 +92,6 @@ protected function execute(InputInterface $input, OutputInterface $output) {
93
92
$ table1 ->render ();
94
93
95
94
$ entries = $ this ->backend ->findUsername ($ uid );
96
- $ entryCount = \count ($ entries );
97
95
98
96
$ output ->writeln ('' );
99
97
$ output ->writeln ('Candidates found in LDAP: ' );
@@ -104,25 +102,13 @@ protected function execute(InputInterface $input, OutputInterface $output) {
104
102
}
105
103
$ table2 ->render ();
106
104
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 ();
124
111
}
125
- $ output ->writeln ('Mapping data replaced ' );
126
112
}
127
113
128
114
private function getMappedUUIDAndDN ($ username ) {
@@ -134,21 +120,6 @@ private function getMappedUUIDAndDN($username) {
134
120
];
135
121
}
136
122
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
-
152
123
/**
153
124
* checks whether the setup allows reliable checking of LDAP user existence
154
125
* @throws \Exception
@@ -162,4 +133,23 @@ private function isAllowed($force) {
162
133
163
134
return true ;
164
135
}
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
+ }
165
155
}
0 commit comments