Helper function used by ``check_perms`` for checking and setting Grant and
    Deny permissions.

    Args:

        obj_name (str):
            The name or full path to the object

        obj_type (Optional[str]):
            The type of object for which to check permissions. Default is 'file'

        new_perms (dict):
            A dictionary containing the user/group and the basic permissions to
            check/grant, ie: ``{'user': {'perms': 'basic_permission'}}``.

        cur_perms (dict):
            A dictionary containing the user/group permissions as they currently
            exists on the target object.

        access_mode (str):
            The access mode to set. Either ``grant`` or ``deny``

        ret (dict):
            A dictionary to append changes to and return. If not passed, will
            create a new dictionary to return.

    Returns:
        dict: A dictionary of return data as expected by the state system

def _check_perms(obj_name, obj_type, new_perms, cur_perms, access_mode, ret):
    '''
    Helper function used by ``check_perms`` for checking and setting Grant and
    Deny permissions.

    Args:

        obj_name (str):
            The name or full path to the object

        obj_type (Optional[str]):
            The type of object for which to check permissions. Default is 'file'

        new_perms (dict):
            A dictionary containing the user/group and the basic permissions to
            check/grant, ie: ``{'user': {'perms': 'basic_permission'}}``.

        cur_perms (dict):
            A dictionary containing the user/group permissions as they currently
            exists on the target object.

        access_mode (str):
            The access mode to set. Either ``grant`` or ``deny``

        ret (dict):
            A dictionary to append changes to and return. If not passed, will
            create a new dictionary to return.

    Returns:
        dict: A dictionary of return data as expected by the state system
    '''
    access_mode = access_mode.lower()
    changes = {}
    for user in new_perms:
        applies_to_text = ''
        # Check that user exists:
        try:
            user_name = get_name(principal=user)
        except CommandExecutionError:
            ret['comment'].append(
                '{0} Perms: User "{1}" missing from Target System'
                ''.format(access_mode.capitalize(), user))
            continue

        # Get the proper applies_to text
        if 'applies_to' in new_perms[user]:
            applies_to = new_perms[user]['applies_to']
            at_flag = flags().ace_prop['file'][applies_to]
            applies_to_text = flags().ace_prop['file'][at_flag]

        else:
            applies_to = None

        if user_name not in cur_perms['Not Inherited']:
            if user not in changes:
                changes[user] = {}
            changes[user][access_mode] = new_perms[user]['perms']
            if applies_to:
                changes[user]['applies_to'] = applies_to
        else:
            # Check Perms for basic perms
            if isinstance(new_perms[user]['perms'], six.string_types):
                if not has_permission(obj_name=obj_name,
                                      principal=user_name,
                                      permission=new_perms[user]['perms'],
                                      access_mode=access_mode,
                                      obj_type=obj_type,
                                      exact=False):

                    if user not in changes:
                        changes[user] = {}
                    changes[user][access_mode] = new_perms[user]['perms']
            # Check Perms for advanced perms
            else:
                for perm in new_perms[user]['perms']:
                    if not has_permission(obj_name=obj_name,
                                          principal=user_name,
                                          permission=perm,
                                          access_mode=access_mode,
                                          obj_type=obj_type,
                                          exact=False):
                        if user not in changes:
                            changes[user] = {access_mode: []}
                        changes[user][access_mode].append(perm)

            # Check if applies_to was passed
            if applies_to:
                # Is there a deny/grant permission set
                if access_mode in cur_perms['Not Inherited'][user_name]:
                    # If the applies to settings are different, use the new one
                    if not cur_perms['Not Inherited'][user_name][access_mode]['applies to'] == applies_to_text:
                        if user not in changes:
                            changes[user] = {}
                        changes[user]['applies_to'] = applies_to

    if changes:
        if 'perms' not in ret['changes']:
            ret['changes']['perms'] = {}
        for user in changes:
            user_name = get_name(principal=user)

            if __opts__['test'] is True:
                if user not in ret['changes']['perms']:
                    ret['changes']['perms'][user] = {}
                ret['changes']['perms'][user][access_mode] = changes[user][access_mode]
            else:
                # Get applies_to
                applies_to = None
                if 'applies_to' not in changes[user]:
                    # Get current "applies to" settings from the file
                    if user_name in cur_perms['Not Inherited'] and \
                            access_mode in cur_perms['Not Inherited'][user_name]:
                        for flag in flags().ace_prop[obj_type]:
                            if flags().ace_prop[obj_type][flag] == cur_perms['Not Inherited'][user_name][access_mode]['applies to']:
                                at_flag = flag
                                for flag1 in flags().ace_prop[obj_type]:
                                    if salt.utils.win_dacl.flags().ace_prop[obj_type][flag1] == at_flag:
                                        applies_to = flag1
                    if not applies_to:
                        if obj_type.lower() in ['registry', 'registry32']:
                            applies_to = 'this_key_subkeys'
                        else:
                            applies_to = 'this_folder_subfolders_files'
                else:
                    applies_to = changes[user]['applies_to']

                perms = []
                if access_mode not in changes[user]:
                    # Get current perms
                    # Check for basic perms
                    for perm in cur_perms['Not Inherited'][user_name][access_mode]['permissions']:
                        for flag in flags().ace_perms[obj_type]['basic']:
                            if flags().ace_perms[obj_type]['basic'][flag] == perm:
                                perm_flag = flag
                                for flag1 in flags().ace_perms[obj_type]['basic']:
                                    if flags().ace_perms[obj_type]['basic'][flag1] == perm_flag:
                                        perms = flag1
                    # Make a list of advanced perms
                    if not perms:
                        for perm in cur_perms['Not Inherited'][user_name][access_mode]['permissions']:
                            for flag in flags().ace_perms[obj_type]['advanced']:
                                if flags().ace_perms[obj_type]['advanced'][flag] == perm:
                                    perm_flag = flag
                                    for flag1 in flags().ace_perms[obj_type]['advanced']:
                                        if flags().ace_perms[obj_type]['advanced'][flag1] == perm_flag:
                                            perms.append(flag1)
                else:
                    perms = changes[user][access_mode]

                try:
                    set_permissions(
                        obj_name=obj_name,
                        principal=user_name,
                        permissions=perms,
                        access_mode=access_mode,
                        applies_to=applies_to,
                        obj_type=obj_type)
                    if user not in ret['changes']['perms']:
                        ret['changes']['perms'][user] = {}
                    ret['changes']['perms'][user][access_mode] = changes[user][access_mode]
                except CommandExecutionError as exc:
                    ret['result'] = False
                    ret['comment'].append(
                        'Failed to change {0} permissions for "{1}" to {2}\n'
                        'Error: {3}'.format(access_mode, user, changes[user], exc.strerror))

    return ret