blob: 262771dd4af9d741c17adef5766e3d71974635f4 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt"
Rushabh Mehtace1d5272013-01-09 16:39:27 +05303
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05304
Anand Doshi486f9df2012-07-19 13:40:31 +05305from __future__ import unicode_literals
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05306import webnotes
Rushabh Mehta6de403f2013-12-13 14:10:14 +05307
Rushabh Mehtac652a622013-12-16 12:48:22 +05308def on_session_creation(login_manager):
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +05309 """
10 called after login
11 update login_from and delete parallel sessions
12 """
Anand Doshib63a0072012-01-03 16:11:04 +053013 # Clear previous sessions i.e. logout previous log-in attempts
Rushabh Mehta686022f2012-12-12 15:25:51 +053014 allow_multiple_sessions = ['demo@erpnext.com', 'Administrator', 'Guest']
15 if webnotes.session['user'] not in allow_multiple_sessions:
16 from webnotes.sessions import clear_sessions
17 clear_sessions(webnotes.session.user, keep_current=True)
Rushabh Mehta49ebfb62012-01-20 15:32:18 +053018
Anand Doshi1ed4ef12012-04-27 15:30:23 +053019 # check if account is expired
20 check_if_expired()
21
Nabin Hait8bbc5a22012-11-05 18:58:15 +053022 if webnotes.session['user'] not in ('Guest', 'demo@erpnext.com'):
Rushabh Mehta63d669f2012-02-03 12:56:12 +053023 # create feed
24 from webnotes.utils import nowtime
Rushabh Mehta91ba3462012-07-13 14:54:40 +053025 from webnotes.profile import get_user_fullname
Rushabh Mehta028abd22013-01-31 11:20:10 +053026 webnotes.conn.begin()
Rushabh Mehta6de403f2013-12-13 14:10:14 +053027 make_feed('Login', 'Profile', login_manager.user, login_manager.user,
Anand Doshiaea82042012-10-15 15:30:56 +053028 '%s logged in at %s' % (get_user_fullname(login_manager.user), nowtime()),
Rushabh Mehta028abd22013-01-31 11:20:10 +053029 login_manager.user=='Administrator' and '#8CA2B3' or '#1B750D')
30 webnotes.conn.commit()
Anand Doshi2ac0a832013-07-10 20:49:44 +053031
Anand Doshi2b1d33f2013-07-25 17:17:47 +053032 if webnotes.conn.get_value("Profile", webnotes.session.user, "user_type") == "Website User":
Rushabh Mehta1f847992013-12-12 19:12:19 +053033 from erpnext.selling.utils.cart import set_cart_count
Anand Doshi2ac0a832013-07-10 20:49:44 +053034 set_cart_count()
35
36def on_logout(login_manager):
Anand Doshide8b6aa2013-09-24 17:17:39 +053037 webnotes._response.set_cookie("cart_count", "")
Anand Doshi2ac0a832013-07-10 20:49:44 +053038
Anand Doshi1ed4ef12012-04-27 15:30:23 +053039def check_if_expired():
40 """check if account is expired. If expired, do not allow login"""
Pratik Vyascfed8c42013-09-21 15:16:47 +053041 from webnotes import conf
Anand Doshi1ed4ef12012-04-27 15:30:23 +053042 # check if expires_on is specified
Anand Doshide8b6aa2013-09-24 17:17:39 +053043 if not 'expires_on' in conf: return
Anand Doshi1ed4ef12012-04-27 15:30:23 +053044
45 # check if expired
46 from datetime import datetime, date
47 expires_on = datetime.strptime(conf.expires_on, '%Y-%m-%d').date()
48 if date.today() <= expires_on: return
49
50 # if expired, stop user from logging in
51 from webnotes.utils import formatdate
Anand Doshidcb6c2c2012-09-28 16:09:49 +053052 msg = """Oops! Your subscription expired on <b>%s</b>.<br>""" % formatdate(conf.expires_on)
Anand Doshi62091db2012-09-28 16:06:06 +053053
Rushabh Mehtad1b2afc2012-12-12 15:58:46 +053054 if 'System Manager' in webnotes.user.get_roles():
Anand Doshi62091db2012-09-28 16:06:06 +053055 msg += """Just drop in a mail at <b>support@erpnext.com</b> and
56 we will guide you to get your account re-activated."""
Anand Doshi1ed4ef12012-04-27 15:30:23 +053057 else:
Anand Doshi62091db2012-09-28 16:06:06 +053058 msg += """Just ask your System Manager to drop in a mail at <b>support@erpnext.com</b> and
59 we will guide him to get your account re-activated."""
60
61 webnotes.msgprint(msg)
Anand Doshi1ed4ef12012-04-27 15:30:23 +053062
63 webnotes.response['message'] = 'Account Expired'
Rushabh Mehtac652a622013-12-16 12:48:22 +053064 raise webnotes.AuthenticationError