blob: 729648f2acaabd2e63d5798b4ee02e688d8c3631 [file] [log] [blame]
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05301import webnotes
Anand Doshi90f6e552012-01-16 14:13:39 +05302import webnotes.defs
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05303from webnotes.utils import cint
Rushabh Mehta63d669f2012-02-03 12:56:12 +05304import home
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05305
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05306def on_login(login_manager):
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +05307 """
8 called from login manager, before login
9 """
Rushabh Mehta7c76f802012-02-16 10:58:45 +010010 if login_manager.user not in ('Guest', None, '') and webnotes.conn.cur_db_name!='accounts' and webnotes.conn.get_value('Control Panel', 'Control Panel', 'account_id')!='s5u011':
Rushabh Mehta63d669f2012-02-03 12:56:12 +053011 try:
Rushabh Mehta533bc772012-02-15 09:35:42 +010012 login_manager = login_as(login_manager)
13 update_account_details()
Rushabh Mehta88ba7b82012-01-18 06:18:52 +010014 import server_tools.gateway_utils
15 server_tools.gateway_utils.check_login(login_manager.user)
Anand Doshi737c2f92012-02-14 16:16:13 +053016
Rushabh Mehta63d669f2012-02-03 12:56:12 +053017 except ImportError:
18 pass
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053019
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053020def on_login_post_session(login_manager):
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +053021 """
22 called after login
23 update login_from and delete parallel sessions
24 """
Anand Doshib63a0072012-01-03 16:11:04 +053025 # Clear previous sessions i.e. logout previous log-in attempts
Rushabh Mehtad31bf622012-01-19 15:09:49 +053026 exception_list = ['demo@webnotestech.com', 'Administrator', 'Guest']
Anand Doshi9fdee382012-01-03 11:26:00 +053027 if webnotes.session['user'] not in exception_list:
Anand Doshib63a0072012-01-03 16:11:04 +053028 sid_list = webnotes.conn.sql("""
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +053029 DELETE FROM `tabSessions`
Anand Doshib63a0072012-01-03 16:11:04 +053030 WHERE
31 user=%s AND
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +053032 sid!=%s""", \
Anand Doshib63a0072012-01-03 16:11:04 +053033 (webnotes.session['user'], webnotes.session['sid']), as_list=1)
Rushabh Mehta49ebfb62012-01-20 15:32:18 +053034
Rushabh Mehta7c76f802012-02-16 10:58:45 +010035 if webnotes.session['user'] not in ('Guest') and webnotes.conn.cur_db_name!='accounts':
Rushabh Mehta63d669f2012-02-03 12:56:12 +053036 # create feed
37 from webnotes.utils import nowtime
38 home.make_feed('Login', 'Profile', login_manager.user, login_manager.user,
39 '%s logged in at %s' % (login_manager.user_fullname, nowtime()),
40 login_manager.user=='Administrator' and '#8CA2B3' or '#1B750D')
41
Rushabh Mehta533bc772012-02-15 09:35:42 +010042
Rushabh Mehta63d669f2012-02-03 12:56:12 +053043def comment_added(doc):
44 """add comment to feed"""
Rushabh Mehta63d669f2012-02-03 12:56:12 +053045 home.make_feed('Comment', doc.comment_doctype, doc.comment_docname, doc.comment_by,
46 '<i>"' + doc.comment + '"</i>', '#6B24B3')
47
Rushabh Mehta49ebfb62012-01-20 15:32:18 +053048def doclist_all(doc, method):
49 """doclist trigger called from webnotes.model.doclist on any event"""
Rushabh Mehta865c00a2012-01-24 14:33:21 +053050 home.update_feed(doc, method)
51
52def boot_session(bootinfo):
53 """boot session - send website info if guest"""
54 import webnotes
55 import webnotes.model.doc
56
Rushabh Mehtac5471dd2012-02-22 12:07:42 +053057 bootinfo['custom_css'] = webnotes.conn.get_value('Style Settings', None, 'custom_css') or ''
58
Rushabh Mehta865c00a2012-01-24 14:33:21 +053059 if webnotes.session['user']=='Guest':
Rushabh Mehta7018b192012-02-02 13:42:28 +053060 bootinfo['website_settings'] = webnotes.model.doc.getsingle('Website Settings')
Rushabh Mehtaf35992f2012-02-07 10:39:17 +053061 bootinfo['website_menus'] = webnotes.conn.sql("""select label, url, custom_page,
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053062 parent_label, parentfield
Rushabh Mehta7018b192012-02-02 13:42:28 +053063 from `tabTop Bar Item` where parent='Website Settings' order by idx asc""", as_dict=1)
Rushabh Mehtaeec8e922012-02-14 17:25:31 +053064 bootinfo['analytics_code'] = \
65 webnotes.conn.get_value('Website Settings', None, 'analytics_code')
66 bootinfo['analytics_call'] = \
67 webnotes.conn.get_value('Website Settings', None, 'analytics_call')
Rushabh Mehta865c00a2012-01-24 14:33:21 +053068 else:
69 bootinfo['letter_heads'] = get_letter_heads()
70
71def get_letter_heads():
72 """load letter heads with startup"""
73 import webnotes
Rushabh Mehta63d669f2012-02-03 12:56:12 +053074 ret = webnotes.conn.sql("""select name, content from `tabLetter Head`
75 where ifnull(disabled,0)=0""")
Rushabh Mehta4b1fe052012-01-25 15:06:28 +053076 return dict(ret)
Anand Doshi737c2f92012-02-14 16:16:13 +053077
78
79def login_as(login_manager):
80 """
81 Login as functionality -- allows signin from signin.erpnext.com
82 """
83 # login as user
84 user = webnotes.form.getvalue('login_as')
85 if user:
86 if isinstance(webnotes.session, dict):
87 webnotes.session['user'] = user
88 else:
89 webnotes.session = {'user': user}
90
91 login_manager.user = user
Rushabh Mehta533bc772012-02-15 09:35:42 +010092 first_name, last_name = webnotes.conn.sql("select first_name, last_name from `tabProfile` where name=%s", user)[0]
93
94 login_manager.user_fullname = (first_name and first_name or "") + (last_name and " " + last_name or "")
Anand Doshi737c2f92012-02-14 16:16:13 +053095
96 if hasattr(webnotes.defs, 'validate_ip'):
97 msg = getattr(webnotes.defs, 'validate_ip')()
98 if msg: webnotes.msgprint(msg, raise_exception=1)
99
100 # alisaing here... so check if the user is disabled
101 if not webnotes.conn.sql("select ifnull(enabled,0) from tabProfile where name=%s", user)[0][0]:
102 # throw execption
103 webnotes.msgprint("Authentication Failed", raise_exception=1)
Rushabh Mehta533bc772012-02-15 09:35:42 +0100104
105 return login_manager
106
107#
108# update account details
109#
110def update_account_details():
111 # additional details (if from gateway)
112 if webnotes.form_dict.get('is_trial'):
113 webnotes.conn.set_global('is_trial', cint(webnotes.form_dict.get('is_trial')))
114
115 if webnotes.form_dict.get('days_to_expiry'):
116 webnotes.conn.set_global('days_to_expiry', webnotes.form_dict.get('days_to_expiry'))
117
118 if webnotes.form_dict.get('first_name'):
119 from server_tools.gateway_utils import update_user_details
120 update_user_details()
121
122 if webnotes.form_dict.get('xpassword') and webnotes.form_dict.get('login_as')!='Administrator':
123 webnotes.conn.sql("""update tabProfile set password=password(%(xpassword)s) where name=%(login_as)s""", (webnotes.form_dict))
124
125 if webnotes.form_dict.get('url_name'):
126 from webnotes.utils import set_default
127 set_default('account_url', 'http://'+webnotes.form_dict.get('url_name'))
128
129#
130# logout the user from SSO
131#
132def on_logout(login_manager):
133 if cint(webnotes.conn.get_value('Control Panel', None, 'sync_with_gateway')):
134 from server_tools.gateway_utils import logout_sso
135 logout_sso(user=login_manager.user)