blob: 53b979e066e5a26a49567b23ffb8fe735883c05e [file] [log] [blame]
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05301import webnotes
2from webnotes.utils import cint
3
4#
5# alias the current user
6#
7def on_login(login_manager):
8
9 # login as
10 if login_manager.user == 'Administrator':
11 user = webnotes.form.getvalue('login_as')
12
13 if user:
14 # create if missing (due to some bug)
15 login_as(user, login_manager)
16
17 # alisaing here... so check if the user is disabled
18 if not webnotes.conn.sql("select ifnull(enabled,0) from tabProfile where name=%s", user)[0][0]:
19 # throw execption
20 raise Exception, "Authentication Failed"
21
22 login_manager.user = user
23
24#
25# update account details
26#
27def update_account_details():
28 # additional details (if from gateway)
29 if webnotes.form_dict.get('is_trial'):
30 webnotes.conn.set_global('is_trial', cint(webnotes.form_dict.get('is_trial')))
31
32 if webnotes.form_dict.get('days_to_expiry'):
33 webnotes.conn.set_global('days_to_expiry', webnotes.form_dict.get('days_to_expiry'))
34
35 if webnotes.form_dict.get('first_name'):
Rushabh Mehtadeb8eb92011-07-18 13:41:35 +053036 from server_tools.gateway_utils import update_user_details
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053037 update_user_details()
38
39#
40# save (login from)
41#
42def on_login_post_session(login_manager):
43 # login from
44 if webnotes.form_dict.get('login_from'):
45 webnotes.session['data']['login_from'] = webnotes.form.getvalue('login_from')
Rushabh Mehta35260702011-07-18 11:35:30 +053046 webnotes.session_obj.update()
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053047
48 update_account_details()
49
50#
51# logout the user from SSO
52#
53def on_logout(login_manager):
54 if cint(webnotes.conn.get_value('Control Panel', None, 'sync_with_gateway')):
Rushabh Mehta9b59f9c2011-07-18 11:31:12 +053055 from server_tools.gateway_utils import logout_sso
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053056 logout_sso()
57
58#
59# create a profile (if logs in for the first time)
60#
61def login_as(user, login_manager):
62 import os
63 import webnotes
64 webnotes.session = {'user': user}
65 ip = os.environ.get('REMOTE_ADDR')
66
67 # validate if user is from SSO
68 if ip == '72.55.168.105' or 1:
69 # if user does not exist, create it
70 if not webnotes.conn.sql("select name from tabProfile where name=%s", user):
71 from webnotes.model.doc import Document
72
73 import webnotes
74 import webnotes.utils.webservice
75
76 p = Document('Profile')
77 p.first_name = webnotes.form_dict.get('first_name')
78 p.last_name = webnotes.form_dict.get('last_name')
79 p.email = user
80 p.name = user
81 p.enabled = 1
82 p.owner = user
83 p.save(1)
84