mirror of https://github.com/tp4a/teleport
temp.
parent
2cada368e6
commit
1d665769ee
|
@ -372,13 +372,6 @@ class InstallerWin(InstallerBase):
|
|||
utils.copy_ex(os.path.join(env.src_path, 'bin'), os.path.join(self._install_path, 'bin'))
|
||||
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
|
||||
|
||||
if not os.path.exists(self._config_path):
|
||||
utils.copy_ex(os.path.join(env.src_path, 'tmp', 'etc'), self._config_path)
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ from .database.upgrade import DatabaseUpgrade
|
|||
|
||||
cfg = app_cfg()
|
||||
|
||||
__all__ = ['get_db']
|
||||
__all__ = ['get_db', 'DbItem']
|
||||
|
||||
|
||||
# 注意,每次调整数据库结构,必须增加版本号,并且在升级接口中编写对应的升级操作
|
||||
|
@ -240,6 +240,20 @@ class TPSqlitePool(TPDatabasePool):
|
|||
cursor.close()
|
||||
|
||||
|
||||
class DbItem(dict):
|
||||
def load(self, db_item, db_fields):
|
||||
if len(db_fields) != len(db_item):
|
||||
raise RuntimeError('!=')
|
||||
for i in range(len(db_item)):
|
||||
self[db_fields[i]] = db_item[i]
|
||||
|
||||
def __getattr__(self, name):
|
||||
try:
|
||||
return self[name]
|
||||
except KeyError:
|
||||
raise
|
||||
|
||||
|
||||
def get_db():
|
||||
"""
|
||||
:rtype : TPDatabase
|
||||
|
|
|
@ -33,12 +33,11 @@ def get_free_space_bytes(folder):
|
|||
|
||||
class LogHandler(TPBaseAdminAuthHandler):
|
||||
def get(self):
|
||||
user_list = user.get_user_list()
|
||||
total_size, free_size = get_free_space_bytes(cfg.data_path)
|
||||
|
||||
# ts_server = '""'
|
||||
param = {
|
||||
'user_list': user.get_user_list(),
|
||||
'user_list': user.get_user_list(with_admin=True),
|
||||
'total_size': total_size,
|
||||
'free_size': free_size,
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ class AuthHandler(TPBaseAdminAuthHandler):
|
|||
|
||||
class GetListHandler(TPBaseAdminAuthJsonHandler):
|
||||
def post(self):
|
||||
user_list = user.get_user_list()
|
||||
user_list = user.get_user_list(with_admin=False)
|
||||
ret = dict()
|
||||
ret['page_index'] = 10
|
||||
ret['total'] = len(user_list)
|
||||
|
|
|
@ -1,29 +1,33 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from .common import *
|
||||
import json
|
||||
import time
|
||||
|
||||
# from .common import *
|
||||
from eom_app.app.db import get_db, DbItem
|
||||
|
||||
|
||||
# 获取主机列表,包括主机的基本信息
|
||||
def get_all_host_info_list(filter, order, limit, with_pwd=False):
|
||||
sql_exec = get_db_con()
|
||||
def get_all_host_info_list(_filter, order, limit, with_pwd=False):
|
||||
# sql_exec = get_db_con()
|
||||
db = get_db()
|
||||
|
||||
_where = ''
|
||||
|
||||
if len(filter) > 0:
|
||||
if len(_filter) > 0:
|
||||
_where = 'WHERE ( '
|
||||
|
||||
need_and = False
|
||||
for k in filter:
|
||||
for k in _filter:
|
||||
if k == 'host_group':
|
||||
if need_and:
|
||||
_where += ' AND'
|
||||
_where += ' b.group_id={}'.format(filter[k])
|
||||
_where += ' `b`.`group_id`={}'.format(_filter[k])
|
||||
need_and = True
|
||||
elif k == 'host_sys_type':
|
||||
if need_and:
|
||||
_where += ' AND'
|
||||
_where += ' a.host_sys_type={}'.format(filter[k])
|
||||
_where += ' `a`.`host_sys_type`={}'.format(_filter[k])
|
||||
need_and = True
|
||||
elif k == 'search':
|
||||
# 查找,限于主机ID和IP地址,前者是数字,只能精确查找,后者可以模糊匹配
|
||||
|
@ -33,7 +37,7 @@ def get_all_host_info_list(filter, order, limit, with_pwd=False):
|
|||
_where += ' AND '
|
||||
|
||||
_where += '('
|
||||
_where += 'a.host_ip LIKE "%{}%" OR a.host_desc LIKE "%{}%" )'.format(filter[k], filter[k], filter[k])
|
||||
_where += '`a`.`host_ip` LIKE "%{}%" OR `a`.`host_desc` LIKE "%{}%" )'.format(_filter[k], _filter[k], _filter[k])
|
||||
need_and = True
|
||||
_where += ')'
|
||||
|
||||
|
@ -43,12 +47,12 @@ def get_all_host_info_list(filter, order, limit, with_pwd=False):
|
|||
|
||||
# field_c = ['id', 'auth_mode', 'user_name']
|
||||
|
||||
str_sql = 'SELECT COUNT(*) ' \
|
||||
'FROM ts_host_info AS a ' \
|
||||
'LEFT JOIN ts_group AS b ON a.group_id = b.group_id ' \
|
||||
'{};'.format(_where)
|
||||
sql = 'SELECT COUNT(*) ' \
|
||||
'FROM `{}host_info` AS a ' \
|
||||
'LEFT JOIN `{}group` AS b ON `a`.`group_id`=`b`.`group_id` ' \
|
||||
'{};'.format(db.table_prefix, db.table_prefix, _where)
|
||||
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
db_ret = db.query(sql)
|
||||
total_count = db_ret[0][0]
|
||||
|
||||
# 修正分页数据
|
||||
|
@ -67,24 +71,24 @@ def get_all_host_info_list(filter, order, limit, with_pwd=False):
|
|||
if order is not None:
|
||||
_order = 'ORDER BY '
|
||||
if 'host_id' == order['name']:
|
||||
_order += 'a.host_id'
|
||||
_order += '`a`.`host_id`'
|
||||
elif 'ip' == order['name']:
|
||||
_order += 'a.host_ip'
|
||||
_order += '`a`.`host_ip`'
|
||||
else:
|
||||
_order = ''
|
||||
|
||||
if not order['asc'] and len(_order) > 0:
|
||||
_order += ' DESC'
|
||||
|
||||
str_sql = 'SELECT {},{} ' \
|
||||
'FROM ts_host_info AS a ' \
|
||||
'LEFT JOIN ts_group AS b ON a.group_id = b.group_id ' \
|
||||
'{} {} {};'.format(
|
||||
','.join(['a.{}'.format(i) for i in field_a]),
|
||||
','.join(['b.{}'.format(i) for i in field_b]),
|
||||
_where, _order, _limit)
|
||||
sql = 'SELECT {},{} ' \
|
||||
'FROM `{}host_info` AS a ' \
|
||||
'LEFT JOIN `{}group` AS b ON `a`.`group_id`=`b`.`group_id` ' \
|
||||
'{} {} {};'.format(','.join(['`a`.`{}`'.format(i) for i in field_a]),
|
||||
','.join(['`b`.`{}`'.format(i) for i in field_b]),
|
||||
db.table_prefix, db.table_prefix,
|
||||
_where, _order, _limit)
|
||||
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
db_ret = db.query(sql)
|
||||
if db_ret is None:
|
||||
return 0, None
|
||||
|
||||
|
@ -121,35 +125,33 @@ def get_all_host_info_list(filter, order, limit, with_pwd=False):
|
|||
return total_count, ret
|
||||
|
||||
|
||||
def get_host_info_list_by_user(filter, order, limit):
|
||||
sql_exec = get_db_con()
|
||||
def get_host_info_list_by_user(_filter, order, limit):
|
||||
# sql_exec = get_db_con()
|
||||
db = get_db()
|
||||
|
||||
_where = ''
|
||||
|
||||
# _where = ''
|
||||
|
||||
# _where = 'WHERE ( a.account_name=\'{}\' '.format(uname)
|
||||
|
||||
if len(filter) > 0:
|
||||
if len(_filter) > 0:
|
||||
_where = 'WHERE ( '
|
||||
|
||||
need_and = False
|
||||
for k in filter:
|
||||
for k in _filter:
|
||||
if k == 'host_group':
|
||||
if need_and:
|
||||
_where += ' AND'
|
||||
_where += ' b.group_id={}'.format(filter[k])
|
||||
_where += ' b.group_id={}'.format(_filter[k])
|
||||
need_and = True
|
||||
elif k == 'host_sys_type':
|
||||
if need_and:
|
||||
_where += ' AND'
|
||||
_where += ' b.host_sys_type={}'.format(filter[k])
|
||||
_where += ' b.host_sys_type={}'.format(_filter[k])
|
||||
need_and = True
|
||||
|
||||
elif k == 'account_name':
|
||||
if need_and:
|
||||
_where += ' AND'
|
||||
_where += ' a.account_name=\'{}\''.format(filter[k])
|
||||
_where += ' a.account_name=\'{}\''.format(_filter[k])
|
||||
need_and = True
|
||||
|
||||
elif k == 'search':
|
||||
|
@ -160,7 +162,7 @@ def get_host_info_list_by_user(filter, order, limit):
|
|||
_where += ' AND '
|
||||
|
||||
_where += '('
|
||||
_where += 'b.host_ip LIKE "%{}%" OR b.host_desc LIKE "%{}%" )'.format(filter[k], filter[k], filter[k])
|
||||
_where += 'b.host_ip LIKE "%{}%" OR b.host_desc LIKE "%{}%" )'.format(_filter[k], _filter[k], _filter[k])
|
||||
need_and = True
|
||||
|
||||
_where += ')'
|
||||
|
@ -170,12 +172,12 @@ def get_host_info_list_by_user(filter, order, limit):
|
|||
field_b = ['host_id', 'host_lock', 'host_ip', 'protocol', 'host_port', 'host_desc', 'group_id', 'host_sys_type']
|
||||
field_c = ['group_name']
|
||||
field_d = ['auth_mode', 'user_name']
|
||||
str_sql = 'SELECT COUNT(DISTINCT a.host_id) ' \
|
||||
'FROM ts_auth AS a ' \
|
||||
'LEFT JOIN ts_host_info AS b ON a.host_id = b.host_id ' \
|
||||
'{};'.format(_where)
|
||||
sql = 'SELECT COUNT(DISTINCT a.host_id) ' \
|
||||
'FROM {}auth AS a ' \
|
||||
'LEFT JOIN {}host_info AS b ON a.host_id = b.host_id ' \
|
||||
'{};'.format(db.table_prefix, db.table_prefix, _where)
|
||||
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
db_ret = db.query(sql)
|
||||
total_count = db_ret[0][0]
|
||||
|
||||
# 修正分页数据
|
||||
|
@ -204,19 +206,20 @@ def get_host_info_list_by_user(filter, order, limit):
|
|||
if not order['asc'] and len(_order) > 0:
|
||||
_order += ' DESC'
|
||||
|
||||
str_sql = 'SELECT {}, {},{},{} ' \
|
||||
'FROM ts_auth AS a ' \
|
||||
'LEFT JOIN ts_host_info AS b ON a.host_id=b.host_id ' \
|
||||
'LEFT JOIN ts_group AS c ON b.group_id = c.group_id ' \
|
||||
'LEFT JOIN ts_auth_info AS d ON d.id = a.host_auth_id ' \
|
||||
'{} {} {};'.format(
|
||||
','.join(['a.{}'.format(i) for i in field_a]),
|
||||
','.join(['b.{}'.format(i) for i in field_b]),
|
||||
','.join(['c.{}'.format(i) for i in field_c]),
|
||||
','.join(['d.{}'.format(i) for i in field_d]),
|
||||
_where, _order, _limit)
|
||||
sql = 'SELECT {}, {},{},{} ' \
|
||||
'FROM {}auth AS a ' \
|
||||
'LEFT JOIN {}host_info AS b ON a.host_id=b.host_id ' \
|
||||
'LEFT JOIN {}group AS c ON b.group_id = c.group_id ' \
|
||||
'LEFT JOIN {}auth_info AS d ON d.id = a.host_auth_id ' \
|
||||
'{} {} {};' \
|
||||
''.format(','.join(['a.{}'.format(i) for i in field_a]),
|
||||
','.join(['b.{}'.format(i) for i in field_b]),
|
||||
','.join(['c.{}'.format(i) for i in field_c]),
|
||||
','.join(['d.{}'.format(i) for i in field_d]),
|
||||
db.table_prefix, db.table_prefix, db.table_prefix, db.table_prefix,
|
||||
_where, _order, _limit)
|
||||
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
db_ret = db.query(sql)
|
||||
ret = list()
|
||||
temp = dict()
|
||||
for item in db_ret:
|
||||
|
@ -266,12 +269,11 @@ def get_host_info_list_by_user(filter, order, limit):
|
|||
|
||||
|
||||
def get_group_list():
|
||||
db = get_db()
|
||||
field_a = ['group_id', 'group_name']
|
||||
sql_exec = get_db_con()
|
||||
str_sql = 'SELECT {} ' \
|
||||
'FROM ts_group AS a; ' \
|
||||
.format(','.join(['a.{}'.format(i) for i in field_a]))
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
# sql_exec = get_db_con()
|
||||
sql = 'SELECT {} FROM `{}group` AS a; '.format(','.join(['`a`.`{}`'.format(i) for i in field_a]), db.table_prefix)
|
||||
db_ret = db.query(sql)
|
||||
ret = list()
|
||||
for item in db_ret:
|
||||
x = DbItem()
|
||||
|
@ -302,6 +304,8 @@ def get_group_list():
|
|||
|
||||
|
||||
def update(host_id, kv):
|
||||
db = get_db()
|
||||
|
||||
if len(kv) == 0:
|
||||
return False
|
||||
|
||||
|
@ -310,31 +314,25 @@ def update(host_id, kv):
|
|||
if len(_val) > 0:
|
||||
_val += ','
|
||||
if k == 'desc':
|
||||
_val += 'host_desc="{}"'.format(kv[k])
|
||||
_val += '`host_desc`="{}"'.format(kv[k])
|
||||
elif k == 'pro_port':
|
||||
temp = json.dumps(kv[k])
|
||||
_val += '{}=\'{}\''.format(k, temp)
|
||||
_val += '`{}`="{}"'.format(k, temp)
|
||||
else:
|
||||
_val += '{}="{}"'.format(k, kv[k])
|
||||
_val += '`{}`="{}"'.format(k, kv[k])
|
||||
|
||||
str_sql = 'UPDATE ts_host_info SET {} ' \
|
||||
'WHERE host_id={};'.format(_val, host_id)
|
||||
|
||||
sql_exec = get_db_con()
|
||||
db_ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
return db_ret
|
||||
sql = 'UPDATE `{}host_info` SET {} WHERE `host_id`={};'.format(db.table_prefix, _val, int(host_id))
|
||||
return db.exec(sql)
|
||||
|
||||
|
||||
def get_cert_list():
|
||||
sql_exec = get_db_con()
|
||||
db = get_db()
|
||||
|
||||
# http://www.jb51.net/article/46015.htm
|
||||
field_a = ['cert_id', 'cert_name', 'cert_pub', 'cert_pri', 'cert_desc']
|
||||
|
||||
str_sql = 'SELECT {} ' \
|
||||
'FROM ts_key as a '.format(','.join(['a.{}'.format(i) for i in field_a]))
|
||||
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
sql = 'SELECT {} FROM `{}key` AS a;'.format(','.join(['`a`.`{}`'.format(i) for i in field_a]), db.table_prefix)
|
||||
db_ret = db.query(sql)
|
||||
|
||||
if db_ret is None:
|
||||
return None
|
||||
|
@ -361,15 +359,14 @@ def get_cert_list():
|
|||
|
||||
|
||||
def add_host(args, must_not_exists=True):
|
||||
sql_exec = get_db_con()
|
||||
db = get_db()
|
||||
|
||||
protocol = args['protocol']
|
||||
host_port = args['host_port']
|
||||
host_ip = args['host_ip']
|
||||
|
||||
str_sql = 'SELECT host_id FROM ts_host_info WHERE (host_ip=\'{}\' and protocol={} and host_port={});' \
|
||||
.format(host_ip, protocol, host_port)
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
sql = 'SELECT `host_id` FROM `{}host_info` WHERE (`host_ip`="{}" AND `protocol`={} AND `host_port`={});'.format(db.table_prefix, host_ip, protocol, host_port)
|
||||
db_ret = db.query(sql)
|
||||
if db_ret is not None and len(db_ret) > 0:
|
||||
if not must_not_exists:
|
||||
return db_ret[0][0]
|
||||
|
@ -392,19 +389,19 @@ def add_host(args, must_not_exists=True):
|
|||
host_lock = 0
|
||||
|
||||
#
|
||||
str_sql = 'INSERT INTO ts_host_info (group_id, host_sys_type, host_ip, ' \
|
||||
'host_port, protocol, host_lock, host_desc) ' \
|
||||
'VALUES ({},{},\'{}\',' \
|
||||
'{},{},{},' \
|
||||
'\'{}\')'.format(group_id, host_sys_type, host_ip,
|
||||
host_port, protocol, host_lock, host_desc)
|
||||
sql = 'INSERT INTO `{}host_info` (group_id, host_sys_type, host_ip, ' \
|
||||
'host_port, protocol, host_lock, host_desc) ' \
|
||||
'VALUES ({},{},"{}",{},{},{},"{}")' \
|
||||
''.format(db.table_prefix,
|
||||
group_id, host_sys_type, host_ip,
|
||||
host_port, protocol, host_lock, host_desc)
|
||||
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
ret = db.query(sql)
|
||||
if not ret:
|
||||
return -101
|
||||
|
||||
str_sql = 'select last_insert_rowid()'
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
sql = 'SELECT last_insert_rowid()'
|
||||
db_ret = db.query(sql)
|
||||
if db_ret is None:
|
||||
return -102
|
||||
host_id = db_ret[0][0]
|
||||
|
@ -412,118 +409,104 @@ def add_host(args, must_not_exists=True):
|
|||
|
||||
|
||||
def lock_host(host_id, lock):
|
||||
sql_exec = get_db_con()
|
||||
#
|
||||
str_sql = 'UPDATE ts_host_info SET host_lock = {} ' \
|
||||
' WHERE host_id = {}'.format(lock, host_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
return ret
|
||||
db = get_db()
|
||||
sql = 'UPDATE `{}host_info` SET `host_lock`={} WHERE `host_id`={}'.format(db.table_prefix, int(lock), int(host_id))
|
||||
return db.exec(sql)
|
||||
|
||||
|
||||
def delete_host(host_list):
|
||||
sql_exec = get_db_con()
|
||||
#
|
||||
# TODO: 使用事务的方式防止删除操作中途失败
|
||||
db = get_db()
|
||||
for item in host_list:
|
||||
host_id = int(item)
|
||||
str_sql = 'DELETE FROM ts_host_info WHERE host_id = {} '.format(host_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
sql = 'DELETE FROM `{}host_info` WHERE `host_id`={};'.format(db.table_prefix, host_id)
|
||||
ret = db.exec(sql)
|
||||
|
||||
str_sql = 'DELETE FROM ts_auth_info WHERE host_id = {} '.format(host_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
sql = 'DELETE FROM `{}auth_info` WHERE `host_id`={};'.format(db.table_prefix, host_id)
|
||||
ret = db.exec(sql)
|
||||
|
||||
str_sql = 'DELETE FROM ts_auth WHERE host_id = {} '.format(host_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
str_sql = 'DELETE FROM `{}auth` WHERE `host_id`={};'.format(db.table_prefix, host_id)
|
||||
ret = db.exec(sql)
|
||||
return True
|
||||
|
||||
|
||||
def add_cert(cert_pub, cert_pri, cert_name):
|
||||
sql_exec = get_db_con()
|
||||
#
|
||||
str_sql = 'INSERT INTO ts_key (cert_pub, cert_pri, cert_name) VALUES (\'{}\',\'{}\',\'{}\')'.format(cert_pub, cert_pri, cert_name)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
return ret
|
||||
db = get_db()
|
||||
sql = 'INSERT INTO `{}key` (`cert_pub`, `cert_pri`, `cert_name`) VALUES ("{}","{}","{}")'.format(db.table_prefix, cert_pub, cert_pri, cert_name)
|
||||
return db.exec(sql)
|
||||
|
||||
|
||||
def delete_cert(cert_id):
|
||||
sql_exec = get_db_con()
|
||||
#
|
||||
str_sql = 'DELETE FROM ts_key WHERE cert_id = {} '.format(cert_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
return ret
|
||||
db = get_db()
|
||||
sql = 'DELETE FROM `{}key` WHERE `cert_id`={};'.format(int(cert_id))
|
||||
return db.exec(sql)
|
||||
|
||||
|
||||
def update_cert(cert_id, cert_pub, cert_pri, cert_name):
|
||||
sql_exec = get_db_con()
|
||||
#
|
||||
db = get_db()
|
||||
|
||||
if 0 == len(cert_pri):
|
||||
str_sql = 'UPDATE ts_key SET cert_pub = \'{}\', ' \
|
||||
'cert_name = \'{}\'' \
|
||||
' WHERE cert_id = {}'.format(cert_pub, cert_name, cert_id)
|
||||
sql = 'UPDATE `{}key` SET `cert_pub`="{}",`cert_name`="{}" ' \
|
||||
'WHERE `cert_id`={};'.format(db.table_prefix, cert_pub, cert_name, int(cert_id))
|
||||
else:
|
||||
str_sql = 'UPDATE ts_key SET cert_pub = \'{}\', ' \
|
||||
'cert_pri = \'{}\', cert_name = \'{}\'' \
|
||||
' WHERE cert_id = {}'.format(cert_pub, cert_pri, cert_name, cert_id)
|
||||
sql = 'UPDATE `{}key` SET `cert_pub`="{}", `cert_pri`="{}", `cert_name`="{}" ' \
|
||||
'WHERE `cert_id`={};'.format(db.table_prefix, cert_pub, cert_pri, cert_name, int(cert_id))
|
||||
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
return ret
|
||||
return db.exec(sql)
|
||||
|
||||
|
||||
def add_group(group_name):
|
||||
sql_exec = get_db_con()
|
||||
#
|
||||
str_sql = 'INSERT INTO ts_group (group_name) VALUES (\'{}\')'.format(group_name)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
return ret
|
||||
db = get_db()
|
||||
sql = 'INSERT INTO `{}group` (`group_name`) VALUES ("{}");'.format(db.table_prefix, group_name)
|
||||
return db.exec(sql)
|
||||
|
||||
|
||||
def delete_group(group_id):
|
||||
sql_exec = get_db_con()
|
||||
#
|
||||
string_sql = 'SELECT host_id FROM ts_host_info WHERE group_id = {};'.format(group_id)
|
||||
db_ret = sql_exec.ExecProcQuery(string_sql)
|
||||
db = get_db()
|
||||
sql = 'SELECT `host_id` FROM `{}host_info` WHERE `group_id`={};'.format(db.table_prefix, int(group_id))
|
||||
db_ret = db.query(sql)
|
||||
if len(db_ret) != 0:
|
||||
return -2
|
||||
|
||||
str_sql = 'DELETE FROM ts_group WHERE group_id = {} '.format(group_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
sql = 'DELETE FROM `{}group` WHERE `group_id`={};'.format(db.table_prefix, group_id)
|
||||
ret = db.exec(sql)
|
||||
if ret:
|
||||
return 0
|
||||
return -3
|
||||
|
||||
|
||||
def update_group(group_id, group_name):
|
||||
sql_exec = get_db_con()
|
||||
str_sql = 'UPDATE ts_group SET group_name = \'{}\' ' \
|
||||
'WHERE group_id = {}'.format(group_name, group_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
return ret
|
||||
db = get_db()
|
||||
sql = 'UPDATE {}group SET `group_name`="{}" ' \
|
||||
'WHERE `group_id`={};'.format(db.table_prefix, group_name, int(group_id))
|
||||
return db.exec(sql)
|
||||
|
||||
|
||||
def add_host_to_group(host_list, group_id):
|
||||
sql_exec = get_db_con()
|
||||
db = get_db()
|
||||
group_id = int(group_id)
|
||||
for item in host_list:
|
||||
host_id = item
|
||||
str_sql = 'UPDATE ts_host_info SET ' \
|
||||
'group_id = {}' \
|
||||
' WHERE host_id = {}'.format(group_id, host_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
host_id = int(item)
|
||||
sql = 'UPDATE `{}host_info` SET group_id={} ' \
|
||||
'WHERE `host_id`={};'.format(db.table_prefix, group_id, host_id)
|
||||
ret = db.exec(sql)
|
||||
return ret
|
||||
|
||||
|
||||
def get_host_auth_info(host_auth_id):
|
||||
sql_exec = get_db_con()
|
||||
db = get_db()
|
||||
|
||||
field_a = ['id', 'auth_mode', 'user_name', 'user_pswd', 'user_param', 'cert_id', 'encrypt']
|
||||
field_b = ['host_id', 'host_lock', 'host_ip', 'host_port', 'host_desc', 'group_id', 'host_sys_type', 'protocol']
|
||||
|
||||
str_sql = 'SELECT {},{} ' \
|
||||
'FROM ts_auth_info AS a ' \
|
||||
'LEFT JOIN ts_host_info AS b ON a.host_id=b.host_id ' \
|
||||
'WHERE a.id = {}'.format(
|
||||
','.join(['a.{}'.format(i) for i in field_a]),
|
||||
','.join(['b.{}'.format(i) for i in field_b]), host_auth_id)
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
sql = 'SELECT {},{} ' \
|
||||
'FROM `{}auth_info` AS a ' \
|
||||
'LEFT JOIN `{}host_info` AS b ON `a`.`host_id`=`b`.`host_id` ' \
|
||||
'WHERE `a`.`id`={};'.format(','.join(['a.{}'.format(i) for i in field_a]),
|
||||
','.join(['b.{}'.format(i) for i in field_b]),
|
||||
db.table_prefix, db.table_prefix,
|
||||
host_auth_id)
|
||||
db_ret = db.query(sql)
|
||||
|
||||
if db_ret is None or len(db_ret) != 1:
|
||||
return None
|
||||
|
@ -557,8 +540,8 @@ def get_host_auth_info(host_auth_id):
|
|||
cert_id = 0
|
||||
else:
|
||||
cert_id = int(x.a_cert_id) # int(user_auth)
|
||||
str_sql = 'SELECT cert_pri FROM ts_key WHERE cert_id = {}'.format(cert_id)
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
sql = 'SELECT `cert_pri` FROM `{}key` WHERE `cert_id`={};'.format(db.table_prefix, cert_id)
|
||||
db_ret = db.query(sql)
|
||||
if db_ret is not None and len(db_ret) == 1:
|
||||
(cert_pri,) = db_ret[0]
|
||||
h['user_auth'] = cert_pri
|
||||
|
@ -572,45 +555,45 @@ def get_host_auth_info(host_auth_id):
|
|||
return h
|
||||
|
||||
|
||||
def update_host_extend_info(host_id, args):
|
||||
sql_exec = get_db_con()
|
||||
|
||||
ip = args['ip']
|
||||
port = int(args['port'])
|
||||
user_name = args['user_name']
|
||||
user_pwd = args['user_pwd']
|
||||
cert_id = int(args['cert_id'])
|
||||
pro_type = int(args['pro_type'])
|
||||
sys_type = int(args['sys_type'])
|
||||
group_id = args['group_id']
|
||||
host_desc = args['desc']
|
||||
host_auth_mode = int(args['host_auth_mode'])
|
||||
host_encrypt = 1
|
||||
|
||||
# if len(user_pwd) == 0 and 0 == cert_id:
|
||||
# return False
|
||||
if 0 == len(user_pwd):
|
||||
str_sql = 'UPDATE ts_host_info SET host_ip = \'{}\', ' \
|
||||
'host_pro_port = {}, host_user_name = \'{}\', ' \
|
||||
'cert_id = {}, host_pro_type = {},host_sys_type={}, group_id={},host_auth_mode={},host_encrypt={}, ' \
|
||||
'host_desc=\'{}\' WHERE host_id = {}'.format(
|
||||
ip, port, user_name, cert_id, pro_type, sys_type, group_id, host_auth_mode, host_encrypt, host_desc, host_id)
|
||||
|
||||
else:
|
||||
str_sql = 'UPDATE ts_host_info SET host_ip = \'{}\', ' \
|
||||
'host_pro_port = {}, host_user_name = \'{}\', host_user_pwd = \'{}\', ' \
|
||||
'cert_id = {}, host_pro_type = {},host_sys_type={}, group_id={},host_auth_mode={},host_encrypt={}, ' \
|
||||
'host_desc=\'{}\' WHERE host_id = {}'.format(
|
||||
ip, port, user_name, user_pwd, cert_id, pro_type, sys_type, group_id, host_auth_mode, host_encrypt, host_desc, host_id)
|
||||
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
return ret
|
||||
# def update_host_extend_info(host_id, args):
|
||||
# db = get_db()
|
||||
#
|
||||
# ip = args['ip']
|
||||
# port = int(args['port'])
|
||||
# user_name = args['user_name']
|
||||
# user_pwd = args['user_pwd']
|
||||
# cert_id = int(args['cert_id'])
|
||||
# pro_type = int(args['pro_type'])
|
||||
# sys_type = int(args['sys_type'])
|
||||
# group_id = args['group_id']
|
||||
# host_desc = args['desc']
|
||||
# host_auth_mode = int(args['host_auth_mode'])
|
||||
# host_encrypt = 1
|
||||
#
|
||||
# # if len(user_pwd) == 0 and 0 == cert_id:
|
||||
# # return False
|
||||
# if 0 == len(user_pwd):
|
||||
# str_sql = 'UPDATE ts_host_info SET host_ip = \'{}\', ' \
|
||||
# 'host_pro_port = {}, host_user_name = \'{}\', ' \
|
||||
# 'cert_id = {}, host_pro_type = {},host_sys_type={}, group_id={},host_auth_mode={},host_encrypt={}, ' \
|
||||
# 'host_desc=\'{}\' WHERE host_id = {}'.format(
|
||||
# ip, port, user_name, cert_id, pro_type, sys_type, group_id, host_auth_mode, host_encrypt, host_desc, host_id)
|
||||
#
|
||||
# else:
|
||||
# str_sql = 'UPDATE ts_host_info SET host_ip = \'{}\', ' \
|
||||
# 'host_pro_port = {}, host_user_name = \'{}\', host_user_pwd = \'{}\', ' \
|
||||
# 'cert_id = {}, host_pro_type = {},host_sys_type={}, group_id={},host_auth_mode={},host_encrypt={}, ' \
|
||||
# 'host_desc=\'{}\' WHERE host_id = {}'.format(
|
||||
# ip, port, user_name, user_pwd, cert_id, pro_type, sys_type, group_id, host_auth_mode, host_encrypt, host_desc, host_id)
|
||||
#
|
||||
# ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
# return ret
|
||||
|
||||
|
||||
def get_cert_info(cert_id):
|
||||
sql_exec = get_db_con()
|
||||
str_sql = 'SELECT cert_pri FROM ts_key WHERE cert_id = {}'.format(cert_id)
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
db = get_db()
|
||||
sql = 'SELECT `cert_pri` FROM `{}key` WHERE `cert_id`={};'.format(db.table_prefix, cert_id)
|
||||
db_ret = db.query(sql)
|
||||
if db_ret is not None and len(db_ret) == 1:
|
||||
(cert_pri,) = db_ret[0]
|
||||
return cert_pri
|
||||
|
@ -619,20 +602,18 @@ def get_cert_info(cert_id):
|
|||
|
||||
|
||||
def sys_user_list(host_id, with_pwd=True, host_auth_id=0):
|
||||
sql_exec = get_db_con()
|
||||
|
||||
db = get_db()
|
||||
field_a = ['id', 'host_id', 'auth_mode', 'user_name', 'user_pswd', 'user_param', 'cert_id', 'log_time']
|
||||
if host_auth_id == 0:
|
||||
str_sql = 'SELECT {} ' \
|
||||
'FROM ts_auth_info AS a ' \
|
||||
'WHERE a.host_id = {};'.format(','.join(['a.{}'.format(i) for i in field_a]), host_id)
|
||||
sql = 'SELECT {} ' \
|
||||
'FROM `{}auth_info` AS a ' \
|
||||
'WHERE `a`.`host_id`={};'.format(','.join(['a.{}'.format(i) for i in field_a]), db.table_prefix, int(host_id))
|
||||
else:
|
||||
str_sql = 'SELECT {} ' \
|
||||
'FROM ts_auth_info AS a ' \
|
||||
'WHERE a.id = {} and a.host_id = {};'.format(','.join(['a.{}'.format(i) for i in field_a]),
|
||||
host_auth_id, host_id)
|
||||
sql = 'SELECT {} ' \
|
||||
'FROM `{}auth_info` AS a ' \
|
||||
'WHERE `a`.`id`={} and `a`.`host_id`={};'.format(','.join(['a.{}'.format(i) for i in field_a]), db.table_prefix, int(host_auth_id), int(host_id))
|
||||
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
db_ret = db.query(sql)
|
||||
|
||||
if db_ret is None:
|
||||
return None
|
||||
|
@ -673,11 +654,11 @@ def GetNowTime():
|
|||
|
||||
|
||||
def sys_user_add(args):
|
||||
host_id = args['host_id']
|
||||
auth_mode = args['auth_mode']
|
||||
host_id = int(args['host_id'])
|
||||
auth_mode = int(args['auth_mode'])
|
||||
user_name = args['user_name']
|
||||
user_pswd = args['user_pswd']
|
||||
cert_id = args['cert_id']
|
||||
cert_id = int(args['cert_id'])
|
||||
|
||||
if 'user_param' in args:
|
||||
user_param = args['user_param']
|
||||
|
@ -686,37 +667,34 @@ def sys_user_add(args):
|
|||
|
||||
encrypt = 1
|
||||
|
||||
sql_exec = get_db_con()
|
||||
db = get_db()
|
||||
|
||||
# 判断此登录账号是否已经存在,如果存在则报错
|
||||
str_sql = 'SELECT id FROM ts_auth_info WHERE (host_id={} and auth_mode={} and user_name=\'{}\');' \
|
||||
.format(host_id, auth_mode, user_name)
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
sql = 'SELECT `id` FROM `{}auth_info` WHERE (`host_id`={} AND `auth_mode`={} AND `user_name`="{}");'.format(db.table_prefix, host_id, auth_mode, user_name)
|
||||
db_ret = db.query(sql)
|
||||
if db_ret is not None and len(db_ret) > 0:
|
||||
return -100
|
||||
|
||||
log_time = GetNowTime()
|
||||
|
||||
if auth_mode == 1:
|
||||
str_sql = 'INSERT INTO ts_auth_info (host_id, auth_mode, user_name, user_pswd, user_param,' \
|
||||
'encrypt, cert_id, log_time) ' \
|
||||
'VALUES ({},{},\'{}\',\'{}\',\'{}\',{}, {},\'{}\')'.format(host_id, auth_mode, user_name, user_pswd, user_param, encrypt, 0, log_time)
|
||||
sql = 'INSERT INTO {}auth_info (host_id, auth_mode, user_name, user_pswd, user_param, encrypt, cert_id, log_time) ' \
|
||||
'VALUES ({},{},"{}","{}","{}",{}, {},"{}")' \
|
||||
''.format(db.table_prefix, host_id, auth_mode, user_name, user_pswd, user_param, encrypt, 0, log_time)
|
||||
elif auth_mode == 2:
|
||||
str_sql = 'INSERT INTO ts_auth_info (host_id, auth_mode, user_name,user_param, ' \
|
||||
'user_pswd,cert_id, encrypt, log_time) ' \
|
||||
'VALUES ({},{},\'{}\',\'{}\',\'{}\',{},{}, \'{}\')'.format(host_id, auth_mode, user_name, user_param,
|
||||
'', cert_id, encrypt, log_time)
|
||||
sql = 'INSERT INTO {}auth_info (host_id, auth_mode, user_name, user_pswd, user_param, encrypt, cert_id, log_time) ' \
|
||||
'VALUES ({},{},"{}","{}","{}",{},{},"{}")' \
|
||||
''.format(db.table_prefix, host_id, auth_mode, user_name, '', user_param, encrypt, cert_id, log_time)
|
||||
elif auth_mode == 0:
|
||||
str_sql = 'INSERT INTO ts_auth_info (host_id, auth_mode, user_name,user_param, ' \
|
||||
'user_pswd,cert_id, encrypt, log_time) ' \
|
||||
'VALUES ({},{},\'{}\',\'{}\',\'{}\',{},{}, \'{}\')'.format(host_id, auth_mode, user_name, user_param,
|
||||
'', 0, encrypt, log_time)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
sql = 'INSERT INTO {}auth_info (host_id, auth_mode, user_name, user_pswd, user_param, encrypt, cert_id, log_time) ' \
|
||||
'VALUES ({},{},"{}","{}","{}",{},{},"{}")' \
|
||||
''.format(db.table_prefix, host_id, auth_mode, user_name, '', user_param, encrypt, 0, log_time)
|
||||
ret = db.exec(sql)
|
||||
if not ret:
|
||||
return -101
|
||||
|
||||
str_sql = 'select last_insert_rowid()'
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
sql = 'SELECT last_insert_rowid()'
|
||||
db_ret = db.query(sql)
|
||||
if db_ret is None:
|
||||
return -102
|
||||
user_id = db_ret[0][0]
|
||||
|
@ -732,25 +710,22 @@ def sys_user_update(_id, kv):
|
|||
if len(_val) > 0:
|
||||
_val += ','
|
||||
|
||||
_val += '{}="{}"'.format(k, kv[k])
|
||||
_val += '`{}`="{}"'.format(k, kv[k])
|
||||
|
||||
str_sql = 'UPDATE ts_auth_info SET {} ' \
|
||||
'WHERE id={};'.format(_val, _id)
|
||||
|
||||
sql_exec = get_db_con()
|
||||
db_ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
return db_ret
|
||||
db = get_db()
|
||||
sql = 'UPDATE `{}auth_info` SET {} WHERE `id`={};'.format(db.table_prefix, _val, int(_id))
|
||||
return db.exec(sql)
|
||||
|
||||
|
||||
def sys_user_delete(_id):
|
||||
sql_exec = get_db_con()
|
||||
db = get_db()
|
||||
try:
|
||||
str_sql = 'DELETE FROM ts_auth_info WHERE id = {} '.format(_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
sql = 'DELETE FROM `{}auth_info` WHERE `id`={};'.format(db.table_prefix, int(_id))
|
||||
ret = db.exec(sql)
|
||||
|
||||
str_sql = 'DELETE FROM ts_auth WHERE host_auth_id = {} '.format(_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
except Exception as e:
|
||||
sql = 'DELETE FROM `{}auth` WHERE `host_auth_id`={};'.format(db.table_prefix, int(_id))
|
||||
ret = db.exec(sql)
|
||||
except:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
@ -762,26 +737,27 @@ def get_auth_info(auth_id):
|
|||
@param auth_id: integer
|
||||
@return:
|
||||
"""
|
||||
sql_exec = get_db_con()
|
||||
db = get_db()
|
||||
|
||||
field_a = ['auth_id', 'account_name', 'host_auth_id', 'host_id']
|
||||
field_b = ['host_sys_type', 'host_ip', 'host_port', 'protocol']
|
||||
field_c = ['user_pswd', 'cert_id', 'user_name', 'encrypt', 'auth_mode', 'user_param']
|
||||
field_d = ['account_lock']
|
||||
|
||||
str_sql = 'SELECT {},{},{},{} ' \
|
||||
'FROM ts_auth AS a ' \
|
||||
'LEFT JOIN ts_host_info AS b ON a.host_id = b.host_id ' \
|
||||
'LEFT JOIN ts_auth_info AS c ON a.host_auth_id = c.id ' \
|
||||
'LEFT JOIN ts_account AS d ON a.account_name = d.account_name ' \
|
||||
'WHERE a.auth_id={};'.format(
|
||||
','.join(['a.{}'.format(i) for i in field_a]),
|
||||
','.join(['b.{}'.format(i) for i in field_b]),
|
||||
','.join(['c.{}'.format(i) for i in field_c]),
|
||||
','.join(['d.{}'.format(i) for i in field_d]),
|
||||
auth_id)
|
||||
sql = 'SELECT {},{},{},{} ' \
|
||||
'FROM `{}auth` AS a ' \
|
||||
'LEFT JOIN `{}host_info` AS b ON `a`.`host_id`=`b`.`host_id` ' \
|
||||
'LEFT JOIN `{}auth_info` AS c ON `a`.`host_auth_id`=`c`.`id` ' \
|
||||
'LEFT JOIN `{}account` AS d ON `a`.`account_name`=`d`.`account_name` ' \
|
||||
'WHERE `a`.`auth_id`={};' \
|
||||
''.format(','.join(['a.{}'.format(i) for i in field_a]),
|
||||
','.join(['b.{}'.format(i) for i in field_b]),
|
||||
','.join(['c.{}'.format(i) for i in field_c]),
|
||||
','.join(['d.{}'.format(i) for i in field_d]),
|
||||
db.table_prefix, db.table_prefix, db.table_prefix, db.table_prefix,
|
||||
auth_id)
|
||||
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
db_ret = db.query(sql)
|
||||
|
||||
if db_ret is None or len(db_ret) != 1:
|
||||
return None
|
||||
|
@ -813,8 +789,8 @@ def get_auth_info(auth_id):
|
|||
elif db_item.c_auth_mode == 2:
|
||||
cert_id = db_item.c_cert_id
|
||||
|
||||
str_sql = 'SELECT cert_pri FROM ts_key WHERE cert_id={}'.format(cert_id)
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
sql = 'SELECT `cert_pri` FROM `{}key` WHERE `cert_id`={}'.format(int(cert_id))
|
||||
db_ret = db.query(sql)
|
||||
if db_ret is None or len(db_ret) > 1:
|
||||
return None
|
||||
ret['user_auth'] = db_ret[0][0]
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
|
||||
import datetime
|
||||
import os
|
||||
import shutil
|
||||
import struct
|
||||
|
||||
from .common import *
|
||||
from eom_app.app.configs import app_cfg
|
||||
from eom_common.eomcore.logger import log
|
||||
from .common import *
|
||||
|
||||
cfg = app_cfg()
|
||||
|
||||
|
|
|
@ -3,15 +3,15 @@
|
|||
import hashlib
|
||||
from eom_app.app.const import *
|
||||
from eom_app.app.configs import app_cfg
|
||||
from eom_app.app.db import get_db
|
||||
from eom_app.app.db import get_db, DbItem
|
||||
from eom_app.app.util import sec_generate_password, sec_verify_password
|
||||
# from eom_common.eomcore.logger import log
|
||||
|
||||
from .common import *
|
||||
|
||||
cfg = app_cfg()
|
||||
# from .common import *
|
||||
|
||||
|
||||
def verify_user(name, password):
|
||||
cfg = app_cfg()
|
||||
db = get_db()
|
||||
|
||||
sql = 'SELECT `account_id`, `account_type`, `account_name`, `account_pwd` FROM `{}account` WHERE `account_name`="{}";'.format(db.table_prefix, name)
|
||||
|
@ -63,30 +63,23 @@ def modify_pwd(old_pwd, new_pwd, user_id):
|
|||
else:
|
||||
return -3
|
||||
|
||||
# sql_exec = get_db_con()
|
||||
# new_pwd = hashlib.sha256(new_pwd.encode()).hexdigest()
|
||||
# old_pwd = hashlib.sha256(old_pwd.encode()).hexdigest()
|
||||
|
||||
# string_sql = 'SELECT account_id FROM ts_account WHERE account_pwd = \'{}\' AND account_id = {};'.format(old_pwd, int(user_id))
|
||||
# db_ret = sql_exec.ExecProcQuery(string_sql)
|
||||
# if len(db_ret) != 1:
|
||||
# return -2
|
||||
# string_sql = 'UPDATE ts_account SET account_pwd = \'{}\' WHERE account_pwd = \'{}\' AND account_id = {}'.format(new_pwd, old_pwd, int(user_id))
|
||||
#
|
||||
# ret = sql_exec.ExecProcNonQuery(string_sql)
|
||||
# if ret:
|
||||
# return 0
|
||||
# return -3
|
||||
|
||||
|
||||
def get_user_list():
|
||||
# TODO: 用户管理页面不需要列出超级管理员,但是日志查看页面需要,所以应该有参数来区分不同的请求。
|
||||
sql_exec = get_db_con()
|
||||
field_a = ['account_id', 'account_type', 'account_name', 'account_status', 'account_lock', 'account_desc']
|
||||
string_sql = 'SELECT {} FROM ts_account as a WHERE account_type<100;'.format(','.join(['a.{}'.format(i) for i in field_a]))
|
||||
# string_sql = 'SELECT {} FROM ts_account as a;'.format(','.join(['a.{}'.format(i) for i in field_a]))
|
||||
db_ret = sql_exec.ExecProcQuery(string_sql)
|
||||
def get_user_list(with_admin=False):
|
||||
db = get_db()
|
||||
ret = list()
|
||||
|
||||
field_a = ['account_id', 'account_type', 'account_name', 'account_status', 'account_lock', 'account_desc']
|
||||
|
||||
if with_admin:
|
||||
where = ''
|
||||
else:
|
||||
where = 'WHERE `a`.`account_type`<100'
|
||||
|
||||
sql = 'SELECT {} FROM `{}account` as a {} ORDER BY `account_name`;'.format(','.join(['`a`.`{}`'.format(i) for i in field_a]), db.table_prefix, where)
|
||||
db_ret = db.query(sql)
|
||||
if db_ret is None:
|
||||
return ret
|
||||
|
||||
for item in db_ret:
|
||||
x = DbItem()
|
||||
x.load(item, ['a_{}'.format(i) for i in field_a])
|
||||
|
@ -102,68 +95,51 @@ def get_user_list():
|
|||
|
||||
|
||||
def delete_user(user_id):
|
||||
sql_exec = get_db_con()
|
||||
#
|
||||
str_sql = 'DELETE FROM ts_account WHERE account_id={};'.format(user_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
return ret
|
||||
db = get_db()
|
||||
sql = 'DELETE FROM `{}account` WHERE `account_id`={};'.format(db.table_prefix, int(user_id))
|
||||
return db.exec(sql)
|
||||
|
||||
|
||||
def lock_user(user_id, lock_status):
|
||||
sql_exec = get_db_con()
|
||||
#
|
||||
str_sql = 'UPDATE ts_account SET account_lock={} ' \
|
||||
'WHERE account_id={};'.format(lock_status, user_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
return ret
|
||||
db = get_db()
|
||||
sql = 'UPDATE `{}account` SET `account_lock`={} WHERE `account_id`={};'.format(db.table_prefix, lock_status, int(user_id))
|
||||
return db.exec(sql)
|
||||
|
||||
|
||||
def reset_user(user_id):
|
||||
# sql_exec = get_db_con()
|
||||
#
|
||||
# user_pwd = hashlib.sha256("123456".encode()).hexdigest()
|
||||
# str_sql = 'UPDATE ts_account SET account_pwd = "{}" ' \
|
||||
# ' WHERE account_id = {}'.format(user_pwd, user_id)
|
||||
# ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
|
||||
db = get_db()
|
||||
_new_sec_password = sec_generate_password('123456')
|
||||
sql = 'UPDATE `{}account` SET `account_pwd`="{}" WHERE `account_id`={};'.format(db.table_prefix, _new_sec_password, int(user_id))
|
||||
ret = db.exec(sql)
|
||||
return ret
|
||||
return db.exec(sql)
|
||||
|
||||
|
||||
def modify_user(user_id, user_desc):
|
||||
sql_exec = get_db_con()
|
||||
#
|
||||
str_sql = 'UPDATE ts_account SET account_desc="{}" ' \
|
||||
'WHERE account_id={};'.format(user_desc, user_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
return ret
|
||||
db = get_db()
|
||||
sql = 'UPDATE `{}account` SET `account_desc`="{}" WHERE `account_id`={};'.format(db.table_prefix, user_desc, int(user_id))
|
||||
return db.exec(sql)
|
||||
|
||||
|
||||
def add_user(user_name, user_pwd, user_desc):
|
||||
sql_exec = get_db_con()
|
||||
#
|
||||
user_pwd = hashlib.sha256(user_pwd.encode()).hexdigest()
|
||||
string_sql = 'SELECT account_id FROM ts_account WHERE account_name = \'{}\';'.format(user_name)
|
||||
db_ret = sql_exec.ExecProcQuery(string_sql)
|
||||
if len(db_ret) != 0:
|
||||
db = get_db()
|
||||
sql = 'SELECT `account_id` FROM `{}account` WHERE `account_name`="{}";'.format(db.table_prefix, user_name)
|
||||
db_ret = db.query(sql)
|
||||
if db_ret is None or len(db_ret) != 0:
|
||||
return -2
|
||||
|
||||
str_sql = 'INSERT INTO ts_account (account_type, account_name, account_pwd, account_status,' \
|
||||
'account_lock,account_desc) VALUES (1,\'{}\',\'{}\',0,0,\'{}\')'.format(user_name, user_pwd, user_desc)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
sec_password = sec_generate_password(user_pwd)
|
||||
sql = 'INSERT INTO `{}account` (`account_type`, `account_name`, `account_pwd`, `account_status`,' \
|
||||
'`account_lock`,`account_desc`) VALUES (1,"{}","{}",0,0,"{}")'.format(db.table_prefix, user_name, sec_password, user_desc)
|
||||
ret = db.exec(sql)
|
||||
if ret:
|
||||
return 0
|
||||
return -3
|
||||
|
||||
|
||||
def alloc_host(user_name, host_list):
|
||||
sql_exec = get_db_con()
|
||||
db = get_db()
|
||||
field_a = ['host_id']
|
||||
string_sql = 'SELECT {} FROM ts_auth as a WHERE account_name=\'{}\';'.format(','.join(['a.{}'.format(i) for i in field_a]), user_name)
|
||||
db_ret = sql_exec.ExecProcQuery(string_sql)
|
||||
sql = 'SELECT {} FROM `{}auth` AS a WHERE `account_name`="{}";'.format(','.join(['a.{}'.format(i) for i in field_a]), db.table_prefix, user_name)
|
||||
db_ret = db.query(sql)
|
||||
ret = dict()
|
||||
for item in db_ret:
|
||||
x = DbItem()
|
||||
|
@ -180,8 +156,8 @@ def alloc_host(user_name, host_list):
|
|||
try:
|
||||
for item in a_list:
|
||||
host_id = int(item)
|
||||
str_sql = 'INSERT INTO ts_auth (account_name, host_id) VALUES (\'{}\', {})'.format(user_name, host_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
sql = 'INSERT INTO `{}auth` (`account_name`, `host_id`) VALUES ("{}", {});'.format(db.table_prefix, user_name, host_id)
|
||||
ret = db.exec(sql)
|
||||
if not ret:
|
||||
return False
|
||||
return True
|
||||
|
@ -190,10 +166,10 @@ def alloc_host(user_name, host_list):
|
|||
|
||||
|
||||
def alloc_host_user(user_name, host_auth_dict):
|
||||
sql_exec = get_db_con()
|
||||
db = get_db()
|
||||
field_a = ['host_id', 'host_auth_id']
|
||||
string_sql = 'SELECT {} FROM ts_auth as a WHERE account_name=\'{}\';'.format(','.join(['a.{}'.format(i) for i in field_a]), user_name)
|
||||
db_ret = sql_exec.ExecProcQuery(string_sql)
|
||||
sql = 'SELECT {} FROM `{}auth` AS a WHERE `account_name`="{}";'.format(','.join(['`a`.`{}`'.format(i) for i in field_a]), db.table_prefix, user_name)
|
||||
db_ret = db.query(sql)
|
||||
ret = dict()
|
||||
for item in db_ret:
|
||||
x = DbItem()
|
||||
|
@ -225,8 +201,8 @@ def alloc_host_user(user_name, host_auth_dict):
|
|||
for k, v in add_dict.items():
|
||||
host_auth_id = int(k)
|
||||
host_id = int(v)
|
||||
str_sql = 'INSERT INTO ts_auth (account_name, host_id, host_auth_id) VALUES (\'{}\', {}, {})'.format(user_name, host_id, host_auth_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
sql = 'INSERT INTO `{}auth` (`account_name`, `host_id`, `host_auth_id`) VALUES ("{}", {}, {});'.format(db.table_prefix, user_name, host_id, host_auth_id)
|
||||
ret = db.exec(sql)
|
||||
if not ret:
|
||||
return False
|
||||
return True
|
||||
|
@ -235,12 +211,12 @@ def alloc_host_user(user_name, host_auth_dict):
|
|||
|
||||
|
||||
def delete_host(user_name, host_list):
|
||||
db = get_db()
|
||||
try:
|
||||
sql_exec = get_db_con()
|
||||
for item in host_list:
|
||||
host_id = int(item)
|
||||
str_sql = 'DELETE FROM ts_auth WHERE account_name = \'{}\' AND host_id={}'.format(user_name, host_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
sql = 'DELETE FROM `{}auth` WHERE `account_name`="{}" AND `host_id`={};'.format(db.table_prefix, user_name, host_id)
|
||||
ret = db.exec(sql)
|
||||
if not ret:
|
||||
return False
|
||||
return True
|
||||
|
@ -249,12 +225,12 @@ def delete_host(user_name, host_list):
|
|||
|
||||
|
||||
def delete_host_user(user_name, auth_id_list):
|
||||
db = get_db()
|
||||
try:
|
||||
sql_exec = get_db_con()
|
||||
for item in auth_id_list:
|
||||
auth_id = int(item)
|
||||
str_sql = 'DELETE FROM ts_auth WHERE account_name = \'{}\' AND auth_id={}'.format(user_name, auth_id)
|
||||
ret = sql_exec.ExecProcNonQuery(str_sql)
|
||||
sql = 'DELETE FROM `{}auth` WHERE `account_name`="{}" AND `auth_id`={};'.format(db.table_prefix, user_name, auth_id)
|
||||
ret = db.exec(sql)
|
||||
if not ret:
|
||||
return False
|
||||
return True
|
||||
|
@ -262,35 +238,26 @@ def delete_host_user(user_name, auth_id_list):
|
|||
return False
|
||||
|
||||
|
||||
# def get_enc_data_helper(data):
|
||||
# try:
|
||||
# ret_code, data = get_enc_data(data)
|
||||
# except Exception as e:
|
||||
# return -100, ''
|
||||
#
|
||||
# return ret_code, data
|
||||
|
||||
|
||||
def get_log_list(filter, limit):
|
||||
sql_exec = get_db_con()
|
||||
def get_log_list(_filter, limit):
|
||||
db = get_db()
|
||||
|
||||
_where = ''
|
||||
|
||||
if len(filter) > 0:
|
||||
if len(_filter) > 0:
|
||||
_where = 'WHERE ( '
|
||||
|
||||
need_and = False
|
||||
for k in filter:
|
||||
for k in _filter:
|
||||
if k == 'account_name':
|
||||
if need_and:
|
||||
_where += ' AND'
|
||||
_where += ' a.account_name=\'{}\''.format(filter[k])
|
||||
_where += ' `a`.`account_name`="{}"'.format(_filter[k])
|
||||
need_and = True
|
||||
|
||||
if k == 'user_name':
|
||||
if need_and:
|
||||
_where += ' AND'
|
||||
_where += ' a.account_name=\'{}\''.format(filter[k])
|
||||
_where += ' `a`.`account_name`="{}"'.format(_filter[k])
|
||||
need_and = True
|
||||
|
||||
elif k == 'search':
|
||||
|
@ -301,7 +268,7 @@ def get_log_list(filter, limit):
|
|||
_where += ' AND '
|
||||
|
||||
_where += '('
|
||||
_where += 'a.host_ip LIKE "%{}%" )'.format(filter[k])
|
||||
_where += '`a`.`host_ip` LIKE "%{}%" )'.format(_filter[k])
|
||||
need_and = True
|
||||
_where += ')'
|
||||
|
||||
|
@ -309,11 +276,9 @@ def get_log_list(filter, limit):
|
|||
field_a = ['id', 'session_id', 'account_name', 'host_ip', 'host_port', 'auth_type', 'sys_type', 'user_name', 'ret_code',
|
||||
'begin_time', 'end_time', 'log_time', 'protocol']
|
||||
|
||||
str_sql = 'SELECT COUNT(*) ' \
|
||||
'FROM ts_log AS a ' \
|
||||
'{};'.format(_where)
|
||||
sql = 'SELECT COUNT(*) FROM `{}log` AS a {};'.format(db.table_prefix, _where)
|
||||
|
||||
db_ret = sql_exec.ExecProcQuery(str_sql)
|
||||
db_ret = db.query(sql)
|
||||
total_count = db_ret[0][0]
|
||||
# 修正分页数据
|
||||
_limit = ''
|
||||
|
@ -324,11 +289,10 @@ def get_log_list(filter, limit):
|
|||
|
||||
if _page_index * _per_page >= total_count:
|
||||
_page_index = int(total_count / _per_page)
|
||||
# log.d(_page_index)
|
||||
_limit = 'LIMIT {},{}'.format(_page_index * _per_page, (_page_index + 1) * _per_page)
|
||||
|
||||
string_sql = 'SELECT {} FROM ts_log as a {} ORDER BY begin_time DESC {};'.format(','.join(['a.{}'.format(i) for i in field_a]), _where, _limit)
|
||||
db_ret = sql_exec.ExecProcQuery(string_sql)
|
||||
sql = 'SELECT {} FROM `{}log` AS a {} ORDER BY `begin_time` DESC {};'.format(','.join(['a.{}'.format(i) for i in field_a]), db.table_prefix, _where, _limit)
|
||||
db_ret = db.query(sql)
|
||||
|
||||
ret = list()
|
||||
for item in db_ret:
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
import datetime
|
||||
import hashlib
|
||||
import os
|
||||
import stat
|
||||
import threading
|
||||
import time
|
||||
|
||||
|
||||
def make_dir(path):
|
||||
|
@ -128,6 +128,10 @@ def timestamp_local_to_utc(t):
|
|||
return int(datetime.datetime.utcfromtimestamp(time.mktime(time.localtime(t))).timestamp())
|
||||
|
||||
|
||||
def timestamp_utc_now():
|
||||
return int(datetime.datetime.utcnow().timestamp())
|
||||
|
||||
|
||||
def bytes_to_string(b, encode='utf8'):
|
||||
l = len(b)
|
||||
for c in range(l):
|
||||
|
@ -157,7 +161,7 @@ def md5file(file_name):
|
|||
|
||||
class UniqueId():
|
||||
def __init__(self):
|
||||
self._id = int(datetime.datetime.utcnow().timestamp())
|
||||
self._id = timestamp_utc_now()
|
||||
self._locker = threading.RLock()
|
||||
|
||||
def generate(self):
|
||||
|
|
Loading…
Reference in New Issue