From 3dca51d23c764d94975044a36b54f1e5bdd83b77 Mon Sep 17 00:00:00 2001 From: ibuler Date: Fri, 23 Jan 2015 22:44:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- juser/views.py | 57 +++++++++++++++++++++++++++++++++-- templates/juser/user_add.html | 2 +- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/juser/views.py b/juser/views.py index 2f1712b1d..2d79f9694 100644 --- a/juser/views.py +++ b/juser/views.py @@ -188,6 +188,8 @@ def user_del(request): def user_edit(request): + header_title, path1, path2 = '编辑用户 | Edit User', 'juser', 'user_edit' + disabled = 'disabled' if request.method == 'GET': username = request.GET.get('username', None) if not username: @@ -205,7 +207,44 @@ def user_edit(request): ssh_pwd = user.ssh_pwd email = user.email - return render_to_response('juser/user_add.html', locals()) + else: + username = request.POST.get('username', None) + password = request.POST.get('password', None) + name = request.POST.get('name', None) + email = request.POST.get('email', '') + groups = request.POST.getlist('groups', None) + groups_str = ' '.join(groups) + role_post = request.POST.get('role', None) + ssh_pwd = request.POST.get('ssh_pwd', None) + ssh_key_pwd1 = request.POST.get('ssh_key_pwd1', None) + is_active = request.POST.get('is_active', '1') + ldap_pwd = gen_rand_pwd(16) + + if username: + user = User.objects.filter(username=username) + else: + return HttpResponseRedirect('/') + + if password != user.password: + password = md5_crypt(password) + + if ssh_pwd != user.ssh_pwd: + ssh_pwd = CRYPTOR.encrypt(ssh_pwd) + + if ssh_key_pwd1 != user.ssh_key_pwd1: + ssh_key_pwd1 = CRYPTOR.encrypt(ssh_key_pwd1) + + db_update_user(username=username, + password=password, + name=name, + email=email, + groups=groups, + role=role_post, + ssh_pwd=ssh_pwd, + ssh_key_pwd1=ssh_key_pwd1) + msg = u'修改用户成功' + + return render_to_response('juser/user_add.html', locals()) def db_add_user(**kwargs): @@ -219,6 +258,20 @@ def db_add_user(**kwargs): user.user_group = group_select +def db_update_user(**kwargs): + groups_post = kwargs.pop('groups') + username = kwargs.get('username') + user = User.objects.filter(username=username) + user.update(**kwargs) + user = User.objects.get(username=username) + group_select = [] + for group_id in groups_post: + group = UserGroup.objects.filter(id=group_id) + group_select.extend(group) + user.save() + user.user_group = group_select + + def db_del_user(username): try: user = User.objects.get(username=username) @@ -301,7 +354,7 @@ def ldap_del_user(username): group_dn = "cn=%s,ou=Group,%s" % (username, LDAP_BASE_DN) sudo_dn = 'cn=%s,ou=Sudoers,%s' % (username, LDAP_BASE_DN) - ldap_conn = LDAPMgmt() + ldap_conn = LDAPMgmt(LDAP_HOST_URL, LDAP_BASE_DN, LDAP_ROOT_DN, LDAP_ROOT_PW) ldap_conn.delete(user_dn) ldap_conn.delete(group_dn) ldap_conn.delete(sudo_dn) diff --git a/templates/juser/user_add.html b/templates/juser/user_add.html index 2524cd3eb..80c9aa217 100644 --- a/templates/juser/user_add.html +++ b/templates/juser/user_add.html @@ -38,7 +38,7 @@
- +