配置TELEPORT服务
-- -
第一步:创建数据表
-请选择要使用的数据库类型(暂时仅支持sqlite,其它类型开发中):
-- -
diff --git a/server/www/teleport/app/eom_app/controller/auth.py b/server/www/teleport/app/eom_app/controller/auth.py index 0abc876..86e5df5 100644 --- a/server/www/teleport/app/eom_app/controller/auth.py +++ b/server/www/teleport/app/eom_app/controller/auth.py @@ -1,152 +1,152 @@ -# -*- coding: utf-8 -*- - -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 - - -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': _ref, - 'user_name': user_name - } - self.render('auth/login.mako', page_param=json.dumps(param)) - - -class VerifyUser(TPBaseJsonHandler): - def post(self): - code = self.get_session('captcha') - if code is None: - return self.write_json(-1, '验证码已失效') - - self.del_session('captcha') - - args = self.get_argument('args', None) - if args is not None: - args = json.loads(args) - captcha = args['captcha'] - username = args['username'] - userpwd = args['userpwd'] - remember = args['remember'] - else: - return self.write_json(-1, '参数错误') - - if code.lower() != captcha.lower(): - return self.write_json(-1, '验证码错误') - - try: - user_id, account_type, nickname, locked = user.verify_user(username, userpwd) - if locked == 1: - return self.write_json(-1, '账号被锁定,请联系管理员!') - if user_id == 0: - if app_cfg().app_mode == APP_MODE_MAINTENANCE: - return self.write_json(-2, '系统维护中,请稍候再试!') - else: - return self.write_json(-1, '用户名/密码错误!') - - _user = self.get_session('user') - if _user is None: - _user = dict() - _user['id'] = 0 - _user['name'] = 'guest' - _user['nick_name'] = '访客' - _user['status'] = 0 - _user['phone_num'] = '110' - _user['type'] = 0 - _user['permission'] = 0 - _user['is_login'] = False - - _user['id'] = user_id - _user['is_login'] = True - _user['name'] = username - _user['nick_name'] = nickname - _user['type'] = account_type - - if remember: - self.set_session('user', _user, 12*60*60) - else: - self.set_session('user', _user) - return self.write_json(0) - - except: - log.e('can not set session.') - return self.write_json(-1, '发生异常,无法登录!') - - -class LogoutHandler(TPBaseUserAuthHandler): - def get(self): - _user = self.get_current_user() - _user['is_login'] = False - self.set_session('user', _user) - - self.redirect('/auth/login') - - -class GetCaptchaHandler(TPBaseHandler): - def get(self): - code, img_data = gen_captcha() - self.set_session('captcha', code) - self.set_header('Content-Type', 'image/jpeg') - self.write(img_data) - - -class VerifyCaptchaHandler(TPBaseJsonHandler): - def post(self): - code = self.get_session('captcha') - if code is None: - return self.write_json(-1, '验证码已失效') - - args = self.get_argument('args', None) - if args is not None: - args = json.loads(args) - captcha = args['captcha'] - else: - return self.write_json(-1, '参数错误') - - if code.lower() != captcha.lower(): - return self.write_json(-1, '验证码错误') - - return self.write_json(0) - - -class ModifyPwd(TPBaseUserAuthJsonHandler): - def post(self): - args = self.get_argument('args', None) - if args is not None: - args = json.loads(args) - else: - return self.write_json(-11, '参数错误') - _old_pwd = args['o_pwd'] - _new_pwd = args['n_pwd'] - - if _old_pwd is None or _new_pwd is None: - return self.write_json(-12, '参数错误') - - user_info = self.get_current_user() - try: - ret = user.modify_pwd(_old_pwd, _new_pwd, user_info['id']) - if 0 == ret: - return self.write_json(0) - else: - return self.write_json(-14, '数据库操作错误,errcode:{}'.format(ret)) - except: - log.e('modify password failed.') - return self.write_json(-13, '发生异常') +# -*- coding: utf-8 -*- + +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 + + +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': _ref, + 'user_name': user_name + } + self.render('auth/login.mako', page_param=json.dumps(param)) + + +class VerifyUser(TPBaseJsonHandler): + def post(self): + code = self.get_session('captcha') + if code is None: + return self.write_json(-1, '验证码已失效') + + self.del_session('captcha') + + args = self.get_argument('args', None) + if args is not None: + args = json.loads(args) + captcha = args['captcha'] + username = args['username'] + userpwd = args['userpwd'] + remember = args['remember'] + else: + return self.write_json(-1, '参数错误') + + if code.lower() != captcha.lower(): + return self.write_json(-1, '验证码错误') + + try: + user_id, account_type, nickname, locked = user.verify_user(username, userpwd) + if locked == 1: + return self.write_json(-1, '账号被锁定,请联系管理员!') + if user_id == 0: + if app_cfg().app_mode == APP_MODE_MAINTENANCE: + return self.write_json(-2, '系统维护中,请稍候再试!') + else: + return self.write_json(-1, '用户名/密码错误!') + + _user = self.get_session('user') + if _user is None: + _user = dict() + _user['id'] = 0 + _user['name'] = 'guest' + _user['nick_name'] = '访客' + _user['status'] = 0 + _user['phone_num'] = '110' + _user['type'] = 0 + _user['permission'] = 0 + _user['is_login'] = False + + _user['id'] = user_id + _user['is_login'] = True + _user['name'] = username + _user['nick_name'] = nickname + _user['type'] = account_type + + if remember: + self.set_session('user', _user, 12*60*60) + else: + self.set_session('user', _user) + return self.write_json(0) + + except: + log.e('can not set session.') + return self.write_json(-1, '发生异常,无法登录!') + + +class LogoutHandler(TPBaseUserAuthHandler): + def get(self): + _user = self.get_current_user() + _user['is_login'] = False + self.set_session('user', _user) + + self.redirect('/auth/login') + + +class GetCaptchaHandler(TPBaseHandler): + def get(self): + code, img_data = gen_captcha() + self.set_session('captcha', code) + self.set_header('Content-Type', 'image/jpeg') + self.write(img_data) + + +class VerifyCaptchaHandler(TPBaseJsonHandler): + def post(self): + code = self.get_session('captcha') + if code is None: + return self.write_json(-1, '验证码已失效') + + args = self.get_argument('args', None) + if args is not None: + args = json.loads(args) + captcha = args['captcha'] + else: + return self.write_json(-1, '参数错误') + + if code.lower() != captcha.lower(): + return self.write_json(-1, '验证码错误') + + return self.write_json(0) + + +class ModifyPwd(TPBaseUserAuthJsonHandler): + def post(self): + args = self.get_argument('args', None) + if args is not None: + args = json.loads(args) + else: + return self.write_json(-1, '参数错误') + _old_pwd = args['o_pwd'] + _new_pwd = args['n_pwd'] + + if _old_pwd is None or _new_pwd is None: + return self.write_json(-2, '参数错误') + + user_info = self.get_current_user() + try: + ret = user.modify_pwd(_old_pwd, _new_pwd, user_info['id']) + if 0 == ret: + return self.write_json(0) + else: + return self.write_json(ret) + except: + log.e('modify password failed.') + return self.write_json(-4, '发生异常') diff --git a/server/www/teleport/app/eom_app/controller/host.py b/server/www/teleport/app/eom_app/controller/host.py index e567f48..681713a 100644 --- a/server/www/teleport/app/eom_app/controller/host.py +++ b/server/www/teleport/app/eom_app/controller/host.py @@ -307,7 +307,10 @@ class AddHost(TPBaseUserAuthJsonHandler): if ret > 0: return self.write_json(0) else: - return self.write_json(-2, '数据库操作失败,errcode:{}'.format(ret)) + if ret == -100: + return self.write_json(-100, '') + else: + return self.write_json(-2, '数据库操作失败,errcode:{}'.format(ret)) except: log.e('add host failed.\n') return self.write_json(-3, '发生异常') @@ -484,10 +487,13 @@ class DeleteCert(TPBaseUserAuthJsonHandler): if ret: return self.write_json(0) else: - return self.write_json(-2, '数据库操作失败,errcode:{}'.format(ret)) + if ret == -2: + return self.write_json(-2, '') + else: + return self.write_json(-3, '数据库操作失败,errcode:{}'.format(ret)) except: log.e('add cert failed.\n') - return self.write_json(-3, '发生异常') + return self.write_json(-4, '发生异常') class UpdateCert(TPBaseUserAuthJsonHandler): @@ -521,7 +527,6 @@ class UpdateCert(TPBaseUserAuthJsonHandler): return self.write_json(0) else: return self.write_json(-4, '数据库操作失败,errcode:{}'.format(ret)) - return except: log.e('update cert failed.\n') return self.write_json(-5, '发生异常') @@ -582,10 +587,13 @@ class DeleteGroup(TPBaseUserAuthJsonHandler): if ret == 0: return self.write_json(0) else: - return self.write_json(-2, '数据库操作失败,errcode:{}'.format(ret)) + if ret == -2: + return self.write_json(-2, '') + else: + return self.write_json(-3, '数据库操作失败,errcode:{}'.format(ret)) except: log.e('delete group failed.\n') - return self.write_json(-3, '发生异常') + return self.write_json(-4, '发生异常') class AddHostToGroup(TPBaseUserAuthJsonHandler): diff --git a/server/www/teleport/app/eom_app/controller/user.py b/server/www/teleport/app/eom_app/controller/user.py index c5ed5d9..a50bec1 100644 --- a/server/www/teleport/app/eom_app/controller/user.py +++ b/server/www/teleport/app/eom_app/controller/user.py @@ -96,7 +96,7 @@ class AddUser(TPBaseUserAuthJsonHandler): if 0 == ret: return self.write_json(0) else: - return self.write_json(-2, 'database op failed. errcode={}'.format(ret)) + return self.write_json(ret, 'database op failed. errcode={}'.format(ret)) except: log.e('add user failed.\n') return self.write_json(-3, 'got exception.') diff --git a/server/www/teleport/app/eom_app/module/user.py b/server/www/teleport/app/eom_app/module/user.py index 7508e85..337a23f 100644 --- a/server/www/teleport/app/eom_app/module/user.py +++ b/server/www/teleport/app/eom_app/module/user.py @@ -1,328 +1,328 @@ -# -*- coding: utf-8 -*- - -import hashlib - -from eom_app.app.configs import app_cfg -from eom_app.app.const import * -from eom_app.app.db import get_db, DbItem -from eom_app.app.util import sec_generate_password, sec_verify_password - - -def verify_user(name, password): - cfg = app_cfg() - db = get_db() - - sql = 'SELECT `account_id`, `account_type`, `account_name`, `account_pwd`, `account_lock` FROM `{}account` WHERE `account_name`="{}";'.format(db.table_prefix, name) - db_ret = db.query(sql) - if db_ret is None: - # 特别地,如果无法取得数据库连接,有可能是新安装的系统,尚未建立数据库,此时应该处于维护模式 - # 因此可以特别地处理用户验证:用户名admin,密码admin可以登录为管理员 - if cfg.app_mode == APP_MODE_MAINTENANCE: - if name == 'admin' and password == 'admin': - return 1, 100, 'admin', 0 - return 0, 0, '', 0 - - if len(db_ret) != 1: - return 0, 0, '', 0 - - user_id = db_ret[0][0] - account_type = db_ret[0][1] - name = db_ret[0][2] - locked = db_ret[0][4] - if locked == 1: - return 0, 0, '', locked - - if not sec_verify_password(password, db_ret[0][3]): - # 按新方法验证密码失败,可能是旧版本的密码散列格式,再尝试一下 - if db_ret[0][3] != hashlib.sha256(password.encode()).hexdigest(): - return 0, 0, '', locked - else: - # 发现此用户的密码散列格式还是旧的,更新成新的吧! - _new_sec_password = sec_generate_password(password) - sql = 'UPDATE `{}account` SET `account_pwd`="{}" WHERE `account_id`={}'.format(db.table_prefix, _new_sec_password, int(user_id)) - db.exec(sql) - - return user_id, account_type, name, locked - - -def modify_pwd(old_pwd, new_pwd, user_id): - db = get_db() - sql = 'SELECT `account_pwd` FROM `{}account` WHERE `account_id`={};'.format(db.table_prefix, int(user_id)) - db_ret = db.query(sql) - if db_ret is None or len(db_ret) != 1: - return -2 - - if not sec_verify_password(old_pwd, db_ret[0][0]): - # 按新方法验证密码失败,可能是旧版本的密码散列格式,再尝试一下 - if db_ret[0][0] != hashlib.sha256(old_pwd.encode()).hexdigest(): - return -2 - - _new_sec_password = sec_generate_password(new_pwd) - sql = 'UPDATE `{}account` SET `account_pwd`="{}" WHERE `account_id`={}'.format(db.table_prefix, _new_sec_password, int(user_id)) - db_ret = db.exec(sql) - if db_ret: - return 0 - else: - return -3 - - -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]) - h = dict() - h['user_id'] = x.a_account_id - h['user_type'] = x.a_account_type - h['user_name'] = x.a_account_name - h['user_status'] = x.a_account_status - h['user_lock'] = x.a_account_lock - h['user_desc'] = x.a_account_desc - ret.append(h) - return ret - - -def delete_user(user_id): - 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): - 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): - 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)) - return db.exec(sql) - - -def modify_user(user_id, user_desc): - 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): - 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 - - 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): - db = get_db() - field_a = ['host_id'] - 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() - x.load(item, ['a_{}'.format(i) for i in field_a]) - host_id = int(x.a_host_id) - ret[host_id] = host_id - - a_list = list() - for item in host_list: - if item in ret: - pass - else: - a_list.append(item) - try: - for item in a_list: - host_id = int(item) - 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 - except: - return False - - -def alloc_host_user(user_name, host_auth_dict): - db = get_db() - field_a = ['host_id', 'host_auth_id'] - 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() - x.load(item, ['a_{}'.format(i) for i in field_a]) - host_id = int(x.a_host_id) - host_auth_id = int(x.a_host_auth_id) - if host_id not in ret: - ret[host_id] = dict() - - temp = ret[host_id] - temp[host_auth_id] = host_id - ret[host_id] = temp - - add_dict = dict() - for k, v in host_auth_dict.items(): - host_id = int(k) - auth_id_list = v - for item in auth_id_list: - host_auth_id = int(item) - if host_id not in ret: - add_dict[host_auth_id] = host_id - continue - temp = ret[host_id] - if host_auth_id not in temp: - add_dict[host_auth_id] = host_id - continue - - try: - for k, v in add_dict.items(): - host_auth_id = int(k) - host_id = int(v) - 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 - except: - return False - - -def delete_host(user_name, host_list): - db = get_db() - try: - for item in host_list: - host_id = int(item) - 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 - except: - return False - - -def delete_host_user(user_name, auth_id_list): - db = get_db() - try: - for item in auth_id_list: - auth_id = int(item) - 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 - except: - return False - - -def get_log_list(_filter, limit): - db = get_db() - - _where = '' - - if len(_filter) > 0: - _where = 'WHERE ( ' - - need_and = False - for k in _filter: - if k == 'account_name': - if need_and: - _where += ' AND' - _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]) - need_and = True - - elif k == 'search': - # 查找,限于主机ID和IP地址,前者是数字,只能精确查找,后者可以模糊匹配 - # 因此,先判断搜索项能否转换为数字。 - - if need_and: - _where += ' AND ' - - _where += '(' - _where += '`a`.`host_ip` LIKE "%{}%" )'.format(_filter[k]) - need_and = True - _where += ')' - - # http://www.jb51.net/article/46015.htm - 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'] - - sql = 'SELECT COUNT(*) FROM `{}log` AS a {};'.format(db.table_prefix, _where) - - db_ret = db.query(sql) - total_count = db_ret[0][0] - # 修正分页数据 - _limit = '' - if len(limit) > 0: - _page_index = limit['page_index'] - _per_page = limit['per_page'] - _limit = 'LIMIT {},{}'.format(_page_index * _per_page, (_page_index + 1) * _per_page) - - if _page_index * _per_page >= total_count: - _page_index = int(total_count / _per_page) - _limit = 'LIMIT {},{}'.format(_page_index * _per_page, (_page_index + 1) * _per_page) - - 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: - x = DbItem() - x.load(item, ['a_{}'.format(i) for i in field_a]) - h = dict() - h['id'] = x.a_id - h['session_id'] = x.a_session_id - h['account_name'] = x.a_account_name - h['host_ip'] = x.a_host_ip - h['host_port'] = x.a_host_port - h['auth_type'] = x.a_auth_type - h['sys_type'] = x.a_sys_type - h['user_name'] = x.a_user_name - h['ret_code'] = x.a_ret_code - cost_time = (x.a_end_time - x.a_begin_time) - if cost_time < 0: - cost_time = 0 - h['cost_time'] = cost_time - h['begin_time'] = x.a_begin_time - if x.a_protocol is not None: - h['protocol'] = x.a_protocol - else: - if x.a_sys_type == 1: - h['protocol'] = 1 - else: - h['protocol'] = 2 - - ret.append(h) - - return total_count, ret +# -*- coding: utf-8 -*- + +import hashlib + +from eom_app.app.configs import app_cfg +from eom_app.app.const import * +from eom_app.app.db import get_db, DbItem +from eom_app.app.util import sec_generate_password, sec_verify_password + + +def verify_user(name, password): + cfg = app_cfg() + db = get_db() + + sql = 'SELECT `account_id`, `account_type`, `account_name`, `account_pwd`, `account_lock` FROM `{}account` WHERE `account_name`="{}";'.format(db.table_prefix, name) + db_ret = db.query(sql) + if db_ret is None: + # 特别地,如果无法取得数据库连接,有可能是新安装的系统,尚未建立数据库,此时应该处于维护模式 + # 因此可以特别地处理用户验证:用户名admin,密码admin可以登录为管理员 + if cfg.app_mode == APP_MODE_MAINTENANCE: + if name == 'admin' and password == 'admin': + return 1, 100, 'admin', 0 + return 0, 0, '', 0 + + if len(db_ret) != 1: + return 0, 0, '', 0 + + user_id = db_ret[0][0] + account_type = db_ret[0][1] + name = db_ret[0][2] + locked = db_ret[0][4] + if locked == 1: + return 0, 0, '', locked + + if not sec_verify_password(password, db_ret[0][3]): + # 按新方法验证密码失败,可能是旧版本的密码散列格式,再尝试一下 + if db_ret[0][3] != hashlib.sha256(password.encode()).hexdigest(): + return 0, 0, '', locked + else: + # 发现此用户的密码散列格式还是旧的,更新成新的吧! + _new_sec_password = sec_generate_password(password) + sql = 'UPDATE `{}account` SET `account_pwd`="{}" WHERE `account_id`={}'.format(db.table_prefix, _new_sec_password, int(user_id)) + db.exec(sql) + + return user_id, account_type, name, locked + + +def modify_pwd(old_pwd, new_pwd, user_id): + db = get_db() + sql = 'SELECT `account_pwd` FROM `{}account` WHERE `account_id`={};'.format(db.table_prefix, int(user_id)) + db_ret = db.query(sql) + if db_ret is None or len(db_ret) != 1: + return -100 + + if not sec_verify_password(old_pwd, db_ret[0][0]): + # 按新方法验证密码失败,可能是旧版本的密码散列格式,再尝试一下 + if db_ret[0][0] != hashlib.sha256(old_pwd.encode()).hexdigest(): + return -101 + + _new_sec_password = sec_generate_password(new_pwd) + sql = 'UPDATE `{}account` SET `account_pwd`="{}" WHERE `account_id`={}'.format(db.table_prefix, _new_sec_password, int(user_id)) + db_ret = db.exec(sql) + if db_ret: + return 0 + else: + return -102 + + +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]) + h = dict() + h['user_id'] = x.a_account_id + h['user_type'] = x.a_account_type + h['user_name'] = x.a_account_name + h['user_status'] = x.a_account_status + h['user_lock'] = x.a_account_lock + h['user_desc'] = x.a_account_desc + ret.append(h) + return ret + + +def delete_user(user_id): + 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): + 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): + 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)) + return db.exec(sql) + + +def modify_user(user_id, user_desc): + 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): + 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 -100 + + 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 -101 + + +def alloc_host(user_name, host_list): + db = get_db() + field_a = ['host_id'] + 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() + x.load(item, ['a_{}'.format(i) for i in field_a]) + host_id = int(x.a_host_id) + ret[host_id] = host_id + + a_list = list() + for item in host_list: + if item in ret: + pass + else: + a_list.append(item) + try: + for item in a_list: + host_id = int(item) + 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 + except: + return False + + +def alloc_host_user(user_name, host_auth_dict): + db = get_db() + field_a = ['host_id', 'host_auth_id'] + 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() + x.load(item, ['a_{}'.format(i) for i in field_a]) + host_id = int(x.a_host_id) + host_auth_id = int(x.a_host_auth_id) + if host_id not in ret: + ret[host_id] = dict() + + temp = ret[host_id] + temp[host_auth_id] = host_id + ret[host_id] = temp + + add_dict = dict() + for k, v in host_auth_dict.items(): + host_id = int(k) + auth_id_list = v + for item in auth_id_list: + host_auth_id = int(item) + if host_id not in ret: + add_dict[host_auth_id] = host_id + continue + temp = ret[host_id] + if host_auth_id not in temp: + add_dict[host_auth_id] = host_id + continue + + try: + for k, v in add_dict.items(): + host_auth_id = int(k) + host_id = int(v) + 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 + except: + return False + + +def delete_host(user_name, host_list): + db = get_db() + try: + for item in host_list: + host_id = int(item) + 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 + except: + return False + + +def delete_host_user(user_name, auth_id_list): + db = get_db() + try: + for item in auth_id_list: + auth_id = int(item) + 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 + except: + return False + + +def get_log_list(_filter, limit): + db = get_db() + + _where = '' + + if len(_filter) > 0: + _where = 'WHERE ( ' + + need_and = False + for k in _filter: + if k == 'account_name': + if need_and: + _where += ' AND' + _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]) + need_and = True + + elif k == 'search': + # 查找,限于主机ID和IP地址,前者是数字,只能精确查找,后者可以模糊匹配 + # 因此,先判断搜索项能否转换为数字。 + + if need_and: + _where += ' AND ' + + _where += '(' + _where += '`a`.`host_ip` LIKE "%{}%" )'.format(_filter[k]) + need_and = True + _where += ')' + + # http://www.jb51.net/article/46015.htm + 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'] + + sql = 'SELECT COUNT(*) FROM `{}log` AS a {};'.format(db.table_prefix, _where) + + db_ret = db.query(sql) + total_count = db_ret[0][0] + # 修正分页数据 + _limit = '' + if len(limit) > 0: + _page_index = limit['page_index'] + _per_page = limit['per_page'] + _limit = 'LIMIT {},{}'.format(_page_index * _per_page, (_page_index + 1) * _per_page) + + if _page_index * _per_page >= total_count: + _page_index = int(total_count / _per_page) + _limit = 'LIMIT {},{}'.format(_page_index * _per_page, (_page_index + 1) * _per_page) + + 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: + x = DbItem() + x.load(item, ['a_{}'.format(i) for i in field_a]) + h = dict() + h['id'] = x.a_id + h['session_id'] = x.a_session_id + h['account_name'] = x.a_account_name + h['host_ip'] = x.a_host_ip + h['host_port'] = x.a_host_port + h['auth_type'] = x.a_auth_type + h['sys_type'] = x.a_sys_type + h['user_name'] = x.a_user_name + h['ret_code'] = x.a_ret_code + cost_time = (x.a_end_time - x.a_begin_time) + if cost_time < 0: + cost_time = 0 + h['cost_time'] = cost_time + h['begin_time'] = x.a_begin_time + if x.a_protocol is not None: + h['protocol'] = x.a_protocol + else: + if x.a_sys_type == 1: + h['protocol'] = 1 + else: + h['protocol'] = 2 + + ret.append(h) + + return total_count, ret diff --git a/server/www/teleport/static/js/ui/admin_host.js b/server/www/teleport/static/js/ui/admin_host.js index dc89ad2..57a8386 100644 --- a/server/www/teleport/static/js/ui/admin_host.js +++ b/server/www/teleport/static/js/ui/admin_host.js @@ -209,8 +209,12 @@ ywl.on_init = function (cb_stack, cb_args) { var _fn_sure = function (cb_stack, cb_args) { ywl.ajax_post_json('/host/delete-host', {host_list: host_list}, function (ret) { - g_host_table.reload(); - ywl.notify_success('删除主机操作成功!'); + if (ret.code === TPE_OK) { + g_host_table.reload(); + ywl.notify_success('删除主机操作成功!'); + } else { + ywl.notify_error('删除主机操作失败:' + ret.message); + } }, function () { ywl.notify_error('网络故障,删除主机操作失败!'); @@ -344,12 +348,16 @@ ywl.on_host_table_created = function (tbl) { var _fn_sure = function (cb_stack, cb_args) { ywl.ajax_post_json('/host/lock-host', {host_id: host_id, lock: host_lock}, function (ret) { - var update_args = {host_lock: host_lock}; - tbl.update_row(row_id, update_args); - ywl.notify_success('操作成功'); + if (ret.code === TPE_OK) { + var update_args = {host_lock: host_lock}; + tbl.update_row(row_id, update_args); + ywl.notify_success('锁定主机操作成功!'); + } else { + ywl.notify_error('锁定主机操作失败:' + ret.message); + } }, function () { - ywl.notify_error('操作失败'); + ywl.notify_error('网络故障,锁定主机操作失败!'); } ); }; @@ -370,11 +378,15 @@ ywl.on_host_table_created = function (tbl) { host_list.push(host_id); ywl.ajax_post_json('/host/delete-host', {host_list: host_list}, function (ret) { - tbl.remove_row(row_id); - ywl.notify_success('操作成功'); + if (ret.code === TPE_OK) { + tbl.remove_row(row_id); + ywl.notify_success('删除主机操作成功!'); + } else { + ywl.notify_error('删除主机操作失败:' + ret.message); + } }, function () { - ywl.notify_error('操作失败'); + ywl.notify_error('网络故障,删除主机操作失败!'); } ); }; @@ -802,22 +814,26 @@ ywl.create_host_edit_dlg = function (tbl) { }; ywl.ajax_post_json('/host/update', {host_id: host_id, kv: args}, function (ret) { - var update_args = { - host_ip: dlg_edit_host.ip, - group_name: dlg_edit_host.group_name, - group_id: dlg_edit_host.group_id, - host_desc: dlg_edit_host.host_desc, - host_sys_type: dlg_edit_host.sys_type, - protocol: protocol, - host_port: host_port - }; + if (ret.code === TPE_OK) { + var update_args = { + host_ip: dlg_edit_host.ip, + group_name: dlg_edit_host.group_name, + group_id: dlg_edit_host.group_id, + host_desc: dlg_edit_host.host_desc, + host_sys_type: dlg_edit_host.sys_type, + protocol: protocol, + host_port: host_port + }; - dlg_edit_host.tbl.update_row(dlg_edit_host.row_id, update_args); - ywl.notify_success('主机 ' + dlg_edit_host.ip + ' 的认证信息已保存!'); - dlg_edit_host.hide(); + dlg_edit_host.tbl.update_row(dlg_edit_host.row_id, update_args); + ywl.notify_success('主机 ' + dlg_edit_host.ip + ' 信息已保存!'); + dlg_edit_host.hide(); + } else { + ywl.notify_error('主机 ' + self.host_ip + ' 信息更新失败:' + ret.message); + } }, function () { - ywl.notify_error('主机 ' + self.host_ip + ' 更新失败!', ''); + ywl.notify_error('网络故障,主机 ' + self.host_ip + ' 信息更新失败!'); } ); }; @@ -837,22 +853,22 @@ ywl.create_host_edit_dlg = function (tbl) { ywl.ajax_post_json('/host/add-host', args, function (ret) { - if (ret.code === 0) { + if (ret.code === TPE_OK) { dlg_edit_host.tbl.reload(); ywl.notify_success('主机 ' + dlg_edit_host.ip + ' 信息已添加!'); dlg_edit_host.hide(); } else { if (ret.code === -100) { - ywl.notify_error('主机 ' + dlg_edit_host.ip + ' 已经添加,请不要重复添加主机!', ''); + ywl.notify_error('主机 ' + dlg_edit_host.ip + ' 已存在,请不要重复添加主机!'); } else { - ywl.notify_error('主机 ' + dlg_edit_host.ip + ' 信息保存失败!' + ret.code, ''); + ywl.notify_error('主机 ' + dlg_edit_host.ip + ' 信息保存失败!' + ret.message); } } }, function () { - ywl.notify_error('主机 ' + dlg_edit_host.ip + ' 信息保存失败!', ''); + ywl.notify_error('网络故障,主机 ' + dlg_edit_host.ip + ' 信息保存失败!', ''); } ); }; @@ -895,9 +911,15 @@ ywl.create_host_user_edit_dlg = function (tbl) { html.push(''); return html.join(''); }; + dlg_user_edit_host.sync_user_info = function (host_id) { ywl.ajax_post_json('/host/sys-user/list', {host_id: host_id}, function (ret) { + if (ret.code !== TPE_OK) { + ywl.notify_error('获取主机用户列表失败:' + ret.message); + return; + } + var data = ret.data; dlg_user_edit_host.auth_list = data; @@ -951,18 +973,15 @@ ywl.create_host_user_edit_dlg = function (tbl) { var host_auth_id = parseInt($(this).attr("auth-id")); ywl.ajax_post_json('/host/sys-user/delete', {host_auth_id: host_auth_id}, function (ret) { - //console.log("ret,", ret); if (ret.code === TPE_OK) { - ywl.notify_success('系统用户删除成功'); - // var host_id = parseInt(dlg_user_edit_host.host_id); + ywl.notify_success('系统用户删除成功!'); g_dlg_edit_host_user.sync_user_info(host_id); - } else { - ywl.notify_error('系统用户删除失败!' + ret.code); + ywl.notify_error('系统用户删除失败:' + ret.message); } }, function () { - ywl.notify_error('系统用户删除失败!'); + ywl.notify_error('网络故障:系统用户删除失败!'); } ); }); @@ -1349,21 +1368,19 @@ ywl.create_sys_user = function (tbl) { cert_id: cert_id, user_param: dlg_sys_user.user_param }; - //console.log("args:", args); + ywl.ajax_post_json('/host/sys-user/update', {host_auth_id: host_auth_id, kv: args}, function (ret) { - //console.log("ret,", ret); - if (ret.code === 0) { - ywl.notify_success('系统用户信息更新成功'); + if (ret.code === TPE_OK) { + ywl.notify_success('系统用户信息更新成功!'); g_dlg_edit_host_user.sync_user_info(host_id); dlg_sys_user.hide(); - } else { - ywl.notify_error('系统用户信息更新失败' + ret.code); + ywl.notify_error('系统用户信息更新失败:' + ret.message); } }, function () { - ywl.notify_error('系统用户信息更新失败'); + ywl.notify_error('网络故障,系统用户信息更新失败!'); } ); }; @@ -1394,15 +1411,15 @@ ywl.create_sys_user = function (tbl) { ywl.ajax_post_json('/host/sys-user/add', args, function (ret) { if (ret.code === TPE_OK) { - ywl.notify_success('系统用户添加成功'); + ywl.notify_success('系统用户添加成功!'); g_dlg_edit_host_user.sync_user_info(host_id); dlg_sys_user.hide(); } else { - ywl.notify_error('系统用户添加失败' + ret.code); + ywl.notify_error('系统用户添加失败:' + ret.message); } }, function () { - ywl.notify_error('系统用户信息更新失败'); + ywl.notify_error('网络故障,系统用户信息更新失败!'); } ); }; @@ -1461,17 +1478,21 @@ ywl.create_batch_join_group_dlg = function (tbl) { ywl.ajax_post_json('/host/add-host-to-group', {host_list: data_list, group_id: group_id}, function (ret) { - var update_args = {group_name: group_name}; - for (var i = 0; i < batch_join_dlg.host_list.length; i++) { - var row_id = batch_join_dlg.host_list[i].row_id; - batch_join_dlg.tbl.update_row(row_id, update_args); - } + if (ret.code === TPE_OK) { + var update_args = {group_name: group_name}; + for (var i = 0; i < batch_join_dlg.host_list.length; i++) { + var row_id = batch_join_dlg.host_list[i].row_id; + batch_join_dlg.tbl.update_row(row_id, update_args); + } - ywl.notify_success("成功设定分组信息!"); - batch_join_dlg.hide(); + ywl.notify_success("成功设定主机分组信息!"); + batch_join_dlg.hide(); + } else { + ywl.notify_error("设定主机分组信息失败:" + ret.message); + } }, function () { - ywl.notify_error("设定分组信息失败!"); + ywl.notify_error("网络故障,设定主机分组信息失败!"); } ); }; diff --git a/server/www/teleport/static/js/ui/auth.js b/server/www/teleport/static/js/ui/auth.js index c75601a..f66c445 100644 --- a/server/www/teleport/static/js/ui/auth.js +++ b/server/www/teleport/static/js/ui/auth.js @@ -1,745 +1,750 @@ -/*! ywl v1.0.1, (c)2015 eomsoft.net */ -"use strict"; -var OS_TYPE_WINDOWS = 1; -var OS_TYPE_LINUX = 2; -var PROTOCOL_TYPE_RDP = 1; -var PROTOCOL_TYPE_SSH = 2; -var PROTOCOL_TYPE_TELNET = 3; -var AUTH_TYPE_PASSWORD = 1; -var AUTH_TYPE_SSHKEY = 2; -var AUTH_NONE = 0; -var g_cert_list = {}; -var g_group_list = {}; -var g_user_host_list = null; - -ywl.on_init = function (cb_stack, cb_args) { - ywl.create_host_table(cb_stack, cb_args); - ywl.create_user_host_table(cb_stack, cb_args); - cb_stack.exec(); -}; - - -ywl.create_host_table = function (cb_stack, cb_args) { - var dom_id = '#ywl_host_list'; - - //=================================== - // 创建页面控件对象 - //=================================== - // 表格数据 - var host_table_options = { - selector: dom_id + " [ywl-table='host-list']", - data_source: { - type: 'ajax-post', - url: '/host/list' - }, - //render: ywl.create_table_render(ywl.on_host_table_render_created),//ywl_TableRender.create(); - column_default: {sort: false, header_align: 'center', cell_align: 'center'}, - columns: [ - {title: '', key: 'select_all', sort: false, width: 24, render: 'make_check_box', fields: {id: 'host_id'}}, - {title: "远程协议", key: "auth_list", width: 280, header_align: 'left', cell_align: 'left', sort: false, render: 'auth_list', fields: {id: 'host_id', protocol: 'protocol', auth_list: 'auth_list'}}, - {title: "主机", key: "host_id", width: 240, sort: true, render: 'host_id', fields: {id: 'host_ip', host_desc: 'host_desc', host_port: 'host_port'}}, - {title: "分组", key: "group_name", width: 240}, - {title: "系统", key: "host_sys_type", width: 64, render: 'sys_type', fields: {sys_type: 'host_sys_type'}}, - {title: "状态", key: "host_lock", render: 'host_lock', fields: {host_lock: 'host_lock'}}, - ], - paging: {selector: dom_id + " [ywl-paging='host-list']", per_page: paging_normal}, - - // 可用的属性设置 - //have_header: true or false - - // 可用的回调函数 - on_created: ywl.on_host_table_created, - on_header_created: ywl.on_host_table_header_created - - // 可重载的函数(在on_created回调函数中重载) - // on_render_created - // on_header_created - // on_paging_created - // on_data_loaded - // on_row_rendered - // on_table_rendered - // on_cell_created - // on_begin_load - // on_after_load - - // 可用的函数 - // load_data - // cancel_load - // set_data - // add_row - // remove_row - // get_row - // update_row - // clear - // reset_filter - }; - - var host_table = ywl.create_table(host_table_options); - - // 主机分组过滤器 - g_cert_list = ywl.page_options.cert_list; - g_group_list = ywl.page_options.group_list; - ywl.create_table_filter_host_group(host_table, dom_id + " [ywl-filter='host-group']", g_group_list); - - ywl.create_table_filter_system_type(host_table, dom_id + " [ywl-filter='system-type']"); - // 搜索框 - ywl.create_table_filter_search_box(host_table, dom_id + " [ywl-filter='search']"); - - //====================================================== - // 事件绑定 - //====================================================== - $("#btn-add-host").click(function () { -// var _data_list = []; - var host_list = []; - var _host_id_objs = $(host_table.selector + " tbody tr td [data-check-box]"); - $.each(_host_id_objs, function (i, _host_id_objs) { - if ($(_host_id_objs).is(':checked')) { - var _row_data = host_table.get_row(_host_id_objs); - var data = {host_id: _row_data.host_id, row_id: _row_data.ywl_row_id}; - host_list.push(data) - } - }); - console.log('host_list', host_list); - var user_dict = {}; - var _user_objs = $(host_table.selector + " tbody tr td [user-check-box]"); - $.each(_user_objs, function (i, _user_objs) { - if ($(_user_objs).is(':checked')) { - var _row_data = host_table.get_row(_user_objs); - var host_auth_id = parseInt($(_user_objs).attr('user-check-box')); -// var data = {host_id: _row_data.host_id, row_id: _row_data.ywl_row_id}; - - if (typeof(user_dict[_row_data.ywl_row_id]) == "undefined") { - user_dict[_row_data.ywl_row_id] = []; - } - user_dict[_row_data.ywl_row_id].push(host_auth_id); - } - }); - console.log('user_dict', user_dict); - var length = 0; - var args = {}; - - for (var i = 0; i < host_list.length; i++) { - var host_id = host_list[i].host_id; - var row_id = host_list[i].row_id; - var host_auth_list = user_dict[row_id]; - if (typeof(host_auth_list) == "undefined") { - continue; - } - args[host_id] = host_auth_list; - length += 1; - } - console.log('args', args); - if (length === 0) { - ywl.notify_error('请先选择要批量授权的主机!'); - return; - } - - ywl.ajax_post_json('/user/alloc-host-user', {host_list: args, user_name: ywl.page_options.user_name}, - function (ret) { - g_user_host_list.reload(); - ywl.notify_success("主机授权操作成功!"); - }, - function () { - ywl.notify_error("主机授权操作时发生错误!"); - } - ); - }); - - // 将刷新按钮点击事件绑定到表格的重新加载函数上,这样,点击刷新就导致表格数据重新加载。 - $(dom_id + " [ywl-filter='reload']").click(host_table.reload); - - cb_stack - .add(host_table.load_data) - .add(host_table.init) - .exec(); -}; - -ywl.create_user_host_table = function (cb_stack, cb_args) { - var dom_id = '#ywl_user_host_list'; - //=================================== - // 创建页面控件对象 - //=================================== - // 表格数据 - var host_table_options = { - selector: dom_id + " [ywl-table='host-list']", - data_source: { - type: 'ajax-post', - url: '/user/host-list' - }, - //render: ywl.create_table_render(ywl.on_host_table_render_created),//ywl_TableRender.create(); - column_default: {sort: false, header_align: 'center', cell_align: 'center'}, - columns: [ - {title: '', key: 'select_all', sort: false, width: 24, render: 'make_check_box', fields: {id: 'host_id'}}, - {title: "远程协议", key: "auth_list", header_align: 'left', cell_align: 'left', render: 'auth_list', fields: {id: 'host_id', protocol: 'protocol', auth_list: 'auth_list'}}, - {title: "主机", key: "host_id", width: 240, sort: true, render: 'host_id', fields: {id: 'host_ip', host_desc: 'host_desc', host_port: 'host_port'}}, - {title: "分组", key: "group_name", width: 240}, - {title: "系统", key: "host_sys_type", width: 64, render: 'sys_type', fields: {sys_type: 'host_sys_type'}}, - {title: "状态", key: "host_lock", render: 'host_lock', fields: {host_lock: 'host_lock'}}, - {title: "操作", key: "action", width: 160, render: 'make_action_btn', fields: {id: 'host_id', host_lock: 'host_lock'}} - ], - paging: {selector: dom_id + " [ywl-paging='host-list']", per_page: paging_normal}, - - // 可用的属性设置 - //have_header: true or false - - // 可用的回调函数 - on_created: ywl.on_user_host_table_created, - on_header_created: ywl.on_user_host_table_header_created - - // 可重载的函数(在on_created回调函数中重载) - // on_render_created - // on_header_created - // on_paging_created - // on_data_loaded - // on_row_rendered - // on_table_rendered - // on_cell_created - // on_begin_load - // on_after_load - - // 可用的函数 - // load_data - // cancel_load - // set_data - // add_row - // remove_row - // get_row - // update_row - // clear - // reset_filter - }; - - var host_table = ywl.create_table(host_table_options); - g_user_host_list = host_table; - // 主机分组过滤器 - g_group_list = ywl.page_options.group_list; - ywl.create_table_filter_host_group(host_table, dom_id + " [ywl-filter='host-group']", g_group_list); - - ywl.create_table_filter_system_type(host_table, dom_id + " [ywl-filter='system-type']"); - // 搜索框 - ywl.create_table_filter_search_box(host_table, dom_id + " [ywl-filter='search']"); - - ywl.create_table_filter_user_name(host_table); - - //====================================================== - // 事件绑定 - //====================================================== - $("#btn-delete-host").click(function () { - var _data_list = []; - var _objs = $(host_table.selector + " tbody tr td [data-check-box]"); - $.each(_objs, function (i, _obj) { - if ($(_obj).is(':checked')) { - var _row_data = host_table.get_row(_obj); - var data = {host_id: _row_data.host_id, row_id: _row_data.ywl_row_id}; - _data_list.push(data); - } - }); - if (_data_list.length === 0) { - ywl.notify_error('请先选择要回收授权的主机!'); - return; - } - - var _fn_sure = function (cb_stack, cb_args) { - // var _data_list = cb_args._data_list || []; - var host_list = []; - for (var i = 0; i < _data_list.length; i++) { - var host_id = _data_list[i].host_id; - host_list.push(host_id); - } - host_list.push(host_id); - ywl.ajax_post_json('/user/delete-host', {host_list: host_list, user_name: ywl.page_options.user_name}, - function (ret) { - g_user_host_list.reload(); - ywl.notify_success('成功回收主机授权!'); - }, - function () { - ywl.notify_error('回收主机授权时发生错误!'); - } - ); - }; - - var cb_stack = CALLBACK_STACK.create(); - ywl.dlg_confirm(cb_stack, - { - msg: '
此操作不可恢复!!
您确定要回收选中主机的授权吗?
', - fn_yes: _fn_sure - // cb_args: _data_list - }); - - - }); - - // $("#user-host-titile").html('已经授权给用户' + ywl.page_options.user_name + "的主机列表"); - // 将刷新按钮点击事件绑定到表格的重新加载函数上,这样,点击刷新就导致表格数据重新加载。 - $(dom_id + " [ywl-filter='reload']").click(host_table.reload); - - cb_stack - .add(host_table.load_data) - .add(host_table.init) - .exec(); -}; - -// 扩展/重载表格的功能 -ywl.on_host_table_created = function (tbl) { - - tbl.on_cell_created = function (row_id, col_key, cell_obj) { - if (col_key == 'select_all') { - // 选择 - $('#host-select-' + row_id).click(function () { - console.log('host-sel'); - var _all_checked = true; - var _objs = $(tbl.selector + ' tbody').find('[data-check-box]'); - $.each(_objs, function (i, _obj) { - if (!$(_obj).is(':checked')) { - _all_checked = false; - return false; - } - }); - - var select_all_dom = $('#host-select-all'); - if (_all_checked) { - select_all_dom.prop('checked', true); - } else { - select_all_dom.prop('checked', false); - } - if ($(this).is(':checked')) { - $(this).parent().parent().parent().find("[user-check-box]").prop('checked', true); - } else { - $(this).parent().parent().parent().find("[user-check-box]").prop('checked', false); - } - //ywl.update_add_to_batch_btn(); - }); - - } else if (col_key == 'host_id') { - // 为主机描述绑定点击事件 - var _link = $(cell_obj).find(" [ywl-host-desc]"); - _link.click(function () { - var row_data = tbl.get_row(row_id); - ywl.create_dlg_modify_host_desc(tbl, row_data.ywl_row_id, row_data.host_id, row_data.host_ip, row_data.host_desc).show(_link); - }); - } else if (col_key == 'auth_list') { - $('#user-check-box-row-' + row_id).click(function () { - var _checked = false; - var _objs = $(this).parent().parent().parent().parent().find('[user-check-box]'); - $.each(_objs, function (i, _obj) { - if ($(_obj).is(':checked')) { - _checked = true; - return false; - } - }); - var select = $('#host-select-' + row_id); - - if (_checked) { - select.prop('checked', true); - } else { - select.prop('checked', false); - } - console.log("xxxxxxx"); - }); - } - }; - - // 重载表格渲染器的部分渲染方式,加入本页面相关特殊操作 - tbl.on_render_created = function (render) { - - render.auth_list = function (row_id, fields) { - // TODO: 这里应该从fields里拿一个数组,是本主机上开放的所有远程访问协议和登录账号的组合 - var auth_list = fields.auth_list; - var protocol = fields.protocol; - var ret = []; - - if (auth_list.length == 0) { - ret.push('尚未添加系统用户'); - return ret.join(''); - } - - for (var i = 0; i < auth_list.length; i++) { - var auth = auth_list[i]; - ret.push('此操作不可恢复!!
您确定要回收选中主机的授权吗?
', - fn_yes: _fn_sure - }); - }); - } else if (col_key == 'auth_list') { - - $(cell_obj).find('[data-remove]').click(function () { - var row_data = tbl.get_row(row_id); - var auth_id = parseInt($(this).attr('data-remove')); -// var host_id = row_data.host_id; - var _fn_sure = function (cb_stack, cb_args) { - var auth_id_list = []; - auth_id_list.push(auth_id); - ywl.ajax_post_json('/user/delete-host-user', {auth_id_list: auth_id_list, user_name: ywl.page_options.user_name}, - function (ret) { - if (ret.code == 0) { - var auth_list = []; - for (var i = 0; i < row_data.auth_list.length; i++) { - var auth = row_data.auth_list[i]; - if (auth.auth_id == auth_id) { - continue; - } else { - auth_list.push(auth); - } - } - console.log('auth_list', auth_list); - if (auth_list.length == 0) { - tbl.remove_row(row_id); - } else { - tbl.update_row(row_id, {auth_list: auth_list}); - } - ywl.notify_success('删除成功'); - } else { - ywl.notify_error('删除成功失败' + ret.code); - } - console.log('row_data', row_data); -// tbl.remove_row(row_id); - - }, - function () { - ywl.notify_error('删除成功失败'); - } - ); - }; - var cb_stack = CALLBACK_STACK.create(); - - ywl.dlg_confirm(cb_stack, - { - msg: '此操作不可恢复!!
您确定要回收选中的用户授权吗?
', - fn_yes: _fn_sure - }); - }); - } - }; - - // 重载表格渲染器的部分渲染方式,加入本页面相关特殊操作 - tbl.on_render_created = function (render) { - render.auth_list = function (row_id, fields) { - var auth_list = fields.auth_list; - var protocol = fields.protocol; - var ret = []; - - if (auth_list.length == 0) { - ret.push('尚未添加系统用户'); - return ret.join(''); - } - - for (var i = 0; i < auth_list.length; i++) { - var auth = auth_list[i]; - ret.push('此操作不可恢复!!
您确定要回收选中主机的授权吗?
', + fn_yes: _fn_sure + // cb_args: _data_list + }); + + + }); + + // $("#user-host-titile").html('已经授权给用户' + ywl.page_options.user_name + "的主机列表"); + // 将刷新按钮点击事件绑定到表格的重新加载函数上,这样,点击刷新就导致表格数据重新加载。 + $(dom_id + " [ywl-filter='reload']").click(host_table.reload); + + cb_stack + .add(host_table.load_data) + .add(host_table.init) + .exec(); +}; + +// 扩展/重载表格的功能 +ywl.on_host_table_created = function (tbl) { + + tbl.on_cell_created = function (row_id, col_key, cell_obj) { + if (col_key === 'select_all') { + // 选择 + $('#host-select-' + row_id).click(function () { + console.log('host-sel'); + var _all_checked = true; + var _objs = $(tbl.selector + ' tbody').find('[data-check-box]'); + $.each(_objs, function (i, _obj) { + if (!$(_obj).is(':checked')) { + _all_checked = false; + return false; + } + }); + + var select_all_dom = $('#host-select-all'); + if (_all_checked) { + select_all_dom.prop('checked', true); + } else { + select_all_dom.prop('checked', false); + } + if ($(this).is(':checked')) { + $(this).parent().parent().parent().find("[user-check-box]").prop('checked', true); + } else { + $(this).parent().parent().parent().find("[user-check-box]").prop('checked', false); + } + //ywl.update_add_to_batch_btn(); + }); + + } else if (col_key === 'host_id') { + // 为主机描述绑定点击事件 + var _link = $(cell_obj).find(" [ywl-host-desc]"); + _link.click(function () { + var row_data = tbl.get_row(row_id); + ywl.create_dlg_modify_host_desc(tbl, row_data.ywl_row_id, row_data.host_id, row_data.host_ip, row_data.host_desc).show(_link); + }); + } else if (col_key === 'auth_list') { + $('#user-check-box-row-' + row_id).click(function () { + var _checked = false; + var _objs = $(this).parent().parent().parent().parent().find('[user-check-box]'); + $.each(_objs, function (i, _obj) { + if ($(_obj).is(':checked')) { + _checked = true; + return false; + } + }); + var select = $('#host-select-' + row_id); + + if (_checked) { + select.prop('checked', true); + } else { + select.prop('checked', false); + } + }); + } + }; + + // 重载表格渲染器的部分渲染方式,加入本页面相关特殊操作 + tbl.on_render_created = function (render) { + + render.auth_list = function (row_id, fields) { + // TODO: 这里应该从fields里拿一个数组,是本主机上开放的所有远程访问协议和登录账号的组合 + var auth_list = fields.auth_list; + var protocol = fields.protocol; + var ret = []; + + if (auth_list.length === 0) { + ret.push('尚未添加系统用户'); + return ret.join(''); + } + + for (var i = 0; i < auth_list.length; i++) { + var auth = auth_list[i]; + ret.push('此操作不可恢复!!
您确定要回收选中主机的授权吗?
', + fn_yes: _fn_sure + }); + }); + } else if (col_key === 'auth_list') { + + $(cell_obj).find('[data-remove]').click(function () { + var row_data = tbl.get_row(row_id); + var auth_id = parseInt($(this).attr('data-remove')); + + var _fn_sure = function (cb_stack, cb_args) { + var auth_id_list = []; + auth_id_list.push(auth_id); + ywl.ajax_post_json('/user/delete-host-user', {auth_id_list: auth_id_list, user_name: ywl.page_options.user_name}, + function (ret) { + if (ret.code === TPE_OK) { + var auth_list = []; + for (var i = 0; i < row_data.auth_list.length; i++) { + var auth = row_data.auth_list[i]; + if (auth.auth_id !== auth_id) { + auth_list.push(auth); + } + } + + if (auth_list.length === 0) { + tbl.remove_row(row_id); + } else { + tbl.update_row(row_id, {auth_list: auth_list}); + } + ywl.notify_success('回收授权成功!'); + } else { + ywl.notify_error('回收授权失败:' + ret.message); + } + }, + function () { + ywl.notify_error('网络故障,回收授权失败!'); + } + ); + }; + var cb_stack = CALLBACK_STACK.create(); + + ywl.dlg_confirm(cb_stack, + { + msg: '此操作不可恢复!!
您确定要回收选中的用户授权吗?
', + fn_yes: _fn_sure + }); + }); + } + }; + + // 重载表格渲染器的部分渲染方式,加入本页面相关特殊操作 + tbl.on_render_created = function (render) { + render.auth_list = function (row_id, fields) { + var auth_list = fields.auth_list; + var protocol = fields.protocol; + var ret = []; + + if (auth_list.length === 0) { + ret.push('尚未添加系统用户'); + return ret.join(''); + } + + for (var i = 0; i < auth_list.length; i++) { + var auth = auth_list[i]; + ret.push('删除操作不可恢复!!
您确定要删除此密钥吗?
', - fn_yes: _fn_sure - }); - }); - - } - }; - - // 重载表格渲染器的部分渲染方式,加入本页面相关特殊操作 - tbl.on_render_created = function (render) { - render.make_action_btn = function (row_id, fields) { - var ret = []; - ret.push('编辑 '); - ret.push('删除'); - return ret.join(''); - }; - - render.cert_pub = function (row_id, fields) { - return ''; - }; - }; -}; - -ywl.on_host_table_header_created = function (tbl) { -}; - -ywl.create_cert_info_dlg = function (tbl) { - var cert_info_dlg = {}; - cert_info_dlg.dom_id = "#dialog_cert_info"; - cert_info_dlg.update = 1; - cert_info_dlg.tbl = tbl; - cert_info_dlg.cert_name = ''; - cert_info_dlg.cert_id = 0; - cert_info_dlg.cert_pub = 0; - cert_info_dlg.cert_pri = 0; - cert_info_dlg.row_id = 0; - cert_info_dlg.title = ''; - - cert_info_dlg.update_show = function (cert_name, cert_id, cert_pub, cert_pri, row_id) { - cert_info_dlg.update = 1; - cert_info_dlg.title = '编辑SSH密钥'; - cert_info_dlg.init(cert_name, cert_id, cert_pub, cert_pri, row_id); - var msg = '如果您只是希望修改密钥名称,那么本区域可以忽略不填写!'; - $(cert_info_dlg.dom_id + ' #dlg-cert-pub').attr('placeholder', msg); - $(cert_info_dlg.dom_id + ' #dlg-cert-pri').attr('placeholder', msg); - $(cert_info_dlg.dom_id).modal(); - }; - cert_info_dlg.create_show = function () { - cert_info_dlg.update = 0; - cert_info_dlg.title = '添加SSH密钥'; - cert_info_dlg.init('', 0, '', '', 0); - $(cert_info_dlg.dom_id + ' #dlg-cert-pub').attr('placeholder', ''); - $(cert_info_dlg.dom_id + ' #dlg-cert-pri').attr('placeholder', ''); - $(cert_info_dlg.dom_id).modal(); - }; - cert_info_dlg.hide = function () { - $(cert_info_dlg.dom_id).modal('hide'); - }; - - cert_info_dlg.init = function (cert_name, cert_id, cert_pub, cert_pri, row_id) { - cert_info_dlg.cert_name = cert_name; - cert_info_dlg.cert_id = cert_id; - cert_info_dlg.cert_pub = cert_pub; - cert_info_dlg.cert_pri = '';//cert_pri; - cert_info_dlg.row_id = row_id; - cert_info_dlg.init_dlg(); - }; - - cert_info_dlg.init_dlg = function () { - $(cert_info_dlg.dom_id + ' #title').html(cert_info_dlg.title); - $(cert_info_dlg.dom_id + ' #dlg-cert-name').val(cert_info_dlg.cert_name); - $(cert_info_dlg.dom_id + ' #dlg-cert-pub').val(cert_info_dlg.cert_pub); -// $(cert_info_dlg.dom_id + ' #dlg-cert-pri').val(cert_info_dlg.cert_pri); - $(cert_info_dlg.dom_id + ' #dlg-cert-pri').val(''); - }; - - cert_info_dlg.check_args = function () { - cert_info_dlg.cert_name = $(cert_info_dlg.dom_id + ' #dlg-cert-name').val(); - cert_info_dlg.cert_pub = $(cert_info_dlg.dom_id + ' #dlg-cert-pub').val(); - cert_info_dlg.cert_pri = $(cert_info_dlg.dom_id + ' #dlg-cert-pri').val(); - if (cert_info_dlg.cert_name === '') { - ywl.notify_error('必须为密钥设置一个名称!'); - return false; - } - if (cert_info_dlg.cert_pub === '') { - ywl.notify_error('必须填写公钥内容!'); - return false; - } - if (cert_info_dlg.update === 0 && cert_info_dlg.cert_pri.length === 0) { - ywl.notify_error('添加密钥时,必须填写私钥内容!'); - return false; - } - return true; - }; - - cert_info_dlg.post = function () { - if (cert_info_dlg.update === 1) { - ywl.ajax_post_json('/host/update-cert', {cert_id: cert_info_dlg.cert_id, cert_name: cert_info_dlg.cert_name, cert_pub: cert_info_dlg.cert_pub, cert_pri: cert_info_dlg.cert_pri}, - function (ret) { - var update_args = {cert_id: cert_info_dlg.cert_id, cert_name: cert_info_dlg.cert_name}; - cert_info_dlg.tbl.update_row(cert_info_dlg.row_id, update_args); - ywl.notify_success('密钥更新成功!'); - cert_info_dlg.hide(); - }, - function () { - ywl.notify_error('密钥更新失败!'); - } - ); - } else { - ywl.ajax_post_json('/host/add-cert', {cert_name: cert_info_dlg.cert_name, cert_pub: cert_info_dlg.cert_pub, cert_pri: cert_info_dlg.cert_pri}, - function (ret) { - if(ret.code === TPE_OK){ - cert_info_dlg.tbl.reload(); - ywl.notify_success('密钥添加成功!'); - cert_info_dlg.hide(); - }else if(ret.code === TPE_NO_CORE_SERVER){ - ywl.notify_error('错误,没有启动核心服务!'); - }else{ - ywl.notify_error('密钥添加失败!code:' + ret.code); - } - - }, - function (ret) { - ywl.notify_error('密钥添加失败!'); - } - ); - } - return true; - }; - - $(cert_info_dlg.dom_id + " #btn-save").click(function () { - if (!cert_info_dlg.check_args()) { - return; - } - - cert_info_dlg.post(); -// if (cert_info_dlg.update == 0) { -// cert_info_dlg.on_get_enc_pri(); -// } else { -// if (cert_info_dlg.cert_pri.length > 0) -// cert_info_dlg.on_get_enc_pri(); -// else -// cert_info_dlg.post(); -// } - }); -// -// cert_info_dlg.on_get_enc_pri = function () { -// ywl.ajax_post_json('/auth/get-enc-data', {pwd: cert_info_dlg.cert_pri}, -// function (ret) { -// var data = ret.data; -// if (data.code == 0) { -//// var temp_password = data.data; -// -// cert_info_dlg.cert_pri = data.data; -// -// cert_info_dlg.post(); -// -// -//// $("#dlg-cert-pri").val(temp_password); -//// ywl.notify_success('成功得到私钥加密字符串'); -// } else { -// ywl.notify_error('获取加密私钥失败! [' + data.code + ']'); -// } -// -// }, -// function () { -// ywl.notify_error('获取加密私钥失败'); -// } -// ); -// -// }; - -// $(cert_info_dlg.dom_id + " #btn-get-enc-data").click(function () { -// var temp_dlg_cer__pri = $("#temp-dlg-cert-pri").val(); -// if (temp_dlg_cer__pri == '') { -// ywl.notify_error('私钥不能为空!'); -// return; -// } -// ywl.ajax_post_json('/auth/get-enc-data', {pwd: temp_dlg_cer__pri}, -// function (ret) { -// var data = ret.data; -// if (data.code == 0) { -// var temp_password = data.data; -// $("#dlg-cert-pri").val(temp_password); -// ywl.notify_success('成功得到私钥加密字符串'); -// } else { -// ywl.notify_error('获取加密私钥失败 ' + data.code); -// } -// -// }, -// function () { -// ywl.notify_error('获取加密私钥失败'); -// } -// ); -// }); - - return cert_info_dlg -}; - +"use strict"; + +var g_cert_dlg_info = null; + +ywl.on_init = function (cb_stack, cb_args) { + var dom_id = '#ywl_cert_list'; + + //=================================== + // 创建页面控件对象 + //=================================== + // 表格数据 + var host_table_options = { + selector: dom_id + " [ywl-table='cert-list']", + data_source: { + type: 'ajax-post', + url: '/cert/list' + }, + column_default: {sort: false, header_align: 'center', cell_align: 'center'}, + columns: [ + {title: "编号", key: "cert_id", width: 80}, + {title: "密钥名称", key: "cert_name", width: 240, header_align: 'left', cell_align: 'left'}, + {title: "公钥", key: "cert_pub", render: 'cert_pub', header_align: 'left', cell_align: 'left'}, + //{title: "私钥", key: "cert_pri"}, + {title: "操作", key: "action", width: 180, render: 'make_action_btn', fields: {id: 'cert_id'}} + ], + paging: {selector: dom_id + " [ywl-paging='cert-list']", per_page: paging_normal}, + + // 可用的属性设置 + //have_header: true or false + + // 可用的回调函数 + on_created: ywl.on_host_table_created, + on_header_created: ywl.on_host_table_header_created + + // 可重载的函数(在on_created回调函数中重载) + // on_render_created + // on_header_created + // on_paging_created + // on_data_loaded + // on_row_rendered + // on_table_rendered + // on_cell_created + // on_begin_load + // on_after_load + + // 可用的函数 + // load_data + // cancel_load + // set_data + // add_row + // remove_row + // get_row + // update_row + // clear + // reset_filter + }; + + var host_table = ywl.create_table(host_table_options); + g_cert_dlg_info = ywl.create_cert_info_dlg(host_table); + $(dom_id + " [ywl-filter='reload']").click(host_table.reload); + $("#btn-add-cert").click(function () { + g_cert_dlg_info.create_show(); + }); + cb_stack + .add(host_table.load_data) + .add(host_table.init) + .exec(); +}; + +// 扩展/重载表格的功能 +ywl.on_host_table_created = function (tbl) { + + tbl.on_cell_created = function (row_id, col_key, cell_obj) { + if (col_key === 'action') { + var row_data = tbl.get_row(row_id); + //console.log('row_data', row_data); + $(cell_obj).find('[ywl-btn-edit]').click(function () { + g_cert_dlg_info.update_show(row_data.cert_name, row_data.cert_id, row_data.cert_pub, row_data.cert_pri, row_id); + }); + $(cell_obj).find('[ywl-btn-delete]').click(function () { + var cert_id = row_data.cert_id; + var _fn_sure = function (cb_stack, cb_args) { + ywl.ajax_post_json('/host/delete-cert', {cert_id: cert_id}, + function (ret) { + if (ret.code === TPE_OK) { + tbl.remove_row(row_id); + ywl.notify_success('密钥删除成功!'); + } else if (ret.code === -2) { + ywl.notify_error('不能删除,有主机使用了此密钥!'); + } else { + ywl.notify_error('密钥删除失败:' + ret.message); + } + + }, + function () { + ywl.notify_error('网络故障,密钥删除失败!'); + } + ); + }; + var cb_stack = CALLBACK_STACK.create(); + + ywl.dlg_confirm(cb_stack, + { + msg: '删除操作不可恢复!!
您确定要删除此密钥吗?
', + fn_yes: _fn_sure + }); + }); + + } + }; + + // 重载表格渲染器的部分渲染方式,加入本页面相关特殊操作 + tbl.on_render_created = function (render) { + render.make_action_btn = function (row_id, fields) { + var ret = []; + ret.push('编辑 '); + ret.push('删除'); + return ret.join(''); + }; + + render.cert_pub = function (row_id, fields) { + return ''; + }; + }; +}; + +ywl.on_host_table_header_created = function (tbl) { +}; + +ywl.create_cert_info_dlg = function (tbl) { + var cert_info_dlg = {}; + cert_info_dlg.dom_id = "#dialog_cert_info"; + cert_info_dlg.update = 1; + cert_info_dlg.tbl = tbl; + cert_info_dlg.cert_name = ''; + cert_info_dlg.cert_id = 0; + cert_info_dlg.cert_pub = 0; + cert_info_dlg.cert_pri = 0; + cert_info_dlg.row_id = 0; + cert_info_dlg.title = ''; + + cert_info_dlg.update_show = function (cert_name, cert_id, cert_pub, cert_pri, row_id) { + cert_info_dlg.update = 1; + cert_info_dlg.title = '编辑SSH密钥'; + cert_info_dlg.init(cert_name, cert_id, cert_pub, cert_pri, row_id); + var msg = '如果您只是希望修改密钥名称,那么本区域可以忽略不填写!'; + $(cert_info_dlg.dom_id + ' #dlg-cert-pub').attr('placeholder', msg); + $(cert_info_dlg.dom_id + ' #dlg-cert-pri').attr('placeholder', msg); + $(cert_info_dlg.dom_id).modal(); + }; + cert_info_dlg.create_show = function () { + cert_info_dlg.update = 0; + cert_info_dlg.title = '添加SSH密钥'; + cert_info_dlg.init('', 0, '', '', 0); + $(cert_info_dlg.dom_id + ' #dlg-cert-pub').attr('placeholder', ''); + $(cert_info_dlg.dom_id + ' #dlg-cert-pri').attr('placeholder', ''); + $(cert_info_dlg.dom_id).modal(); + }; + cert_info_dlg.hide = function () { + $(cert_info_dlg.dom_id).modal('hide'); + }; + + cert_info_dlg.init = function (cert_name, cert_id, cert_pub, cert_pri, row_id) { + cert_info_dlg.cert_name = cert_name; + cert_info_dlg.cert_id = cert_id; + cert_info_dlg.cert_pub = cert_pub; + cert_info_dlg.cert_pri = '';//cert_pri; + cert_info_dlg.row_id = row_id; + cert_info_dlg.init_dlg(); + }; + + cert_info_dlg.init_dlg = function () { + $(cert_info_dlg.dom_id + ' #title').html(cert_info_dlg.title); + $(cert_info_dlg.dom_id + ' #dlg-cert-name').val(cert_info_dlg.cert_name); + $(cert_info_dlg.dom_id + ' #dlg-cert-pub').val(cert_info_dlg.cert_pub); +// $(cert_info_dlg.dom_id + ' #dlg-cert-pri').val(cert_info_dlg.cert_pri); + $(cert_info_dlg.dom_id + ' #dlg-cert-pri').val(''); + }; + + cert_info_dlg.check_args = function () { + cert_info_dlg.cert_name = $(cert_info_dlg.dom_id + ' #dlg-cert-name').val(); + cert_info_dlg.cert_pub = $(cert_info_dlg.dom_id + ' #dlg-cert-pub').val(); + cert_info_dlg.cert_pri = $(cert_info_dlg.dom_id + ' #dlg-cert-pri').val(); + if (cert_info_dlg.cert_name === '') { + ywl.notify_error('必须为密钥设置一个名称!'); + return false; + } + if (cert_info_dlg.cert_pub === '') { + ywl.notify_error('必须填写公钥内容!'); + return false; + } + if (cert_info_dlg.update === 0 && cert_info_dlg.cert_pri.length === 0) { + ywl.notify_error('添加密钥时,必须填写私钥内容!'); + return false; + } + return true; + }; + + cert_info_dlg.post = function () { + if (cert_info_dlg.update === 1) { + ywl.ajax_post_json('/host/update-cert', {cert_id: cert_info_dlg.cert_id, cert_name: cert_info_dlg.cert_name, cert_pub: cert_info_dlg.cert_pub, cert_pri: cert_info_dlg.cert_pri}, + function (ret) { + if (ret.code === TPE_OK) { + var update_args = {cert_id: cert_info_dlg.cert_id, cert_name: cert_info_dlg.cert_name}; + cert_info_dlg.tbl.update_row(cert_info_dlg.row_id, update_args); + ywl.notify_success('密钥更新成功!'); + cert_info_dlg.hide(); + } else { + ywl.notify_error('密钥更新失败:' + ret.message); + } + }, + function () { + ywl.notify_error('网络故障,密钥更新失败!'); + } + ); + } else { + ywl.ajax_post_json('/host/add-cert', {cert_name: cert_info_dlg.cert_name, cert_pub: cert_info_dlg.cert_pub, cert_pri: cert_info_dlg.cert_pri}, + function (ret) { + if (ret.code === TPE_OK) { + cert_info_dlg.tbl.reload(); + ywl.notify_success('密钥添加成功!'); + cert_info_dlg.hide(); + } else if (ret.code === TPE_NO_CORE_SERVER) { + ywl.notify_error('错误,没有启动核心服务!'); + } else { + ywl.notify_error('密钥添加失败:' + ret.message); + } + + }, + function (ret) { + ywl.notify_error('网络故障,密钥添加失败!'); + } + ); + } + return true; + }; + + $(cert_info_dlg.dom_id + " #btn-save").click(function () { + if (!cert_info_dlg.check_args()) { + return; + } + + cert_info_dlg.post(); + }); + + return cert_info_dlg +}; + diff --git a/server/www/teleport/static/js/ui/common.js b/server/www/teleport/static/js/ui/common.js index d433627..995c965 100644 --- a/server/www/teleport/static/js/ui/common.js +++ b/server/www/teleport/static/js/ui/common.js @@ -1,413 +1,420 @@ -/*! ywl v1.0.1, (c)2015 eomsoft.net */ -"use strict"; - - -ywl.notify_error = function (message_, title_) { - var _title = title_ || ''; - $.gritter.add({ - //sticky:true, - class_name: 'gritter-error', - time: 10000, - title: ' 错误:' + _title, - text: message_ - }); -}; - -ywl.notify_success = function (message_, title_) { - var _title = title_ || null; - if (_title !== null) - _title = ' ' + _title; - $.gritter.add({ - //sticky:true, - class_name: 'gritter-success', - time: 10000, - title: _title, - text: message_ - }); -}; - - -function get_host_group_by_id(gid) { - var _all = {id: 0, group_name: '全部'}; - return _all; -} - -function get_user_info_by_id(user_id) { - var _all = {id: 0, nickname: '未知'}; - return _all; -} - -function get_event_code_by_id(e_id) { - var _all = {id: 0, e_desc: '未知'}; - var ret = ywl.assist.get_cache_by_id(CACHE_TYPE_EVENT_CODE, e_id); - - if (ret == null) - return _all; - else - return ret; -} -function get_current_system_group() { - return get_system_group_by_id(0); -} - -function get_system_group_by_id(gid) { - var _all = {id: 0, name: '全部'}; - var ret = null; - - $.each(system_group, function (i, group) { - if (group.id == gid) { - ret = group; - return false; - } - }); - - if (ret == null) - return _all; - else - return ret; -} - - -//function get_command_name_by_id(cmd_id) { -// return ywl.assist.get_cache_by_id(CACHE_TYPE_COMMAND, cmd_id); -//} - -//function notify_error(message_, title_) { -// var _title = title_ || ''; -// $.gritter.add({ -// sticky: true, -// class_name: 'gritter-error', -// time: 10000, -// title: ' 错误:' + _title, -// text: message_ -// }); -//} -//function notify_success(message_, title_) { -// var _title = title_ || null; -// if (_title !== null) -// _title = ' ' + _title; -// $.gritter.add({ -// //sticky:true, -// class_name: 'gritter-success', -// time: 10000, -// title: _title, -// text: message_ -// }); -//} - -// 切换一个dom节点显示与否 -ywl.toggle_display = function (selector) { - var obj = $(selector); - if (typeof(obj) == 'undefined') - return; - - if (obj.is(':hidden')) { - obj.show(); - } else { - obj.hide(); - } -}; - -//====================================================== -// Dialog-box for confirm operation. -//====================================================== -ywl.dlg_confirm = function (cb_stack, cb_args) { - var self = {}; - self._cb_stack = cb_stack; - self._title = cb_args.title || '操作确认:'; - self._msg = cb_args.msg || ''; - self._btn_yes = cb_args.btn_yes || '确定'; - self._btn_no = cb_args.btn_no || '取消'; - self._fn_yes = cb_args.fn_yes || null; - self._fn_no = cb_args.fn_no || null; - self._dlg_id = _.uniqueId('dlg-confirm-'); - self._cb_args = cb_args.cb_args || {}; - - self._make_message_box = function () { - var _html = [ - '注意:移除操作不可恢复!!
您确定要删除此分组吗?
', - fn_yes: _fn_sure - }); + ywl.dlg_confirm(cb_stack, + { + msg: '注意:移除操作不可恢复!!
您确定要删除此分组吗?
', + fn_yes: _fn_sure + }); - }); + }); - } - }; + } + }; - // 重载表格渲染器的部分渲染方式,加入本页面相关特殊操作 - tbl.on_render_created = function (render) { - render.make_action_btn = function (row_id, fields) { - var ret = []; - ret.push('编辑 '); - ret.push('删除'); - return ret.join(''); - } + // 重载表格渲染器的部分渲染方式,加入本页面相关特殊操作 + tbl.on_render_created = function (render) { + render.make_action_btn = function (row_id, fields) { + var ret = []; + ret.push('编辑 '); + ret.push('删除'); + return ret.join(''); + } - }; + }; }; ywl.on_host_table_header_created = function (tbl) { }; ywl.create_group_info_dlg = function (tbl) { - var group_info_dlg = {}; - group_info_dlg.dom_id = "#dialog_group_info"; - group_info_dlg.update = 1; - group_info_dlg.tbl = tbl; - group_info_dlg.group_name = ''; - group_info_dlg.group_id = 0; - group_info_dlg.row_id = 0; + var group_info_dlg = {}; + group_info_dlg.dom_id = "#dialog_group_info"; + group_info_dlg.update = 1; + group_info_dlg.tbl = tbl; + group_info_dlg.group_name = ''; + group_info_dlg.group_id = 0; + group_info_dlg.row_id = 0; - group_info_dlg.update_show = function (group_name, group_id, row_id) { - group_info_dlg.update = 1; - group_info_dlg.init(group_name, group_id, row_id); - $(group_info_dlg.dom_id).modal(); - }; - group_info_dlg.create_show = function () { - group_info_dlg.update = 0; - group_info_dlg.init('', 0, 0); - $(group_info_dlg.dom_id).modal(); - }; + group_info_dlg.update_show = function (group_name, group_id, row_id) { + group_info_dlg.update = 1; + group_info_dlg.init(group_name, group_id, row_id); + $(group_info_dlg.dom_id).modal(); + }; + group_info_dlg.create_show = function () { + group_info_dlg.update = 0; + group_info_dlg.init('', 0, 0); + $(group_info_dlg.dom_id).modal(); + }; - group_info_dlg.hide = function() { - $(group_info_dlg.dom_id).modal('hide'); - }; + group_info_dlg.hide = function () { + $(group_info_dlg.dom_id).modal('hide'); + }; - group_info_dlg.init = function (group_name, group_id, row_id) { - group_info_dlg.group_name = group_name; - group_info_dlg.group_id = group_id; - group_info_dlg.row_id = row_id; - group_info_dlg.init_dlg(); - }; - group_info_dlg.init_dlg = function () { - $(group_info_dlg.dom_id + ' #group_name').val(group_info_dlg.group_name); - }; + group_info_dlg.init = function (group_name, group_id, row_id) { + group_info_dlg.group_name = group_name; + group_info_dlg.group_id = group_id; + group_info_dlg.row_id = row_id; + group_info_dlg.init_dlg(); + }; + group_info_dlg.init_dlg = function () { + $(group_info_dlg.dom_id + ' #group_name').val(group_info_dlg.group_name); + }; - group_info_dlg.check_args = function () { - group_info_dlg.group_name = $(group_info_dlg.dom_id + ' #group_name').val(); - return true; - }; - group_info_dlg.post = function () { - if (group_info_dlg.update == 1) { - ywl.ajax_post_json('/host/update-group', {group_id: group_info_dlg.group_id, group_name: group_info_dlg.group_name}, - function (ret) { - var update_args = {id: group_info_dlg.group_id, group_name: group_info_dlg.group_name}; - group_info_dlg.tbl.update_row(group_info_dlg.row_id, update_args); - ywl.notify_success('更新分组信息成功!'); - group_info_dlg.hide(); - }, - function (ret) { - ywl.notify_error('更新分组信息失败!'); - } - ); - } else { - ywl.ajax_post_json('/host/add-group', {group_name: group_info_dlg.group_name}, - function (ret) { - group_info_dlg.tbl.reload(); - ywl.notify_success('分组创建成功!'); - group_info_dlg.hide(); - }, - function (ret) { - ywl.notify_error('分组创建失败!'); - } - ); - } - //group_info_dlg.group_name = $(group_info_dlg.dom_id + ' #group_name').val(); - return true; - }; - $(group_info_dlg.dom_id + " #btn-save").click(function () { - if (!group_info_dlg.check_args()) { - return; - } - group_info_dlg.post(); - }); - return group_info_dlg + group_info_dlg.check_args = function () { + group_info_dlg.group_name = $(group_info_dlg.dom_id + ' #group_name').val(); + return true; + }; + group_info_dlg.post = function () { + if (group_info_dlg.update == 1) { + ywl.ajax_post_json('/host/update-group', {group_id: group_info_dlg.group_id, group_name: group_info_dlg.group_name}, + function (ret) { + if (ret.code === TPE_OK) { + var update_args = {id: group_info_dlg.group_id, group_name: group_info_dlg.group_name}; + group_info_dlg.tbl.update_row(group_info_dlg.row_id, update_args); + ywl.notify_success('更新分组信息成功!'); + group_info_dlg.hide(); + } else { + ywl.notify_error('更新分组失败:' + ret.message); + } + }, + function () { + ywl.notify_error('网络故障,更新分组信息失败!'); + } + ); + } else { + ywl.ajax_post_json('/host/add-group', {group_name: group_info_dlg.group_name}, + function (ret) { + if (ret.code === TPE_OK) { + group_info_dlg.tbl.reload(); + ywl.notify_success('创建分组成功!'); + group_info_dlg.hide(); + } else { + ywl.notify_error('创建分组失败:' + ret.message); + } + }, + function () { + ywl.notify_error('网络故障,创建分组失败!'); + } + ); + } + //group_info_dlg.group_name = $(group_info_dlg.dom_id + ' #group_name').val(); + return true; + }; + $(group_info_dlg.dom_id + " #btn-save").click(function () { + if (!group_info_dlg.check_args()) { + return; + } + group_info_dlg.post(); + }); + return group_info_dlg } \ No newline at end of file diff --git a/server/www/teleport/static/js/ui/log.js b/server/www/teleport/static/js/ui/log.js index 1d34d2a..a18d2a3 100644 --- a/server/www/teleport/static/js/ui/log.js +++ b/server/www/teleport/static/js/ui/log.js @@ -1,386 +1,369 @@ -"use strict"; - -ywl.on_init = function (cb_stack, cb_args) { - var dom_id = '#ywl_log_list'; - - //=================================== - // 创建页面控件对象 - //=================================== - // 表格数据 - var disk_rate = parseInt(ywl.page_options.free_size * 100 / ywl.page_options.total_size); - $('#disk-status').text('日志磁盘大小:' + size2str(ywl.page_options.total_size, 2) + ',剩余空间:' + size2str(ywl.page_options.free_size, 2) + ',空闲' + disk_rate + '%'); - if (disk_rate < 10) { - $('#disk-status').removeClass().addClass('badge badge-danger'); - } else if (disk_rate < 30) { - $('#disk-status').removeClass().addClass('badge badge-warning'); - } else { - $('#disk-status').removeClass().addClass('badge badge-ignore'); - } - - var host_table_options = { - selector: dom_id + " [ywl-table='log-list']", - data_source: { - type: 'ajax-post', - url: '/log/list' - }, - column_default: {sort: false, header_align: 'center', cell_align: 'center'}, - columns: [ - { - title: '', - key: 'select_all', - sort: false, - width: 24, - render: 'make_check_box', - fields: {id: 'id'} - }, - {title: "ID", key: "id"}, - {title: "操作者", key: "account_name"}, - {title: "系统用户", key: "user_name"}, - {title: "协议", key: "protocol", render: 'protocol', fields: {protocol: 'protocol'}}, - {title: "系统", key: "sys_type", width: 40, render: 'sys_type', fields: {sys_type: 'sys_type'}}, - {title: "远程主机地址", key: "host_ip", render: 'server_info', fields: {host_ip: 'host_ip', host_port: 'host_port'}}, - {title: "开始时间", key: "begin_time", width: 160, render: 'begin_time', fields: {begin_time: 'begin_time'}}, - {title: "耗时", key: "cost_time", render: 'cost_time', fields: {cost_time: 'cost_time', ret_code: 'ret_code'}}, - {title: "状态", key: "ret_code", render: 'ret_code', fields: {ret_code: 'ret_code'}}, - { - title: "操作", - key: "action", - width: 160, - header_align: 'left', cell_align: 'left', - render: 'make_action_btn', - fields: {ID: 'id', ret_code:'ret_code', sys_type: 'sys_type', cost_time: 'cost_time', protocol: 'protocol'} - } - ], - paging: {selector: dom_id + " [ywl-paging='log-list']", per_page: paging_normal}, - - // 可用的属性设置 - //have_header: true or false - - // 可用的回调函数 - on_created: ywl.on_host_table_created, - on_header_created: ywl.on_host_table_header_created - - // 可重载的函数(在on_created回调函数中重载) - // on_render_created - // on_header_created - // on_paging_created - // on_data_loaded - // on_row_rendered - // on_table_rendered - // on_cell_created - // on_begin_load - // on_after_load - - // 可用的函数 - // load_data - // cancel_load - // set_data - // add_row - // remove_row - // get_row - // update_row - // clear - // reset_filter - }; - - var host_table = ywl.create_table(host_table_options); - - $(dom_id + " [ywl-filter='reload']").click(host_table.reload); - - $("#delete-log").click(function () { - var log_list = []; - var _objs = $(host_table.selector + " tbody tr td [data-check-box]"); - $.each(_objs, function (i, _obj) { - if ($(_obj).is(':checked')) { - var _row_data = host_table.get_row(_obj); - log_list.push(_row_data.id); - } - }); - if (log_list.length === 0) { - ywl.notify_error('请选择要批量删除的日志'); - return; - } - ; - var _fn_sure = function (cb_stack, cb_args) { - ywl.ajax_post_json_time_out('/log/delete-log', {log_list: log_list},1000*30, - function (ret) { - host_table.reload(); - ywl.notify_success('操作成功'); - }, - function () { - ywl.notify_error('操作失败'); - } - ); - }; - var cb_stack = CALLBACK_STACK.create(); - - ywl.dlg_confirm(cb_stack, { - msg: '您确定要删除选定的日志吗?此操作不可恢复!!
', - fn_yes: _fn_sure - }); - }); - - ywl.create_table_filter_user_list(host_table, dom_id + " [ywl-filter='user-name']"); - - ywl.create_table_filter_search_box(host_table, dom_id + " [ywl-filter='search']"); - - cb_stack - .add(host_table.load_data) - .add(host_table.init) - .exec(); -}; -ywl.on_host_table_header_created = function (tbl) { - $('#host-select-all').click(function () { - var _is_selected = $(this).is(':checked'); - $(tbl.selector + ' tbody').find('[data-check-box]').prop('checked', _is_selected); - }); -}; -// 扩展/重载表格的功能 -ywl.on_host_table_created = function (tbl) { - - tbl.on_cell_created = function (row_id, col_key, cell_obj) { - if (col_key == 'select_all') { - // 选择 - $('#host-select-' + row_id).click(function () { - var _all_checked = true; - var _objs = $(tbl.selector + ' tbody').find('[data-check-box]'); - $.each(_objs, function (i, _obj) { - if (!$(_obj).is(':checked')) { - _all_checked = false; - return false; - } - }); - - var select_all_dom = $('#host-select-all'); - if (_all_checked) { - select_all_dom.prop('checked', true); - } else { - select_all_dom.prop('checked', false); - } - - //ywl.update_add_to_batch_btn(); - }); - - } else if (col_key === 'action') { - var row_data = tbl.get_row(row_id); - var protocol = parseInt(row_data.protocol); - - if (protocol === PROTOCOL_TYPE_RDP) { - $(cell_obj).find('[ywl-btn-record]').click(function () { - var ip = window.location.hostname; - var port = parseInt(window.location.port); - var url = 'http://' + ip + ':' + port + '/log/replay/rdp/' + row_data.id; - var tail = 'log/replay/rdp/' + prefixInteger(row_data.id, 6); - var args = {}; - args.id = parseInt(row_data.id); - args.host = ip; - args.port = port; - args.tail = tail; - args.url = url; - start_rdp_replay(args, - function () { - ywl.notify_success('RDP 录像播放器成功启动!'); - }, - function (code, msg) { - if (code === TPE_NO_ASSIST) - g_assist.alert_assist_not_found(); - else { - ywl.notify_error(msg); - console.log('error:', msg) - } - }); - }); - } - else if (protocol === PROTOCOL_TYPE_SSH) { - $(cell_obj).find('[ywl-btn-record]').click(function () { - window.open('/log/record/' + parseInt(row_data.protocol) + '/' + row_data.id); - }); - - $(cell_obj).find('[ywl-btn-log]').click(function () { - window.open('/log/command-log/' + parseInt(row_data.protocol) + '/' + row_data.id); - }); - } - } - }; - - // 重载表格渲染器的部分渲染方式,加入本页面相关特殊操作f成功 - tbl.on_render_created = function (render) { - render.ret_code = function (row_id, fields) { - var msg = ''; - switch (fields.ret_code) { - case 0: - return '使用中' -// return '-'; - case 9999: - return '成功'; - case 1: - msg = '认证失败'; - break; - case 2: - msg = '连接失败'; - break; - case 3: - msg = '私钥错误'; - break; - case 4: - msg = '内部错误'; - break; - case 5: - msg = '协议不支持'; - break; - case 6: - msg = '通讯错误'; - break; - case 7: - msg = '错误重置'; - break; - default: - //return '' + fields.ret_code + ''; - msg = fields.ret_code; - } - - return '' + msg + ''; - -// if (fields.ret_code == 0) { -// -// } else if (fields.ret_code == 9999) { -// return '成功'; -// } else { -// return '' + fields.ret_code + ''; -// } - - }; - render.begin_time = function (row_id, fields) { - return '' + format_datetime(utc_to_local(fields.begin_time)) + ' '; - }; - - render.cost_time = function (row_id, fields) { - if (fields.ret_code == 0) { - return '使用中'; - } else { - return '' + second2str(fields.cost_time) + ''; - } - }; - render.server_info = function (row_id, fields) { - //return '' + fields.host_ip + ':' + fields.host_port + ''; - return '' + fields.host_ip + ':' + fields.host_port + ''; - }; -// render.auth_type = function (row_id, fields) { -// switch (fields.auth_type) { -// case 0: -// return '无认证'; -// case 1: -// return '用户名/密码'; -// case 2: -// return 'SSH密钥'; -// default: -// return '未知'; -// } -// }; - render.protocol = function (row_id, fields) { - switch (fields.protocol) { - case 1: - return 'RDP'; - case 2: - return 'SSH'; - case 3: - return 'TELNET'; - default: - return '未知'; - } - }; - - render.make_check_box = function (row_id, fields) { - return ''; - }; - - - render.make_action_btn = function (row_id, fields) { - var ret = []; - if (fields.protocol === PROTOCOL_TYPE_RDP) { - ret.push('录像查看 '); - } else if (fields.protocol === PROTOCOL_TYPE_SSH) { - if (fields.ret_code === 9999 && fields.cost_time > 0) { - ret.push('录像查看 '); - ret.push('日志查看 '); - } - } - - return ret.join(''); - } - }; -}; - -ywl.create_table_filter_user_list = function (tbl, selector, on_created) { - var _tblf_st = {}; - - // 此表格绑定的DOM对象的ID,用于JQuery的选择器 - _tblf_st.selector = selector; - // 此过滤器绑定的表格控件 - _tblf_st._table_ctrl = tbl; - _tblf_st._table_ctrl.append_filter_ctrl(_tblf_st); - - // 过滤器内容 - _tblf_st.filter_name = 'user_name'; - _tblf_st.filter_default = '全部'; - _tblf_st.filter_value = ''; - - _tblf_st.get_filter = function () { - var _ret = {}; - _ret[_tblf_st.filter_name] = _tblf_st.filter_value; - return _ret; - }; - - _tblf_st.reset = function (cb_stack, cb_args) { - if (_tblf_st.filter_value == _tblf_st.filter_default) { - cb_stack.exec(); - return; - } - - cb_stack - .add(function (cb_stack) { - _tblf_st.filter_value = _tblf_st.filter_default; - $(_tblf_st.selector + ' button span:first').html(_tblf_st.filter_value); - cb_stack.exec(); - }); - }; - - _tblf_st.init = function (cb_stack) { - var node = ''; - var user_list = ywl.page_options.user_list; - node += '您确定要删除选定的日志吗?此操作不可恢复!!
', + fn_yes: _fn_sure + }); + }); + + ywl.create_table_filter_user_list(host_table, dom_id + " [ywl-filter='user-name']"); + + ywl.create_table_filter_search_box(host_table, dom_id + " [ywl-filter='search']"); + + cb_stack + .add(host_table.load_data) + .add(host_table.init) + .exec(); +}; +ywl.on_host_table_header_created = function (tbl) { + $('#host-select-all').click(function () { + var _is_selected = $(this).is(':checked'); + $(tbl.selector + ' tbody').find('[data-check-box]').prop('checked', _is_selected); + }); +}; +// 扩展/重载表格的功能 +ywl.on_host_table_created = function (tbl) { + + tbl.on_cell_created = function (row_id, col_key, cell_obj) { + if (col_key === 'select_all') { + // 选择 + $('#host-select-' + row_id).click(function () { + var _all_checked = true; + var _objs = $(tbl.selector + ' tbody').find('[data-check-box]'); + $.each(_objs, function (i, _obj) { + if (!$(_obj).is(':checked')) { + _all_checked = false; + return false; + } + }); + + var select_all_dom = $('#host-select-all'); + if (_all_checked) { + select_all_dom.prop('checked', true); + } else { + select_all_dom.prop('checked', false); + } + + //ywl.update_add_to_batch_btn(); + }); + + } else if (col_key === 'action') { + var row_data = tbl.get_row(row_id); + var protocol = parseInt(row_data.protocol); + + if (protocol === PROTOCOL_TYPE_RDP) { + $(cell_obj).find('[ywl-btn-record]').click(function () { + var ip = window.location.hostname; + var port = parseInt(window.location.port); + var url = 'http://' + ip + ':' + port + '/log/replay/rdp/' + row_data.id; + var tail = 'log/replay/rdp/' + prefixInteger(row_data.id, 6); + var args = {}; + args.id = parseInt(row_data.id); + args.host = ip; + args.port = port; + args.tail = tail; + args.url = url; + start_rdp_replay(args, + function () { + ywl.notify_success('RDP 录像播放器成功启动!'); + }, + function (code, msg) { + if (code === TPE_NO_ASSIST) + g_assist.alert_assist_not_found(); + else { + ywl.notify_error(msg); + console.log('error:', msg) + } + }); + }); + } + else if (protocol === PROTOCOL_TYPE_SSH) { + $(cell_obj).find('[ywl-btn-record]').click(function () { + window.open('/log/record/' + parseInt(row_data.protocol) + '/' + row_data.id); + }); + + $(cell_obj).find('[ywl-btn-log]').click(function () { + window.open('/log/command-log/' + parseInt(row_data.protocol) + '/' + row_data.id); + }); + } + } + }; + + // 重载表格渲染器的部分渲染方式,加入本页面相关特殊操作f成功 + tbl.on_render_created = function (render) { + render.ret_code = function (row_id, fields) { + var msg = ''; + switch (fields.ret_code) { + case 0: + return '使用中' + case 9999: + return '成功'; + case 1: + msg = '认证失败'; + break; + case 2: + msg = '连接失败'; + break; + case 3: + msg = '私钥错误'; + break; + case 4: + msg = '内部错误'; + break; + case 5: + msg = '协议不支持'; + break; + case 6: + msg = '通讯错误'; + break; + case 7: + msg = '错误重置'; + break; + default: + msg = fields.ret_code; + } + + return '' + msg + ''; + }; + render.begin_time = function (row_id, fields) { + return '' + format_datetime(utc_to_local(fields.begin_time)) + ' '; + }; + + render.cost_time = function (row_id, fields) { + if (fields.ret_code === 0) { + return '使用中'; + } else { + return '' + second2str(fields.cost_time) + ''; + } + }; + render.server_info = function (row_id, fields) { + //return '' + fields.host_ip + ':' + fields.host_port + ''; + return '' + fields.host_ip + ':' + fields.host_port + ''; + }; + + render.protocol = function (row_id, fields) { + switch (fields.protocol) { + case 1: + return 'RDP'; + case 2: + return 'SSH'; + case 3: + return 'TELNET'; + default: + return '未知'; + } + }; + + render.make_check_box = function (row_id, fields) { + return ''; + }; + + + render.make_action_btn = function (row_id, fields) { + var ret = []; + if (fields.protocol === PROTOCOL_TYPE_RDP) { + ret.push('录像查看 '); + } else if (fields.protocol === PROTOCOL_TYPE_SSH) { + if (fields.ret_code === 9999 && fields.cost_time > 0) { + ret.push('录像查看 '); + ret.push('日志查看 '); + } + } + + return ret.join(''); + } + }; +}; + +ywl.create_table_filter_user_list = function (tbl, selector, on_created) { + var _tblf_st = {}; + + // 此表格绑定的DOM对象的ID,用于JQuery的选择器 + _tblf_st.selector = selector; + // 此过滤器绑定的表格控件 + _tblf_st._table_ctrl = tbl; + _tblf_st._table_ctrl.append_filter_ctrl(_tblf_st); + + // 过滤器内容 + _tblf_st.filter_name = 'user_name'; + _tblf_st.filter_default = '全部'; + _tblf_st.filter_value = ''; + + _tblf_st.get_filter = function () { + var _ret = {}; + _ret[_tblf_st.filter_name] = _tblf_st.filter_value; + return _ret; + }; + + _tblf_st.reset = function (cb_stack, cb_args) { + if (_tblf_st.filter_value === _tblf_st.filter_default) { + cb_stack.exec(); + return; + } + + cb_stack + .add(function (cb_stack) { + _tblf_st.filter_value = _tblf_st.filter_default; + $(_tblf_st.selector + ' button span:first').html(_tblf_st.filter_value); + cb_stack.exec(); + }); + }; + + _tblf_st.init = function (cb_stack) { + var node = ''; + var user_list = ywl.page_options.user_list; + node += '' + message + '
', - fn_yes: _fn_sure - }); - }); + ywl.dlg_confirm(cb_stack, + { + msg: '' + message + '
', + fn_yes: _fn_sure + }); + }); - $(cell_obj).find('[ywl-btn-lock]').click(function () { - var user_id = row_data.user_id; - var user_lock = row_data.user_lock; - var message = ''; - if (user_lock === 0) { - user_lock = 1; - message = '被锁定的用户将无法登陆系统,确认要锁定该用户吗?'; - } else { - user_lock = 0; - message = '确认要解锁该用户吗?'; - } - var _fn_sure = function (cb_stack, cb_args) { - ywl.ajax_post_json('/user/lock-user', {user_id: user_id, lock_status: user_lock}, - function (ret) { - var update_args = {user_lock: user_lock}; - tbl.update_row(row_id, update_args); - ywl.notify_success('操作成功!'); - }, - function () { - ywl.notify_error('操作失败!'); - } - ); - }; - var cb_stack = CALLBACK_STACK.create(); + $(cell_obj).find('[ywl-btn-lock]').click(function () { + var user_id = row_data.user_id; + var user_lock = row_data.user_lock; + var message = ''; + if (user_lock === 0) { + user_lock = 1; + message = '被锁定的用户将无法登陆系统,确认要锁定该用户吗?'; + } else { + user_lock = 0; + message = '确认要解锁该用户吗?'; + } + var _fn_sure = function (cb_stack, cb_args) { + ywl.ajax_post_json('/user/lock-user', {user_id: user_id, lock_status: user_lock}, + function (ret) { + if (ret.code === TPE_OK) { + var update_args = {user_lock: user_lock}; + tbl.update_row(row_id, update_args); + ywl.notify_success('操作成功!'); + } else { + ywl.notify_error('操作失败!'); + } + }, + function () { + ywl.notify_error('网络故障,操作失败!'); + } + ); + }; + var cb_stack = CALLBACK_STACK.create(); - ywl.dlg_confirm(cb_stack, - { - msg: '' + message + '
', - fn_yes: _fn_sure - }); + ywl.dlg_confirm(cb_stack, + { + msg: '' + message + '
', + fn_yes: _fn_sure + }); - }); + }); - $(cell_obj).find('[ywl-btn-delete]').click(function () { - var user_id = row_data.user_id; - var _fn_sure = function (cb_stack, cb_args) { - ywl.ajax_post_json('/user/delete-user', {user_id: user_id}, - function (ret) { - if (ret.code == 0) { - tbl.remove_row(row_id); - ywl.notify_success('删除用户成功!'); - } else { - ywl.notify_error('删除用户失败!'); - } + $(cell_obj).find('[ywl-btn-delete]').click(function () { + var user_id = row_data.user_id; + var _fn_sure = function (cb_stack, cb_args) { + ywl.ajax_post_json('/user/delete-user', {user_id: user_id}, + function (ret) { + if (ret.code === TPE_OK) { + tbl.remove_row(row_id); + ywl.notify_success('删除用户成功!'); + } else { + ywl.notify_error('删除用户失败:' + ret.message); + } - }, - function (ret) { - ywl.notify_error('删除用户失败!'); - } - ); - }; - var cb_stack = CALLBACK_STACK.create(); + }, + function () { + ywl.notify_error('网络故障,删除用户失败!'); + } + ); + }; + var cb_stack = CALLBACK_STACK.create(); - ywl.dlg_confirm(cb_stack, - { - msg: '注意:移除操作不可恢复!!
您确定要删除此用户吗?
', - fn_yes: _fn_sure - }); + ywl.dlg_confirm(cb_stack, + { + msg: '注意:移除操作不可恢复!!
您确定要删除此用户吗?
', + fn_yes: _fn_sure + }); - }); - $(cell_obj).find('[ywl-auth-allo]').click(function () { - window.open("/user/auth/" + row_data.user_name); - }); + }); + $(cell_obj).find('[ywl-auth-allo]').click(function () { + window.open("/user/auth/" + row_data.user_name); + }); - } - }; + } + }; - // 重载表格渲染器的部分渲染方式,加入本页面相关特殊操作 - tbl.on_render_created = function (render) { - render.user_lock = function (row_id, fields) { - switch (fields.user_lock) { - case 0: - return '允许访问'; - case 1: - return '禁止访问'; - default: - return '未知'; - } - }; - render.make_action_btn = function (row_id, fields) { - var ret = []; - ret.push('请选择要使用的数据库类型(暂时仅支持sqlite,其它类型开发中):
-请选择要使用的数据库类型(暂时仅支持sqlite,其它类型开发中):
+