def user_has_permission(doc, verbose=True, user=None): from frappe.defaults import get_user_permissions user_permissions = get_user_permissions(user) user_permissions_keys = user_permissions.keys() def check_user_permission(d): result = True meta = frappe.get_meta(d.get("doctype")) for df in meta.get_fields_to_check_permissions(user_permissions_keys): if d.get(df.fieldname) and d.get( df.fieldname) not in user_permissions[df.options]: result = False if verbose: msg = _("Not allowed to access {0} with {1} = {2}").format( df.options, _(df.label), d.get(df.fieldname)) if d.parentfield: msg = "{doctype}, {row} #{idx}, ".format( doctype=_( d.doctype), row=_("Row"), idx=d.idx) + msg msgprint(msg) return result _user_has_permission = check_user_permission(doc) for d in doc.get_all_children(): _user_has_permission = check_user_permission( d) and _user_has_permission return _user_has_permission
def permission_query_conditions(user): if user == "Administrator" or user == "*****@*****.**": return "1=1" from frappe.defaults import get_user_permissions user_permissions = get_user_permissions(user) "','".join(user_permissions.get('Territory', [])) return "(consoleerp_territory in ('{}'))".format("','".join( user_permissions.get('Territory', [])))
def get_pos_required_data(sales_user): sales_user_permissions = get_user_permissions(sales_user) pos_dict = {} pos_dict['customer'] = get_customer_list(sales_user_permissions.get('Customer')) pos_dict['item_group'] = get_item_group_list() pos_dict['vendor'] = get_supplier_list(sales_user_permissions.get('Supplier')) item_data = get_item_list(sales_user) pos_dict['price_list'] = item_data.get("price_list") pos_dict['item'] = item_data.get("item_list") pos_dict['company'] = frappe.db.sql(" select value from `tabSingles` where doctype='Global Defaults' and field='default_company' ",as_list=1)[0][0] return pos_dict
def user_has_permission(doc, verbose=True, user=None, user_permission_doctypes=None): from frappe.defaults import get_user_permissions user_permissions = get_user_permissions(user) user_permission_doctypes = get_user_permission_doctypes( user_permission_doctypes, user_permissions) def check_user_permission(d): meta = frappe.get_meta(d.get("doctype")) end_result = False messages = {} # check multiple sets of user_permission_doctypes using OR condition for doctypes in user_permission_doctypes: result = True for df in meta.get_fields_to_check_permissions(doctypes): if (df.options in user_permissions and d.get(df.fieldname) and d.get( df.fieldname) not in user_permissions[df.options]): result = False if verbose: msg = _( "Not allowed to access {0} with {1} = {2}").format( df.options, _(df.label), d.get(df.fieldname)) if d.parentfield: msg = "{doctype}, {row} #{idx}, ".format( doctype=_( d.doctype), row=_("Row"), idx=d.idx) + msg messages[df.fieldname] = msg end_result = end_result or result if not end_result and messages: for fieldname, msg in messages.items(): msgprint(msg) return end_result _user_has_permission = check_user_permission(doc) for d in doc.get_all_children(): _user_has_permission = check_user_permission( d) and _user_has_permission return _user_has_permission
def has_permission(doc, ptype, user): """ create submit read write Currently everything under one-perm """ if user == "Administrator" or user == "*****@*****.**": return True from frappe.defaults import get_user_permissions user_permissions = get_user_permissions(user) return doc.consoleerp_territory in user_permissions.get('Territory', [])
def get_pos_required_data(sales_user): sales_user_permissions = get_user_permissions(sales_user) pos_dict = {} pos_dict['customer'] = get_customer_list( sales_user_permissions.get('Customer')) pos_dict['item_group'] = get_item_group_list() pos_dict['vendor'] = get_supplier_list( sales_user_permissions.get('Supplier')) item_data = get_item_list(sales_user) pos_dict['price_list'] = item_data.get("price_list") pos_dict['item'] = item_data.get("item_list") pos_dict['company'] = frappe.db.sql( " select value from `tabSingles` where doctype='Global Defaults' and field='default_company' ", as_list=1)[0][0] return pos_dict
def set_price_list(out, party, party_type, given_price_list): # price list price_list = filter(None, get_user_permissions().get("Price List", [])) if isinstance(price_list, list): price_list = price_list[0] if len(price_list)==1 else None if not price_list: price_list = get_default_price_list(party) if not price_list: price_list = given_price_list if price_list: out.price_list_currency = frappe.db.get_value("Price List", price_list, "currency") out["selling_price_list" if party.doctype=="Customer" else "buying_price_list"] = price_list
def set_price_list(out, party, party_type, given_price_list): # price list price_list = filter(None, get_user_permissions() .get("Price List", {}) .get("docs", [])) price_list = list(price_list) if price_list: price_list = price_list[0] else: price_list = get_default_price_list(party) or given_price_list if price_list: out.price_list_currency = frappe.db.get_value("Price List", price_list, "currency") out["selling_price_list" if party.doctype=="Customer" else "buying_price_list"] = price_list
def get_warehouse_list(filters): from frappe.defaults import get_user_permissions condition = '' user_permitted_warehouse = filter(None, get_user_permissions() .get("Warehouse", {}) .get("docs", [])) value = () if user_permitted_warehouse: condition = "and name in %s" value = set(user_permitted_warehouse) elif not user_permitted_warehouse and filters.get("warehouse"): condition = "and name = %s" value = filters.get("warehouse") return frappe.db.sql("""select name from `tabWarehouse` where is_group = 0 {condition}""".format(condition=condition), value, as_dict=1)
def user_has_permission(doc, verbose=True, user=None, user_permission_doctypes=None): from frappe.defaults import get_user_permissions user_permissions = get_user_permissions(user) user_permission_doctypes = get_user_permission_doctypes(user_permission_doctypes, user_permissions) def check_user_permission(d): meta = frappe.get_meta(d.get("doctype")) end_result = False messages = {} # check multiple sets of user_permission_doctypes using OR condition for doctypes in user_permission_doctypes: result = True for df in meta.get_fields_to_check_permissions(doctypes): if (d.get(df.fieldname) and d.get(df.fieldname) not in user_permissions.get(df.options, [])): result = False if verbose: msg = _("Not allowed to access {0} with {1} = {2}").format(df.options, _(df.label), d.get(df.fieldname)) if d.parentfield: msg = "{doctype}, {row} #{idx}, ".format(doctype=_(d.doctype), row=_("Row"), idx=d.idx) + msg messages[df.fieldname] = msg end_result = end_result or result if not end_result and messages: for fieldname, msg in messages.items(): msgprint(msg) return end_result _user_has_permission = check_user_permission(doc) for d in doc.get_all_children(): _user_has_permission = check_user_permission(d) and _user_has_permission return _user_has_permission
def add_additional_department_leaves(events, start, end): user = frappe.session.user employee = frappe.db.get_value("Employee", {"user_id": user}, ["department", "company"], as_dict=True) if not employee: return departments = get_user_permissions().get("Department", []) if employee.department and employee.department in (departments or []): # since leaves for this department are already added by the standard method departments.remove(employee.department) if not departments: return department_employees = frappe.db.sql_list("""select name from `tabEmployee` where company=%s and department in ({0})""".format(", ".join(["%s"] * len(departments))), tuple([employee.company] + departments)) # leaves for additional department employees match_conditions = "employee in (\"{0}\")".format('", "'.join(department_employees)) add_leaves(events, start, end, match_conditions=match_conditions)