Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 1 | # ERPNext - web based ERP (http://erpnext.com) |
| 2 | # Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 17 | import webnotes |
Anand Doshi | 90f6e55 | 2012-01-16 14:13:39 +0530 | [diff] [blame] | 18 | import webnotes.defs |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 19 | from webnotes.utils import cint |
Rushabh Mehta | 63d669f | 2012-02-03 12:56:12 +0530 | [diff] [blame] | 20 | import home |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 21 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 22 | def on_login(login_manager): |
Rushabh Mehta | bedc1fe | 2012-01-17 18:17:06 +0530 | [diff] [blame] | 23 | """ |
| 24 | called from login manager, before login |
| 25 | """ |
Rushabh Mehta | 7c76f80 | 2012-02-16 10:58:45 +0100 | [diff] [blame] | 26 | if login_manager.user not in ('Guest', None, '') and webnotes.conn.cur_db_name!='accounts' and webnotes.conn.get_value('Control Panel', 'Control Panel', 'account_id')!='s5u011': |
Rushabh Mehta | 63d669f | 2012-02-03 12:56:12 +0530 | [diff] [blame] | 27 | try: |
Rushabh Mehta | 533bc77 | 2012-02-15 09:35:42 +0100 | [diff] [blame] | 28 | login_manager = login_as(login_manager) |
| 29 | update_account_details() |
Rushabh Mehta | 88ba7b8 | 2012-01-18 06:18:52 +0100 | [diff] [blame] | 30 | import server_tools.gateway_utils |
| 31 | server_tools.gateway_utils.check_login(login_manager.user) |
Anand Doshi | 737c2f9 | 2012-02-14 16:16:13 +0530 | [diff] [blame] | 32 | |
Rushabh Mehta | 63d669f | 2012-02-03 12:56:12 +0530 | [diff] [blame] | 33 | except ImportError: |
| 34 | pass |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 35 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 36 | def on_login_post_session(login_manager): |
Rushabh Mehta | bedc1fe | 2012-01-17 18:17:06 +0530 | [diff] [blame] | 37 | """ |
| 38 | called after login |
| 39 | update login_from and delete parallel sessions |
| 40 | """ |
Anand Doshi | b63a007 | 2012-01-03 16:11:04 +0530 | [diff] [blame] | 41 | # Clear previous sessions i.e. logout previous log-in attempts |
Rushabh Mehta | d31bf62 | 2012-01-19 15:09:49 +0530 | [diff] [blame] | 42 | exception_list = ['demo@webnotestech.com', 'Administrator', 'Guest'] |
Anand Doshi | 9fdee38 | 2012-01-03 11:26:00 +0530 | [diff] [blame] | 43 | if webnotes.session['user'] not in exception_list: |
Anand Doshi | b63a007 | 2012-01-03 16:11:04 +0530 | [diff] [blame] | 44 | sid_list = webnotes.conn.sql(""" |
Rushabh Mehta | bedc1fe | 2012-01-17 18:17:06 +0530 | [diff] [blame] | 45 | DELETE FROM `tabSessions` |
Anand Doshi | b63a007 | 2012-01-03 16:11:04 +0530 | [diff] [blame] | 46 | WHERE |
| 47 | user=%s AND |
Rushabh Mehta | bedc1fe | 2012-01-17 18:17:06 +0530 | [diff] [blame] | 48 | sid!=%s""", \ |
Anand Doshi | b63a007 | 2012-01-03 16:11:04 +0530 | [diff] [blame] | 49 | (webnotes.session['user'], webnotes.session['sid']), as_list=1) |
Rushabh Mehta | 49ebfb6 | 2012-01-20 15:32:18 +0530 | [diff] [blame] | 50 | |
Rushabh Mehta | 7c76f80 | 2012-02-16 10:58:45 +0100 | [diff] [blame] | 51 | if webnotes.session['user'] not in ('Guest') and webnotes.conn.cur_db_name!='accounts': |
Rushabh Mehta | 63d669f | 2012-02-03 12:56:12 +0530 | [diff] [blame] | 52 | # create feed |
| 53 | from webnotes.utils import nowtime |
| 54 | home.make_feed('Login', 'Profile', login_manager.user, login_manager.user, |
| 55 | '%s logged in at %s' % (login_manager.user_fullname, nowtime()), |
| 56 | login_manager.user=='Administrator' and '#8CA2B3' or '#1B750D') |
| 57 | |
Rushabh Mehta | 533bc77 | 2012-02-15 09:35:42 +0100 | [diff] [blame] | 58 | |
Rushabh Mehta | 63d669f | 2012-02-03 12:56:12 +0530 | [diff] [blame] | 59 | def comment_added(doc): |
| 60 | """add comment to feed""" |
Rushabh Mehta | 63d669f | 2012-02-03 12:56:12 +0530 | [diff] [blame] | 61 | home.make_feed('Comment', doc.comment_doctype, doc.comment_docname, doc.comment_by, |
| 62 | '<i>"' + doc.comment + '"</i>', '#6B24B3') |
| 63 | |
Rushabh Mehta | 49ebfb6 | 2012-01-20 15:32:18 +0530 | [diff] [blame] | 64 | def doclist_all(doc, method): |
| 65 | """doclist trigger called from webnotes.model.doclist on any event""" |
Rushabh Mehta | 865c00a | 2012-01-24 14:33:21 +0530 | [diff] [blame] | 66 | home.update_feed(doc, method) |
| 67 | |
| 68 | def boot_session(bootinfo): |
| 69 | """boot session - send website info if guest""" |
| 70 | import webnotes |
| 71 | import webnotes.model.doc |
| 72 | |
Rushabh Mehta | c5471dd | 2012-02-22 12:07:42 +0530 | [diff] [blame] | 73 | bootinfo['custom_css'] = webnotes.conn.get_value('Style Settings', None, 'custom_css') or '' |
| 74 | |
Rushabh Mehta | 865c00a | 2012-01-24 14:33:21 +0530 | [diff] [blame] | 75 | if webnotes.session['user']=='Guest': |
Rushabh Mehta | 7018b19 | 2012-02-02 13:42:28 +0530 | [diff] [blame] | 76 | bootinfo['website_settings'] = webnotes.model.doc.getsingle('Website Settings') |
Rushabh Mehta | f35992f | 2012-02-07 10:39:17 +0530 | [diff] [blame] | 77 | bootinfo['website_menus'] = webnotes.conn.sql("""select label, url, custom_page, |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 78 | parent_label, parentfield |
Rushabh Mehta | 7018b19 | 2012-02-02 13:42:28 +0530 | [diff] [blame] | 79 | from `tabTop Bar Item` where parent='Website Settings' order by idx asc""", as_dict=1) |
Rushabh Mehta | eec8e92 | 2012-02-14 17:25:31 +0530 | [diff] [blame] | 80 | bootinfo['analytics_code'] = \ |
| 81 | webnotes.conn.get_value('Website Settings', None, 'analytics_code') |
| 82 | bootinfo['analytics_call'] = \ |
| 83 | webnotes.conn.get_value('Website Settings', None, 'analytics_call') |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 84 | |
Rushabh Mehta | 865c00a | 2012-01-24 14:33:21 +0530 | [diff] [blame] | 85 | else: |
| 86 | bootinfo['letter_heads'] = get_letter_heads() |
| 87 | |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 88 | import webnotes.model.doctype |
| 89 | bootinfo['docs'] += webnotes.model.doctype.get('Event') |
Rushabh Mehta | 17da764 | 2012-02-28 18:56:56 +0530 | [diff] [blame] | 90 | |
| 91 | bootinfo['modules_list'] = webnotes.conn.get_global('modules_list') |
Rushabh Mehta | 12852e7 | 2012-02-29 15:11:06 +0530 | [diff] [blame] | 92 | |
| 93 | # if no company, show a dialog box to create a new company |
| 94 | bootinfo['setup_complete'] = webnotes.conn.sql("""select name from |
| 95 | tabCompany limit 1""") and 'Yes' or 'No' |
Rushabh Mehta | 412a65c | 2012-02-29 18:38:18 +0530 | [diff] [blame] | 96 | |
| 97 | bootinfo['user_background'] = webnotes.conn.get_value("Profile", webnotes.session['user'], 'background_image') or '' |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 98 | |
Rushabh Mehta | 865c00a | 2012-01-24 14:33:21 +0530 | [diff] [blame] | 99 | def get_letter_heads(): |
| 100 | """load letter heads with startup""" |
| 101 | import webnotes |
Rushabh Mehta | 63d669f | 2012-02-03 12:56:12 +0530 | [diff] [blame] | 102 | ret = webnotes.conn.sql("""select name, content from `tabLetter Head` |
| 103 | where ifnull(disabled,0)=0""") |
Rushabh Mehta | 4b1fe05 | 2012-01-25 15:06:28 +0530 | [diff] [blame] | 104 | return dict(ret) |
Anand Doshi | 737c2f9 | 2012-02-14 16:16:13 +0530 | [diff] [blame] | 105 | |
Anand Doshi | 737c2f9 | 2012-02-14 16:16:13 +0530 | [diff] [blame] | 106 | def login_as(login_manager): |
| 107 | """ |
| 108 | Login as functionality -- allows signin from signin.erpnext.com |
| 109 | """ |
| 110 | # login as user |
| 111 | user = webnotes.form.getvalue('login_as') |
| 112 | if user: |
| 113 | if isinstance(webnotes.session, dict): |
| 114 | webnotes.session['user'] = user |
| 115 | else: |
| 116 | webnotes.session = {'user': user} |
| 117 | |
| 118 | login_manager.user = user |
Rushabh Mehta | 533bc77 | 2012-02-15 09:35:42 +0100 | [diff] [blame] | 119 | first_name, last_name = webnotes.conn.sql("select first_name, last_name from `tabProfile` where name=%s", user)[0] |
| 120 | |
| 121 | login_manager.user_fullname = (first_name and first_name or "") + (last_name and " " + last_name or "") |
Anand Doshi | 737c2f9 | 2012-02-14 16:16:13 +0530 | [diff] [blame] | 122 | |
| 123 | if hasattr(webnotes.defs, 'validate_ip'): |
| 124 | msg = getattr(webnotes.defs, 'validate_ip')() |
| 125 | if msg: webnotes.msgprint(msg, raise_exception=1) |
| 126 | |
| 127 | # alisaing here... so check if the user is disabled |
| 128 | if not webnotes.conn.sql("select ifnull(enabled,0) from tabProfile where name=%s", user)[0][0]: |
| 129 | # throw execption |
| 130 | webnotes.msgprint("Authentication Failed", raise_exception=1) |
Rushabh Mehta | 533bc77 | 2012-02-15 09:35:42 +0100 | [diff] [blame] | 131 | |
| 132 | return login_manager |
| 133 | |
| 134 | # |
| 135 | # update account details |
| 136 | # |
| 137 | def update_account_details(): |
| 138 | # additional details (if from gateway) |
| 139 | if webnotes.form_dict.get('is_trial'): |
| 140 | webnotes.conn.set_global('is_trial', cint(webnotes.form_dict.get('is_trial'))) |
| 141 | |
| 142 | if webnotes.form_dict.get('days_to_expiry'): |
| 143 | webnotes.conn.set_global('days_to_expiry', webnotes.form_dict.get('days_to_expiry')) |
| 144 | |
| 145 | if webnotes.form_dict.get('first_name'): |
| 146 | from server_tools.gateway_utils import update_user_details |
| 147 | update_user_details() |
| 148 | |
| 149 | if webnotes.form_dict.get('xpassword') and webnotes.form_dict.get('login_as')!='Administrator': |
| 150 | webnotes.conn.sql("""update tabProfile set password=password(%(xpassword)s) where name=%(login_as)s""", (webnotes.form_dict)) |
| 151 | |
| 152 | if webnotes.form_dict.get('url_name'): |
| 153 | from webnotes.utils import set_default |
| 154 | set_default('account_url', 'http://'+webnotes.form_dict.get('url_name')) |
| 155 | |
| 156 | # |
| 157 | # logout the user from SSO |
| 158 | # |
| 159 | def on_logout(login_manager): |
| 160 | if cint(webnotes.conn.get_value('Control Panel', None, 'sync_with_gateway')): |
| 161 | from server_tools.gateway_utils import logout_sso |
| 162 | logout_sso(user=login_manager.user) |