Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

277 linhas
8.2 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. import webnotes
  24. import webnotes.db
  25. import webnotes.utils
  26. import webnotes.profile
  27. import conf
  28. from webnotes.sessions import Session
  29. class HTTPRequest:
  30. def __init__(self):
  31. # Get Environment variables
  32. self.domain = webnotes.get_env_vars('HTTP_HOST')
  33. if self.domain and self.domain.startswith('www.'):
  34. self.domain = self.domain[4:]
  35. # language
  36. self.set_lang(webnotes.get_env_vars('HTTP_ACCEPT_LANGUAGE'))
  37. webnotes.remote_ip = webnotes.get_env_vars('REMOTE_ADDR')
  38. # load cookies
  39. webnotes.cookie_manager = CookieManager()
  40. webnotes.request_method = webnotes.get_env_vars("REQUEST_METHOD")
  41. # override request method. All request to be of type POST, but if _type == "POST" then commit
  42. if webnotes.form_dict.get("_type"):
  43. webnotes.request_method = webnotes.form_dict.get("_type")
  44. del webnotes.form_dict["_type"]
  45. # set db
  46. self.connect()
  47. # login
  48. webnotes.login_manager = LoginManager()
  49. # start session
  50. webnotes.session_obj = Session()
  51. webnotes.session = webnotes.session_obj.data
  52. # check status
  53. if webnotes.conn.get_global("__session_status")=='stop':
  54. webnotes.msgprint(webnotes.conn.get_global("__session_status_message"))
  55. raise webnotes.SessionStopped('Session Stopped')
  56. # load profile
  57. self.setup_profile()
  58. # run login triggers
  59. if webnotes.form_dict.get('cmd')=='login':
  60. webnotes.login_manager.run_trigger('on_login_post_session')
  61. # write out cookies
  62. webnotes.cookie_manager.set_cookies()
  63. def set_lang(self, lang):
  64. try:
  65. from startup import lang_list
  66. except ImportError, e:
  67. return
  68. if not lang:
  69. return
  70. if ";" in lang: # not considering weightage
  71. lang = lang.split(";")[0]
  72. if "," in lang:
  73. lang = lang.split(",")
  74. else:
  75. lang = [lang]
  76. for l in lang:
  77. code = l.strip()
  78. if code in lang_list:
  79. webnotes.lang = code
  80. return
  81. # check if parent language (pt) is setup, if variant (pt-BR)
  82. if "-" in code:
  83. code = code.split("-")[0]
  84. if code in lang_list:
  85. webnotes.lang = code
  86. return
  87. def setup_profile(self):
  88. webnotes.user = webnotes.profile.Profile()
  89. def get_db_name(self):
  90. """get database name from conf"""
  91. return conf.db_name
  92. def connect(self, ac_name = None):
  93. """connect to db, from ac_name or db_name"""
  94. webnotes.conn = webnotes.db.Database(user = self.get_db_name(), \
  95. password = getattr(conf,'db_password', ''))
  96. class LoginManager:
  97. def __init__(self):
  98. if webnotes.form_dict.get('cmd')=='login':
  99. # clear cache
  100. from webnotes.sessions import clear_cache
  101. clear_cache(webnotes.form_dict.get('usr'))
  102. self.authenticate()
  103. self.post_login()
  104. webnotes.response['message'] = 'Logged In'
  105. def post_login(self):
  106. self.run_trigger()
  107. self.validate_ip_address()
  108. self.validate_hour()
  109. def authenticate(self, user=None, pwd=None):
  110. if not (user and pwd):
  111. user, pwd = webnotes.form_dict.get('usr'), webnotes.form_dict.get('pwd')
  112. if not (user and pwd):
  113. self.fail('Incomplete login details')
  114. self.check_if_enabled(user)
  115. self.user = self.check_password(user, pwd)
  116. def check_if_enabled(self, user):
  117. """raise exception if user not enabled"""
  118. from webnotes.utils import cint
  119. if user=='Administrator': return
  120. if not cint(webnotes.conn.get_value('Profile', user, 'enabled')):
  121. self.fail('User disabled or missing')
  122. def check_password(self, user, pwd):
  123. """check password"""
  124. user = webnotes.conn.sql("""select `user` from __Auth where `user`=%s
  125. and `password`=password(%s)""", (user, pwd))
  126. if not user:
  127. self.fail('Incorrect password')
  128. else:
  129. return user[0][0] # in correct case
  130. def fail(self, message):
  131. webnotes.response['message'] = message
  132. raise webnotes.AuthenticationError
  133. def run_trigger(self, method='on_login'):
  134. try:
  135. from startup import event_handlers
  136. if hasattr(event_handlers, method):
  137. getattr(event_handlers, method)(self)
  138. return
  139. except ImportError, e:
  140. pass
  141. def validate_ip_address(self):
  142. """check if IP Address is valid"""
  143. ip_list = webnotes.conn.get_value('Profile', self.user, 'restrict_ip', ignore=True)
  144. if not ip_list:
  145. return
  146. ip_list = ip_list.replace(",", "\n").split('\n')
  147. ip_list = [i.strip() for i in ip_list]
  148. for ip in ip_list:
  149. if webnotes.remote_ip.startswith(ip):
  150. return
  151. webnotes.msgprint('Not allowed from this IP Address')
  152. raise webnotes.AuthenticationError
  153. def validate_hour(self):
  154. """check if user is logging in during restricted hours"""
  155. login_before = int(webnotes.conn.get_value('Profile', self.user, 'login_before', ignore=True) or 0)
  156. login_after = int(webnotes.conn.get_value('Profile', self.user, 'login_after', ignore=True) or 0)
  157. if not (login_before or login_after):
  158. return
  159. from webnotes.utils import now_datetime
  160. current_hour = int(now_datetime().strftime('%H'))
  161. if login_before and current_hour > login_before:
  162. webnotes.msgprint('Not allowed to login after restricted hour', raise_exception=1)
  163. if login_after and current_hour < login_after:
  164. webnotes.msgprint('Not allowed to login before restricted hour', raise_exception=1)
  165. def login_as_guest(self):
  166. """login as guest"""
  167. self.user = 'Guest'
  168. self.post_login()
  169. def logout(self, arg='', user=None):
  170. if not user: user = webnotes.session.user
  171. self.user = user
  172. self.run_trigger('on_logout')
  173. if user in ['demo@erpnext.com', 'Administrator']:
  174. webnotes.conn.sql('delete from tabSessions where sid=%s', webnotes.session.get('sid'))
  175. webnotes.cache().delete_value("session:" + webnotes.session.get("sid"))
  176. else:
  177. from webnotes.sessions import clear_sessions
  178. clear_sessions(user)
  179. class CookieManager:
  180. def __init__(self):
  181. import Cookie
  182. webnotes.cookies = Cookie.SimpleCookie()
  183. self.get_incoming_cookies()
  184. def get_incoming_cookies(self):
  185. import os
  186. cookies = {}
  187. if 'HTTP_COOKIE' in os.environ:
  188. c = os.environ['HTTP_COOKIE']
  189. webnotes.cookies.load(c)
  190. for c in webnotes.cookies.values():
  191. cookies[c.key] = c.value
  192. webnotes.incoming_cookies = cookies
  193. def set_cookies(self):
  194. if not webnotes.session.get('sid'): return
  195. import datetime
  196. # sid expires in 3 days
  197. expires = datetime.datetime.now() + datetime.timedelta(days=3)
  198. expires = expires.strftime('%a, %d %b %Y %H:%M:%S')
  199. webnotes.cookies[b'sid'] = webnotes.session['sid'].encode('utf-8')
  200. webnotes.cookies[b'sid'][b'expires'] = expires.encode('utf-8')
  201. webnotes.cookies[b'sid'][b'path'] = b'/'
  202. def set_remember_me(self):
  203. from webnotes.utils import cint
  204. if not cint(webnotes.form_dict.get('remember_me')): return
  205. remember_days = webnotes.conn.get_value('Control Panel', None,
  206. 'remember_for_days') or 7
  207. import datetime
  208. expires = datetime.datetime.now() + \
  209. datetime.timedelta(days=remember_days)
  210. expires = expires.strftime('%a, %d %b %Y %H:%M:%S')
  211. webnotes.cookies[b'remember_me'] = 1
  212. for k in webnotes.cookies.keys():
  213. webnotes.cookies[k][b'expires'] = expires.encode('utf-8')
  214. def update_password(user, password):
  215. webnotes.conn.sql("""insert into __Auth (user, `password`)
  216. values (%s, password(%s))
  217. on duplicate key update `password`=password(%s)""", (user,
  218. password, password))