blob: 8def5bb5bd0f7bdd2cecf1ec6486112fb1b5323b [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)
29 update_account_details()
Rushabh Mehta88ba7b82012-01-18 06:18:52 +010030 import server_tools.gateway_utils
31 server_tools.gateway_utils.check_login(login_manager.user)
Anand Doshi737c2f92012-02-14 16:16:13 +053032
Rushabh Mehta63d669f2012-02-03 12:56:12 +053033 except ImportError:
34 pass
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053035
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053036def on_login_post_session(login_manager):
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +053037 """
38 called after login
39 update login_from and delete parallel sessions
40 """
Anand Doshib63a0072012-01-03 16:11:04 +053041 # Clear previous sessions i.e. logout previous log-in attempts
Rushabh Mehtad31bf622012-01-19 15:09:49 +053042 exception_list = ['demo@webnotestech.com', 'Administrator', 'Guest']
Anand Doshi9fdee382012-01-03 11:26:00 +053043 if webnotes.session['user'] not in exception_list:
Anand Doshib63a0072012-01-03 16:11:04 +053044 sid_list = webnotes.conn.sql("""
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +053045 DELETE FROM `tabSessions`
Anand Doshib63a0072012-01-03 16:11:04 +053046 WHERE
47 user=%s AND
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +053048 sid!=%s""", \
Anand Doshib63a0072012-01-03 16:11:04 +053049 (webnotes.session['user'], webnotes.session['sid']), as_list=1)
Rushabh Mehta49ebfb62012-01-20 15:32:18 +053050
Rushabh Mehta7c76f802012-02-16 10:58:45 +010051 if webnotes.session['user'] not in ('Guest') and webnotes.conn.cur_db_name!='accounts':
Rushabh Mehta63d669f2012-02-03 12:56:12 +053052 # create feed
53 from webnotes.utils import nowtime
54 home.make_feed('Login', 'Profile', login_manager.user, login_manager.user,
55 '%s logged in at %s' % (login_manager.user_fullname, nowtime()),
56 login_manager.user=='Administrator' and '#8CA2B3' or '#1B750D')
57
Rushabh Mehta533bc772012-02-15 09:35:42 +010058
Rushabh Mehta63d669f2012-02-03 12:56:12 +053059def comment_added(doc):
60 """add comment to feed"""
Rushabh Mehta63d669f2012-02-03 12:56:12 +053061 home.make_feed('Comment', doc.comment_doctype, doc.comment_docname, doc.comment_by,
62 '<i>"' + doc.comment + '"</i>', '#6B24B3')
63
Rushabh Mehta49ebfb62012-01-20 15:32:18 +053064def doclist_all(doc, method):
65 """doclist trigger called from webnotes.model.doclist on any event"""
Rushabh Mehta865c00a2012-01-24 14:33:21 +053066 home.update_feed(doc, method)
67
68def boot_session(bootinfo):
69 """boot session - send website info if guest"""
70 import webnotes
71 import webnotes.model.doc
72
Rushabh Mehtac5471dd2012-02-22 12:07:42 +053073 bootinfo['custom_css'] = webnotes.conn.get_value('Style Settings', None, 'custom_css') or ''
74
Rushabh Mehta865c00a2012-01-24 14:33:21 +053075 if webnotes.session['user']=='Guest':
Rushabh Mehta7018b192012-02-02 13:42:28 +053076 bootinfo['website_settings'] = webnotes.model.doc.getsingle('Website Settings')
Rushabh Mehtaf35992f2012-02-07 10:39:17 +053077 bootinfo['website_menus'] = webnotes.conn.sql("""select label, url, custom_page,
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053078 parent_label, parentfield
Rushabh Mehta7018b192012-02-02 13:42:28 +053079 from `tabTop Bar Item` where parent='Website Settings' order by idx asc""", as_dict=1)
Rushabh Mehtaeec8e922012-02-14 17:25:31 +053080 bootinfo['analytics_code'] = \
81 webnotes.conn.get_value('Website Settings', None, 'analytics_code')
82 bootinfo['analytics_call'] = \
83 webnotes.conn.get_value('Website Settings', None, 'analytics_call')
Rushabh Mehta865c00a2012-01-24 14:33:21 +053084 else:
85 bootinfo['letter_heads'] = get_letter_heads()
86
87def get_letter_heads():
88 """load letter heads with startup"""
89 import webnotes
Rushabh Mehta63d669f2012-02-03 12:56:12 +053090 ret = webnotes.conn.sql("""select name, content from `tabLetter Head`
91 where ifnull(disabled,0)=0""")
Rushabh Mehta4b1fe052012-01-25 15:06:28 +053092 return dict(ret)
Anand Doshi737c2f92012-02-14 16:16:13 +053093
94
95def login_as(login_manager):
96 """
97 Login as functionality -- allows signin from signin.erpnext.com
98 """
99 # login as user
100 user = webnotes.form.getvalue('login_as')
101 if user:
102 if isinstance(webnotes.session, dict):
103 webnotes.session['user'] = user
104 else:
105 webnotes.session = {'user': user}
106
107 login_manager.user = user
Rushabh Mehta533bc772012-02-15 09:35:42 +0100108 first_name, last_name = webnotes.conn.sql("select first_name, last_name from `tabProfile` where name=%s", user)[0]
109
110 login_manager.user_fullname = (first_name and first_name or "") + (last_name and " " + last_name or "")
Anand Doshi737c2f92012-02-14 16:16:13 +0530111
112 if hasattr(webnotes.defs, 'validate_ip'):
113 msg = getattr(webnotes.defs, 'validate_ip')()
114 if msg: webnotes.msgprint(msg, raise_exception=1)
115
116 # alisaing here... so check if the user is disabled
117 if not webnotes.conn.sql("select ifnull(enabled,0) from tabProfile where name=%s", user)[0][0]:
118 # throw execption
119 webnotes.msgprint("Authentication Failed", raise_exception=1)
Rushabh Mehta533bc772012-02-15 09:35:42 +0100120
121 return login_manager
122
123#
124# update account details
125#
126def update_account_details():
127 # additional details (if from gateway)
128 if webnotes.form_dict.get('is_trial'):
129 webnotes.conn.set_global('is_trial', cint(webnotes.form_dict.get('is_trial')))
130
131 if webnotes.form_dict.get('days_to_expiry'):
132 webnotes.conn.set_global('days_to_expiry', webnotes.form_dict.get('days_to_expiry'))
133
134 if webnotes.form_dict.get('first_name'):
135 from server_tools.gateway_utils import update_user_details
136 update_user_details()
137
138 if webnotes.form_dict.get('xpassword') and webnotes.form_dict.get('login_as')!='Administrator':
139 webnotes.conn.sql("""update tabProfile set password=password(%(xpassword)s) where name=%(login_as)s""", (webnotes.form_dict))
140
141 if webnotes.form_dict.get('url_name'):
142 from webnotes.utils import set_default
143 set_default('account_url', 'http://'+webnotes.form_dict.get('url_name'))
144
145#
146# logout the user from SSO
147#
148def on_logout(login_manager):
149 if cint(webnotes.conn.get_value('Control Panel', None, 'sync_with_gateway')):
150 from server_tools.gateway_utils import logout_sso
151 logout_sso(user=login_manager.user)