From 6a8db896149c7aac4b46d8e90a54107b96aa230c Mon Sep 17 00:00:00 2001 From: guanghongwei Date: Wed, 28 Jan 2015 17:35:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=80=E4=B8=8Bbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jumpserver.conf | 6 +- jumpserver/templatetags/mytags.py | 9 +- juser/views.py | 415 +++++++++++++++--------------- templates/juser/group_list.html | 4 +- templates/juser/user_add.html | 10 +- templates/juser/user_detail.html | 5 +- 6 files changed, 221 insertions(+), 228 deletions(-) diff --git a/jumpserver.conf b/jumpserver.conf index 18364c398..3a7d59784 100644 --- a/jumpserver.conf +++ b/jumpserver.conf @@ -8,11 +8,7 @@ password = mysql234 database = jumpserver [ldap] -host_url = ldap://192.168.8.60:389 -base_dn = dc=fengxing,dc=org -root_dn = cn=admin,dc=fengxing,dc=org -root_pw = 123456 -ldap_enable = 0 +ldap_enable = 1 host_url = ldap://127.0.0.1:389 base_dn = dc=jumpserver,dc=org root_dn = cn=admin,dc=jumpserver,dc=org diff --git a/jumpserver/templatetags/mytags.py b/jumpserver/templatetags/mytags.py index 0352e7e1e..88e709546 100644 --- a/jumpserver/templatetags/mytags.py +++ b/jumpserver/templatetags/mytags.py @@ -3,7 +3,7 @@ import time from django import template from django.db.models import Q -from juser.models import User +from juser.models import User, UserGroup register = template.Library() @@ -56,6 +56,12 @@ def perm_count(user_id): return user.perm_set.all().count() +@register.filter(name='member_count') +def member_count(group_id): + group = UserGroup.objects.get(id=group_id) + return group.user_set.count() + + @register.filter(name='group_type_to_str') def group_type_to_str(type_name): group_types = { @@ -63,5 +69,4 @@ def group_type_to_str(type_name): 'M': '管理组', 'A': '授权组', } - return group_types.get(type_name) diff --git a/juser/views.py b/juser/views.py index 0f4969af5..2dd3c625b 100644 --- a/juser/views.py +++ b/juser/views.py @@ -122,213 +122,17 @@ def group_db_add(**kwargs): UserGroup.objects.create(**kwargs) -def add_user_to_group(username, group_name): +def group_add_user(group_name, user_id=None, username=None): try: - user = User.objects.get(username=username) - group = UserGroup.objects.get(name=group_name) - except ObjectDoesNotExist: - raise AddError('User %s or group % does not exit. ' % username, group_name) - else: - groups = [group] - for g in user.user_group.all(): - groups.append(g) - user.user_group = groups - - -def group_add_user(group_name, user_id): - group = UserGroup.objects.get(name=group_name) - user = User.objects.get(id=user_id) - group.user_set.add(user) - - -def group_add(request): - error = '' - msg = '' - header_title, path1, path2 = '添加属组 | Add Group', 'juser', 'group_add' - group_types = { - 'P': '私有组', - 'M': '管理组', - 'A': '授权组', - } - users = User.objects.all() - if request.method == 'POST': - group_name = request.POST.get('group_name', '') - group_type = request.POST.get('group_type', 'A') - users_selected = request.POST.getlist('users_selected', '') - comment = request.POST.get('comment', '') - - try: - if not group_name: - error = u'组名不能为空' - raise AddError - group_db_add(name=group_name, comment=comment, type=group_type) - for user_id in users_selected: - group_add_user(group_name, user_id) - - except AddError: - pass - except TypeError: - error = u'保存用户组失败' + if user_id: + user = User.objects.get(id=user_id) else: - msg = u'添加组 %s 成功' % group_name - - return render_to_response('juser/group_add.html', locals()) - - -def group_list(request): - header_title, path1, path2 = '查看属组 | Show Group', 'juser', 'group_list' - groups = contact_list = UserGroup.objects.filter(Q(type='M') | Q(type='A')).order_by('id') - p = paginator = Paginator(contact_list, 10) - - try: - page = int(request.GET.get('page', '1')) - except ValueError: - page = 1 - - try: - contacts = paginator.page(page) - except (EmptyPage, InvalidPage): - contacts = paginator.page(paginator.num_pages) - return render_to_response('juser/group_list.html', locals()) - - -def group_detail(request): - group_id = request.GET.get('id', None) - if not group_id: - return HttpResponseRedirect('/') - group = UserGroup.objects.get(id=group_id) - return render_to_response('juser/group_detail.html', locals()) - - -def group_del(request): - group_id = request.GET.get('id', None) - if not group_id: - return HttpResponseRedirect('/') - group = UserGroup.objects.get(id=group_id) - group.delete() - return HttpResponseRedirect('/juser/group_list/', locals()) - - -def group_edit(request): - error = '' - msg = '' - header_title, path1, path2 = '修改属组 | Edit Group', 'juser', 'group_edit' - if request.method == 'GET': - group_id = request.GET.get('id', None) - group = UserGroup.objects.get(id=group_id) - group_name = group.name - comment = group.comment - - return render_to_response('juser/group_add.html', locals()) - else: - group_id = request.POST.get('group_id', None) - group_name = request.POST.get('group_name', None) - comment = request.POST.get('comment', '') - group = UserGroup.objects.filter(id=group_id) - group.update(name=group_name, comment=comment) - - return HttpResponseRedirect('/juser/group_list/') - - -def user_list(request): - user_role = {'SU': u'超级管理员', 'GA': u'组管理员', 'CU': u'普通用户'} - header_title, path1, path2 = '查看用户 | Show User', 'juser', 'user_list' - users = contact_list = User.objects.all().order_by('id') - p = paginator = Paginator(contact_list, 10) - - try: - page = int(request.GET.get('page', '1')) - except ValueError: - page = 1 - - try: - contacts = paginator.page(page) - except (EmptyPage, InvalidPage): - contacts = paginator.page(paginator.num_pages) - return render_to_response('juser/user_list.html', locals()) - - -def user_detail(request): - user_id = request.GET.get('id', None) - if not user_id: - return HttpResponseRedirect('/') - user = User.objects.get(id=user_id) - return render_to_response('juser/user_detail.html', locals()) - - -def user_del(request): - user_id = request.GET.get('id', None) - if not user_id: - return HttpResponseRedirect('/') - user = User.objects.get(id=user_id) - user.delete() - group = UserGroup.objects.get(name=user.username) - group.delete() - return HttpResponseRedirect('/juser/user_list/', locals()) - - -def user_edit(request): - header_title, path1, path2 = '编辑用户 | Edit User', 'juser', 'user_edit' - readonly = "readonly" - if request.method == 'GET': - user_id = request.GET.get('id', None) - if not user_id: - return HttpResponseRedirect('/') - user = User.objects.get(id=user_id) - username = user.username - password = user.password - ssh_key_pwd = user.ssh_key_pwd - name = user.name - all_group = UserGroup.objects.all() - groups = user.user_group.filter(type='M') - groups_str = ' '.join([str(group.id) for group in groups]) - user_role = {'SU': u'超级管理员', 'GA': u'组管理员', 'CU': u'普通用户'} - role_post = user.role - ssh_pwd = user.ssh_pwd - email = user.email - - 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_pwd = request.POST.get('ssh_key_pwd', None) - is_active = request.POST.get('is_active', '1') - ldap_pwd = gen_rand_pwd(16) - all_group = UserGroup.objects.all() - user_role = {'SU': u'超级管理员', 'GA': u'组管理员', 'CU': u'普通用户'} - - if username: user = User.objects.get(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_pwd != user.ssh_key_pwd: - ssh_key_pwd = CRYPTOR.encrypt(ssh_key_pwd) - - db_update_user(username=username, - password=password, - name=name, - email=email, - groups=groups, - role=role_post, - ssh_pwd=ssh_pwd, - ssh_key_pwd=ssh_key_pwd) - msg = u'修改用户成功' - - return HttpResponseRedirect('/juser/user_list/') - - return render_to_response('juser/user_add.html', locals()) + except ObjectDoesNotExist: + raise AddError('用户获取失败') + else: + group = UserGroup.objects.get(name=group_name) + group.user_set.add(user) def db_add_user(**kwargs): @@ -444,13 +248,154 @@ def ldap_del_user(username): ldap_conn.delete(sudo_dn) -def user_add(request): +def group_add(request): error = '' msg = '' - header_title, path1, path2 = '添加用户 | Add User', 'juser', 'user_add' - user_role = {'SU': u'超级管理员', 'GA': u'组管理员', 'CU': u'普通用户'} - all_group = UserGroup.objects.filter(type='M') + header_title, path1, path2 = '添加属组 | Add Group', 'juser', 'group_add' + group_types = { + 'P': '私有组', + 'M': '管理组', + 'A': '授权组', + } + users = User.objects.all() if request.method == 'POST': + group_name = request.POST.get('group_name', '') + group_type = request.POST.get('group_type', 'A') + users_selected = request.POST.getlist('users_selected', '') + comment = request.POST.get('comment', '') + + try: + if not group_name: + error = u'组名不能为空' + raise AddError + group_db_add(name=group_name, comment=comment, type=group_type) + for user_id in users_selected: + group_add_user(group_name, user_id=user_id) + + except AddError: + pass + except TypeError: + error = u'保存用户组失败' + else: + msg = u'添加组 %s 成功' % group_name + + return render_to_response('juser/group_add.html', locals()) + + +def group_list(request): + header_title, path1, path2 = '查看属组 | Show Group', 'juser', 'group_list' + groups = contact_list = UserGroup.objects.filter(Q(type='M') | Q(type='A')).order_by('id') + p = paginator = Paginator(contact_list, 10) + + try: + page = int(request.GET.get('page', '1')) + except ValueError: + page = 1 + + try: + contacts = paginator.page(page) + except (EmptyPage, InvalidPage): + contacts = paginator.page(paginator.num_pages) + return render_to_response('juser/group_list.html', locals()) + + +def group_detail(request): + group_id = request.GET.get('id', None) + if not group_id: + return HttpResponseRedirect('/') + group = UserGroup.objects.get(id=group_id) + users = group.user_set.all() + return render_to_response('juser/group_detail.html', locals()) + + +def group_del(request): + group_id = request.GET.get('id', None) + if not group_id: + return HttpResponseRedirect('/') + group = UserGroup.objects.get(id=group_id) + group.delete() + return HttpResponseRedirect('/juser/group_list/', locals()) + + +def group_edit(request): + error = '' + msg = '' + header_title, path1, path2 = '修改属组 | Edit Group', 'juser', 'group_edit' + if request.method == 'GET': + group_id = request.GET.get('id', None) + group = UserGroup.objects.get(id=group_id) + group_name = group.name + comment = group.comment + + return render_to_response('juser/group_add.html', locals()) + else: + group_id = request.POST.get('group_id', None) + group_name = request.POST.get('group_name', None) + comment = request.POST.get('comment', '') + group = UserGroup.objects.filter(id=group_id) + group.update(name=group_name, comment=comment) + + return HttpResponseRedirect('/juser/group_list/') + + +def user_list(request): + user_role = {'SU': u'超级管理员', 'GA': u'组管理员', 'CU': u'普通用户'} + header_title, path1, path2 = '查看用户 | Show User', 'juser', 'user_list' + users = contact_list = User.objects.all().order_by('id') + p = paginator = Paginator(contact_list, 10) + + try: + page = int(request.GET.get('page', '1')) + except ValueError: + page = 1 + + try: + contacts = paginator.page(page) + except (EmptyPage, InvalidPage): + contacts = paginator.page(paginator.num_pages) + return render_to_response('juser/user_list.html', locals()) + + +def user_detail(request): + user_id = request.GET.get('id', None) + if not user_id: + return HttpResponseRedirect('/') + user = User.objects.get(id=user_id) + return render_to_response('juser/user_detail.html', locals()) + + +def user_del(request): + user_id = request.GET.get('id', None) + if not user_id: + return HttpResponseRedirect('/') + user = User.objects.get(id=user_id) + user.delete() + group = UserGroup.objects.get(name=user.username) + group.delete() + return HttpResponseRedirect('/juser/user_list/', locals()) + + +def user_edit(request): + header_title, path1, path2 = '编辑用户 | Edit User', 'juser', 'user_edit' + readonly = "readonly" + if request.method == 'GET': + user_id = request.GET.get('id', None) + if not user_id: + return HttpResponseRedirect('/') + user = User.objects.get(id=user_id) + username = user.username + password = user.password + ssh_key_pwd = user.ssh_key_pwd + name = user.name + all_group = UserGroup.objects.filter(Q(type='M') | Q(type='A')) + groups = user.user_group.filter(Q(type='M') | Q(type='A')) + groups_str = ' '.join([str(group.id) for group in groups]) + user_role = {'SU': u'超级管理员', 'GA': u'组管理员', 'CU': u'普通用户'} + role_post = user.role + ssh_pwd = user.ssh_pwd + email = user.email + + else: username = request.POST.get('username', None) password = request.POST.get('password', None) name = request.POST.get('name', None) @@ -462,6 +407,56 @@ def user_add(request): ssh_key_pwd = request.POST.get('ssh_key_pwd', None) is_active = request.POST.get('is_active', '1') ldap_pwd = gen_rand_pwd(16) + all_group = UserGroup.objects.filter(Q(type='M') | Q(type='A')) + user_role = {'SU': u'超级管理员', 'GA': u'组管理员', 'CU': u'普通用户'} + + if username: + user = User.objects.get(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_pwd != user.ssh_key_pwd: + ssh_key_pwd = CRYPTOR.encrypt(ssh_key_pwd) + + db_update_user(username=username, + password=password, + name=name, + email=email, + groups=groups, + role=role_post, + ssh_pwd=ssh_pwd, + ssh_key_pwd=ssh_key_pwd) + msg = u'修改用户成功' + + return HttpResponseRedirect('/juser/user_list/') + + return render_to_response('juser/user_add.html', locals()) + + +def user_add(request): + error = '' + msg = '' + header_title, path1, path2 = '添加用户 | Add User', 'juser', 'user_add' + user_role = {'SU': u'超级管理员', 'GA': u'组管理员', 'CU': u'普通用户'} + all_group = UserGroup.objects.filter(Q(type='M') | Q(type='A')).order_by('-type') + if request.method == 'POST': + username = request.POST.get('username', None) + password = request.POST.get('password', '') + 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', 'CU') + ssh_pwd = request.POST.get('ssh_pwd', '') + ssh_key_pwd = request.POST.get('ssh_key_pwd', '') + is_active = request.POST.get('is_active', '1') + ldap_pwd = gen_rand_pwd(16) try: if None in [username, password, ssh_key_pwd, name, groups, role_post, is_active]: @@ -481,7 +476,7 @@ def user_add(request): password=md5_crypt(password), name=name, email=email, groups=groups, role=role_post, - ssh_pwd=CRYPTOR.encrypt(ssh_pwd), + ssh_pwd=CRYPTOR.encrypt(ssh_pwd) if ssh_pwd else '', ssh_key_pwd=CRYPTOR.encrypt(ssh_key_pwd), ldap_pwd=CRYPTOR.encrypt(ldap_pwd), is_active=is_active, @@ -489,7 +484,7 @@ def user_add(request): server_add_user(username, password, ssh_key_pwd) group_db_add(name=username, comment=username, type='P') - add_user_to_group(username=username, group_name=username) + group_add_user(group_name=username, username=username) if LDAP_ENABLE: ldap_add_user(username, ldap_pwd) msg = u'添加用户 %s 成功!' % username diff --git a/templates/juser/group_list.html b/templates/juser/group_list.html index 89f1f0f83..0a045d8f7 100644 --- a/templates/juser/group_list.html +++ b/templates/juser/group_list.html @@ -36,10 +36,10 @@ - + @@ -47,10 +47,10 @@ {% for group in contacts.object_list %} - +
ID 组名 类型成员数量 备注 操作
{{ group.id }} {{ group.name }} {{ group.type|group_type_to_str }} {{ group.id|member_count }} {{ group.comment }} 成员 diff --git a/templates/juser/user_add.html b/templates/juser/user_add.html index 9bf685edc..cede4b4d4 100644 --- a/templates/juser/user_add.html +++ b/templates/juser/user_add.html @@ -73,19 +73,19 @@
- {% for group in all_group %} {% if groups_str %} {% if group.id|int2str in groups_str %} - + {% else %} - + {% endif %} {% else %} {% if forloop.first %} - + {% else %} - + {% endif %} {% endif %} {% endfor %} diff --git a/templates/juser/user_detail.html b/templates/juser/user_detail.html index be846b9a5..528795193 100644 --- a/templates/juser/user_detail.html +++ b/templates/juser/user_detail.html @@ -16,9 +16,6 @@

{{ user.name }} 用户详情

- @@ -58,7 +55,7 @@ - +
添加时间{{ user.joined }}{{ user.date_joined|stamp2str }}
最后登录