增加分组

pull/2/head
guanghongwei 2014-10-03 22:04:36 +08:00
parent 7c5f2d216f
commit c8444ef73a
10 changed files with 270 additions and 57 deletions

View File

@ -17,6 +17,8 @@ urlpatterns = patterns('',
(r'^showUser/$', views.showUser),
(r'^addUser/$', views.addUser),
(r'^chgUser/$', views.chgUser),
(r'^showGroup/$', views.showGroup),
(r'^addGroup/$', views.addGroup),
(r'^showAssets/$', views.showAssets),
(r'^addAssets/$', views.addAssets),
(r'^showPerm/$', views.showPerm),

View File

@ -218,10 +218,11 @@ def is_super_user(request):
def install(request):
user = User.objects.filter(username='admin')
if user:
return HttpResponseRedirect('/login/')
error = '已经安装,请出重复安装.'
return render_to_response('info.html', {'error': error})
else:
u = User(
id=700,
id=800,
username='admin',
password=md5_crypt('admin'),
key_pass=md5_crypt('admin'),
@ -230,7 +231,8 @@ def install(request):
is_superuser=True,
ldap_password=md5_crypt('admin'))
u.save()
return HttpResponse('Install successfully, please refresh this page.')
msg = '安装成功'
return render_to_response('info.html', {'msg': msg})
def login(request):
@ -466,7 +468,6 @@ def chgUser(request):
"""修改用户信息"""
error = ''
msg = ''
jm = PyCrypt(key)
if request.method == "GET":
username = request.GET.get('username')
@ -489,11 +490,6 @@ def chgUser(request):
group_post = request.REQUEST.getlist('group')
groups = []
user = User.objects.get(username=username)
all_groups = Group.objects.all()
# 获得原来的,然设为初始值
ori_is_admin = "checked" if user.is_admin else ''
ori_is_superuser = 'checked' if user.is_superuser else ''
# 属组的获取
for group_name in group_post:
@ -511,10 +507,7 @@ def chgUser(request):
error = u'带*内容不能为空'
if error:
return render_to_response('chgUser.html',
{'user': user, 'user_menu': 'active', 'is_admin': ori_is_admin,
'is_superuser': ori_is_superuser, 'groups': all_groups, 'error': error},
context_instance=RequestContext(request))
return render_to_response('info.html', {'error': error})
u = User.objects.get(username=username)
u.name = name
@ -524,10 +517,63 @@ def chgUser(request):
u.save()
msg = u'修改用户信息成功'
return render_to_response('chgUser.html',
{'user': user, 'user_menu': 'active', 'is_admin': is_admin,
'is_superuser': is_superuser, 'groups': all_groups, 'msg': msg},
context_instance=RequestContext(request))
return render_to_response('info.html', {'msg': msg})
@superuser_required
def addGroup(request):
error = ''
msg = ''
if request.method == 'POST':
group_name = request.POST.get('name')
if not group_name:
group = Group(name=group_name)
group.save()
msg = u'%s 属组添加成功' % group_name
else:
error = u'不能为空'
return render_to_response('addGroup.html',
{'error': error, 'msg': msg},
context_instance=RequestContext(request))
@superuser_required
def showGroup(request):
groups = Group.objects.all()
if request.method == 'POST':
selected_group = request.REQUEST.getlist('selected')
if selected_group:
for group_id in selected_group:
group = Group.objects.get(id=group_id)
group.delete()
msg = '删除成功'
else:
error = '请选择删除的组'
return render_to_response('showGroup.html', {'error': error, 'msg': msg},
context_instance=RequestContext(request))
@superuser_required
def chgGroup(request):
error = ''
msg = ''
if request.method == 'GET':
group_id = request.GET.get(id)
group = Group.objects.get(id=group_id)
else:
group_id = request.POST.get(id)
group_name = request.POST.get(name)
if not group_name:
error = u'不能为空'
else:
group = Group.objects.get(id=group_id)
group['name'] = group_name
group.save()
msg = u'修改成功'
return render_to_response('chgGroup.html', {'group': group, 'error': error, 'msg': msg},
context_instance=RequestContext(request))
@admin_required
@ -600,8 +646,8 @@ def showPerm(request):
elif request.method == 'GET':
if request.GET.get('username'):
username = request.GET.get('username')
user = User.objects.filter(username=username)[0]
assets_user = AssetsUser.objects.filter(uid=user.id)
user = User.objects.get(username=username)
assets_user = AssetsUser.objects.filter(uid=user.id).order_by()
return render_to_response('perms.html',
{'user': user, 'assets': assets_user, 'perm_menu': 'active'},
context_instance=RequestContext(request))
@ -655,28 +701,42 @@ def addPerm(request):
@login_required
def chgPass(request):
"""修改登录系统的密码"""
"""修改登录密码"""
error = ''
msg = ''
if request.method == 'POST':
username = request.session.get('username')
oldpass = request.POST.get('oldpass')
is_self = False
if request.method == 'GET':
if is_admin_user(request):
username = request.GET.get('username')
else:
username = request.session.get('username')
is_self = True
return render_to_response('chgKey.html',
{'username': username, 'is_self': is_self},
context_instance=RequestContext(request))
else:
username = request.POST.get('username')
user = User.objects.get(username=username)
password = request.POST.get('password')
password_confirm = request.POST.get('password_confirm')
user = User.objects.get(username)
if '' in [oldpass, password, password_confirm]:
error = '带*内容不能为空'
elif md5_crypt(oldpass) != user.password:
error = '密码不正确'
elif password != password_confirm:
error = '两次密码不匹配'
password_again = request.POST.get('password_again')
if not error:
user.password = md5_crypt(password)
user.save()
if not is_admin_user(request):
oldpass = request.POST.get('oldpass')
if oldpass != user.password:
error = '原来密码不正确'
return render_to_response('chgPass.html', {'msg': msg, 'error': error, 'pass_menu': 'active'},
context_instance=RequestContext(request))
if password != password_again:
error = '密码不匹配'
if error:
return render_to_response('info.html', {'error': error})
user['password'] = password
user.save()
return render_to_response('info.html', {'msg': '修改密码成功'})
@login_required
@ -687,8 +747,9 @@ def chgKey(request):
is_self = False
if request.method == 'GET':
username = request.GET.get('username')
if not username:
if is_admin_user(request):
username = request.GET.get('username')
else:
username = request.session.get('username')
is_self = True
@ -696,35 +757,30 @@ def chgKey(request):
{'username': username, 'is_self': is_self},
context_instance=RequestContext(request))
else:
try:
oldpass = request.POST.get('oldpass')
except KeyError:
pass
else:
username = request.POST.get('username')
user = User.objects.get(username=username)
if oldpass != user.key_pass:
error = '原来密码不正确'
username = request.POST.get('username')
user = User.objects.get(username=username)
password = request.POST.get('password')
password_again = request.POST.get('password_again')
if not is_admin_user(request):
oldpass = request.POST.get('oldpass')
if oldpass != user.key_pass:
error = '原来密码不正确'
if password != password_again:
error = '密码不匹配'
if error:
return HttpResponse(error)
return render_to_response('info.html', {'error': error})
keyfile = '%s/keys/%s' % (base_dir, username)
jm = PyCrypt(key)
ret = bash('ssh-keygen -p -P %s -N %s -f %s' % (jm.decrypt(user.key_pass), password, keyfile))
if ret != 0:
error = '更改私钥密码错误'
return HttpResponse(error)
return render_to_response('info.hmtl', {'error': error})
user['keypass'] = password
user.save()
return HttpResponse('修改密码成功')
return render_to_response('info.html', {'msg': '修改密码成功'})

View File

@ -0,0 +1,30 @@
{% extends 'base.html' %}
{% block content %}
<form class="form-horizontal" role="form" method="post" action="" >
<fieldset >
<legend>添加属组</legend>
{% if error %}
<div class="alert alert-danger">
{{ error }}
</div>
{% endif %}
{% if msg %}
<div class="alert alert-success">
{{ msg }}
</div>
{% endif %}
<div class="form-group">
<label for="name" class="col-sm-2 control-label">属组<span style="color: red"> *</span></label>
<div class="col-sm-4">
<input type="text" class="form-control" id="name" name="name" placeholder="属组">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<button class="btn btn-primary">添加</button>
</div>
</div>
</fieldset>
</form>
{% endblock %}

View File

@ -90,6 +90,9 @@
<ul id="userMenu" class="nav nav-list navbar-collapse.in menu-second">
<li><a href="/showUser/"><i class="glyphicon glyphicon-send"></i> 查看用户</a></li>
<li><a href="/addUser/"><i class="glyphicon glyphicon-send"></i> 添加用户</a></li>
<li><a href="/showGroup/"><i class="glyphicon glyphicon-send"></i> 查看属组</a></li>
<li><a href="/addGroup/"><i class="glyphicon glyphicon-send"></i> 添加属组</a></li>
<li><a href="/addUser/"><i class="glyphicon glyphicon-send"></i> Sudo管理</a></li>
</ul>
<!--<div class="menu-first" ><a href="#monitorMenu" data-toggle="collapse"><i class="glyphicon glyphicon-th"></i> 执行命令</a></div>
<ul id="monitorMenu" class="nav nav-list navbar-collapse.in menu-second">

View File

@ -0,0 +1,41 @@
{% extends 'base.html' %}
{% block content %}
<form class="form-horizontal" role="form" method="post" action="/chgGroup/" >
<fieldset >
<legend>修改用户信息</legend>
{% if error %}
<div class="alert alert-danger">
{{ error }}
</div>
{% endif %}
{% if msg %}
<div class="alert alert-success">
{{ msg }}
</div>
{% endif %}
<div class="form-group">
<div class="col-sm-4">
<input type="text" id="oldname" name="oldname" class="form-control" value="{{ group.id }}" hidden="hidden">
</div>
</div>
<div class="form-group">
<label for="oldname" class="col-sm-2 control-label">原来属组<span style="color: red"> *</span></label>
<div class="col-sm-4">
<input type="text" id="oldname" name="oldname" class="form-control" value="{{ group.name }}" readOnly="readOnly">
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">属组<span style="color: red"> *</span></label>
<div class="col-sm-4">
<input type="text" id="name" name="name" class="form-control" placeholder="属组">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<button class="btn btn-primary">修改</button>
</div>
</div>
</fieldset>
</form>
{% endblock %}

View File

@ -13,12 +13,14 @@
{{ msg }}
</div>
{% endif %}
{% if is_self %}
<div class="form-group">
<label for="oldpass" class="col-sm-2 control-label">原来密码<span style="color: red"> *</span></label>
<label for="oldpass" class="col-sm-2 control-label">原来密码<span style="color: red"> *</span></label>
<div class="col-sm-4">
<input type="password" class="form-control" id="oldpass" name="oldpass" placeholder="用户名">
<input type="password" class="form-control" id="oldpass" name="oldpass" placeholder="原来密码">
</div>
</div>
{% endif %}
<div class="form-group">
<label for="password" class="col-sm-2 control-label">新密码<span style="color: red"> *</span></label>
<div class="col-sm-4">
@ -26,9 +28,9 @@
</div>
</div>
<div class="form-group">
<label for="password_confirm" class="col-sm-2 control-label">确认密码<span style="color: red"> *</span></label>
<label for="password_again" class="col-sm-2 control-label">确认密码<span style="color: red"> *</span></label>
<div class="col-sm-4">
<input type="password" class="form-control" id="password_confirm" name="password_confirm" placeholder="确认密码">
<input type="password" class="form-control" id="password_again" name="password_again" placeholder="确认密码">
</div>
</div>
<div class="form-group">

View File

@ -34,7 +34,7 @@
</div>
<div class="form-group">
<label for="key_pass" class="col-sm-2 control-label">密钥密码<span style="color: red"> *</span></label>
<a href="/chgKey/?username={{ user.username }}">修改密钥密</a>
<a href="/chgKey/?username={{ user.username }}">修改密码</a>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">姓名<span style="color: red"> *</span></label>

View File

@ -3,7 +3,7 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>错误页面</title>
<title>通知</title>
<style type="text/css">
body{

View File

@ -0,0 +1,47 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>信息</title>
<style type="text/css">
body{
font-family:"Hiragino Sans GB W3", "微软雅黑", "Helvetica Neue", Helvetica, Arial, "宋体", sans-serif;
color: #575757;
background: #444 url(/static/images/bg-login.gif)
}
</style>
<script language="javascript">
var i=2;
window.setInterval("settime()",1000);
window.setTimeout("toload()",5000);
function settime(){
var divinner=document.getElementById("settime");
divinner.innerText=i;
i--;
}
function toload(){
history.go(-1);
}
</script>
</head>
<body>
{% if msg %}
<div class="alert alert-success"><b>成功:</b>{{ msg }}</div>
{% endif %}
{% if error %}
<div class="alert alert-danger"><b>错误:</b>{{ error }}</div>
{% endif %}
</body>
</html>

View File

@ -0,0 +1,32 @@
{% extends 'base.html' %}
{% block content %}
<form method="post" action="">
{% if info %}
<p class="alert alert-success">
{{ info }}
</p>
{% endif %}
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
<th><input onclick="selectAll()" type="checkbox" name="select_all" style="select_all" id="select_all"/></th>
<th>ID</th>
<th>属组</th>
<th>修改</th>
</tr>
</thead>
<tbody>
{% for group in groups %}
<tr>
<td><input type="checkbox" name="selected" value="{{ group.id }}"></td>
<td>{{ group.id }}</td>
<td>{{ group.name }}</td>
<td><a href="/chgGroup/?id={{ group.id }}">修改</a></td>
</tr>
{% endfor %}
</tbody>
</form>
</table>
<button class="btn btn-primary">删除</button>
</form>
{% endblock %}