blob: 9878d3ca7c1fc9712b5a146eccf214f23dce0beb [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
Anand Doshi486f9df2012-07-19 13:40:31 +053017from __future__ import unicode_literals
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +053018import webnotes
19import json
20
21from webnotes.model.doc import Document
22from webnotes.utils import cint
23
24@webnotes.whitelist()
25def 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()
33def get_roles(arg=None):
Rushabh Mehta91ba3462012-07-13 14:54:40 +053034 """return all roles except standard"""
35 return _get_roles(webnotes.form_dict['uid'])
36
37def _get_roles(user):
38 """return all roles except standard"""
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +053039 return [r[0] for r in webnotes.conn.sql("""select name from tabRole
Rushabh Mehta91ba3462012-07-13 14:54:40 +053040 where name not in ('Administrator', 'Guest', 'All') order by name""", user)]
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +053041
42@webnotes.whitelist()
43def 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()
49def 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()
57def 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()
83def update_security(args=''):
84 args = json.loads(args)
Anand Doshif09bd672012-05-03 16:44:54 +053085 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 Mehtaaaf86ba2012-02-28 17:40:13 +053088 webnotes.conn.set_value('Profile', args['user'], 'enabled', int(args.get('enabled',0)) or 0)
Anand Doshicec84e12012-07-02 11:55:29 +053089
90 # logout a disabled user
91 if not int(args.get('enabled',0) or 0):
92 webnotes.login_manager.logout(user=args['user'])
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +053093
Anand Doshia7c2de62012-03-02 11:27:50 +053094 if args.get('new_password') and args.get('sys_admin_pwd'):
Anand Doshi96188f02012-03-15 11:11:57 +053095 from webnotes.utils import cint
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +053096 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()
108def add_user(args):
109 args = json.loads(args)
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +0530110 add_profile(args)
111
112@webnotes.whitelist()
113def add_profile(args):
114 from webnotes.utils import validate_email_add, now
Anand Doshi1ed4ef12012-04-27 15:30:23 +0530115 email = args['user']
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +0530116 sql = webnotes.conn.sql
Anand Doshi1ed4ef12012-04-27 15:30:23 +0530117
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 Doshi5c2a7922012-04-30 20:03:23 +0530124 if active_users >= conf.max_users and conf.max_users:
Anand Doshi1ed4ef12012-04-27 15:30:23 +0530125 # 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 Mehtaaaf86ba2012-02-28 17:40:13 +0530133
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()
164def 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 Doshi9a639962012-02-29 18:59:45 +0530171 'account_url': webnotes.conn.get_value('Website Settings',
172 'Website Settings', 'subdomain') or ""
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +0530173 })
174 if not args.get('last_name'): args['last_name'] = ''
Anand Doshi090a12d2012-07-10 15:15:46 +0530175 sendmail_md(pr.email, subject="Welcome to ERPNext", msg=welcome_txt % args)
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +0530176
177#
178# delete user
179#
180@webnotes.whitelist()
181def 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 Mehtaaaf86ba2012-02-28 17:40:13 +0530185 webnotes.login_manager.logout(user=webnotes.form_dict['uid'])
186
187welcome_txt = """
188## %(company)s
189
190Dear %(first_name)s %(last_name)s
191
192Welcome!
193
194A new account has been created for you, here are your details:
195
196login-id: %(user)s
197password: %(password)s
198
199To login to your new ERPNext account, please go to:
200
201%(account_url)s
Anand Doshifd5a2132012-02-29 18:58:02 +0530202"""