修改修改用户bug

pull/26/head
ibuler 2015-09-02 21:42:31 +08:00
parent c21cdc2131
commit 3efd810fe1
6 changed files with 103 additions and 90 deletions

View File

@ -27,10 +27,9 @@ def int2str(value):
@register.filter(name='get_role')
def get_role(user_id):
user_role = {'SU': u'超级管理员', 'DA': u'部门管理员', 'CU': u'普通用户'}
user = User.objects.filter(id=user_id)
user_role = {'SU': u'超级管理员', 'GA': u'管理员', 'CU': u'普通用户'}
user = get_object(User, id=user_id)
if user:
user = user[0]
return user_role.get(str(user.role), u"普通用户")
else:
return u"普通用户"

View File

@ -130,4 +130,7 @@ class AdminGroup(models.Model):
user = models.ForeignKey(User)
group = models.ForeignKey(UserGroup)
def __unicode__(self):
return '%s: %s' % (self.user.username, self.group.name)

View File

@ -81,20 +81,29 @@ def db_update_user(**kwargs):
数据库更新用户信息
"""
groups_post = kwargs.pop('groups')
admin_groups_post = kwargs.pop('admin_groups')
user_id = kwargs.pop('user_id')
user = User.objects.filter(id=user_id)
if user:
user.update(**kwargs)
user = User.objects.get(id=user_id)
user = user[0]
user.save()
else:
return None
if groups_post:
group_select = []
if groups_post:
for group_id in groups_post:
group = UserGroup.objects.filter(id=group_id)
group_select.extend(group)
user.group = group_select
if admin_groups_post != '':
user.admingroup_set.all().delete()
for group_id in admin_groups_post:
group = get_object(UserGroup, id=group_id)
AdminGroup(user=user, group=group).save()
def db_del_user(username):
"""

View File

@ -527,54 +527,60 @@ def user_edit(request):
if not user_id:
return HttpResponseRedirect('/')
user_role = {'SU': u'超级管理员', 'DA': u'组管理员', 'CU': u'普通用户'}
user_role = {'SU': u'超级管理员', 'GA': u'组管理员', 'CU': u'普通用户'}
user = get_object(User, id=user_id)
group_all = UserGroup.objects.all()
if user:
groups_str = ' '.join([str(group.id) for group in user.group.all()])
admin_groups_str = ' '.join([str(admin_group.group.id) for admin_group in user.admingroup_set.all()])
else:
user_id = request.POST.get('user_id', '')
user_id = request.GET.get('id', '')
password = request.POST.get('password', '')
name = request.POST.get('name', '')
email = request.POST.get('email', '')
dept_id = request.POST.get('dept_id')
groups = request.POST.getlist('groups', [])
role_post = request.POST.get('role', 'CU')
ssh_key_pwd = request.POST.get('ssh_key_pwd', '')
is_active = True if request.POST.get('is_active', '1') == '1' else False
admin_groups = request.POST.getlist('admin_groups', [])
extra = request.POST.getlist('extra', [])
is_active = True if '0' in extra else False
email_need = True if '2' in extra else False
user_role = {'SU': u'超级管理员', 'DA': u'部门管理员', 'CU': u'普通用户'}
dept = DEPT.objects.filter(id=dept_id)
if dept:
dept = dept[0]
else:
dept = DEPT.objects.get(id='2')
print '#'*10 + str(email_need)
print extra
if user_id:
user = User.objects.filter(id=user_id)
if user:
user = user[0]
user = get_object(User, id=user_id)
else:
return HttpResponseRedirect('/juser/user_list/')
if password != user.password:
password_decode = password
password = CRYPTOR.md5_crypt(password)
if ssh_key_pwd != user.ssh_key_pwd:
gen_ssh_key(user.username, ssh_key_pwd)
ssh_key_pwd = CRYPTOR.encrypt(ssh_key_pwd)
else:
password_decode = None
db_update_user(user_id=user_id,
password=password,
name=name,
email=email,
groups=groups,
dept=dept,
admin_groups=admin_groups,
role=role_post,
is_active=is_active,
ssh_key_pwd=ssh_key_pwd)
is_active=is_active)
if email_need:
msg = u"""
Hi %s:
您的信息已修改请登录跳板机查看详细信息
地址%s
用户名 %s
密码%s (如果密码为None代表密码为原密码)
角色%s
""" % (user.name, URL, user.username, password_decode, user_role.get(role_post, u''))
send_mail('您的信息已修改', msg, MAIL_FROM, [email], fail_silently=False)
return HttpResponseRedirect('/juser/user_list/')

View File

@ -1,6 +1,6 @@
<div class="footer fixed">
<div class="pull-right">
Version <strong>2.0.0</strong> GPL.
Version <strong>0.2.0</strong> GPL.
</div>
<div>
<strong>Copyright</strong> Jumpserver.org Team &copy; 2014-2015

View File

@ -23,7 +23,7 @@
</div>
</div>
<div class="ibox-content">
<form method="post" id="userForm" class="form-horizontal" action="">
<form method="post" id="userForm" class="form-horizontal" action="/juser/user_edit/?id={{ user.id }}">
{% if error %}
<div class="alert alert-warning text-center">{{ error }}</div>
{% endif %}
@ -33,7 +33,6 @@
<div class="form-group">
<label for="username" class="col-sm-2 control-label">用户名<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<input id="user_id" name="user_id" type="text" value="{{ user.id }}" style="display: none">
<input id="username" name="username" placeholder="Username" type="text" class="form-control" value="{{ user.username }}" readonly>
</div>
</div>
@ -48,16 +47,6 @@
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="ssh_key_pwd" class="col-sm-2 control-label">密钥密码<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<input id="ssh_key_pwd" name="ssh_key_pwd" placeholder="SSH Key Password" type="password" class="form-control" value="{{ user.ssh_key_pwd }}">
<span class="help-block m-b-none">
登陆 Jumpserver 使用的SSH密钥的密码更改密钥密码需要重新下载密钥
</span>
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">姓名<span class="red-fonts">*</span></label>
<div class="col-sm-8">
@ -79,23 +68,38 @@
</select>
</div>
</div>
{% ifequal session_role_id 2 %}
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="role" class="col-lg-2 control-label">角色<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<select id="role" name="role" class="form-control m-b">
{% for r, role_name in user_role.items %}
{% ifequal r user.role %}
<option value="{{ r }}" selected>{{ role_name }}</option>
{% else %}
<option value="{{ r }}">{{ role_name }}</option>
{% endifequal %}
<div class="col-sm-3">
<div class="radio i-checks">
<label><input type="radio" value="{{ r }}" class="role" name="role" {% ifequal r user.role %} checked {% endifequal %}>{{ role_name }}</label>
</div>
</div>
{% endfor %}
</div>
</div>
<div class="form-group" id="admin_groups" {% ifnotequal user.role "GA" %} style="display: none" {% endifnotequal %}>
<label for="role" class="col-sm-2 control-label">管理用户组</label>
<div class="col-sm-8">
{% for user_group in group_all %}
<div class="col-sm-3">
<div class="checkbox i-checks">
<label>
{% if user_group.id|int2str in admin_groups_str %}
<input type="checkbox" value="{{ user_group.id }}" name="admin_groups" checked >
{% else %}
<input type="checkbox" value="{{ user_group.id }}" name="admin_groups" >
{% endif %}
{{ user_group.name }}
</label>
</div>
</div>
{% endfor %}
</select>
</div>
</div>
{% endifequal %}
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email<span class="red-fonts">*</span></label>
@ -104,15 +108,15 @@
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group"><label class="col-sm-2 control-label">是否启用</label>
<div class="col-sm-8">
<div class="radio i-checks">
<label>
<input type="radio" value="1" name="is_active" {% if user.is_active %} } checked {% endif %}>启用
</label>
<div class="form-group"><label class="col-sm-2 control-label">额外</label>
<div class="col-sm-2">
<div class="checkbox i-checks">
<label><input type="checkbox" value="0" name="extra" {% ifequal user.is_active 1 %} checked {% endifequal %} >禁用 </label>
</div>
<div class="radio i-checks">
<label><input type="radio" value="0" name="is_active" {% if not user.is_active %} checked {% endif %}>禁用 </label>
</div>
<div class="col-sm-2">
<div class="checkbox i-checks">
<label><input type="checkbox" value="2" name="extra">发送邮件 </label>
</div>
</div>
</div>
@ -129,18 +133,20 @@
</div>
</div>
</div>
{% endblock %}
{% block self_footer_js %}
<script>
$('#userForm').validator({
timely: 2,
theme: "yellow_right_effect",
rules: {
check_ip: [/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/, 'ip地址不正确'],
check_port: [/^\d{1,5}$/, '端口号不正确'],
type_m: function(element){
return $("#M").is(":checked");
}
},
{# rules: {#}
{# check_ip: [/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/, 'ip地址不正确'],#}
{# check_port: [/^\d{1,5}$/, '端口号不正确'],#}
{# type_m: function (element) {#}
{# return $("#M").is(":checked");#}
{# }#}
{# },#}
fields: {
"username": {
rule: "required",
@ -154,18 +160,6 @@ $('#userForm').validator({
ok: "",
msg: {required: "必须填写!"}
},
"ssh_key_pwd": {
rule: "required;length[6~50]",
tip: "ssh私钥密码",
ok: "",
msg: {required: "必须填写"}
},
"dept_id": {
rule: "checked",
tip: "选择部门",
ok: "",
msg: {checked: "至少选择一个部门"}
},
"name": {
rule: "required",
tip: "姓名",
@ -178,19 +172,21 @@ $('#userForm').validator({
ok: "",
msg: {required: "必须填写"}
},
"role": {
rule: "checked",
tip: "角色",
ok: "",
msg: {required: "选择一个"}
}
},
valid: function(form) {
valid: function (form) {
form.submit();
}
}
});
$("document").ready(function(){
$("input.role").click(function(){
if($("input.role[value=GA]").is( ":checked" )){
$("#admin_groups").css("display", 'block');
}
else {
$("#admin_groups").css("display", 'none');
}
})
})
</script>
{% endblock %}