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'): |
Anand Doshi | 96188f0 | 2012-03-15 11:11:57 +0530 | [diff] [blame] | 86 | from webnotes.utils import cint |
Rushabh Mehta | aaf86ba | 2012-02-28 17:40:13 +0530 | [diff] [blame] | 87 | webnotes.conn.sql("update tabProfile set password=password(%s) where name=%s", |
| 88 | (args['new_password'], args['user'])) |
| 89 | else: |
| 90 | webnotes.msgprint('Settings Updated') |
| 91 | |
| 92 | |
| 93 | |
| 94 | # |
| 95 | # user addition |
| 96 | # |
| 97 | |
| 98 | @webnotes.whitelist() |
| 99 | def add_user(args): |
| 100 | args = json.loads(args) |
Rushabh Mehta | aaf86ba | 2012-02-28 17:40:13 +0530 | [diff] [blame] | 101 | add_profile(args) |
| 102 | |
| 103 | @webnotes.whitelist() |
| 104 | def add_profile(args): |
| 105 | from webnotes.utils import validate_email_add, now |
Anand Doshi | 1ed4ef1 | 2012-04-27 15:30:23 +0530 | [diff] [blame] | 106 | email = args['user'] |
Rushabh Mehta | aaf86ba | 2012-02-28 17:40:13 +0530 | [diff] [blame] | 107 | sql = webnotes.conn.sql |
Anand Doshi | 1ed4ef1 | 2012-04-27 15:30:23 +0530 | [diff] [blame] | 108 | |
| 109 | # validate max number of users exceeded or not |
| 110 | import conf |
| 111 | if hasattr(conf, 'max_users'): |
| 112 | active_users = sql("""select count(*) from tabProfile |
| 113 | where ifnull(enabled, 0)=1 and docstatus<2 |
| 114 | and name not in ('Administrator', 'Guest')""")[0][0] |
| 115 | if active_users >= conf.max_users: |
| 116 | # same message as in users.js |
| 117 | webnotes.msgprint("""Alas! <br />\ |
| 118 | You already have <b>%(active_users)s</b> active users, \ |
| 119 | which is the maximum number that you are currently allowed to add. <br /><br /> \ |
| 120 | So, to add more users, you can:<br /> \ |
| 121 | 1. <b>Upgrade to the unlimited users plan</b>, or<br /> \ |
| 122 | 2. <b>Disable one or more of your existing users and try again</b>""" \ |
| 123 | % {'active_users': active_users}, raise_exception=1) |
Rushabh Mehta | aaf86ba | 2012-02-28 17:40:13 +0530 | [diff] [blame] | 124 | |
| 125 | if not email: |
| 126 | email = webnotes.form_dict.get('user') |
| 127 | if not validate_email_add(email): |
| 128 | raise Exception |
| 129 | return 'Invalid Email Id' |
| 130 | |
| 131 | if sql("select name from tabProfile where name = %s", email): |
| 132 | # exists, enable it |
| 133 | sql("update tabProfile set enabled = 1, docstatus=0 where name = %s", email) |
| 134 | webnotes.msgprint('Profile exists, enabled it with new password') |
| 135 | else: |
| 136 | # does not exist, create it! |
| 137 | pr = Document('Profile') |
| 138 | pr.name = email |
| 139 | pr.email = email |
| 140 | pr.first_name = args.get('first_name') |
| 141 | pr.last_name = args.get('last_name') |
| 142 | pr.enabled = 1 |
| 143 | pr.user_type = 'System User' |
| 144 | pr.save(1) |
| 145 | |
| 146 | if args.get('password'): |
| 147 | sql(""" |
| 148 | UPDATE tabProfile |
| 149 | SET password = PASSWORD(%s), modified = %s |
| 150 | WHERE name = %s""", (args.get('password'), now, email)) |
| 151 | |
| 152 | send_welcome_mail(email, args) |
| 153 | |
| 154 | @webnotes.whitelist() |
| 155 | def send_welcome_mail(email, args): |
| 156 | """send welcome mail to user with password and login url""" |
| 157 | pr = Document('Profile', email) |
| 158 | from webnotes.utils.email_lib import sendmail_md |
| 159 | args.update({ |
| 160 | 'company': webnotes.conn.get_default('company'), |
| 161 | 'password': args.get('password'), |
Anand Doshi | 9a63996 | 2012-02-29 18:59:45 +0530 | [diff] [blame] | 162 | 'account_url': webnotes.conn.get_value('Website Settings', |
| 163 | 'Website Settings', 'subdomain') or "" |
Rushabh Mehta | aaf86ba | 2012-02-28 17:40:13 +0530 | [diff] [blame] | 164 | }) |
| 165 | if not args.get('last_name'): args['last_name'] = '' |
| 166 | sendmail_md(pr.email, subject="Welcome to ERPNext", msg=welcome_txt % args, from_defs=1) |
| 167 | |
| 168 | # |
| 169 | # delete user |
| 170 | # |
| 171 | @webnotes.whitelist() |
| 172 | def delete(arg=None): |
| 173 | """delete user""" |
| 174 | webnotes.conn.sql("update tabProfile set enabled=0, docstatus=2 where name=%s", |
| 175 | webnotes.form_dict['uid']) |
Rushabh Mehta | aaf86ba | 2012-02-28 17:40:13 +0530 | [diff] [blame] | 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 | """ |