blob: ec01479bfa40bb9fd98393b4e453d2e1cf4f2451 [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):
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()
38def 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()
44def 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()
52def 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()
78def 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 Doshia7c2de62012-03-02 11:27:50 +053085 if args.get('new_password') and args.get('sys_admin_pwd'):
Anand Doshi96188f02012-03-15 11:11:57 +053086 import webnotes.defs
87 from webnotes.utils import cint
88 if hasattr(webnotes.defs, 'sync_with_gateway') and \
89 cint(webnotes.defs.sync_with_gateway) or 0:
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +053090 import server_tools.gateway_utils
91 res = server_tools.gateway_utils.change_password('', args['new_password'],
92 args['user'], args['sys_admin_pwd'])
93 if 'Traceback' not in res['message']:
94 webnotes.msgprint(res['message'])
95 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)
109 # erpnext-saas
Anand Doshi96188f02012-03-15 11:11:57 +0530110 import webnotes.defs
111 from webnotes.utils import cint
112 if hasattr(webnotes.defs, 'sync_with_gateway') and \
113 cint(webnotes.defs.sync_with_gateway) or 0:
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +0530114 from server_tools.gateway_utils import add_user_gateway
Anand Doshid536f162012-03-02 17:05:04 +0530115 add_user_gateway(args)
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +0530116
117 add_profile(args)
118
119@webnotes.whitelist()
120def add_profile(args):
121 from webnotes.utils import validate_email_add, now
122 email = args['user']
123
124 sql = webnotes.conn.sql
125
126 if not email:
127 email = webnotes.form_dict.get('user')
128 if not validate_email_add(email):
129 raise Exception
130 return 'Invalid Email Id'
131
132 if sql("select name from tabProfile where name = %s", email):
133 # exists, enable it
134 sql("update tabProfile set enabled = 1, docstatus=0 where name = %s", email)
135 webnotes.msgprint('Profile exists, enabled it with new password')
136 else:
137 # does not exist, create it!
138 pr = Document('Profile')
139 pr.name = email
140 pr.email = email
141 pr.first_name = args.get('first_name')
142 pr.last_name = args.get('last_name')
143 pr.enabled = 1
144 pr.user_type = 'System User'
145 pr.save(1)
146
147 if args.get('password'):
148 sql("""
149 UPDATE tabProfile
150 SET password = PASSWORD(%s), modified = %s
151 WHERE name = %s""", (args.get('password'), now, email))
152
153 send_welcome_mail(email, args)
154
155@webnotes.whitelist()
156def send_welcome_mail(email, args):
157 """send welcome mail to user with password and login url"""
158 pr = Document('Profile', email)
159 from webnotes.utils.email_lib import sendmail_md
160 args.update({
161 'company': webnotes.conn.get_default('company'),
162 'password': args.get('password'),
Anand Doshi9a639962012-02-29 18:59:45 +0530163 'account_url': webnotes.conn.get_value('Website Settings',
164 'Website Settings', 'subdomain') or ""
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +0530165 })
166 if not args.get('last_name'): args['last_name'] = ''
167 sendmail_md(pr.email, subject="Welcome to ERPNext", msg=welcome_txt % args, from_defs=1)
168
169#
170# delete user
171#
172@webnotes.whitelist()
173def delete(arg=None):
174 """delete user"""
175 webnotes.conn.sql("update tabProfile set enabled=0, docstatus=2 where name=%s",
176 webnotes.form_dict['uid'])
177 # erpnext-saas
Anand Doshi96188f02012-03-15 11:11:57 +0530178 import webnotes.defs
179 from webnotes.utils import cint
180 if hasattr(webnotes.defs, 'sync_with_gateway') and \
181 cint(webnotes.defs.sync_with_gateway) or 0:
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +0530182 from server_tools.gateway_utils import remove_user_gateway
183 remove_user_gateway(webnotes.form_dict['uid'])
184
185 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"""