Exemple #1
0
def login(request):
    if is_authenticated(request):
        return redirect('index')

    error = ''
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')

        if username and password:
            users = Admin.objects.filter(name=username, pwd=md5(password))
            if users:
                user = users[0]
                if user.status == Admin.STATUS_CLOSE:
                    error = u'该用户已经停用,请联系管理员'
                else:
                    request.session.flush()
                    request.session['current_admin_id'] = user.id
                    request.session['current_role_id'] = user.role_id

                    user.last_login_ip = request.META['REMOTE_ADDR']
                    user.last_login_time = datetime.now()
                    user.save()

                    return redirect('index')
            else:
                error = u'用户名或密码错误'
        else:
            error = u'用户名或密码错误'
    return render_to_response('login.html', locals())
Exemple #2
0
def profile(request):
    """
    编辑账号 VIEW
    """
    current_admin = get_current_admin(request)
    if not current_admin:
        jump_view = 'login'
        message = '对不起, 您还没有登陆'
        return render_to_response('error.html', locals())

    pk_id = current_admin.id

    admin = Admin.objects.get(id=pk_id)
    org_role_id = admin.role_id
    if request.method == 'POST':
        form = ProfileEditForm(request.POST, instance=admin)
        if form.is_valid():
            admin = form.save(commit=False)

            pwd = request.POST.get('pwd')
            if pwd:
                admin.pwd = md5(pwd)

            admin.save()
            form.save_m2m()
            return redirect('index')
        else:
            emg = u'账号: 修改个人信息失败'

    roles = Role.objects.all()
    return render_to_response('profile.html', locals(), request)
Exemple #3
0
def admin_edit(request):
    """
    编辑账号 VIEW
    """
    current_admin = get_current_admin(request)
    if not current_admin or current_admin.is_super != Admin.IS_SUPPER:
        jump_view = 'admin_list'
        message = '对不起, 您没有添加账号的权限'
        return render_to_response('error.html', locals())

    pk_id = request.GET.get('id', '')

    admin = Admin.objects.get(id=pk_id)
    org_role_id = admin.role_id
    if request.method == 'POST':
        form = AdminEditForm(request.POST, instance=admin)
        if form.is_valid():
            admin = form.save(commit=False)

            pwd = request.POST.get('pwd')
            if pwd:
                admin.pwd = md5(pwd)

            admin.save()
            form.save_m2m()
            return redirect('admin_list')
        else:
            emg = u'账号: 添加失败'

    roles = Role.objects.all()
    return render_to_response('account/admin_edit.html', locals(), request)
Exemple #4
0
def admin_add(request):
    """
    添加账号 VIEW
    """
    import uuid
    from common.utils.account import server_add_user, user_add_mail

    current_admin = get_current_admin(request)
    if not current_admin or current_admin.is_super != Admin.IS_SUPPER:
        jump_view = 'admin_list'
        message = '对不起, 您没有添加账号的权限'
        return render_to_response('error.html', locals())

    if request.method == 'POST':
        form = AdminForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            pids = request.POST.getlist('pids', [])
            if Admin.objects.filter(name=name):
                emg = u'添加失败, 此账号 %s 已存在!' % name
            else:
                admin = form.save()

                pwd = request.POST.get('pwd')
                password = pwd if pwd else gen_rand_password()
                admin.pwd = md5(password)

                if pids:
                    checked_projects = Project.objects.in_bulk(pids)
                    admin.projects = checked_projects

                admin.uuid = uuid.uuid4().get_hex()
                admin.save()

                # user_add_mail(admin)

                return redirect('admin_list')
        else:
            emg = u'账号: 添加失败'

    roles = Role.objects.all()
    projects = Project.objects.all()
    return render_to_response('account/admin_add.html', locals())
Exemple #5
0
def admin_reset_pwd(request):
    """
    重置账号密码 VIEW
    """
    import uuid
    from common.utils.account import server_add_user, user_add_mail

    current_admin = get_current_admin(request)
    if not current_admin or current_admin.is_super != Admin.IS_SUPPER:
        jump_view = 'admin_list'
        message = '对不起, 您没有添加账号的权限'
        return render_to_response('error.html', locals())

    jump_view = 'admin_list'
    pk_id = request.GET.get('id', '')

    try:
        admin = Admin.objects.get(id=pk_id)
    except:
        admin = None

    if not admin:
        message = u'更新失败,不存在该账号ID'
        return render_to_response('error.html', locals())

    password = gen_rand_password()
    admin.pwd = md5(password)
    admin.uuid = uuid.uuid4().get_hex()
    admin.save()

    ssh_key_pwd = gen_rand_password()
    server_add_user(admin.name, ssh_key_pwd)

    # Todo: 推送公钥至所有关联服务器

    user_add_mail(admin, password=password, ssh_key_pwd=ssh_key_pwd)

    message = u'密码更新成功,请检查邮件'

    return render_to_response('success.html', locals())