2015-01-05 15:55:05 +00:00
|
|
|
# coding: utf-8
|
2015-01-10 07:24:16 +00:00
|
|
|
# Author: Guanghongwei
|
|
|
|
# Email: ibuler@qq.com
|
|
|
|
|
|
|
|
import time
|
2015-01-10 07:54:09 +00:00
|
|
|
import hashlib
|
|
|
|
import random
|
2015-01-05 15:55:05 +00:00
|
|
|
|
2015-01-03 12:42:20 +00:00
|
|
|
from django.shortcuts import render_to_response
|
|
|
|
|
2015-01-10 05:50:59 +00:00
|
|
|
from juser.models import UserGroup, User
|
2015-01-10 07:54:09 +00:00
|
|
|
from connect import PyCrypt, KEY
|
|
|
|
from jumpserver.views import header_path
|
|
|
|
|
|
|
|
|
|
|
|
cryptor = PyCrypt(KEY)
|
|
|
|
|
|
|
|
|
|
|
|
def md5_crypt(string):
|
|
|
|
return hashlib.new("md5", string).hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
def gen_rand_pass(num):
|
|
|
|
"""生成随机密码"""
|
|
|
|
seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
salt_list = []
|
|
|
|
for i in range(num):
|
|
|
|
salt_list.append(random.choice(seed))
|
|
|
|
salt = ''.join(salt_list)
|
|
|
|
return salt
|
2015-01-07 15:56:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AddError(Exception):
|
|
|
|
pass
|
|
|
|
|
2015-01-03 12:42:20 +00:00
|
|
|
|
2015-01-07 15:20:48 +00:00
|
|
|
def group_add(request):
|
2015-01-07 15:56:36 +00:00
|
|
|
error = ''
|
|
|
|
msg = ''
|
2015-01-10 07:54:09 +00:00
|
|
|
header_title, path1, path2 = header_path('添加属组 | Add Group', 'juser', 'group_add')
|
|
|
|
|
2015-01-07 15:56:36 +00:00
|
|
|
if request.method == 'POST':
|
2015-01-10 07:15:49 +00:00
|
|
|
group_name = request.POST.get('group_name', None)
|
|
|
|
comment = request.POST.get('comment', None)
|
2015-01-07 15:56:36 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
if not group_name:
|
|
|
|
error = u'组名不能为空'
|
|
|
|
raise AddError
|
|
|
|
|
2015-01-10 05:50:59 +00:00
|
|
|
group = UserGroup.objects.filter(name=group_name)
|
2015-01-07 15:56:36 +00:00
|
|
|
if group:
|
|
|
|
error = u'组 %s 已存在' % group_name
|
2015-01-07 16:01:41 +00:00
|
|
|
raise AddError
|
2015-01-07 15:56:36 +00:00
|
|
|
|
2015-01-10 05:50:59 +00:00
|
|
|
group = UserGroup(name=group_name, comment=comment)
|
2015-01-07 15:56:36 +00:00
|
|
|
group.save()
|
|
|
|
except AddError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
except TypeError:
|
|
|
|
error = u'保存用户失败'
|
|
|
|
|
|
|
|
else:
|
2015-01-07 16:07:00 +00:00
|
|
|
msg = u'添加组 %s 成功' % group_name
|
2015-01-07 15:56:36 +00:00
|
|
|
|
2015-01-07 15:20:48 +00:00
|
|
|
return render_to_response('juser/group_add.html',
|
2015-01-10 07:54:09 +00:00
|
|
|
locals())
|
2015-01-07 15:20:48 +00:00
|
|
|
|
|
|
|
|
2015-01-09 14:10:38 +00:00
|
|
|
def group_list(request):
|
2015-01-10 07:54:09 +00:00
|
|
|
|
2015-01-10 05:50:59 +00:00
|
|
|
groups = UserGroup.objects.all()
|
2015-01-09 14:51:46 +00:00
|
|
|
return render_to_response('juser/group_list.html',
|
|
|
|
{'header_title': u'查看属组 | Add Group',
|
|
|
|
'path1': 'juser', 'path2': 'group_add',
|
|
|
|
'groups': groups})
|
2015-01-09 14:10:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def user_list(request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-01-10 06:52:35 +00:00
|
|
|
def db_add_user(**kwargs):
|
2015-01-10 07:24:16 +00:00
|
|
|
groups_post = kwargs.pop('groups')
|
2015-01-10 06:52:35 +00:00
|
|
|
user = User(**kwargs)
|
|
|
|
group_select = []
|
2015-01-10 07:24:16 +00:00
|
|
|
for group_id in groups_post:
|
2015-01-10 06:52:35 +00:00
|
|
|
group = UserGroup.objects.filter(id=group_id)
|
|
|
|
group_select.extend(group)
|
|
|
|
user.save()
|
2015-01-10 07:24:16 +00:00
|
|
|
user.user_group = group_select
|
2015-01-10 06:52:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def db_del_user(username):
|
|
|
|
user = User.objects.get(username=username)
|
|
|
|
user.delete()
|
|
|
|
|
|
|
|
|
2015-01-05 13:20:09 +00:00
|
|
|
def user_add(request):
|
2015-01-10 05:10:44 +00:00
|
|
|
error = ''
|
|
|
|
msg = ''
|
|
|
|
|
2015-01-10 06:52:35 +00:00
|
|
|
user_role = {'SU': u'超级管理员', 'GA': u'组管理员', 'CU': u'普通用户'}
|
|
|
|
all_group = UserGroup.objects.all()
|
2015-01-10 05:10:44 +00:00
|
|
|
if request.method == 'POST':
|
2015-01-10 07:15:49 +00:00
|
|
|
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)
|
|
|
|
role = 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')
|
2015-01-10 05:10:44 +00:00
|
|
|
|
2015-01-10 06:52:35 +00:00
|
|
|
try:
|
|
|
|
if None in [username, password, ssh_key_pwd1, name, groups, role, is_active]:
|
|
|
|
error = u'带*内容不能为空'
|
|
|
|
raise AddError
|
|
|
|
user = User.objects.filter(username=username)
|
|
|
|
if user:
|
|
|
|
error = u'用户 %s 已存在' % username
|
|
|
|
raise AddError
|
|
|
|
|
|
|
|
except AddError:
|
|
|
|
pass
|
|
|
|
else:
|
2015-01-10 07:24:16 +00:00
|
|
|
time_now = time.time()
|
2015-01-10 06:52:35 +00:00
|
|
|
db_add_user(username=username, password=password, name=name, email=email,
|
|
|
|
groups=groups, role=role, ssh_pwd=ssh_pwd, ssh_key_pwd1=ssh_key_pwd1,
|
2015-01-10 07:24:16 +00:00
|
|
|
is_active=is_active, date_joined=time_now)
|
2015-01-10 06:52:35 +00:00
|
|
|
msg = u'添加用户成功'
|
2015-01-07 15:20:48 +00:00
|
|
|
return render_to_response('juser/user_add.html',
|
2015-01-10 05:10:44 +00:00
|
|
|
{'header_title': u'添加用户 | Add User',
|
|
|
|
'path1': 'juser', 'path2': 'user_add',
|
2015-01-10 06:52:35 +00:00
|
|
|
'roles': user_role, 'all_group': all_group,
|
|
|
|
'error': error, 'msg': msg})
|
2015-01-07 15:20:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-03 12:42:20 +00:00
|
|
|
|
2014-12-22 09:18:51 +00:00
|
|
|
|
2015-01-10 04:30:21 +00:00
|
|
|
|
|
|
|
|