user: Update logic to check if user exists in macOS (#76592)

'dscl -list' returns 0 even if the user does not exists. This
leads to errorenous condition in user module.
Using 'dscl -read UniqueID' can return if user exists or not.

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/76671/head
Abhijeet Kasurde 3 years ago committed by GitHub
parent 8e0654504f
commit 66e392d4e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- user - update logic to check if user exists or not in MacOS.

@ -2283,13 +2283,10 @@ class DarwinUser(User):
# sys.stderr.write('*** |%s| %s -> %s\n' % (property, out, lines)) # sys.stderr.write('*** |%s| %s -> %s\n' % (property, out, lines))
if len(lines) == 1: if len(lines) == 1:
return lines[0].split(': ')[1] return lines[0].split(': ')[1]
else:
if len(lines) > 2: if len(lines) > 2:
return '\n'.join([lines[1].strip()] + lines[2:]) return '\n'.join([lines[1].strip()] + lines[2:])
else:
if len(lines) == 2: if len(lines) == 2:
return lines[1].strip() return lines[1].strip()
else:
return None return None
def _get_next_uid(self, system=None): def _get_next_uid(self, system=None):
@ -2444,7 +2441,7 @@ class DarwinUser(User):
def user_exists(self): def user_exists(self):
'''Check is SELF.NAME is a known user on the system.''' '''Check is SELF.NAME is a known user on the system.'''
cmd = self._get_dscl() cmd = self._get_dscl()
cmd += ['-list', '/Users/%s' % self.name] cmd += ['-read', '/Users/%s' % self.name, 'UniqueID']
(rc, out, err) = self.execute_command(cmd, obey_checkmode=False) (rc, out, err) = self.execute_command(cmd, obey_checkmode=False)
return rc == 0 return rc == 0

Loading…
Cancel
Save