brotherton-erpnext/erpnext/startup/event_handlers.py

104 lines
2.8 KiB
Python
Raw Normal View History

import webnotes
import webnotes.defs
from webnotes.utils import cint
#
# alias the current user
#
def on_login(login_manager):
# login as
if login_manager.user == 'Administrator':
user = webnotes.form.getvalue('login_as')
if user:
# create if missing (due to some bug)
login_as(user, login_manager)
# alisaing here... so check if the user is disabled
if not webnotes.conn.sql("select ifnull(enabled,0) from tabProfile where name=%s", user)[0][0]:
# throw execption
webnotes.msgprint("Authentication Failed", raise_exception=1)
if hasattr(webnotes.defs, 'validate_ip'):
msg = getattr(webnotes.defs, 'validate_ip')()
if msg: webnotes.msgprint(msg, raise_exception=1)
login_manager.user = user
#
# update account details
#
def update_account_details():
# additional details (if from gateway)
if webnotes.form_dict.get('is_trial'):
webnotes.conn.set_global('is_trial', cint(webnotes.form_dict.get('is_trial')))
if webnotes.form_dict.get('days_to_expiry'):
webnotes.conn.set_global('days_to_expiry', webnotes.form_dict.get('days_to_expiry'))
if webnotes.form_dict.get('first_name'):
2011-07-18 08:11:35 +00:00
from server_tools.gateway_utils import update_user_details
update_user_details()
#
# save (login from)
#
def on_login_post_session(login_manager):
# login from
if webnotes.form_dict.get('login_from'):
webnotes.session['data']['login_from'] = webnotes.form.getvalue('login_from')
2011-07-18 06:05:30 +00:00
webnotes.session_obj.update()
2012-01-03 10:41:04 +00:00
# Clear previous sessions i.e. logout previous log-in attempts
exception_list = ['demo@webnotestech.com', 'Administrator']
if webnotes.session['user'] not in exception_list:
2012-01-03 10:41:04 +00:00
sid_list = webnotes.conn.sql("""
SELECT sid
FROM `tabSessions`
WHERE
user=%s AND
sid!=%s
ORDER BY lastupdate desc""", \
(webnotes.session['user'], webnotes.session['sid']), as_list=1)
for sid in sid_list:
webnotes.conn.sql("DELETE FROM `tabSessions` WHERE sid=%s", sid[0])
update_account_details()
#
# logout the user from SSO
#
def on_logout(login_manager):
if cint(webnotes.conn.get_value('Control Panel', None, 'sync_with_gateway')):
2011-07-18 06:01:12 +00:00
from server_tools.gateway_utils import logout_sso
2012-01-03 10:30:03 +00:00
logout_sso(user=login_manager.user)
#
# create a profile (if logs in for the first time)
#
def login_as(user, login_manager):
import os
import webnotes
webnotes.session = {'user': user}
ip = os.environ.get('REMOTE_ADDR')
# validate if user is from SSO
if ip == '72.55.168.105' or 1:
# if user does not exist, create it
if not webnotes.conn.sql("select name from tabProfile where name=%s", user):
from webnotes.model.doc import Document
import webnotes
import webnotes.utils.webservice
p = Document('Profile')
p.first_name = webnotes.form_dict.get('first_name')
p.last_name = webnotes.form_dict.get('last_name')
p.email = user
p.name = user
p.enabled = 1
p.owner = user
p.save(1)