'S3 framework function to define whether a user can access a record
in manner "method". Designed to be called from the RESTlike
controller.
@param method: the access method as string, one of
"create", "read", "update", "delete"
@param table: the table or tablename
@param record_id: the record ID (if any)
@param c: the controller name (overrides current.request)
@param f: the function name (overrides current.request)'

def s3_has_permission(self, method, table, record_id=None, c=None, f=None):

    if self.override:
        return True
    sr = self.get_system_roles()
    if (not hasattr(table, '_tablename')):
        tablename = table
        table = current.s3db.table(tablename, db_only=True)
        if (table is None):
            current.log.warning(("Permission    check    on    Table    %s    failed    as    couldn't    load    table.    Module    disabled?" % tablename))
            return None
    policy = current.deployment_settings.get_security_policy()
    if (policy == 1):
        if (method == 'read'):
            authorised = True
        else:
            authorised = self.s3_logged_in()
    elif (policy == 2):
        if (method == 'read'):
            authorised = True
        elif (method == 'create'):
            authorised = self.s3_logged_in()
        elif ((record_id == 0) and (method == 'update')):
            authorised = self.s3_logged_in()
        else:
            authorised = self.s3_has_role(sr.EDITOR)
            if ((not authorised) and self.user and ('owned_by_user' in table)):
                query = (table.id == record_id)
                record = current.db(query).select(table.owned_by_user, limitby=(0, 1)).first()
                if (record and (self.user.id == record.owned_by_user)):
                    authorised = True
    elif (policy in (3, 4, 5, 6, 7, 8)):
        authorised = self.permission.has_permission(method, c=c, f=f, t=table, record=record_id)
    elif self.s3_logged_in():
        if self.s3_has_role(sr.ADMIN):
            authorised = True
        else:
            authorised = self.has_permission(method, table, record_id)
    else:
        authorised = False
    return authorised
