'add match conditions if applicable'

def build_match_conditions(self, as_condition=True):

    self.match_filters = []
    self.match_conditions = []
    only_if_shared = False
    if (not self.user):
        self.user = frappe.session.user
    if (not self.tables):
        self.extract_tables()
    meta = frappe.get_meta(self.doctype)
    role_permissions = frappe.permissions.get_role_permissions(meta, user=self.user)
    self.shared = frappe.share.get_shared(self.doctype, self.user)
    if ((not meta.istable) and (not role_permissions.get(u'read')) and (not self.flags.ignore_permissions)):
        only_if_shared = True
        if (not self.shared):
            frappe.throw(_(u'No    permission    to    read    {0}').format(self.doctype), frappe.PermissionError)
        else:
            self.conditions.append(self.get_share_condition())
    else:
        if role_permissions.get(u'apply_user_permissions', {}).get(u'read'):
            user_permissions = frappe.permissions.get_user_permissions(self.user)
            self.add_user_permissions(user_permissions, user_permission_doctypes=role_permissions.get(u'user_permission_doctypes').get(u'read'))
        if role_permissions.get(u'if_owner', {}).get(u'read'):
            self.match_conditions.append(u"`tab{0}`.owner    =    '{1}'".format(self.doctype, frappe.db.escape(self.user, percent=False)))
    if as_condition:
        conditions = u''
        if self.match_conditions:
            conditions = ((u'((' + u')    or    ('.join(self.match_conditions)) + u'))')
        doctype_conditions = self.get_permission_query_conditions()
        if doctype_conditions:
            conditions += ((u'    and    ' + doctype_conditions) if conditions else doctype_conditions)
        if ((not only_if_shared) and self.shared and conditions):
            conditions = u'({conditions})    or    ({shared_condition})'.format(conditions=conditions, shared_condition=self.get_share_condition())
        return conditions
    else:
        return self.match_filters
