user: make system=yes work on Darwin systems. (#19464)

* Update system/user.py module.

Add ability to add real system users with next free system uid (< 500) on macOS.

* Improve syntax in system/user.py module.

Remove complex if else line and replace by simple comparison which yields the same boolean value.

* Remove "True" comparison of user.py.

Remove comparison to true, as it is not pep8 conform.
pull/19497/head
Rezart Qelibari 8 years ago committed by Brian Coca
parent 71685b3258
commit 507b96ff30

@ -1537,8 +1537,11 @@ class DarwinUser(User):
else:
return None
def _get_next_uid(self):
'''Return the next available uid'''
def _get_next_uid(self, system=None):
'''
Return the next available uid. If system=True, then
uid should be below of 500, if possible.
'''
cmd = self._get_dscl()
cmd += ['-list', '/Users', 'UniqueID']
(rc, out, err) = self.execute_command(cmd, obey_checkmode=False)
@ -1549,10 +1552,18 @@ class DarwinUser(User):
out=out,
err=err
)
max_uid = 0
max_system_uid = 0
for line in out.splitlines():
if max_uid < int(line.split()[1]):
max_uid = int(line.split()[1])
current_uid = int(line.split(' ')[-1])
if max_uid < current_uid:
max_uid = current_uid
if max_system_uid < current_uid and current_uid < 500:
max_system_uid = current_uid
if system and (0 < max_system_uid < 499):
return max_system_uid + 1
return max_uid + 1
def _change_user_password(self):
@ -1711,7 +1722,7 @@ class DarwinUser(User):
self._make_group_numerical()
if self.uid is None:
self.uid = str(self._get_next_uid())
self.uid = str(self._get_next_uid(self.system))
# Homedir is not created by default
if self.createhome:

Loading…
Cancel
Save