blob: 107a5d759e91f99e2d3ecdc63418a9068a4e6d78 [file] [log] [blame]
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05301# ERPNext - web based ERP (http://erpnext.com)
2# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053017import webnotes
Anand Doshi90f6e552012-01-16 14:13:39 +053018import webnotes.defs
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053019from webnotes.utils import cint
Rushabh Mehta63d669f2012-02-03 12:56:12 +053020import home
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053021
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053022def on_login(login_manager):
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +053023 """
24 called from login manager, before login
25 """
Rushabh Mehta7c76f802012-02-16 10:58:45 +010026 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 +053027 try:
Rushabh Mehta533bc772012-02-15 09:35:42 +010028 login_manager = login_as(login_manager)
Anand Doshi96188f02012-03-15 11:11:57 +053029 if hasattr(webnotes.defs, 'sync_with_gateway') and \
30 cint(webnotes.defs.sync_with_gateway) or 0:
31 update_account_details()
32 import server_tools.gateway_utils
33 server_tools.gateway_utils.check_login(login_manager.user)
Anand Doshi737c2f92012-02-14 16:16:13 +053034
Rushabh Mehta63d669f2012-02-03 12:56:12 +053035 except ImportError:
36 pass
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053037
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053038def on_login_post_session(login_manager):
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +053039 """
40 called after login
41 update login_from and delete parallel sessions
42 """
Anand Doshib63a0072012-01-03 16:11:04 +053043 # Clear previous sessions i.e. logout previous log-in attempts
Rushabh Mehtad31bf622012-01-19 15:09:49 +053044 exception_list = ['demo@webnotestech.com', 'Administrator', 'Guest']
Anand Doshi9fdee382012-01-03 11:26:00 +053045 if webnotes.session['user'] not in exception_list:
Anand Doshib63a0072012-01-03 16:11:04 +053046 sid_list = webnotes.conn.sql("""
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +053047 DELETE FROM `tabSessions`
Anand Doshib63a0072012-01-03 16:11:04 +053048 WHERE
49 user=%s AND
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +053050 sid!=%s""", \
Anand Doshib63a0072012-01-03 16:11:04 +053051 (webnotes.session['user'], webnotes.session['sid']), as_list=1)
Rushabh Mehta49ebfb62012-01-20 15:32:18 +053052
Anand Doshi5cc8cd72012-03-02 15:42:08 +053053 if webnotes.session['user'] not in ('Guest', 'demo@webnotestech.com') and webnotes.conn.cur_db_name!='accounts':
Rushabh Mehta63d669f2012-02-03 12:56:12 +053054 # create feed
55 from webnotes.utils import nowtime
56 home.make_feed('Login', 'Profile', login_manager.user, login_manager.user,
57 '%s logged in at %s' % (login_manager.user_fullname, nowtime()),
58 login_manager.user=='Administrator' and '#8CA2B3' or '#1B750D')
59
Rushabh Mehta533bc772012-02-15 09:35:42 +010060
Rushabh Mehta63d669f2012-02-03 12:56:12 +053061def comment_added(doc):
62 """add comment to feed"""
Rushabh Mehta63d669f2012-02-03 12:56:12 +053063 home.make_feed('Comment', doc.comment_doctype, doc.comment_docname, doc.comment_by,
64 '<i>"' + doc.comment + '"</i>', '#6B24B3')
65
Rushabh Mehta49ebfb62012-01-20 15:32:18 +053066def doclist_all(doc, method):
67 """doclist trigger called from webnotes.model.doclist on any event"""
Rushabh Mehta865c00a2012-01-24 14:33:21 +053068 home.update_feed(doc, method)
69
70def boot_session(bootinfo):
71 """boot session - send website info if guest"""
72 import webnotes
73 import webnotes.model.doc
74
Rushabh Mehtac5471dd2012-02-22 12:07:42 +053075 bootinfo['custom_css'] = webnotes.conn.get_value('Style Settings', None, 'custom_css') or ''
76
Rushabh Mehta865c00a2012-01-24 14:33:21 +053077 if webnotes.session['user']=='Guest':
Rushabh Mehta7018b192012-02-02 13:42:28 +053078 bootinfo['website_settings'] = webnotes.model.doc.getsingle('Website Settings')
Rushabh Mehtaf35992f2012-02-07 10:39:17 +053079 bootinfo['website_menus'] = webnotes.conn.sql("""select label, url, custom_page,
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053080 parent_label, parentfield
Rushabh Mehta7018b192012-02-02 13:42:28 +053081 from `tabTop Bar Item` where parent='Website Settings' order by idx asc""", as_dict=1)
Rushabh Mehtaeec8e922012-02-14 17:25:31 +053082 bootinfo['analytics_code'] = \
83 webnotes.conn.get_value('Website Settings', None, 'analytics_code')
84 bootinfo['analytics_call'] = \
85 webnotes.conn.get_value('Website Settings', None, 'analytics_call')
Rushabh Mehtaaa999df2012-02-27 12:54:04 +053086
Rushabh Mehta865c00a2012-01-24 14:33:21 +053087 else:
88 bootinfo['letter_heads'] = get_letter_heads()
89
Rushabh Mehtaaa999df2012-02-27 12:54:04 +053090 import webnotes.model.doctype
91 bootinfo['docs'] += webnotes.model.doctype.get('Event')
Rushabh Mehta17da7642012-02-28 18:56:56 +053092
93 bootinfo['modules_list'] = webnotes.conn.get_global('modules_list')
Rushabh Mehta12852e72012-02-29 15:11:06 +053094
95 # if no company, show a dialog box to create a new company
96 bootinfo['setup_complete'] = webnotes.conn.sql("""select name from
97 tabCompany limit 1""") and 'Yes' or 'No'
Rushabh Mehta412a65c2012-02-29 18:38:18 +053098
99 bootinfo['user_background'] = webnotes.conn.get_value("Profile", webnotes.session['user'], 'background_image') or ''
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530100
Rushabh Mehta865c00a2012-01-24 14:33:21 +0530101def get_letter_heads():
102 """load letter heads with startup"""
103 import webnotes
Rushabh Mehta63d669f2012-02-03 12:56:12 +0530104 ret = webnotes.conn.sql("""select name, content from `tabLetter Head`
105 where ifnull(disabled,0)=0""")
Rushabh Mehta4b1fe052012-01-25 15:06:28 +0530106 return dict(ret)
Anand Doshi737c2f92012-02-14 16:16:13 +0530107
Anand Doshi737c2f92012-02-14 16:16:13 +0530108def login_as(login_manager):
109 """
110 Login as functionality -- allows signin from signin.erpnext.com
111 """
112 # login as user
113 user = webnotes.form.getvalue('login_as')
114 if user:
115 if isinstance(webnotes.session, dict):
116 webnotes.session['user'] = user
117 else:
118 webnotes.session = {'user': user}
119
120 login_manager.user = user
Rushabh Mehta533bc772012-02-15 09:35:42 +0100121 first_name, last_name = webnotes.conn.sql("select first_name, last_name from `tabProfile` where name=%s", user)[0]
122
123 login_manager.user_fullname = (first_name and first_name or "") + (last_name and " " + last_name or "")
Anand Doshi737c2f92012-02-14 16:16:13 +0530124
125 if hasattr(webnotes.defs, 'validate_ip'):
126 msg = getattr(webnotes.defs, 'validate_ip')()
127 if msg: webnotes.msgprint(msg, raise_exception=1)
128
129 # alisaing here... so check if the user is disabled
130 if not webnotes.conn.sql("select ifnull(enabled,0) from tabProfile where name=%s", user)[0][0]:
131 # throw execption
132 webnotes.msgprint("Authentication Failed", raise_exception=1)
Rushabh Mehta533bc772012-02-15 09:35:42 +0100133
134 return login_manager
135
136#
137# update account details
138#
139def update_account_details():
140 # additional details (if from gateway)
141 if webnotes.form_dict.get('is_trial'):
142 webnotes.conn.set_global('is_trial', cint(webnotes.form_dict.get('is_trial')))
143
144 if webnotes.form_dict.get('days_to_expiry'):
145 webnotes.conn.set_global('days_to_expiry', webnotes.form_dict.get('days_to_expiry'))
146
147 if webnotes.form_dict.get('first_name'):
148 from server_tools.gateway_utils import update_user_details
149 update_user_details()
150
151 if webnotes.form_dict.get('xpassword') and webnotes.form_dict.get('login_as')!='Administrator':
152 webnotes.conn.sql("""update tabProfile set password=password(%(xpassword)s) where name=%(login_as)s""", (webnotes.form_dict))
153
154 if webnotes.form_dict.get('url_name'):
155 from webnotes.utils import set_default
156 set_default('account_url', 'http://'+webnotes.form_dict.get('url_name'))
157
158#
159# logout the user from SSO
160#
161def on_logout(login_manager):
Anand Doshi96188f02012-03-15 11:11:57 +0530162 import webnotes.defs
163 if hasattr(webnotes.defs, 'sync_with_gateway') and \
164 cint(webnotes.defs.sync_with_gateway) or 0:
Rushabh Mehta533bc772012-02-15 09:35:42 +0100165 from server_tools.gateway_utils import logout_sso
166 logout_sso(user=login_manager.user)