Rushabh Mehta | aaf86ba | 2012-02-28 17:40:13 +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 | |
| 17 | import webnotes |
| 18 | import json |
| 19 | |
| 20 | from webnotes.model.doc import Document |
| 21 | from webnotes.utils import cint |
| 22 | |
| 23 | @webnotes.whitelist() |
| 24 | def get(arg=None): |
| 25 | """return all users""" |
| 26 | return webnotes.conn.sql("""select name, file_list, enabled, gender, |
| 27 | restrict_ip, login_before, login_after from tabProfile |
| 28 | where docstatus<2 and name not in ('Administrator', 'Guest') order by |
| 29 | ifnull(enabled,0) desc, name""", as_dict=1) |
| 30 | |
| 31 | @webnotes.whitelist() |
| 32 | def get_roles(arg=None): |
| 33 | """return all roles""" |
| 34 | return [r[0] for r in webnotes.conn.sql("""select name from tabRole |
| 35 | where name not in ('Administrator', 'Guest', 'All') order by name""")] |
| 36 | |
| 37 | @webnotes.whitelist() |
| 38 | def get_user_roles(arg=None): |
| 39 | """get roles for a user""" |
| 40 | return [r[0] for r in webnotes.conn.sql("""select role from tabUserRole |
| 41 | where parent=%s""", webnotes.form_dict['uid'])] |
| 42 | |
| 43 | @webnotes.whitelist() |
| 44 | def get_perm_info(arg=None): |
| 45 | """get permission info""" |
| 46 | return webnotes.conn.sql("""select parent, permlevel, `read`, `write`, submit, |
| 47 | cancel, amend from tabDocPerm where role=%s |
| 48 | and docstatus<2 order by parent, permlevel""", |
| 49 | webnotes.form_dict['role'], as_dict=1) |
| 50 | |
| 51 | @webnotes.whitelist() |
| 52 | def update_roles(arg=None): |
| 53 | """update set and unset roles""" |
| 54 | # remove roles |
| 55 | unset = json.loads(webnotes.form_dict['unset_roles']) |
| 56 | webnotes.conn.sql("""delete from tabUserRole where parent='%s' |
| 57 | and role in ('%s')""" % (webnotes.form_dict['uid'], "','".join(unset))) |
| 58 | |
| 59 | # check for 1 system manager |
| 60 | if not webnotes.conn.sql("""select parent from tabUserRole where role='System Manager' |
| 61 | and docstatus<2"""): |
| 62 | webnotes.msgprint("Sorry there must be atleast one 'System Manager'") |
| 63 | raise webnotes.ValidationError |
| 64 | |
| 65 | # add roles |
| 66 | roles = get_user_roles() |
| 67 | toset = json.loads(webnotes.form_dict['set_roles']) |
| 68 | for role in toset: |
| 69 | if not role in roles: |
| 70 | d = Document('UserRole') |
| 71 | d.role = role |
| 72 | d.parent = webnotes.form_dict['uid'] |
| 73 | d.save() |
| 74 | |
| 75 | webnotes.msgprint('Roles Updated') |
| 76 | |
| 77 | @webnotes.whitelist() |
| 78 | def update_security(args=''): |
| 79 | args = json.loads(args) |
| 80 | webnotes.conn.set_value('Profile', args['user'], 'restrict_ip', args.get('restrict_ip')) |
| 81 | webnotes.conn.set_value('Profile', args['user'], 'login_after', args.get('login_after')) |
| 82 | webnotes.conn.set_value('Profile', args['user'], 'login_before', args.get('login_before')) |
| 83 | webnotes.conn.set_value('Profile', args['user'], 'enabled', int(args.get('enabled',0)) or 0) |
| 84 | |
Anand Doshi | a7c2de6 | 2012-03-02 11:27:50 +0530 | [diff] [blame] | 85 | if args.get('new_password') and args.get('sys_admin_pwd'): |
Rushabh Mehta | aaf86ba | 2012-02-28 17:40:13 +0530 | [diff] [blame] | 86 | if cint(webnotes.conn.get_value('Control Panel',None,'sync_with_gateway')): |
| 87 | import server_tools.gateway_utils |
| 88 | res = server_tools.gateway_utils.change_password('', args['new_password'], |
| 89 | args['user'], args['sys_admin_pwd']) |
| 90 | if 'Traceback' not in res['message']: |
| 91 | webnotes.msgprint(res['message']) |
| 92 | webnotes.conn.sql("update tabProfile set password=password(%s) where name=%s", |
| 93 | (args['new_password'], args['user'])) |
| 94 | else: |
| 95 | webnotes.msgprint('Settings Updated') |
| 96 | |
| 97 | |
| 98 | |
| 99 | # |
| 100 | # user addition |
| 101 | # |
| 102 | |
| 103 | @webnotes.whitelist() |
| 104 | def add_user(args): |
| 105 | args = json.loads(args) |
| 106 | # erpnext-saas |
| 107 | if cint(webnotes.conn.get_value('Control Panel', None, 'sync_with_gateway')): |
| 108 | from server_tools.gateway_utils import add_user_gateway |
Anand Doshi | d536f16 | 2012-03-02 17:05:04 +0530 | [diff] [blame] | 109 | add_user_gateway(args) |
Rushabh Mehta | aaf86ba | 2012-02-28 17:40:13 +0530 | [diff] [blame] | 110 | |
| 111 | add_profile(args) |
| 112 | |
| 113 | @webnotes.whitelist() |
| 114 | def add_profile(args): |
| 115 | from webnotes.utils import validate_email_add, now |
| 116 | email = args['user'] |
| 117 | |
| 118 | sql = webnotes.conn.sql |
| 119 | |
| 120 | if not email: |
| 121 | email = webnotes.form_dict.get('user') |
| 122 | if not validate_email_add(email): |
| 123 | raise Exception |
| 124 | return 'Invalid Email Id' |
| 125 | |
| 126 | if sql("select name from tabProfile where name = %s", email): |
| 127 | # exists, enable it |
| 128 | sql("update tabProfile set enabled = 1, docstatus=0 where name = %s", email) |
| 129 | webnotes.msgprint('Profile exists, enabled it with new password') |
| 130 | else: |
| 131 | # does not exist, create it! |
| 132 | pr = Document('Profile') |
| 133 | pr.name = email |
| 134 | pr.email = email |
| 135 | pr.first_name = args.get('first_name') |
| 136 | pr.last_name = args.get('last_name') |
| 137 | pr.enabled = 1 |
| 138 | pr.user_type = 'System User' |
| 139 | pr.save(1) |
| 140 | |
| 141 | if args.get('password'): |
| 142 | sql(""" |
| 143 | UPDATE tabProfile |
| 144 | SET password = PASSWORD(%s), modified = %s |
| 145 | WHERE name = %s""", (args.get('password'), now, email)) |
| 146 | |
| 147 | send_welcome_mail(email, args) |
| 148 | |
| 149 | @webnotes.whitelist() |
| 150 | def send_welcome_mail(email, args): |
| 151 | """send welcome mail to user with password and login url""" |
| 152 | pr = Document('Profile', email) |
| 153 | from webnotes.utils.email_lib import sendmail_md |
| 154 | args.update({ |
| 155 | 'company': webnotes.conn.get_default('company'), |
| 156 | 'password': args.get('password'), |
Anand Doshi | 9a63996 | 2012-02-29 18:59:45 +0530 | [diff] [blame] | 157 | 'account_url': webnotes.conn.get_value('Website Settings', |
| 158 | 'Website Settings', 'subdomain') or "" |
Rushabh Mehta | aaf86ba | 2012-02-28 17:40:13 +0530 | [diff] [blame] | 159 | }) |
| 160 | if not args.get('last_name'): args['last_name'] = '' |
| 161 | sendmail_md(pr.email, subject="Welcome to ERPNext", msg=welcome_txt % args, from_defs=1) |
| 162 | |
| 163 | # |
| 164 | # delete user |
| 165 | # |
| 166 | @webnotes.whitelist() |
| 167 | def delete(arg=None): |
| 168 | """delete user""" |
| 169 | webnotes.conn.sql("update tabProfile set enabled=0, docstatus=2 where name=%s", |
| 170 | webnotes.form_dict['uid']) |
| 171 | # erpnext-saas |
| 172 | if int(webnotes.conn.get_value('Control Panel', None, 'sync_with_gateway')): |
| 173 | from server_tools.gateway_utils import remove_user_gateway |
| 174 | remove_user_gateway(webnotes.form_dict['uid']) |
| 175 | |
| 176 | webnotes.login_manager.logout(user=webnotes.form_dict['uid']) |
| 177 | |
| 178 | welcome_txt = """ |
| 179 | ## %(company)s |
| 180 | |
| 181 | Dear %(first_name)s %(last_name)s |
| 182 | |
| 183 | Welcome! |
| 184 | |
| 185 | A new account has been created for you, here are your details: |
| 186 | |
| 187 | login-id: %(user)s |
| 188 | password: %(password)s |
| 189 | |
| 190 | To login to your new ERPNext account, please go to: |
| 191 | |
| 192 | %(account_url)s |
Anand Doshi | fd5a213 | 2012-02-29 18:58:02 +0530 | [diff] [blame] | 193 | """ |