Permalink
Browse files

iupdate the user state.

This commit partitions out the detection of what changes are required in
a user and adds the testinterface
  • Loading branch information...
1 parent 19af379 commit 9ebdfcbdffb99d6a3d5950c3443dc4c2ddcf8db1 @thatch45 thatch45 committed Apr 11, 2012
Showing with 127 additions and 70 deletions.
  1. +127 −70 salt/states/user.py
View
@@ -21,6 +21,77 @@
'''
+def _changes(
+ name,
+ uid=None,
+ gid=None,
+ groups=None,
+ home=True,
+ password=None,
+ enforce_password=True,
+ shell=None,
+ fullname=None,
+ roomnumber=None,
+ workphone=None,
+ homephone=None,
+ other=None,
+ unique=True,
+ ):
+ '''
+ Return a dict of the changes required for a user if the user is present,
+ otherwise return False.
+ '''
+
+ change = {}
+ found = False
+
+ if __grains__['os'] != 'FreeBSD':
+ lshad = __salt__['shadow.info'](name)
+
+ for lusr in __salt__['user.getent']():
+ # Scan over the users
+ if lusr['name'] == name:
+ found = True
+ if uid:
+ if lusr['uid'] != uid:
+ change['uid'] = uid
+ if gid:
+ if lusr['gid'] != gid:
+ change['gid'] = gid
+ if groups:
+ if lusr['groups'] != sorted(groups):
+ change['groups'] = groups
+ if home:
+ if lusr['home'] != home:
+ change['home'] = home
+ if shell:
+ if lusr['shell'] != shell:
+ change['shell'] = shell
+ if password:
+ if __grains__['os'] != 'FreeBSD':
+ if lshad['pwd'] == '!' or \
+ lshad['pwd'] != '!' and enforce_password:
+ if lshad['pwd'] != password:
+ change['passwd'] = password
+ if fullname:
+ if lusr['fullname'] != fullname:
+ change['fullname'] = fullname
+ if roomnumber:
+ if lusr['roomnumber'] != roomnumber:
+ change['roomnumber'] = roomnumber
+ if workphone:
+ if lusr['workphone'] != workphone:
+ change['workphone'] = workphone
+ if homephone:
+ if lusr['homephone'] != homephone:
+ change['homephone'] = homephone
+ if other:
+ if lusr['other'] != other:
+ change['other'] = other
+ if not found:
+ return
+ return change
+
def present(
name,
uid=None,
@@ -98,79 +169,61 @@ def present(
'result': True,
'comment': 'User {0} is present and up to date'.format(name)}
- if __grains__['os'] != 'FreeBSD':
- lshad = __salt__['shadow.info'](name)
-
- for lusr in __salt__['user.getent']():
- # Scan over the users
- if lusr['name'] == name:
- # The user is present, verify the params
- pre = __salt__['user.info'](name)
- if uid:
- if lusr['uid'] != uid:
- # Fix the uid
- __salt__['user.chuid'](name, uid)
- if gid:
- if lusr['gid'] != gid:
- # Fix the gid
- __salt__['user.chgid'](name, gid)
- if groups:
- if lusr['groups'] != sorted(groups):
- # Fix the groups
- __salt__['user.chgroups'](name, groups)
- if home:
- if lusr['home'] != home:
- # Fix the home dir
- __salt__['user.chhome'](name, home, True)
- if shell:
- if lusr['shell'] != shell:
- # Fix the shell
- __salt__['user.chshell'](name, shell)
- if password:
- if __grains__['os'] != 'FreeBSD':
- if lshad['pwd'] == '!' or \
- lshad['pwd'] != '!' and enforce_password:
- if lshad['pwd'] != password:
- # Set the new password
- __salt__['shadow.set_password'](name, password)
- if fullname:
- if lusr['fullname'] != fullname:
- # Fix the fullname
- __salt__['user.chfullname'](name, fullname)
- if roomnumber:
- if lusr['roomnumber'] != roomnumber:
- # Fix the roomnumber
- __salt__['user.chroomnumber'](name, roomnumber)
- if workphone:
- if lusr['workphone'] != workphone:
- # Fix the workphone
- __salt__['user.chworkphone'](name, workphone)
- if homephone:
- if lusr['homephone'] != homephone:
- # Fix the homephone
- __salt__['user.chhomephone'](name, homephone)
- if other:
- if lusr['other'] != other:
- # Fix the other
- __salt__['user.chother'](name, other)
- post = __salt__['user.info'](name)
- spost = {}
- if __grains__['os'] != 'FreeBSD':
- if lshad['pwd'] != password:
- spost = __salt__['shadow.info'](name)
- # See if anything changed
- for key in post:
- if post[key] != pre[key]:
- ret['changes'][key] = post[key]
- if __grains__['os'] != 'FreeBSD':
- for key in spost:
- if lshad[key] != spost[key]:
- ret['changes'][key] = spost[key]
- if ret['changes']:
- ret['comment'] = 'Updated user {0}'.format(name)
+ changes = _changes(
+ name,
+ uid,
+ gid,
+ groups,
+ home,
+ password,
+ enforce_password,
+ shell,
+ fullname,
+ roomnumber,
+ workphone,
+ homephone,
+ other,
+ unique)
+ if changes:
+ if __opts__['test']:
+ ret['result'] = None
+ ret['comment'] = ('The following user attributes are set to be '
+ 'changed:\n')
+ for key, val in changes.items():
+ ret['comment'] += '{0}: {1}\n'.format(key, val)
return ret
+ # The user is present
+ if __grains__['os'] != 'FreeBSD':
+ lshad = __salt__['shadow.info'](name)
+ pre = __salt__['user.info'](name)
+ for key, val in changes.items():
+ if key == 'passwd':
+ __salt__['shadow.set_password'](name, password)
+ continue
+ __salt__['user.ch{0}'.format(key)](name, val)
+ post = __salt__['user.info'](name)
+ spost = {}
+ if __grains__['os'] != 'FreeBSD':
+ if lshad['pwd'] != password:
+ spost = __salt__['shadow.info'](name)
+ # See if anything changed
+ for key in post:
+ if post[key] != pre[key]:
+ ret['changes'][key] = post[key]
+ if __grains__['os'] != 'FreeBSD':
+ for key in spost:
+ if lshad[key] != spost[key]:
+ ret['changes'][key] = spost[key]
+ if ret['changes']:
+ ret['comment'] = 'Updated user {0}'.format(name)
+ return ret
+
# The user is not present, make it!
+ if __opts__['test']:
+ ret['result'] = None
+ ret['comment'] = 'User {0} set to be added'.format(name)
+ return ret
if __salt__['user.add'](name,
uid=uid,
gid=gid,
@@ -223,6 +276,10 @@ def absent(name, purge=False, force=False):
# Scan over the users
if lusr['name'] == name:
# The user is present, make it not present
+ if __opts__['test']:
+ ret['result'] = None
+ ret['comment'] = 'User {0} set for removal'.format(name)
+ return ret
ret['result'] = __salt__['user.delete'](name, purge, force)
if ret['result']:
ret['changes'] = {name: 'removed'}

0 comments on commit 9ebdfcb

Please sign in to comment.