def user_assigned_policies(user):
    """Return sequence of policies assigned to a user (or the anonymous
    user is ``user`` is ``None``).  (Also installed as
    ``assigned_policies`` method on ``User`` model.

    """
    key = user_cache_key(user)
    cached = cache.get(key)
    if cached is not None:
        return cached

    if user is None:
        pset = PermissionSet.objects.filter(anonymous_user=True).first()
    else:
        pset = user.permissionset.first()
    if pset is None:
        return []

    res = []
    skip_role_policies = False
    skip_role = None
    skip_role_variables = None
    for pi in pset.policyinstance_set.select_related('policy', 'role'):
        if skip_role_policies:
            if pi.role == skip_role and pi.variables == skip_role_variables:
                continue
            else:
                skip_role_policies = False
        if pi.role:
            res.append(pi.role)
            skip_role = pi.role
            skip_role_variables = pi.variables
            skip_role_policies = True
        else:
            if pi.variables != '{}':
                res.append((pi.policy, json.loads(pi.variables)))
            else:
                res.append(pi.policy)
    cache.set(key, res)
    return res