您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

150 行
4.4 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes
  5. from webnotes import _, msgprint, _dict
  6. from webnotes.utils import cint
  7. def check_admin_or_system_manager():
  8. if ("System Manager" not in webnotes.get_roles()) and \
  9. (webnotes.session.user!="Administrator"):
  10. msgprint("Only Allowed for Role System Manager or Administrator", raise_exception=True)
  11. def has_permission(doctype, ptype="read", refdoc=None, verbose=True):
  12. """check if user has permission"""
  13. if webnotes.conn.get_value("DocType", doctype, "istable")==1:
  14. return True
  15. meta = webnotes.get_doctype(doctype)
  16. if ptype=="submit" and not cint(meta[0].is_submittable):
  17. return False
  18. if ptype=="import" and not cint(meta[0].allow_import):
  19. return False
  20. if webnotes.session.user=="Administrator":
  21. return True
  22. # get user permissions
  23. perms = get_user_perms(meta, ptype)
  24. if not perms:
  25. return False
  26. elif refdoc:
  27. if isinstance(refdoc, basestring):
  28. refdoc = webnotes.doc(meta[0].name, refdoc)
  29. if (has_only_permitted_data(meta, refdoc, verbose=verbose) and has_match(perms, refdoc)):
  30. return True
  31. else:
  32. return False
  33. else:
  34. return True
  35. def get_user_perms(meta, ptype, user=None):
  36. user_roles = webnotes.get_roles(user)
  37. return [p for p in meta.get({"doctype": "DocPerm"})
  38. if cint(p.get(ptype))==1 and cint(p.permlevel)==0 and (p.role=="All" or p.role in user_roles)]
  39. def has_only_permitted_data(meta, refdoc, verbose=True):
  40. from webnotes.defaults import get_restrictions
  41. restrictions = get_restrictions()
  42. if not restrictions:
  43. return True
  44. fields_to_check = meta.get_restricted_fields(restrictions.keys())
  45. has_restricted_data = False
  46. for df in fields_to_check:
  47. if refdoc.get(df.fieldname) and refdoc.get(df.fieldname) not in restrictions[df.options]:
  48. if verbose:
  49. msg = "{not_allowed}: {doctype} {having} {label} = {value}".format(
  50. not_allowed=_("Sorry, you are not allowed to access"), doctype=_(df.options),
  51. having=_("having"), label=_(df.label), value=refdoc.get(df.fieldname))
  52. if refdoc.parentfield:
  53. msg = "{doctype}, {row} #{idx}, ".format(doctype=_(refdoc.doctype),
  54. row=_("Row"), idx=refdoc.idx) + msg
  55. msgprint(msg)
  56. has_restricted_data = True
  57. # check all restrictions before returning
  58. return False if has_restricted_data else True
  59. def has_match(perms, refdoc):
  60. """check owner match (if exists)"""
  61. for p in perms:
  62. if p.get("match")=="owner":
  63. if refdoc.get("owner")==webnotes.local.session.user:
  64. # owner matches :)
  65. return True
  66. else:
  67. # found a permission without owner match :)
  68. return True
  69. # no match :(
  70. return False
  71. def can_restrict_user(user, doctype, docname=None):
  72. if not can_restrict(doctype, docname):
  73. return False
  74. meta = webnotes.get_doctype(doctype)
  75. # check if target user does not have restrict permission
  76. if has_only_non_restrict_role(meta, user):
  77. return True
  78. return False
  79. def can_restrict(doctype, docname=None):
  80. # System Manager can always restrict
  81. if "System Manager" in webnotes.get_roles():
  82. return True
  83. meta = webnotes.get_doctype(doctype)
  84. # check if current user has read permission for docname
  85. if docname and not has_permission(doctype, "read", docname):
  86. return False
  87. # check if current user has a role with restrict permission
  88. if not has_restrict_permission(meta):
  89. return False
  90. return True
  91. def has_restrict_permission(meta=None, user=None):
  92. return any((perm for perm in get_user_perms(meta, "read", user)
  93. if cint(perm.restrict)==1))
  94. def has_only_non_restrict_role(meta, user):
  95. # check if target user does not have restrict permission
  96. if has_restrict_permission(meta, user):
  97. return False
  98. # and has non-restrict role
  99. return any((perm for perm in get_user_perms(meta, "read", user)
  100. if cint(perm.restrict)==0))
  101. def can_import(doctype, raise_exception=False):
  102. if not ("System Manager" in webnotes.get_roles() or has_permission(doctype, "import")):
  103. if raise_exception:
  104. raise webnotes.PermissionError("You are not allowed to import: {doctype}".format(doctype=doctype))
  105. else:
  106. return False
  107. return True
  108. def can_export(doctype, raise_exception=False):
  109. if not ("System Manager" in webnotes.get_roles() or has_permission(doctype, "export")):
  110. if raise_exception:
  111. raise webnotes.PermissionError("You are not allowed to export: {doctype}".format(doctype=doctype))
  112. else:
  113. return False
  114. return True