blob: 304d2e4187a5d0b9d8cd1f8d502ee0ffba727f42 [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
4
5#
6# alias the current user
7#
8def on_login(login_manager):
9
10 # login as
11 if login_manager.user == 'Administrator':
12 user = webnotes.form.getvalue('login_as')
13
14 if user:
15 # create if missing (due to some bug)
16 login_as(user, login_manager)
17
18 # alisaing here... so check if the user is disabled
19 if not webnotes.conn.sql("select ifnull(enabled,0) from tabProfile where name=%s", user)[0][0]:
20 # throw execption
Anand Doshi90f6e552012-01-16 14:13:39 +053021 webnotes.msgprint("Authentication Failed", raise_exception=1)
22
23 if hasattr(webnotes.defs, 'validate_ip'):
24 msg = getattr(webnotes.defs, 'validate_ip')()
25 if msg: webnotes.msgprint(msg, raise_exception=1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053026
27 login_manager.user = user
28
29#
30# update account details
31#
32def update_account_details():
33 # additional details (if from gateway)
34 if webnotes.form_dict.get('is_trial'):
35 webnotes.conn.set_global('is_trial', cint(webnotes.form_dict.get('is_trial')))
36
37 if webnotes.form_dict.get('days_to_expiry'):
38 webnotes.conn.set_global('days_to_expiry', webnotes.form_dict.get('days_to_expiry'))
39
40 if webnotes.form_dict.get('first_name'):
Rushabh Mehtadeb8eb92011-07-18 13:41:35 +053041 from server_tools.gateway_utils import update_user_details
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053042 update_user_details()
43
44#
45# save (login from)
46#
47def on_login_post_session(login_manager):
48 # login from
49 if webnotes.form_dict.get('login_from'):
50 webnotes.session['data']['login_from'] = webnotes.form.getvalue('login_from')
Rushabh Mehta35260702011-07-18 11:35:30 +053051 webnotes.session_obj.update()
Anand Doshi9fdee382012-01-03 11:26:00 +053052
Anand Doshib63a0072012-01-03 16:11:04 +053053 # Clear previous sessions i.e. logout previous log-in attempts
Anand Doshi523f3a02012-01-05 14:34:30 +053054 exception_list = ['demo@webnotestech.com', 'Administrator']
Anand Doshi9fdee382012-01-03 11:26:00 +053055 if webnotes.session['user'] not in exception_list:
Anand Doshib63a0072012-01-03 16:11:04 +053056 sid_list = webnotes.conn.sql("""
57 SELECT sid
58 FROM `tabSessions`
59 WHERE
60 user=%s AND
61 sid!=%s
62 ORDER BY lastupdate desc""", \
63 (webnotes.session['user'], webnotes.session['sid']), as_list=1)
Anand Doshia6272af2012-01-03 17:02:20 +053064 for sid in sid_list:
65 webnotes.conn.sql("DELETE FROM `tabSessions` WHERE sid=%s", sid[0])
Anand Doshid56d57d2012-01-02 16:25:05 +053066
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053067 update_account_details()
68
69#
70# logout the user from SSO
71#
72def on_logout(login_manager):
73 if cint(webnotes.conn.get_value('Control Panel', None, 'sync_with_gateway')):
Rushabh Mehta9b59f9c2011-07-18 11:31:12 +053074 from server_tools.gateway_utils import logout_sso
Anand Doshid34ff762012-01-03 16:00:03 +053075 logout_sso(user=login_manager.user)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053076
77#
78# create a profile (if logs in for the first time)
79#
80def login_as(user, login_manager):
81 import os
82 import webnotes
83 webnotes.session = {'user': user}
84 ip = os.environ.get('REMOTE_ADDR')
85
86 # validate if user is from SSO
87 if ip == '72.55.168.105' or 1:
88 # if user does not exist, create it
89 if not webnotes.conn.sql("select name from tabProfile where name=%s", user):
90 from webnotes.model.doc import Document
91
92 import webnotes
93 import webnotes.utils.webservice
94
95 p = Document('Profile')
96 p.first_name = webnotes.form_dict.get('first_name')
97 p.last_name = webnotes.form_dict.get('last_name')
98 p.email = user
99 p.name = user
100 p.enabled = 1
101 p.owner = user
102 p.save(1)
Anand Doshid56d57d2012-01-02 16:25:05 +0530103