blob: 4eec0bc167655f7b334f0ad539fb608fea95bcac [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()
Anand Doshi9fdee382012-01-03 11:26:00 +053047
Anand Doshib63a0072012-01-03 16:11:04 +053048 # Clear previous sessions i.e. logout previous log-in attempts
Anand Doshi523f3a02012-01-05 14:34:30 +053049 exception_list = ['demo@webnotestech.com', 'Administrator']
Anand Doshi9fdee382012-01-03 11:26:00 +053050 if webnotes.session['user'] not in exception_list:
Anand Doshib63a0072012-01-03 16:11:04 +053051 sid_list = webnotes.conn.sql("""
52 SELECT sid
53 FROM `tabSessions`
54 WHERE
55 user=%s AND
56 sid!=%s
57 ORDER BY lastupdate desc""", \
58 (webnotes.session['user'], webnotes.session['sid']), as_list=1)
Anand Doshia6272af2012-01-03 17:02:20 +053059 for sid in sid_list:
60 webnotes.conn.sql("DELETE FROM `tabSessions` WHERE sid=%s", sid[0])
Anand Doshid56d57d2012-01-02 16:25:05 +053061
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053062 update_account_details()
63
64#
65# logout the user from SSO
66#
67def on_logout(login_manager):
68 if cint(webnotes.conn.get_value('Control Panel', None, 'sync_with_gateway')):
Rushabh Mehta9b59f9c2011-07-18 11:31:12 +053069 from server_tools.gateway_utils import logout_sso
Anand Doshid34ff762012-01-03 16:00:03 +053070 logout_sso(user=login_manager.user)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053071
72#
73# create a profile (if logs in for the first time)
74#
75def login_as(user, login_manager):
76 import os
77 import webnotes
78 webnotes.session = {'user': user}
79 ip = os.environ.get('REMOTE_ADDR')
80
81 # validate if user is from SSO
82 if ip == '72.55.168.105' or 1:
83 # if user does not exist, create it
84 if not webnotes.conn.sql("select name from tabProfile where name=%s", user):
85 from webnotes.model.doc import Document
86
87 import webnotes
88 import webnotes.utils.webservice
89
90 p = Document('Profile')
91 p.first_name = webnotes.form_dict.get('first_name')
92 p.last_name = webnotes.form_dict.get('last_name')
93 p.email = user
94 p.name = user
95 p.enabled = 1
96 p.owner = user
97 p.save(1)
Anand Doshid56d57d2012-01-02 16:25:05 +053098