修改一下bug

pull/6/head
guanghongwei 2015-01-28 17:35:06 +08:00
parent 5cd09a6503
commit 6a8db89614
6 changed files with 221 additions and 228 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -36,10 +36,10 @@
<table class="table table-striped table-bordered table-hover " id="editable" >
<thead>
<tr>
<th class="text-center"><input type="checkbox" class="i-checks" name=""></th>
<th class="text-center">ID</th>
<th class="text-center">缁勫悕</th>
<th class="text-center">绫诲瀷</th>
<th class="text-center">鎴愬憳鏁伴噺</th>
<th class="text-center">澶囨敞</th>
<th class="text-center">鎿嶄綔</th>
</tr>
@ -47,10 +47,10 @@
<tbody>
{% for group in contacts.object_list %}
<tr class="gradeX">
<td class="text-center"><input type="checkbox" class="i-checks" name=""></td>
<td class="text-center"> {{ group.id }} </td>
<td class="text-center"> {{ group.name }} </td>
<td class="text-center"> {{ group.type|group_type_to_str }} </td>
<td class="text-center"> {{ group.id|member_count }} </td>
<td class="text-center"> {{ group.comment }} </td>
<td class="text-center">
<a href="../group_detail/?id={{ group.id }}" class="iframe btn btn-xs btn-primary">鎴愬憳</a>

View File

@ -73,19 +73,19 @@
<div class="form-group">
<label for="groups" class="col-lg-2 control-label">灞炵粍<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<select id="groups" name="groups" class="form-control m-b" multiple>
<select id="groups" name="groups" class="form-control m-b" multiple size="10">
{% for group in all_group %}
{% if groups_str %}
{% if group.id|int2str in groups_str %}
<option value="{{ group.id }}" selected>{{ group.name }}</option>
<option value="{{ group.id }}" selected>{{ group.name }} --- {{ group.type|group_type_to_str }}</option>
{% else %}
<option value="{{ group.id }}">{{ group.name }}</option>
<option value="{{ group.id }}">{{ group.name }} --- {{ group.type|group_type_to_str }}</option>
{% endif %}
{% else %}
{% if forloop.first %}
<option value="{{ group.id }}" selected>{{ group.name }}</option>
<option value="{{ group.id }}" selected>{{ group.name }} --- {{ group.type|group_type_to_str }}</option>
{% else %}
<option value="{{ group.id }}">{{ group.name }}</option>
<option value="{{ group.id }}">{{ group.name }} --- {{ group.type|group_type_to_str }}</option>
{% endif %}
{% endif %}
{% endfor %}

View File

@ -16,9 +16,6 @@
<div class="contact-box">
<h2 class="text-center">{{ user.name }} 鐢ㄦ埛璇︽儏</h2>
<div class="ibox-content">
<div class="">
<a target="_blank" href="/juser/user_add/" class="btn btn-sm btn-primary "> 娣诲姞 </a>
</div>
<table class="table table-striped table-bordered table-hover " id="editable" >
<thead>
@ -58,7 +55,7 @@
</tr>
<tr class="gradeX">
<td class="text-center">娣诲姞鏃堕棿</td>
<td class="text-center">{{ user.joined }}</td>
<td class="text-center">{{ user.date_joined|stamp2str }}</td>
</tr>
<tr class="gradeX">
<td class="text-center">鏈鍚庣櫥褰</td>