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.
This commit is contained in:
Rezart Qelibari 2016-12-19 22:35:56 +01:00 committed by Brian Coca
parent 71685b3258
commit 507b96ff30

View file

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