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'): |
| 36 | from server_tools.server_tools.gateway_utils import update_user_details |
| 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') |
| 46 | |
| 47 | update_account_details() |
| 48 | |
| 49 | # |
| 50 | # logout the user from SSO |
| 51 | # |
| 52 | def on_logout(login_manager): |
| 53 | if cint(webnotes.conn.get_value('Control Panel', None, 'sync_with_gateway')): |
| 54 | from server_tools.server_tools.gateway_utils import logout_sso |
| 55 | logout_sso() |
| 56 | |
| 57 | # |
| 58 | # create a profile (if logs in for the first time) |
| 59 | # |
| 60 | def login_as(user, login_manager): |
| 61 | import os |
| 62 | import webnotes |
| 63 | webnotes.session = {'user': user} |
| 64 | ip = os.environ.get('REMOTE_ADDR') |
| 65 | |
| 66 | # validate if user is from SSO |
| 67 | if ip == '72.55.168.105' or 1: |
| 68 | # if user does not exist, create it |
| 69 | if not webnotes.conn.sql("select name from tabProfile where name=%s", user): |
| 70 | from webnotes.model.doc import Document |
| 71 | |
| 72 | import webnotes |
| 73 | import webnotes.utils.webservice |
| 74 | |
| 75 | p = Document('Profile') |
| 76 | p.first_name = webnotes.form_dict.get('first_name') |
| 77 | p.last_name = webnotes.form_dict.get('last_name') |
| 78 | p.email = user |
| 79 | p.name = user |
| 80 | p.enabled = 1 |
| 81 | p.owner = user |
| 82 | p.save(1) |
| 83 | |