From 66e33d38418d8b8a0485fd6abd17e5e88152a176 Mon Sep 17 00:00:00 2001 From: Mr Chen Date: Wed, 9 Jan 2019 16:37:57 +0800 Subject: [PATCH] up --- plugins/mysql/index.py | 77 ++++++++++++++++++++++++++++++--------- plugins/mysql/js/mysql.js | 5 ++- 2 files changed, 63 insertions(+), 19 deletions(-) diff --git a/plugins/mysql/index.py b/plugins/mysql/index.py index eb4c4bb4c..5556e620b 100755 --- a/plugins/mysql/index.py +++ b/plugins/mysql/index.py @@ -378,6 +378,20 @@ def mapToList(map_obj): return [] +def __createUser(dbname, username, password, address): + pdb = pMysqlDb() + pdb.execute( + "CREATE USER `%s`@`localhost` IDENTIFIED BY '%s'" % (username, password)) + pdb.execute( + "grant all privileges on %s.* to `%s`@`localhost`" % (dbname, username)) + for a in address.split(','): + pdb.execute( + "CREATE USER `%s`@`%s` IDENTIFIED BY '%s'" % (username, a, password)) + pdb.execute( + "grant all privileges on %s.* to `%s`@`%s`" % (dbname, username, a)) + pdb.execute("flush privileges") + + def getDbList(): args = getArgs() page = 1 @@ -449,19 +463,59 @@ def syncGetDatabases(): if value[0] == 'test': ps = public.getMsg('DATABASE_TEST') addTime = time.strftime('%Y-%m-%d %X', time.localtime()) - if psdb.table('databases').add('name,username,password,accept,ps,addtime', (value[0], value[0], '', host, ps, addTime)): + if psdb.add('name,username,password,accept,ps,addtime', (value[0], value[0], '', host, ps, addTime)): n += 1 msg = public.getInfo('本次共从服务器获取了{1}个数据库!', (str(n),)) return public.returnJson(True, msg) -def toDbBase(): - return 'fff' +def toDbBase(find): + pdb = pMysqlDb() + psdb = pSqliteDb('databases') + if len(find['password']) < 3: + find['username'] = find['name'] + find['password'] = public.md5(str(time.time()) + find['name'])[0:10] + psdb.where("id=?", (find['id'],)).save( + 'password,username', (find['password'], find['username'])) + + result = pdb.execute("create database `" + find['name'] + "`") + if "using password:" in str(result): + return -1 + if "Connection refused" in str(result): + return -1 + + password = find['password'] + __createUser(find['name'], find['username'], password, find['accept']) + return 1 def syncToDatabases(): - return public.returnJson(False, 'f') + args = getArgs() + data = checkArgs(args, ['type']) + if not data[0]: + return data[1] + + pdb = pMysqlDb() + result = pdb.execute("show databases") + isError = isSqlError(result) + if isError: + return isError + + stype = int(args['type']) + psdb = pSqliteDb('databases') + n = 0 + data = psdb.field('id,name,username,password,accept').select() + if stype == 0: + for value in data: + result = toDbBase(value) + if result == 1: + n += 1 + else: + pass + + msg = public.getInfo('本次共同步了{1}个数据库!', (str(n),)) + return public.returnJson(True, msg) def setRootPwd(): @@ -534,20 +588,6 @@ def setUserPwd(): return public.returnJson(False, public.getInfo('修改数据库[{1}]密码失败!', (name))) -def __createUser(dbname, username, password, address): - pdb = pMysqlDb() - pdb.execute( - "CREATE USER `%s`@`localhost` IDENTIFIED BY '%s'" % (username, password)) - pdb.execute( - "grant all privileges on %s.* to `%s`@`localhost`" % (dbname, username)) - for a in address.split(','): - pdb.execute( - "CREATE USER `%s`@`%s` IDENTIFIED BY '%s'" % (username, a, password)) - pdb.execute( - "grant all privileges on %s.* to `%s`@`%s`" % (dbname, username, a)) - pdb.execute("flush privileges") - - def addDb(): args = getArgs() @@ -691,6 +731,7 @@ def setDbAccess(): __createUser(dbname, name, password, access) + psdb.where('username=?', (name,)).save('accept', (access,)) return public.returnJson(True, '设置成功!') if __name__ == "__main__": diff --git a/plugins/mysql/js/mysql.js b/plugins/mysql/js/mysql.js index ad8a7fb47..540c1fd59 100755 --- a/plugins/mysql/js/mysql.js +++ b/plugins/mysql/js/mysql.js @@ -422,7 +422,10 @@ function syncGetDatabase(){ } function syncToDatabase(type){ - myPost('sync_to_databases', null, function(data){ + + var data = 'type='+type; + + myPost('sync_to_databases', data, function(data){ console.log(data); var rdata = $.parseJSON(data.data); console.log(rdata);