import pwd import time # Required by the copy/pastes below. from salt.modules.mac_user import _dscl import salt.utils # The function to override from salt.utils import get_group_list as list_groups __virtualname__ = 'user' print("LOADED MY MAC USER MODULE") def __virtual__(): if (__grains__.get('kernel') != 'Darwin' or __grains__['osrelease_info'] < (10, 7)): print("Returning False from __virtual__") return False else: print("Returning from __virtual__ with %s" % __virtualname__) return __virtualname__ # {{{ Copy-pasted identically from the mac_user module (and prints added) def _format_info(data): ''' Return user information in a pretty way ''' print("Called my mac_user _format_info function") return {'gid': data.pw_gid, 'groups': list_groups(data.pw_name), 'home': data.pw_dir, 'name': data.pw_name, 'shell': data.pw_shell, 'uid': data.pw_uid, 'fullname': data.pw_gecos} def chgroups(name, groups, append=False): ''' Change the groups to which the user belongs. Note that the user's primary group does not have to be one of the groups passed, membership in the user's primary group is automatically assumed. groups Groups to which the user should belong, can be passed either as a python list or a comma-separated string append Instead of removing user from groups not included in the ``groups`` parameter, just add user to any groups for which they are not members CLI Example: .. code-block:: bash salt '*' user.chgroups foo wheel,root ''' print("Called my mac_user chgroups function") ### NOTE: **args isn't used here but needs to be included in this ### function for compatibility with the user.present state uinfo = info(name) if not uinfo: raise CommandExecutionError('User \'{0}\' does not exist'.format(name)) if isinstance(groups, string_types): groups = groups.split(',') bad_groups = [x for x in groups if salt.utils.contains_whitespace(x)] if bad_groups: raise SaltInvocationError( 'Invalid group name(s): {0}'.format(', '.join(bad_groups)) ) ugrps = set(list_groups(name)) desired = set(str(x) for x in groups if bool(str(x))) primary_group = __salt__['file.gid_to_group'](uinfo['gid']) if primary_group: desired.add(primary_group) if ugrps == desired: return True # Add groups from which user is missing for group in desired - ugrps: _dscl( ['/Groups/{0}'.format(group), 'GroupMembership', name], ctype='append' ) if not append: # Remove from extra groups for group in ugrps - desired: _dscl( ['/Groups/{0}'.format(group), 'GroupMembership', name], ctype='delete' ) time.sleep(1) return set(list_groups(name)) == desired def getent(refresh=False): ''' Return the list of all info for all users CLI Example: .. code-block:: bash salt '*' user.getent ''' print("Called my mac_user getent function") if 'user.getent' in __context__ and not refresh: return __context__['user.getent'] ret = [] for data in pwd.getpwall(): ret.append(_format_info(data)) __context__['user.getent'] = ret return ret def info(name): ''' Return user information CLI Example: .. code-block:: bash salt '*' user.info root ''' print("Called my mac_user info function") try: data = pwd.getpwnam(name) except KeyError: return {} else: return _format_info(data) # }}}