'Check whether the currently logged-in user has a certain role
(auth_group membership).
@param role: the record ID or UID of the role
@param for_pe: check for this particular realm, possible values:
None - for any entity
0 - site-wide
X - for entity X'

def s3_has_role(self, role, for_pe=None):

    if self.override:
        return True
    system_roles = self.get_system_roles()
    if (role == system_roles.ANONYMOUS):
        return True
    s3 = current.session.s3
    self.s3_logged_in()
    if (not s3):
        return False
    realms = None
    if self.user:
        realms = self.user.realms
    elif s3.roles:
        realms = Storage([(r, None) for r in s3.roles])
    if (not realms):
        return False
    if (system_roles.ADMIN in realms):
        return True
    if isinstance(role, str):
        if role.isdigit():
            role = int(role)
        else:
            gtable = self.settings.table_group
            query = ((gtable.uuid == role) & (gtable.deleted != True))
            row = current.db(query).select(gtable.id, cache=(current.cache.ram, 600), limitby=(0, 1)).first()
            if row:
                role = row.id
            else:
                return False
    if (role in realms):
        realm = realms[role]
        if ((realm is None) or (for_pe is None) or (for_pe in realm)):
            return True
    return False
