spacepaste

  1.  
  2. import pwd
  3. import time
  4. # Required by the copy/pastes below.
  5. from salt.modules.mac_user import _dscl
  6. import salt.utils
  7. # The function to override
  8. from salt.utils import get_group_list as list_groups
  9. __virtualname__ = 'user'
  10. print("LOADED MY MAC USER MODULE")
  11. def __virtual__():
  12. if (__grains__.get('kernel') != 'Darwin' or
  13. __grains__['osrelease_info'] < (10, 7)):
  14. print("Returning False from __virtual__")
  15. return False
  16. else:
  17. print("Returning from __virtual__ with %s" % __virtualname__)
  18. return __virtualname__
  19. # {{{ Copy-pasted identically from the mac_user module (and prints added)
  20. def _format_info(data):
  21. '''
  22. Return user information in a pretty way
  23. '''
  24. print("Called my mac_user _format_info function")
  25. return {'gid': data.pw_gid,
  26. 'groups': list_groups(data.pw_name),
  27. 'home': data.pw_dir,
  28. 'name': data.pw_name,
  29. 'shell': data.pw_shell,
  30. 'uid': data.pw_uid,
  31. 'fullname': data.pw_gecos}
  32. def chgroups(name, groups, append=False):
  33. '''
  34. Change the groups to which the user belongs. Note that the user's primary
  35. group does not have to be one of the groups passed, membership in the
  36. user's primary group is automatically assumed.
  37. groups
  38. Groups to which the user should belong, can be passed either as a
  39. python list or a comma-separated string
  40. append
  41. Instead of removing user from groups not included in the ``groups``
  42. parameter, just add user to any groups for which they are not members
  43. CLI Example:
  44. .. code-block:: bash
  45. salt '*' user.chgroups foo wheel,root
  46. '''
  47. print("Called my mac_user chgroups function")
  48. ### NOTE: **args isn't used here but needs to be included in this
  49. ### function for compatibility with the user.present state
  50. uinfo = info(name)
  51. if not uinfo:
  52. raise CommandExecutionError('User \'{0}\' does not exist'.format(name))
  53. if isinstance(groups, string_types):
  54. groups = groups.split(',')
  55. bad_groups = [x for x in groups if salt.utils.contains_whitespace(x)]
  56. if bad_groups:
  57. raise SaltInvocationError(
  58. 'Invalid group name(s): {0}'.format(', '.join(bad_groups))
  59. )
  60. ugrps = set(list_groups(name))
  61. desired = set(str(x) for x in groups if bool(str(x)))
  62. primary_group = __salt__['file.gid_to_group'](uinfo['gid'])
  63. if primary_group:
  64. desired.add(primary_group)
  65. if ugrps == desired:
  66. return True
  67. # Add groups from which user is missing
  68. for group in desired - ugrps:
  69. _dscl(
  70. ['/Groups/{0}'.format(group), 'GroupMembership', name],
  71. ctype='append'
  72. )
  73. if not append:
  74. # Remove from extra groups
  75. for group in ugrps - desired:
  76. _dscl(
  77. ['/Groups/{0}'.format(group), 'GroupMembership', name],
  78. ctype='delete'
  79. )
  80. time.sleep(1)
  81. return set(list_groups(name)) == desired
  82. def getent(refresh=False):
  83. '''
  84. Return the list of all info for all users
  85. CLI Example:
  86. .. code-block:: bash
  87. salt '*' user.getent
  88. '''
  89. print("Called my mac_user getent function")
  90. if 'user.getent' in __context__ and not refresh:
  91. return __context__['user.getent']
  92. ret = []
  93. for data in pwd.getpwall():
  94. ret.append(_format_info(data))
  95. __context__['user.getent'] = ret
  96. return ret
  97. def info(name):
  98. '''
  99. Return user information
  100. CLI Example:
  101. .. code-block:: bash
  102. salt '*' user.info root
  103. '''
  104. print("Called my mac_user info function")
  105. try:
  106. data = pwd.getpwnam(name)
  107. except KeyError:
  108. return {}
  109. else:
  110. return _format_info(data)
  111. # }}}
  112.