From 6e2ebd5c79f6f7a1fd25d04952cb32a3b9782782 Mon Sep 17 00:00:00 2001 From: guanghongwei Date: Sun, 28 Sep 2014 16:28:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=87=EE=86=BD=E6=95=BC=20=E6=B7=87?= =?UTF-8?q?=EE=86=BD=E6=95=BC=E9=90=A2=E3=84=A6=E5=9F=9B=E6=B7=87=E2=84=83?= =?UTF-8?q?=E4=BC=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webroot/AutoSa/AutoSa/views.py | 77 ++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 13 deletions(-) diff --git a/webroot/AutoSa/AutoSa/views.py b/webroot/AutoSa/AutoSa/views.py index 14cfde5bd..48051cd3d 100644 --- a/webroot/AutoSa/AutoSa/views.py +++ b/webroot/AutoSa/AutoSa/views.py @@ -367,7 +367,7 @@ def addUser(request): user = form.cleaned_data username = user['username'] password = md5_crypt(user['password']) - key_pass = md5_crypt(user['key_pass']) + key_pass = jm.encrypt(user['key_pass']) name = user['name'] is_admin = user['is_admin'] is_superuser = user['is_superuser'] @@ -460,6 +460,69 @@ def addUser(request): context_instance=RequestContext(request)) +@admin_required +def chgUser(request): + """修改用户信息""" + error = '' + msg = '' + form = UserAddForm() + jm = PyCrypt() + + if request.method == "GET": + username = request.GET.get('username') + user = User.objects.get(username=username) + return render_to_response('chgUser.html', + {'user': user, 'user_menu': 'active', 'form': form}, + context_instance=RequestContext(request)) + else: + form = UserAddForm(request.POST) + if form.is_valid(): + user = form.cleaned_data + username = user['username'] + password = md5_crypt(user['password']) + key_pass = md5_crypt(user['key_pass']) + name = user['name'] + is_admin = user['is_admin'] + is_superuser = user['is_superuser'] + ldap_password = jm.encrypt(keygen(16)) + group_post = user['group'] + groups = [] + + keyfile = '%s/keys/%s' % (base_dir, username) + + # 如果用户是admin,那么不能委任其他admin或者超级用户 + if is_admin_user(request): + is_admin = False + is_superuser = False + + # 组 + for group_name in group_post: + groups.append(Group.objects.get(name=group_name)) + + u = User.objects.get(username=username) + + chg_keypass = bash('ssh-keygen -p -P %s -N %s -f %s' % (jm.decrypt(u.password), password, keyfile)) + if chg_keypass != 0: + error = '修改密钥密码失败' + return render_to_response('chgUser.html', + {'user': user, 'user_menu': 'active', 'form': form, 'error': error}, + context_instance=RequestContext(request)) + + u.password = password + u.key_pass = key_pass + u.name = name + u.is_admin = is_admin + u.is_superuser = is_superuser + u.ldap_password = ldap_password + u.group = groups + + u.save() + msg = '修改用户信息成功' + return render_to_response('chgUser.html', + {'user': user, 'user_menu': 'active', 'form': form, 'msg': msg}, + context_instance=RequestContext(request)) + + @admin_required def showAssets(request): """查看服务器""" @@ -580,19 +643,7 @@ def addPerm(request): context_instance=RequestContext(request)) -@admin_required -def chgUser(request): - """修改用户信息""" - error = '' - msg = '' - form = UserAddForm() - if request.method == "GET": - username = request.GET.get('username') - user = User.objects.get(username=username) - return render_to_response('chgUser.html', - {'user': user, 'user_menu': 'active', 'form': form}, - context_instance=RequestContext(request)) @login_required