Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 1 | import webnotes |
| 2 | from webnotes.utils import cint |
| 3 | |
| 4 | # |
| 5 | # alias the current user |
| 6 | # |
| 7 | def 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 | # |
| 27 | def 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 Mehta | deb8eb9 | 2011-07-18 13:41:35 +0530 | [diff] [blame] | 36 | from server_tools.gateway_utils import update_user_details |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 37 | update_user_details() |
| 38 | |
| 39 | # |
| 40 | # save (login from) |
| 41 | # |
| 42 | def 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 Mehta | 3526070 | 2011-07-18 11:35:30 +0530 | [diff] [blame] | 46 | webnotes.session_obj.update() |
Anand Doshi | 9fdee38 | 2012-01-03 11:26:00 +0530 | [diff] [blame] | 47 | |
Anand Doshi | b63a007 | 2012-01-03 16:11:04 +0530 | [diff] [blame] | 48 | # Clear previous sessions i.e. logout previous log-in attempts |
Anand Doshi | 523f3a0 | 2012-01-05 14:34:30 +0530 | [diff] [blame] | 49 | exception_list = ['demo@webnotestech.com', 'Administrator'] |
Anand Doshi | 9fdee38 | 2012-01-03 11:26:00 +0530 | [diff] [blame] | 50 | if webnotes.session['user'] not in exception_list: |
Anand Doshi | b63a007 | 2012-01-03 16:11:04 +0530 | [diff] [blame] | 51 | 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 Doshi | a6272af | 2012-01-03 17:02:20 +0530 | [diff] [blame] | 59 | for sid in sid_list: |
| 60 | webnotes.conn.sql("DELETE FROM `tabSessions` WHERE sid=%s", sid[0]) |
Anand Doshi | d56d57d | 2012-01-02 16:25:05 +0530 | [diff] [blame] | 61 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 62 | update_account_details() |
| 63 | |
| 64 | # |
| 65 | # logout the user from SSO |
| 66 | # |
| 67 | def on_logout(login_manager): |
| 68 | if cint(webnotes.conn.get_value('Control Panel', None, 'sync_with_gateway')): |
Rushabh Mehta | 9b59f9c | 2011-07-18 11:31:12 +0530 | [diff] [blame] | 69 | from server_tools.gateway_utils import logout_sso |
Anand Doshi | d34ff76 | 2012-01-03 16:00:03 +0530 | [diff] [blame] | 70 | logout_sso(user=login_manager.user) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 71 | |
| 72 | # |
| 73 | # create a profile (if logs in for the first time) |
| 74 | # |
| 75 | def 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 Doshi | d56d57d | 2012-01-02 16:25:05 +0530 | [diff] [blame] | 98 | |