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