mirror of https://github.com/tp4a/teleport
调整了无数据库、有数据库等情况下普通用户登录与管理员登录时进入维护界面的流程,下一步可以开始开发安装和升级的实际操作界面了。
parent
ac88e72630
commit
09176d78f9
|
@ -373,11 +373,11 @@ class InstallerWin(InstallerBase):
|
|||
utils.copy_ex(os.path.join(env.src_path, 'www'), os.path.join(self._install_path, 'www'))
|
||||
|
||||
# 创建一个标志文件,这样访问后台时会进入维护模式
|
||||
try:
|
||||
with open(os.path.join(self._install_path, 'www', 'teleport', 'maintenance-mode'), 'w') as f:
|
||||
f.write('!!! DO NOT TOUCH !!!')
|
||||
except:
|
||||
pass
|
||||
# try:
|
||||
# with open(os.path.join(self._install_path, 'www', 'teleport', 'maintenance-mode'), 'w') as f:
|
||||
# f.write('!!! DO NOT TOUCH !!!')
|
||||
# except:
|
||||
# pass
|
||||
|
||||
if not os.path.exists(self._config_path):
|
||||
utils.copy_ex(os.path.join(env.src_path, 'tmp', 'etc'), self._config_path)
|
||||
|
|
|
@ -18,7 +18,7 @@ import eom_common.eomcore.utils as utils
|
|||
from eom_common.eomcore.logger import log
|
||||
from .const import *
|
||||
from .configs import app_cfg
|
||||
from .db import db
|
||||
from .db import get_db
|
||||
from .session import web_session
|
||||
|
||||
cfg = app_cfg()
|
||||
|
@ -39,10 +39,10 @@ class WebServerCore:
|
|||
cfg.cfg_path = os.path.abspath(options['cfg_path'])
|
||||
|
||||
# cfg.app_mode = APP_MODE_NORMAL
|
||||
if os.path.exists(os.path.join(cfg.cfg_path, 'maintenance-mode')):
|
||||
cfg.app_mode = APP_MODE_MAINTENANCE
|
||||
else:
|
||||
cfg.app_mode = APP_MODE_NORMAL
|
||||
# if os.path.exists(os.path.join(cfg.cfg_path, 'maintenance-mode')):
|
||||
# cfg.app_mode = APP_MODE_MAINTENANCE
|
||||
# else:
|
||||
# cfg.app_mode = APP_MODE_NORMAL
|
||||
|
||||
_cfg_file = os.path.join(cfg.cfg_path, 'web.ini')
|
||||
if not cfg.load_web(_cfg_file):
|
||||
|
@ -72,7 +72,11 @@ class WebServerCore:
|
|||
# db_path = os.path.join(cfg.data_path, 'ts_db.db')
|
||||
get_sqlite_pool().init(cfg.data_path)
|
||||
|
||||
db.init_sqlite(os.path.join(cfg.data_path, 'ts_db.db'))
|
||||
get_db().init_sqlite(os.path.join(cfg.data_path, 'ts_db.db'))
|
||||
if get_db().need_create or get_db().need_upgrade:
|
||||
cfg.app_mode = APP_MODE_MAINTENANCE
|
||||
else:
|
||||
cfg.app_mode = APP_MODE_NORMAL
|
||||
|
||||
return True
|
||||
|
||||
|
|
|
@ -3,18 +3,24 @@
|
|||
import os
|
||||
import threading
|
||||
import sqlite3
|
||||
import builtins
|
||||
|
||||
from .configs import app_cfg
|
||||
from eom_common.eomcore.logger import log
|
||||
|
||||
cfg = app_cfg()
|
||||
|
||||
__all__ = ['get_db']
|
||||
|
||||
__all__ = ['db']
|
||||
# 注意,每次调整数据库结构,必须增加版本号,并且在升级接口中编写对应的升级操作
|
||||
TELEPORT_DATABASE_VERSION = 2
|
||||
|
||||
|
||||
class TPDatabase:
|
||||
def __init__(self):
|
||||
if '__teleport_db__' in builtins.__dict__:
|
||||
raise RuntimeError('TPDatabase object exists, you can not create more than one instance.')
|
||||
|
||||
self.need_create = False # 数据尚未存在,需要创建
|
||||
self.need_upgrade = False # 数据库已存在但版本较低,需要升级
|
||||
self._conn_pool = None
|
||||
|
@ -27,18 +33,21 @@ class TPDatabase:
|
|||
self._conn_pool = TPSqlitePool(db_file)
|
||||
|
||||
if not os.path.exists(db_file):
|
||||
log.w('database need create.\n')
|
||||
self.need_create = True
|
||||
return
|
||||
|
||||
# 看看数据库中是否存在用户表(如果不存在,可能是一个空数据库文件),则可能是一个新安装的系统
|
||||
ret = self._conn_pool.query('SELECT COUNT(*) FROM sqlite_master where type="table" and name="ts_account";')
|
||||
if ret[0][0] == 0:
|
||||
log.w('database need create.\n')
|
||||
self.need_create = True
|
||||
return
|
||||
|
||||
# 尝试从配置表中读取当前数据库版本号(如果不存在,说明是比较旧的版本了,则置为0)
|
||||
ret = self._conn_pool.query('SELECT value FROM ts_config where name="db_ver";')
|
||||
if 0 == len(ret):
|
||||
log.w('database need upgrade.\n')
|
||||
self.need_upgrade = True
|
||||
|
||||
|
||||
|
@ -89,12 +98,16 @@ class TPSqlitePool(TPDatabasePool):
|
|||
cursor.execute(sql)
|
||||
db_ret = cursor.fetchall()
|
||||
return db_ret
|
||||
except:
|
||||
log.e('')
|
||||
except sqlite3.OperationalError:
|
||||
return list()
|
||||
finally:
|
||||
cursor.close()
|
||||
|
||||
|
||||
db = TPDatabase()
|
||||
del TPDatabase
|
||||
def get_db():
|
||||
"""
|
||||
:rtype : TPDatabase
|
||||
"""
|
||||
if '__teleport_db__' not in builtins.__dict__:
|
||||
builtins.__dict__['__teleport_db__'] = TPDatabase()
|
||||
return builtins.__dict__['__teleport_db__']
|
||||
|
|
|
@ -23,8 +23,8 @@ __all__ = ['controllers']
|
|||
controllers = [
|
||||
(r'/', index.IndexHandler),
|
||||
|
||||
# (r'/install/', maintenance.InstallHandler),
|
||||
# (r'/install', maintenance.InstallHandler),
|
||||
(r'/maintenance/install', maintenance.InstallHandler),
|
||||
(r'/maintenance/upgrade', maintenance.UpgradeHandler),
|
||||
(r'/maintenance/', maintenance.IndexHandler),
|
||||
(r'/maintenance', maintenance.IndexHandler),
|
||||
|
||||
|
|
|
@ -2,22 +2,32 @@
|
|||
|
||||
import json
|
||||
|
||||
from eom_app.app.const import *
|
||||
from eom_app.app.configs import app_cfg
|
||||
from eom_app.module import user
|
||||
from eom_common.eomcore.logger import *
|
||||
from .base import TPBaseHandler, TPBaseUserAuthHandler, TPBaseJsonHandler, TPBaseUserAuthJsonHandler
|
||||
from eom_app.app.util import gen_captcha
|
||||
|
||||
cfg = app_cfg()
|
||||
|
||||
|
||||
class LoginHandler(TPBaseHandler):
|
||||
def get(self):
|
||||
_user = self.get_current_user()
|
||||
_ref = self.get_argument('ref', '/')
|
||||
|
||||
if _user['is_login']:
|
||||
self.redirect(_ref)
|
||||
return
|
||||
|
||||
if _user['id'] == 0:
|
||||
user_name = ''
|
||||
else:
|
||||
user_name = _user['name']
|
||||
|
||||
param = {
|
||||
'ref': self.get_argument('ref', '/'),
|
||||
'ref': _ref,
|
||||
'user_name': user_name
|
||||
}
|
||||
self.render('auth/login.mako', page_param=json.dumps(param))
|
||||
|
@ -27,7 +37,7 @@ class VerifyUser(TPBaseJsonHandler):
|
|||
def post(self):
|
||||
code = self.get_session('captcha')
|
||||
if code is None:
|
||||
self.write_json(-1, 'can not get captcha')
|
||||
self.write_json(-1, '验证码已失效')
|
||||
return
|
||||
|
||||
self.del_session('captcha')
|
||||
|
@ -39,17 +49,20 @@ class VerifyUser(TPBaseJsonHandler):
|
|||
username = args['username']
|
||||
userpwd = args['userpwd']
|
||||
else:
|
||||
self.write_json(-1, 'invalid param')
|
||||
self.write_json(-1, '系统内部错误')
|
||||
return
|
||||
|
||||
if code.lower() != captcha.lower():
|
||||
self.write_json(-1, 'invalid captcha')
|
||||
self.write_json(-1, '验证码错误')
|
||||
return
|
||||
|
||||
try:
|
||||
user_id, account_type, nickname = user.verify_user(username, userpwd)
|
||||
if user_id == 0:
|
||||
self.write_json(-1, 'no such user or password.')
|
||||
if cfg.app_mode == APP_MODE_MAINTENANCE:
|
||||
self.write_json(-2, '系统维护中,请稍候再试')
|
||||
else:
|
||||
self.write_json(-1, '用户名/密码错误')
|
||||
return
|
||||
|
||||
_user = self.get_session('user')
|
||||
|
@ -75,7 +88,7 @@ class VerifyUser(TPBaseJsonHandler):
|
|||
|
||||
except:
|
||||
log.e('can not set session.')
|
||||
self.write_json(-1, 'session error.')
|
||||
self.write_json(-1, '无法记录用户登录状态')
|
||||
|
||||
|
||||
class LogoutHandler(TPBaseUserAuthHandler):
|
||||
|
|
|
@ -10,9 +10,10 @@ import mako.template
|
|||
import tornado.web
|
||||
from tornado.escape import json_encode
|
||||
|
||||
from eom_app.app.const import *
|
||||
from eom_app.app.session import web_session, SESSION_EXPIRE
|
||||
from eom_app.app.configs import app_cfg
|
||||
from eom_app.app.const import *
|
||||
from eom_app.app.db import get_db
|
||||
|
||||
cfg = app_cfg()
|
||||
|
||||
|
@ -24,6 +25,7 @@ class TPBaseHandler(tornado.web.RequestHandler):
|
|||
|
||||
MODE_HTTP = 0
|
||||
MODE_JSON = 1
|
||||
|
||||
# MODE_JSONP = 2
|
||||
|
||||
def __init__(self, application, request, **kwargs):
|
||||
|
@ -165,6 +167,7 @@ class TPBaseUserAuthHandler(TPBaseHandler):
|
|||
return
|
||||
|
||||
reference = self.request.uri
|
||||
print(reference)
|
||||
|
||||
user = self.get_current_user()
|
||||
if not user['is_login']:
|
||||
|
@ -174,10 +177,19 @@ class TPBaseUserAuthHandler(TPBaseHandler):
|
|||
else:
|
||||
self.redirect('/auth/login')
|
||||
else:
|
||||
if cfg.app_mode == APP_MODE_MAINTENANCE and user['type'] != 100:
|
||||
self.render('maintenance/index.mako')
|
||||
else:
|
||||
pass
|
||||
# if cfg.app_mode == APP_MODE_MAINTENANCE and user['type'] != 100:
|
||||
# self.render('maintenance/index.mako')
|
||||
# else:
|
||||
# pass
|
||||
if cfg.app_mode == APP_MODE_MAINTENANCE:
|
||||
if user['type'] != 100:
|
||||
self.render('maintenance/index.mako')
|
||||
else:
|
||||
if not reference.startswith('/maintenance/'):
|
||||
if get_db().need_create:
|
||||
self.redirect('/maintenance/install')
|
||||
elif get_db().need_upgrade:
|
||||
self.redirect('/maintenance/upgrade')
|
||||
|
||||
|
||||
class TPBaseAdminAuthHandler(TPBaseHandler):
|
||||
|
@ -193,17 +205,18 @@ class TPBaseAdminAuthHandler(TPBaseHandler):
|
|||
|
||||
user = self.get_current_user()
|
||||
if not user['is_login'] or user['type'] != 100:
|
||||
if reference != '/auth/login': # 防止循环重定向
|
||||
if reference != '/auth/login': # 防止循环重定向
|
||||
x = quote(reference)
|
||||
self.redirect('/auth/login?ref={}'.format(x))
|
||||
else:
|
||||
self.redirect('/auth/login')
|
||||
else:
|
||||
if cfg.app_mode == APP_MODE_MAINTENANCE:
|
||||
# TODO: 如果是维护模式,且尚未建立数据库,则引导用户进入安装界面,否则检查数据库版本,可能引导用户进入升级界面。
|
||||
self.render('maintenance/index.mako')
|
||||
else:
|
||||
pass
|
||||
if not reference.startswith('/maintenance/'):
|
||||
if get_db().need_create:
|
||||
self.redirect('/maintenance/install')
|
||||
elif get_db().need_upgrade:
|
||||
self.redirect('/maintenance/upgrade')
|
||||
|
||||
|
||||
class TPBaseUserAuthJsonHandler(TPBaseJsonHandler):
|
||||
|
|
|
@ -1,9 +1,25 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from .base import TPBaseHandler
|
||||
from .base import TPBaseUserAuthHandler, TPBaseAdminAuthHandler
|
||||
from eom_app.app.db import get_db
|
||||
|
||||
|
||||
class IndexHandler(TPBaseHandler):
|
||||
class IndexHandler(TPBaseUserAuthHandler):
|
||||
def get(self):
|
||||
self.render('maintenance/index.mako')
|
||||
|
||||
|
||||
class InstallHandler(TPBaseAdminAuthHandler):
|
||||
def get(self):
|
||||
if get_db().need_upgrade:
|
||||
return self.redirect('/maintenance/upgrade')
|
||||
|
||||
self.render('maintenance/install.mako')
|
||||
|
||||
|
||||
class UpgradeHandler(TPBaseAdminAuthHandler):
|
||||
def get(self):
|
||||
if get_db().need_create:
|
||||
return self.redirect('/maintenance/install')
|
||||
|
||||
self.render('maintenance/upgrade.mako')
|
||||
|
|
|
@ -22,7 +22,6 @@ def verify_user(name, password):
|
|||
if cfg.app_mode == APP_MODE_MAINTENANCE:
|
||||
if name == 'admin' and password == 'admin':
|
||||
return 1, 100, 'admin'
|
||||
else:
|
||||
return 0, 0, ''
|
||||
|
||||
if len(db_ret) != 1:
|
||||
|
|
|
@ -152,7 +152,7 @@ ywl.create_app = function () {
|
|||
}
|
||||
else {
|
||||
hide_op_box();
|
||||
show_op_box('error', '无法登录TELEPORT!');
|
||||
show_op_box('error', '无法登录TELEPORT:'+ret.message);
|
||||
console.log(ret);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
<%!
|
||||
page_title_ = '安装配置'
|
||||
## page_menu_ = ['user']
|
||||
## page_id_ = 'user'
|
||||
%>
|
||||
<%inherit file="../page_maintenance_base.mako"/>
|
||||
|
||||
<%block name="breadcrumb">
|
||||
<ol class="breadcrumb">
|
||||
<li><i class="fa fa-cog fa-fw"></i> ${self.attr.page_title_}</li>
|
||||
</ol>
|
||||
</%block>
|
||||
|
||||
<%block name="embed_css">
|
||||
<style type="text/css">
|
||||
.content_box {
|
||||
margin-top:48px;
|
||||
}
|
||||
|
||||
.content_box .error_sidebar {
|
||||
float: left;
|
||||
width: 160px;
|
||||
margin-left: 120px;
|
||||
font-size: 260px;
|
||||
color: #e3693b;
|
||||
}
|
||||
|
||||
.content_box .error_content {
|
||||
min-height: 400px;
|
||||
width: 800px;
|
||||
padding: 30px;
|
||||
margin-left: 300px;
|
||||
background: #ffffff;
|
||||
border-radius: 5px;
|
||||
|
||||
background: rgba(255, 255, 255, 0.8);
|
||||
background: #fff \9;
|
||||
z-index: 9;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
h1 .fa-spin {
|
||||
color:#aaa;
|
||||
}
|
||||
</style>
|
||||
</%block>
|
||||
|
||||
## Begin Main Body.
|
||||
|
||||
<div class="page-content">
|
||||
|
||||
<div class="content_box">
|
||||
<div class="container">
|
||||
|
||||
<div class="error_sidebar">
|
||||
<i class="fa fa-exclamation-triangle"></i>
|
||||
</div>
|
||||
|
||||
<div class="error_content">
|
||||
<br/>
|
||||
<h1><i class="fa fa-cog fa-spin"></i> 系统维护中...</h1>
|
||||
<hr/>
|
||||
<p>系统管理员正在紧张地维护系统,请稍后刷新页面重试!</p>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
|
@ -0,0 +1,69 @@
|
|||
<%!
|
||||
page_title_ = '系统升级'
|
||||
## page_menu_ = ['user']
|
||||
## page_id_ = 'user'
|
||||
%>
|
||||
<%inherit file="../page_maintenance_base.mako"/>
|
||||
|
||||
<%block name="breadcrumb">
|
||||
<ol class="breadcrumb">
|
||||
<li><i class="fa fa-cog fa-fw"></i> ${self.attr.page_title_}</li>
|
||||
</ol>
|
||||
</%block>
|
||||
|
||||
<%block name="embed_css">
|
||||
<style type="text/css">
|
||||
.content_box {
|
||||
margin-top:48px;
|
||||
}
|
||||
|
||||
.content_box .error_sidebar {
|
||||
float: left;
|
||||
width: 160px;
|
||||
margin-left: 120px;
|
||||
font-size: 260px;
|
||||
color: #e3693b;
|
||||
}
|
||||
|
||||
.content_box .error_content {
|
||||
min-height: 400px;
|
||||
width: 800px;
|
||||
padding: 30px;
|
||||
margin-left: 300px;
|
||||
background: #ffffff;
|
||||
border-radius: 5px;
|
||||
|
||||
background: rgba(255, 255, 255, 0.8);
|
||||
background: #fff \9;
|
||||
z-index: 9;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
h1 .fa-spin {
|
||||
color:#aaa;
|
||||
}
|
||||
</style>
|
||||
</%block>
|
||||
|
||||
## Begin Main Body.
|
||||
|
||||
<div class="page-content">
|
||||
|
||||
<div class="content_box">
|
||||
<div class="container">
|
||||
|
||||
<div class="error_sidebar">
|
||||
<i class="fa fa-exclamation-triangle"></i>
|
||||
</div>
|
||||
|
||||
<div class="error_content">
|
||||
<br/>
|
||||
<h1><i class="fa fa-cog fa-spin"></i> 系统维护中...</h1>
|
||||
<hr/>
|
||||
<p>系统管理员正在紧张地维护系统,请稍后刷新页面重试!</p>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
Loading…
Reference in New Issue