You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

139 lines
3.7 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import frappe
  5. from frappe import _
  6. import frappe.utils
  7. import frappe.async
  8. import frappe.sessions
  9. import frappe.utils.file_manager
  10. import frappe.desk.form.run_method
  11. from frappe.utils.response import build_response
  12. from werkzeug.wrappers import Response
  13. from six import string_types
  14. def handle():
  15. """handle request"""
  16. cmd = frappe.local.form_dict.cmd
  17. data = None
  18. if cmd!='login':
  19. data = execute_cmd(cmd)
  20. if data:
  21. if isinstance(data, Response):
  22. # method returns a response object, pass it on
  23. return data
  24. # add the response to `message` label
  25. frappe.response['message'] = data
  26. return build_response("json")
  27. def execute_cmd(cmd, from_async=False):
  28. """execute a request as python module"""
  29. for hook in frappe.get_hooks("override_whitelisted_methods", {}).get(cmd, []):
  30. # override using the first hook
  31. cmd = hook
  32. break
  33. try:
  34. method = get_attr(cmd)
  35. except:
  36. frappe.respond_as_web_page(title='Invalid Method', html='Method not found',
  37. indicator_color='red', http_status_code=404)
  38. return
  39. if from_async:
  40. method = method.queue
  41. is_whitelisted(method)
  42. return frappe.call(method, **frappe.form_dict)
  43. def is_whitelisted(method):
  44. # check if whitelisted
  45. if frappe.session['user'] == 'Guest':
  46. if (method not in frappe.guest_methods):
  47. frappe.msgprint(_("Not permitted"))
  48. raise frappe.PermissionError('Not Allowed, {0}'.format(method))
  49. if method not in frappe.xss_safe_methods:
  50. # strictly sanitize form_dict
  51. # escapes html characters like <> except for predefined tags like a, b, ul etc.
  52. for key, value in frappe.form_dict.items():
  53. if isinstance(value, string_types):
  54. frappe.form_dict[key] = frappe.utils.sanitize_html(value)
  55. else:
  56. if not method in frappe.whitelisted:
  57. frappe.msgprint(_("Not permitted"))
  58. raise frappe.PermissionError('Not Allowed, {0}'.format(method))
  59. @frappe.whitelist(allow_guest=True)
  60. def version():
  61. return frappe.__version__
  62. @frappe.whitelist()
  63. def runserverobj(method, docs=None, dt=None, dn=None, arg=None, args=None):
  64. frappe.desk.form.run_method.runserverobj(method, docs=docs, dt=dt, dn=dn, arg=arg, args=args)
  65. @frappe.whitelist(allow_guest=True)
  66. def logout():
  67. frappe.local.login_manager.logout()
  68. frappe.db.commit()
  69. @frappe.whitelist(allow_guest=True)
  70. def web_logout():
  71. frappe.local.login_manager.logout()
  72. frappe.db.commit()
  73. frappe.respond_as_web_page(_("Logged Out"), _("You have been successfully logged out"),
  74. indicator_color='green')
  75. @frappe.whitelist(allow_guest=True)
  76. def run_custom_method(doctype, name, custom_method):
  77. """cmd=run_custom_method&doctype={doctype}&name={name}&custom_method={custom_method}"""
  78. doc = frappe.get_doc(doctype, name)
  79. if getattr(doc, custom_method, frappe._dict()).is_whitelisted:
  80. frappe.call(getattr(doc, custom_method), **frappe.local.form_dict)
  81. else:
  82. frappe.throw(_("Not permitted"), frappe.PermissionError)
  83. @frappe.whitelist()
  84. def uploadfile():
  85. try:
  86. if frappe.form_dict.get('from_form'):
  87. try:
  88. ret = frappe.utils.file_manager.upload()
  89. except frappe.DuplicateEntryError:
  90. # ignore pass
  91. ret = None
  92. frappe.db.rollback()
  93. else:
  94. if frappe.form_dict.get('method'):
  95. method = frappe.get_attr(frappe.form_dict.method)
  96. is_whitelisted(method)
  97. ret = method()
  98. except Exception:
  99. frappe.errprint(frappe.utils.get_traceback())
  100. frappe.response['http_status_code'] = 500
  101. ret = None
  102. return ret
  103. def get_attr(cmd):
  104. """get method object from cmd"""
  105. if '.' in cmd:
  106. method = frappe.get_attr(cmd)
  107. else:
  108. method = globals()[cmd]
  109. frappe.log("method:" + cmd)
  110. return method
  111. @frappe.whitelist()
  112. def ping():
  113. return "pong"