'If there are no facilities that the user has permission for,
prevents create & update of records in table & gives a
warning if the user tries to.
@param table: the table or table name
@param error_msg: error message
@param redirect_on_error: whether to redirect on error
@param facility_type: restrict to this particular type of
facilities (a tablename)'

def permitted_facilities(self, table=None, error_msg=None, redirect_on_error=True, facility_type=None):

    T = current.T
    ERROR = T('You    do    not    have    permission    for    any    facility    to    perform    this    action.')
    HINT = T('Create    a    new    facility    or    ensure    that    you    have    permissions    for    an    existing    facility.')
    if (not error_msg):
        error_msg = ERROR
    s3db = current.s3db
    site_ids = []
    if (facility_type is None):
        site_types = self.org_site_types
    else:
        if (facility_type not in self.org_site_types):
            return
        site_types = [s3db[facility_type]]
    for site_type in site_types:
        try:
            ftable = s3db[site_type]
            if (not ('site_id' in ftable.fields)):
                continue
            query = self.s3_accessible_query('update', ftable)
            if ('deleted' in ftable):
                query &= (ftable.deleted != True)
            rows = current.db(query).select(ftable.site_id)
            site_ids += [row.site_id for row in rows]
        except:
            pass
    if site_ids:
        return site_ids
    args = current.request.args
    if (('update' in args) or ('create' in args)):
        if redirect_on_error:
            current.session.error = ('%s    %s' % (error_msg, HINT))
            redirect(URL(c='default', f='index'))
    elif (table is not None):
        if hasattr(table, '_tablename'):
            tablename = original_tablename(table)
        else:
            tablename = table
        s3db.configure(tablename, insertable=False)
    return site_ids
