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

permissions.py 5.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. if not get_user_perms(meta).get(ptype):
  24. return False
  25. if refdoc:
  26. if isinstance(refdoc, basestring):
  27. refdoc = webnotes.doc(meta[0].name, refdoc)
  28. if not has_unrestricted_access(meta, refdoc, verbose=verbose):
  29. return False
  30. if not has_additional_permission(refdoc):
  31. return False
  32. return True
  33. rights = ["read", "write", "create", "submit", "cancel", "amend",
  34. "report", "import", "export", "print", "email", "restrict", "delete", "restricted"]
  35. def get_user_perms(meta, user=None):
  36. cache_key = (meta[0].name, user)
  37. if not webnotes.local.user_perms.get(cache_key):
  38. perms = webnotes._dict()
  39. user_roles = webnotes.get_roles(user)
  40. for p in meta.get({"doctype": "DocPerm"}):
  41. if cint(p.permlevel)==0 and (p.role=="All" or p.role in user_roles):
  42. for ptype in rights:
  43. if ptype == "restricted":
  44. perms[ptype] = perms.get(ptype, 1) and cint(p.get(ptype))
  45. else:
  46. perms[ptype] = perms.get(ptype, 0) or cint(p.get(ptype))
  47. webnotes.local.user_perms[cache_key] = perms
  48. return webnotes.local.user_perms[cache_key]
  49. def has_unrestricted_access(meta, refdoc, verbose=True):
  50. from webnotes.defaults import get_restrictions
  51. restrictions = get_restrictions()
  52. if get_user_perms(meta).restricted:
  53. if refdoc.owner == webnotes.session.user:
  54. # owner is always allowed for restricted permissions
  55. return True
  56. elif not restrictions:
  57. return False
  58. else:
  59. if not restrictions:
  60. return True
  61. # evaluate specific restrictions
  62. fields_to_check = meta.get_restricted_fields(restrictions.keys())
  63. has_restricted_data = False
  64. for df in fields_to_check:
  65. if refdoc.get(df.fieldname) and refdoc.get(df.fieldname) not in restrictions[df.options]:
  66. if verbose:
  67. msg = "{not_allowed}: {doctype} {having} {label} = {value}".format(
  68. not_allowed=_("Sorry, you are not allowed to access"), doctype=_(df.options),
  69. having=_("having"), label=_(df.label), value=refdoc.get(df.fieldname))
  70. if refdoc.parentfield:
  71. msg = "{doctype}, {row} #{idx}, ".format(doctype=_(refdoc.doctype),
  72. row=_("Row"), idx=refdoc.idx) + msg
  73. msgprint(msg)
  74. has_restricted_data = True
  75. # check all restrictions before returning
  76. return False if has_restricted_data else True
  77. def has_additional_permission(doc):
  78. condition_methods = webnotes.get_hooks("has_permission:" + doc.doctype)
  79. for method in webnotes.get_hooks("has_permission:" + doc.doctype):
  80. if not webnotes.get_attr(method)(doc):
  81. return False
  82. return True
  83. def can_restrict_user(user, doctype, docname=None):
  84. if not can_restrict(doctype, docname):
  85. return False
  86. meta = webnotes.get_doctype(doctype)
  87. # check if target user does not have restrict permission
  88. if has_only_non_restrict_role(meta, user):
  89. return True
  90. return False
  91. def can_restrict(doctype, docname=None):
  92. # System Manager can always restrict
  93. if "System Manager" in webnotes.get_roles():
  94. return True
  95. meta = webnotes.get_doctype(doctype)
  96. # check if current user has read permission for docname
  97. if docname and not has_permission(doctype, "read", docname):
  98. return False
  99. # check if current user has a role with restrict permission
  100. if not has_restrict_permission(meta):
  101. return False
  102. return True
  103. def has_restrict_permission(meta=None, user=None):
  104. return get_user_perms(meta, user).restrict==1
  105. def has_only_non_restrict_role(meta, user):
  106. # check if target user does not have restrict permission
  107. if has_restrict_permission(meta, user):
  108. return False
  109. # and has non-restrict role
  110. return get_user_perms(meta, user).restrict==0
  111. def can_import(doctype, raise_exception=False):
  112. if not ("System Manager" in webnotes.get_roles() or has_permission(doctype, "import")):
  113. if raise_exception:
  114. raise webnotes.PermissionError("You are not allowed to import: {doctype}".format(doctype=doctype))
  115. else:
  116. return False
  117. return True
  118. def can_export(doctype, raise_exception=False):
  119. if not ("System Manager" in webnotes.get_roles() or has_permission(doctype, "export")):
  120. if raise_exception:
  121. raise webnotes.PermissionError("You are not allowed to export: {doctype}".format(doctype=doctype))
  122. else:
  123. return False
  124. return True