new: verify the register message
This commit is contained in:
parent
ef058e2770
commit
a5579e04f0
@ -7,6 +7,7 @@ database_passwd = "guest1234"
|
||||
database_name = "GUESTDB"
|
||||
table_name = "user_info"
|
||||
|
||||
DEBUG = False
|
||||
|
||||
# 打开数据库连接
|
||||
db = pymysql.connect(host=database_ip, user=database_user, password=database_passwd, database=database_name )
|
||||
@ -24,9 +25,27 @@ def DB_Check_byName(user_name):
|
||||
passwd = results[0][2]
|
||||
return passwd
|
||||
except:
|
||||
print("There is no the user's info !")
|
||||
if DEBUG:
|
||||
print("There is no the user's info !")
|
||||
return False
|
||||
|
||||
|
||||
def DB_Check_byEmail(email):
|
||||
sql = "SELECT * FROM %s WHERE user_email='%s' " % (table_name,email)
|
||||
try:
|
||||
# 执行SQL语句
|
||||
cursor.execute(sql)
|
||||
# 获取所有记录列表
|
||||
results = cursor.fetchall()
|
||||
passwd = results[0][2]
|
||||
return passwd
|
||||
except:
|
||||
if DEBUG:
|
||||
print("There is no the user's info !")
|
||||
return False
|
||||
|
||||
|
||||
|
||||
def DB_Insert_User(user_info):
|
||||
user_name = user_info["username"]
|
||||
user_passwd = user_info["password"]
|
||||
@ -38,12 +57,14 @@ def DB_Insert_User(user_info):
|
||||
cursor.execute(sql)
|
||||
# 执行sql语句
|
||||
db.commit()
|
||||
print("Insert user_info successfully!")
|
||||
if DEBUG:
|
||||
print("Insert user_info successfully!")
|
||||
return True
|
||||
except:
|
||||
# 发生错误时回滚
|
||||
db.rollback()
|
||||
print("Insert user_info error!")
|
||||
if DEBUG:
|
||||
print("Insert user_info error!")
|
||||
return False
|
||||
|
||||
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -15,6 +15,27 @@ def add_user(username,password,email=''):
|
||||
result = DB_Insert_User({'username':username,'password':password,'email':email,'date':create_time})
|
||||
return result
|
||||
|
||||
def check_register(username,password,email):
|
||||
check_name = DB_Check_byName(username)
|
||||
check_email = DB_Check_byEmail(email)
|
||||
check_pw = check_password(password)
|
||||
|
||||
if check_name != False:
|
||||
return 1
|
||||
if check_email != False:
|
||||
return 2
|
||||
if check_pw == False:
|
||||
return 3
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def check_password(password):
|
||||
if(len(password)<4):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
# USER_DATA = {
|
||||
# 'name':'user1',
|
||||
# 'password':'1234'
|
||||
|
@ -1,5 +1,5 @@
|
||||
from .main import AuthBaseHandler
|
||||
from .account import authenticate, add_user
|
||||
from .account import authenticate, add_user, check_register
|
||||
from .MongoDB import *
|
||||
|
||||
DEBUG = False
|
||||
@ -62,13 +62,22 @@ class RegisterHandler(AuthBaseHandler):
|
||||
password2 = self.get_argument('password2','')
|
||||
|
||||
if username and password1 and (password1 == password2):
|
||||
success = add_user(username,password1,email)
|
||||
if success:
|
||||
Mongo.connect(DataBase='example',Collection=username)
|
||||
Mongo.update(behavior='register',tags='auth')
|
||||
self.redirect('/login')
|
||||
else:
|
||||
self.write({'msg':'register fail'})
|
||||
flag_checck = check_register(username,password1,email)
|
||||
if flag_checck == 0:
|
||||
success = add_user(username,password1,email)
|
||||
if success:
|
||||
Mongo.connect(DataBase='example',Collection=username)
|
||||
Mongo.update(behavior='register',tags='auth')
|
||||
self.redirect('/login')
|
||||
else:
|
||||
self.write({'msg':'register fail'})
|
||||
elif flag_checck == 1:
|
||||
self.write({'msg':'the username is not avaliable!'})
|
||||
elif flag_checck == 2:
|
||||
self.write({'msg':'the email is not avaliable!'})
|
||||
elif flag_checck == 3:
|
||||
self.write({'msg':'the password is too shor!'})
|
||||
|
||||
else:
|
||||
if DEBUG:
|
||||
print('register again')
|
||||
|
@ -8,7 +8,7 @@ class AuthBaseHandler(tornado.web.RequestHandler,SessionMixin):
|
||||
return self.session.get('username',None) #session是一种会话状态,跟数据库的session可能不一样
|
||||
|
||||
#添加装饰器,装饰需要验证的请求
|
||||
class IndexHandler(AuthBaseHandler):
|
||||
class IndexHandler(AuthBaseHandler):
|
||||
@tornado.web.authenticated #@tornado.web.authenticated装饰器包裹get方法时,表示这个方法只有在用户合法时才会调用,authenticated装饰器会调用get_current_user()方法获取current_user的值,若值为False,则重定向到登录url装饰器判断有没有登录,如果没有则跳转到配置的路由下去,但是要在app.py里面设置login_url
|
||||
def get(self,*args,**kwargs):
|
||||
# self.render('main/index_Zh.html')
|
||||
@ -43,13 +43,21 @@ class Index_En_Handler(AuthBaseHandler):
|
||||
self.render('main/index_En.html')
|
||||
|
||||
#添加装饰器,装饰需要验证的请求
|
||||
class Open_Index_Zh_Handler(tornado.web.RequestHandler):
|
||||
class Open_Index_Zh_Handler(AuthBaseHandler):
|
||||
|
||||
def get(self,*args,**kwargs):
|
||||
self.render('main/open_index_Zh.html')
|
||||
username = self.get_current_user()
|
||||
if username == None:
|
||||
self.render('main/open_index_Zh.html')
|
||||
else:
|
||||
self.render('main/index_Zh.html')
|
||||
|
||||
#添加装饰器,装饰需要验证的请求
|
||||
class Open_Index_En_Handler(tornado.web.RequestHandler):
|
||||
class Open_Index_En_Handler(AuthBaseHandler):
|
||||
|
||||
def get(self,*args,**kwargs):
|
||||
self.render('main/open_index_En.html')
|
||||
username = self.get_current_user()
|
||||
if username == None:
|
||||
self.render('main/open_index_En.html')
|
||||
else:
|
||||
self.render('main/index_En.html')
|
Loading…
Reference in New Issue
Block a user