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