Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

permission_manager.py 2.9 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from __future__ import unicode_literals
  2. import webnotes
  3. import webnotes.defaults
  4. @webnotes.whitelist(allow_roles=["System Manager", "Administrator"])
  5. def get_roles_and_doctypes():
  6. return {
  7. "doctypes": [d[0] for d in webnotes.conn.sql("""select name from tabDocType where
  8. ifnull(istable,0)=0 and
  9. ifnull(issingle,0)=0 and
  10. name not in ('DocType')""")],
  11. "roles": [d[0] for d in webnotes.conn.sql("""select name from tabRole where name not in
  12. ('Guest', 'Administrator')""")]
  13. }
  14. @webnotes.whitelist(allow_roles=["System Manager", "Administrator"])
  15. def get_permissions(doctype=None, role=None):
  16. return webnotes.conn.sql("""select * from tabDocPerm
  17. where %s%s order by parent, permlevel, role""" % (\
  18. doctype and (" parent='%s'" % doctype) or "",
  19. role and ((doctype and " and " or "") + " role='%s'" % role) or "",
  20. ), as_dict=True)
  21. @webnotes.whitelist(allow_roles=["System Manager", "Administrator"])
  22. def remove(doctype, name):
  23. match = webnotes.conn.get_value("DocPerm", name, "match")
  24. webnotes.conn.sql("""delete from tabDocPerm where name=%s""", name)
  25. validate_and_reset(doctype, for_remove=True)
  26. if match:
  27. webnotes.defaults.clear_cache()
  28. @webnotes.whitelist(allow_roles=["System Manager", "Administrator"])
  29. def add(parent, role, permlevel):
  30. webnotes.doc(fielddata={
  31. "doctype":"DocPerm",
  32. "__islocal": 1,
  33. "parent": parent,
  34. "parenttype": "DocType",
  35. "parentfield": "permissions",
  36. "role": role,
  37. "permlevel": permlevel,
  38. "read": 1
  39. }).save()
  40. validate_and_reset(parent)
  41. @webnotes.whitelist(allow_roles=["System Manager", "Administrator"])
  42. def update(name, doctype, ptype, value=0):
  43. webnotes.conn.sql("""update tabDocPerm set `%s`=%s where name=%s"""\
  44. % (ptype, '%s', '%s'), (value, name))
  45. validate_and_reset(doctype)
  46. if ptype == "read" and webnotes.conn.get_value("DocPerm", name, "match"):
  47. webnotes.defaults.clear_cache()
  48. @webnotes.whitelist(allow_roles=["System Manager", "Administrator"])
  49. def update_match(name, doctype, match=""):
  50. webnotes.conn.sql("""update tabDocPerm set `match`=%s where name=%s""",
  51. (match, name))
  52. validate_and_reset(doctype)
  53. webnotes.defaults.clear_cache()
  54. def validate_and_reset(doctype, for_remove=False):
  55. from core.doctype.doctype.doctype import validate_permissions_for_doctype
  56. validate_permissions_for_doctype(doctype, for_remove)
  57. webnotes.clear_cache(doctype=doctype)
  58. @webnotes.whitelist(allow_roles=["System Manager", "Administrator"])
  59. def reset(doctype):
  60. webnotes.reset_perms(doctype)
  61. webnotes.clear_cache(doctype=doctype)
  62. webnotes.defaults.clear_cache()
  63. @webnotes.whitelist(allow_roles=["System Manager", "Administrator"])
  64. def get_users_with_role(role):
  65. return [p[0] for p in webnotes.conn.sql("""select distinct tabProfile.name
  66. from tabUserRole, tabProfile where
  67. tabUserRole.role=%s
  68. and tabProfile.name != "Administrator"
  69. and tabUserRole.parent = tabProfile.name
  70. and ifnull(tabProfile.enabled,0)=1""", role)]