Example #1
0
def login( request):
	'''
	登录
	'''
	Random = int(time.time() * 100)
	from settings import Title,LoginFailNums,LonginAgainSec
	Now = datetime.datetime.now()
	ip = request.META['REMOTE_ADDR']
	if request.POST.get('action','') == "GetMePublickey":#获取公钥匙
		return HttpResponse(GetMePublickey(request))
	if request.method ==  'POST' and request.session.get('verify',''):
		request.session.setdefault('loginfail',0)
		if request.session['loginfail'] > LoginFailNums:
			request.session.setdefault('logintime',Now)
			TimeDifference = (Now - request.session['logintime']).seconds
			if TimeDifference > LonginAgainSec:
				del request.session['logintime']
				request.session['loginfail'] = 0
			else:
				return HttpResponse('<script>alert("%s秒 后再试")</script>' % (LonginAgainSec - TimeDifference))	
		verify = request.session.get('verify','1')
		checkcode = request.POST.get('check','2')
		password = request.POST.get('password','')
		loginname = request.POST.get('loginname','')
		if  verify == checkcode and len(password) == 256:#Rsa1024为加密后长度为256
			privkey = request.session['privkey']
			password = privatekey_decodeing(password,privkey)
			loginname = privatekey_decodeing(loginname,privkey)
			try:
				user_objects = user.objects.get(loginname=loginname,password=Sha1Md5(password)) 
				if user_objects.lock == False :
					request.session['username'] = user_objects.username
					request.session['loginname'] = user_objects.loginname
					request.session['uid'] = user_objects.id
					request.session['permission'] = user_objects.permission
					request.session['accesstime'] = Now
					request.session['logintime'] = Now
					request.session['loginip'] = ip
					request.session['verify'] = ''
					user_objects.logintime = Now
					user_objects.loginip = ip
					user_objects.save()
					request.session['loginfail'] = 0
					Log( request,u'登录成功','')
					_fromurl = request.GET.get('fromurl','') or '/'
					return HttpResponse('<script> window.top.location="%s"</script>' % _fromurl)
					#return HttpResponseRedirect('/')
				else:
					msg = u'用户已锁定!' 
					Log( request,u'登录失败',u'用户锁定',loginname)
			except:
				request.session['loginfail'] += 1
				msg = u'登录失败,你还有[%s]次机会!'%(LoginFailNums-request.session['loginfail'])
				Log( request,u'登录失败',u'认证失败',loginname)
		else:
			request.session['loginfail'] += 1
			msg = u'验证错误,你还有[%s]次机会!'%(LoginFailNums-request.session['loginfail'])

	return render_to_response('login.html',locals())
Example #2
0
def EditUser(request, Me=False):
    '''
    编辑用户
    '''
    Random = int(time.time() * 100)
    User, Uid = GetPost(request, ['user', 'uid'])
    User = request.session['loginname'] if Me else User
    Uid = request.session['uid'] if Me else Uid
    if Uid:
        obj = user.objects.get(id=Uid)
        UserName = obj.username
        LoginName = User
        Contact = obj.contact
        if request.method == "POST":
            loginname, username, password, contact = GetPost(
                request, ['loginname', 'username', 'password', 'contact'])
            if password:
                privkey = request.session['privkey']
                password = privatekey_decodeing(password, privkey)
                password = Sha1Md5(password)
                obj.password = password
            obj.username, obj.contact = username, contact
            if not Me:
                obj.loginname = LoginName
            obj.save()
            return HttpResponse(u'编辑%s[%s]成功!' % (User, username))
    button = [[u'确定', u'?uid = %s&user = %s' % (Uid, User)]]
    return render_to_response('EditUser.html', locals())
Example #3
0
def AddUser(request):
    '''
    增加用户
    '''
    Random = int(time.time() * 100)
    loginname, username, password, contact, viewslist = GetPost(request,
                                                                ['loginname', 'username', 'password', 'contact', 'checkbox'])
    if request.method == "POST":
        if viewslist and username and loginname and password:
            privkey = request.session['privkey']
            password = privatekey_decodeing(password, privkey)
            password = Sha1Md5(password)
            createuser = '******' % (
                request.session['username'], request.session['loginname'])
            if not user.objects.filter(loginname=loginname):
                permission = views.MakePermission(viewslist)
                user.objects.create(
                    username=username, createuser=createuser, loginname=loginname,
                    password=password, permission=permission, contact=contact)
                return HttpResponse('注册成功!')
            else:
                return HttpResponse('用户已存在!')
        return HttpResponse(u'请至少选择一个权限!') if not viewslist else HttpResponse(u'注册失败!')
    Views = GetPermission(request.session['permission'], False)

    return render_to_response('AddUser.html', locals())
Example #4
0
def EditUser(request, Me=False):
    '''
    编辑用户
    '''
    Random = int(time.time() * 100)
    User, Uid = GetPost(request, ['user', 'uid'])
    User = request.session['loginname'] if Me else User
    Uid = request.session['uid'] if Me else Uid
    if Uid:
        obj = user.objects.get(id=Uid)
        UserName = obj.username
        LoginName = User
        Contact = obj.contact
        if request.method == "POST":
            loginname, username, password, contact = GetPost(
                request, ['loginname', 'username', 'password', 'contact'])
            if password:
                privkey = request.session['privkey']
                password = privatekey_decodeing(password, privkey)
                password = Sha1Md5(password)
                obj.password = password
            obj.username, obj.contact = username, contact
            if not Me:
                obj.loginname = LoginName
            obj.save()
            return HttpResponse(u'编辑%s[%s]成功!' % (User, username))
    button = [[u'确定', u'?uid = %s&user = %s' % (Uid, User)]]
    return render_to_response('EditUser.html', locals())
Example #5
0
def AddUser(request):
    '''
    增加用户
    '''
    Random = int(time.time() * 100)
    loginname, username, password, contact, viewslist = GetPost(
        request, ['loginname', 'username', 'password', 'contact', 'checkbox'])
    if request.method == "POST":
        if viewslist and username and loginname and password:
            privkey = request.session['privkey']
            password = privatekey_decodeing(password, privkey)
            password = Sha1Md5(password)
            createuser = '******' % (request.session['username'],
                                     request.session['loginname'])
            if not user.objects.filter(loginname=loginname):
                permission = views.MakePermission(viewslist)
                user.objects.create(username=username,
                                    createuser=createuser,
                                    loginname=loginname,
                                    password=password,
                                    permission=permission,
                                    contact=contact)
                return HttpResponse('注册成功!')
            else:
                return HttpResponse('用户已存在!')
        return HttpResponse(u'请至少选择一个权限!') if not viewslist else HttpResponse(
            u'注册失败!')
    Views = GetPermission(request.session['permission'], False)

    return render_to_response('AddUser.html', locals())
Example #6
0
def AddHost(request):
    '''
    增加与编辑主机
    '''
    Random=int(time.time()*100)
    Hostgroupselect=hostgroup.GetHostGroup()
    if request.POST.get('action','')=="GetMePublickey":#获取公钥
        return HttpResponse(GetMePublickey(request))
            
    ajaxurl=request.path_info
    action,hid,ip,port,alias,user,password,group,describe,sshtype=GetPost(request,
    ['action','hid','HostIP','HostPort','HostAlias','HostUser_e','HostPassword_e','SelectHostGroup','HostDescribe','checkbox'])
    if request.method=="POST":
        sshtype=1 if sshtype else 0
        if user or password :
            privkey=request.session['privkey']
            user=privatekey_decodeing(user,privkey) if user else user
            password=privatekey_decodeing(password,privkey) if password else password
        if action=="AddHost":
                if  ip and port and user and password  and not hosts.objects.filter(ip=ip,user=user) :
                    alias=alias or ''
                    host=Host(ip,port,user,password,sshtype)
                    c,s,t=host.SetPublicKey()

                    if c==0:
                        o=hosts.objects.create(alias=alias,ip=ip,user=user,
                                            password=password,
                                            port=int(port),
                                            group=group.rstrip(',')+',' ,
                                            describe=describe,
                                            sshtype=sshtype)
                        if not alias:
                            o.alias='Host_%s'%o.id
                            o.save()
                        SettingHostGroup()
                        Newrequest=Myrequest()
                        Newrequest.POST['hid'],Newrequest.POST['ip']=(str(o.id),ip)
                        return HostAttr(Newrequest)
                        #return HttpResponse(u'增加%s@%s成功!<br>%s'%(user,ip,s))
                    else:
                        return HttpResponse(u"不能设置公钥!<br>%s"%str(s))
                return HttpResponse(u'<h2 style="color:red">增加%s@%s出错!</h2>'%(user,ip))
        elif  hid and action=="EditHost" :
                try:
                    o=hosts.objects.get(id=hid)
                    if alias:o.alias=alias
                    if password:o.password=password
                    if user:o.user=user
                    if ip:o.ip=ip
                    if port:o.port=int(port)
                    o.group=group.rstrip(',')+',' if group else ''
                    if describe:o.describe=describe
                    o.sshtype=sshtype
                    o.save()
                    SettingHostGroup()
                    return HttpResponse(u'修改%s@%s成功!'%(user,ip))
                    
                except:
                    return HttpResponse(u'修改%s@%s失败!'%(user,ip))			
        return HttpResponse('Not')
    if hid:
        o=hosts.objects.get(id=hid)
        alias,ip,port,user,group,describe=o.alias,o.ip,o.port,o.user,o.group,o.describe
        sudocheckbox=MakeCheckbox('sudo',u'sudo为root',True if o.sshtype==1 else False)
        button=[[u'修改主机',MakeURL(AddHost)+'?action=EditHost&hid=%s'%hid]]
    else:
        sudocheckbox=MakeCheckbox('sudo',u'sudo为root', False)
        button=[[u'增加主机',MakeURL(AddHost)+'?action=AddHost','load','']]
    return render_to_response('AddHost.html',locals())
Example #7
0
def login(request):
    '''
    登录
    '''
    Random = int(time.time() * 100)
    from settings import Title, LoginFailNums, LonginAgainSec
    Now = datetime.datetime.now()
    ip = request.META['REMOTE_ADDR']
    if request.POST.get('action', '') == "GetMePublickey":  # 获取公钥匙
        return HttpResponse(GetMePublickey(request))
    if request.method == 'POST' and request.session.get('verify', ''):
        request.session.setdefault('loginfail', 0)
        if request.session['loginfail'] > LoginFailNums:
            request.session.setdefault('logintime', Now)
            TimeDifference = (Now - request.session['logintime']).seconds
            if TimeDifference > LonginAgainSec:
                del request.session['logintime']
                request.session['loginfail'] = 0
            else:
                return HttpResponse('<script>alert("%s秒 后再试")</script>' %
                                    (LonginAgainSec - TimeDifference))
        verify = request.session.get('verify', '1')
        checkcode = request.POST.get('check', '2')
        password = request.POST.get('password', '')
        loginname = request.POST.get('loginname', '')
        if verify == checkcode and len(password) == 256:  # Rsa1024为加密后长度为256
            privkey = request.session['privkey']
            password = privatekey_decodeing(password, privkey)
            loginname = privatekey_decodeing(loginname, privkey)
            try:
                user_objects = user.objects.get(loginname=loginname,
                                                password=Sha1Md5(password))
                if user_objects.lock == False:
                    request.session['username'] = user_objects.username
                    request.session['loginname'] = user_objects.loginname
                    request.session['uid'] = user_objects.id
                    request.session['permission'] = user_objects.permission
                    request.session['accesstime'] = Now
                    request.session['logintime'] = Now
                    request.session['loginip'] = ip
                    request.session['verify'] = ''
                    user_objects.logintime = Now
                    user_objects.loginip = ip
                    user_objects.save()
                    request.session['loginfail'] = 0
                    Log(request, u'登录成功', '')
                    _fromurl = request.GET.get('fromurl', '') or '/'
                    return HttpResponse(
                        '<script> window.top.location="%s"</script>' %
                        _fromurl)
                    # return HttpResponseRedirect('/')
                else:
                    msg = u'用户已锁定!'
                    Log(request, u'登录失败', u'用户锁定', loginname)
            except:
                request.session['loginfail'] += 1
                msg = u'登录失败,你还有[%s]次机会!' % (LoginFailNums -
                                             request.session['loginfail'])
                Log(request, u'登录失败', u'认证失败', loginname)
        else:
            request.session['loginfail'] += 1
            msg = u'验证错误,你还有[%s]次机会!' % (LoginFailNums -
                                         request.session['loginfail'])

    return render_to_response('login.html', locals())
Example #8
0
def AddHost(request):
    '''
    增加与编辑主机
    '''
    Random = int(time.time() * 100)
    Hostgroupselect = hostgroup.GetHostGroup()
    if request.POST.get('action', '') == "GetMePublickey":  # 获取公钥
        return HttpResponse(GetMePublickey(request))

    ajaxurl = request.path_info
    action, hid, ip, port, alias, user, password, group, describe, sshtype = GetPost(
        request, [
            'action', 'hid', 'HostIP', 'HostPort', 'HostAlias', 'HostUser_e',
            'HostPassword_e', 'SelectHostGroup', 'HostDescribe', 'checkbox'
        ])
    if request.method == "POST":
        sshtype = 1 if sshtype else 0
        if user or password:
            privkey = request.session['privkey']
            user = privatekey_decodeing(user, privkey) if user else user
            password = privatekey_decodeing(password,
                                            privkey) if password else password
        if action == "AddHost":
            if ip and port and user and password and not hosts.objects.filter(
                    ip=ip, user=user):
                alias = alias or ''
                host = Host(ip, port, user, password, sshtype)
                c, s, t = host.SetPublicKey()

                if c == 0:
                    o = hosts.objects.create(alias=alias,
                                             ip=ip,
                                             user=user,
                                             password=password,
                                             port=int(port),
                                             group=group.rstrip(',') + ',',
                                             describe=describe,
                                             sshtype=sshtype)
                    if not alias:
                        o.alias = 'Host_%s' % o.id
                        o.save()
                    SettingHostGroup()
                    Newrequest = Myrequest()
                    Newrequest.POST['hid'], Newrequest.POST['ip'] = (str(o.id),
                                                                     ip)
                    return HostAttr(Newrequest)
                    # return HttpResponse(u'增加%s@%s成功!<br>%s'%(user,ip,s))
                else:
                    return HttpResponse(u"不能设置公钥!<br>%s" % str(s))
            return HttpResponse(u'<h2 style="color:red">增加%s@%s出错!</h2>' %
                                (user, ip))
        elif hid and action == "EditHost":
            try:
                o = hosts.objects.get(id=hid)
                if alias:
                    o.alias = alias
                if password:
                    o.password = password
                if user:
                    o.user = user
                if ip:
                    o.ip = ip
                if port:
                    o.port = int(port)
                o.group = group.rstrip(',') + ',' if group else ''
                if describe:
                    o.describe = describe
                o.sshtype = sshtype
                o.save()
                SettingHostGroup()
                return HttpResponse(u'修改%s@%s成功!' % (user, ip))

            except:
                return HttpResponse(u'修改%s@%s失败!' % (user, ip))
        return HttpResponse('Not')
    if hid:
        o = hosts.objects.get(id=hid)
        alias, ip, port, user, group, describe = o.alias, o.ip, o.port, o.user, o.group, o.describe
        sudocheckbox = MakeCheckbox('sudo', u'sudo为root',
                                    True if o.sshtype == 1 else False)
        button = [[
            u'修改主机',
            MakeURL(AddHost) + '?action=EditHost&hid=%s' % hid
        ]]
    else:
        sudocheckbox = MakeCheckbox('sudo', u'sudo为root', False)
        button = [[u'增加主机', MakeURL(AddHost) + '?action=AddHost', 'load', '']]
    return render_to_response('AddHost.html', locals())