def chgroups(name, groups, append=False):
    ### 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
Change the groups to which the user belongs. Note that the user's primary
dataset/ETHPy150Open saltstack/salt/salt/modules/mac_user.py/chgroups