Modernizes SQL admin loading (#35264)

* updates sqladmins with excluded flags and tempminning

* rank, editing and permission protections; json backup

* fixes

* reverted the wrong file

* fix slidecolor cascading and disabled switches not being sent in form

* removes debug uncommenting

* whoops this too

* commas fix + versions for changelog entry headers since 3.0

* actually account for can_edit_flags and use of @ with - or *

* fixes and rearrangement of admin > localhost > autoadmin precedence

* in case you want to not have autologin autoadmins
This commit is contained in:
Jordie
2018-03-02 11:38:26 +11:00
committed by oranges
parent 109e376f14
commit 23a45889ed
25 changed files with 723 additions and 539 deletions

View File

@@ -0,0 +1,93 @@
#Python 3+ Script for importing admins.txt and admin_ranks.txt made by Jordie0608
#
#Before starting ensure you have installed the mysqlclient package https://github.com/PyMySQL/mysqlclient-python
#It can be downloaded from command line with pip:
#pip install mysqlclient
#And that you have run the most recent commands listed in database_changelog.txt
#
#To view the parameters for this script, execute it with the argument --help
#All the positional arguments are required, remember to include prefixes in your table names if you use them
#An example of the command used to execute this script from powershell:
#python admin_import_2018-02-03.py "localhost" "root" "password" "feedback" "SS13_admin" "SS13_admin_ranks"
#
#This script performs no error-correction, improper configurations of admins.txt or admin_ranks.txt will cause either breaking exceptions or invalid table rows
#It's safe to run this script with your game server(s) active.
import MySQLdb
import argparse
import re
import sys
def parse_text_flags(text, previous):
flag_values = {"BUILDMODE":1, "BUILD":1, "ADMIN":2, "REJUVINATE":2, "REJUV":2, "BAN":4, "FUN":8, "SERVER":16, "DEBUG":32, "POSSESS":64, "PERMISSIONS":128, "RIGHTS":128, "STEALTH":256, "POLL":512, "VAREDIT":1024, "SOUNDS":2048, "SOUND":2048, "SPAWN":4096, "CREATE":4096, "AUTOLOGIN":8192, "AUTOADMIN":8192, "DBRANKS":16384}
flags_int = 8192
exclude_flags_int = 0
can_edit_flags_int = 0
flags = text.split(" ")
if flags:
for flag in flags:
sign = flag[:1]
if flag[1:] in ("@", "prev"):
if sign is "+":
flags_int = previous[0]
elif sign is "-":
exclude_flags_int = previous[1]
elif sign is "*":
can_edit_flags_int = previous[2]
continue
if flag[1:] in ("EVERYTHING", "HOST", "ALL"):
if sign is "+":
flags_int = 65535
elif sign is "-":
exclude_flags_int = 65535
elif sign is "*":
can_edit_flags_int = 65535
continue
if flag[1:] in flag_values:
if sign is "+":
flags_int += flag_values[flag[1:]]
elif sign is "-":
exclude_flags_int += flag_values[flag[1:]]
elif sign is "*":
can_edit_flags_int += flag_values[flag[1:]]
flags_int = max(min(65535, flags_int), 0)
exclude_flags_int = max(min(65535, exclude_flags_int), 0)
can_edit_flags_int = max(min(65535, can_edit_flags_int), 0)
return flags_int, exclude_flags_int, can_edit_flags_int
if sys.version_info[0] < 3:
raise Exception("Python must be at least version 3 for this script.")
parser = argparse.ArgumentParser()
parser.add_argument("address", help="MySQL server address (use localhost for the current computer)")
parser.add_argument("username", help="MySQL login username")
parser.add_argument("password", help="MySQL login username")
parser.add_argument("database", help="Database name")
parser.add_argument("admintable", help="Name of the current admin table (remember prefixes if you use them)")
parser.add_argument("rankstable", help="Name of the current admin ranks (remember prefixes)")
args = parser.parse_args()
db=MySQLdb.connect(host=args.address, user=args.username, passwd=args.password, db=args.database)
cursor=db.cursor()
ranks_table = args.rankstable
admin_table = args.admintable
with open("..\\config\\admin_ranks.txt") as rank_file:
previous = 0
for line in rank_file:
if line.strip():
if line.startswith("#"):
continue
matches = re.match("(.+)\\b\\s+=\\s*(.*)", line)
flags = parse_text_flags(matches.group(2), previous)
previous = flags
cursor.execute("INSERT INTO {0} (rank, flags, exclude_flags, can_edit_flags) VALUES ('{1}', {2}, {3}, {4})".format(ranks_table, matches.group(1), flags[0], flags[1], flags[2]))
with open("..\\config\\admins.txt") as admins_file:
previous = 0
for line in admins_file:
if line.strip():
if line.startswith("#"):
continue
matches = re.match("(.+)\\b\\s+=\\s+(.+)", line)
cursor.execute("INSERT INTO {0} (ckey, rank) VALUES ('{1}', '{2}')".format(admin_table, matches.group(1).lower(), matches.group(2)))
db.commit()
cursor.close()
print("Import complete.")

View File

@@ -1,16 +1,50 @@
Any time you make a change to the schema files, remember to increment the database schema version. Generally increment the minor number, major should be reserved for significant changes to the schema. Both values go up to 255.
The latest database version is 4.0; The query to update the schema revision table is:
The latest database version is 4.1; The query to update the schema revision table is:
INSERT INTO `schema_revision` (`major`, `minor`) VALUES (4, 0);
INSERT INTO `schema_revision` (`major`, `minor`) VALUES (4, 1);
or
INSERT INTO `SS13_schema_revision` (`major`, `minor`) VALUES (4, 0);
INSERT INTO `SS13_schema_revision` (`major`, `minor`) VALUES (4, 1);
In any query remember to add a prefix to the table names if you use one.
----------------------------------------------------
12 November 2017, by Jordie0608
Version 4.1, 3 February 2018, by Jordie0608
Modified tables 'admin', 'admin_log' and 'admin_rank', removing unnecessary columns and adding support for excluding rights flags from admin ranks.
This change was made to enable use of sql-based admin loading.
To import your existing admins and ranks run the included script 'admin_import_2018-02-03.py', see the file for use instructions.
Legacy file-based admin loading is still supported, if you want to continue using it the script doesn't need to be run.
ALTER TABLE `admin`
CHANGE COLUMN `rank` `rank` VARCHAR(32) NOT NULL AFTER `ckey`,
DROP COLUMN `id`,
DROP COLUMN `level`,
DROP COLUMN `flags`,
DROP COLUMN `email`,
DROP PRIMARY KEY,
ADD PRIMARY KEY (`ckey`);
ALTER TABLE `admin_log`
CHANGE COLUMN `datetime` `datetime` DATETIME NOT NULL AFTER `id`,
CHANGE COLUMN `adminckey` `adminckey` VARCHAR(32) NOT NULL AFTER `datetime`,
CHANGE COLUMN `adminip` `adminip` INT(10) UNSIGNED NOT NULL AFTER `adminckey`,
ADD COLUMN `operation` ENUM('add admin','remove admin','change admin rank','add rank','remove rank','change rank flags') NOT NULL AFTER `adminip`,
CHANGE COLUMN `log` `log` VARCHAR(1000) NOT NULL AFTER `operation`;
ALTER TABLE `admin_ranks`
CHANGE COLUMN `rank` `rank` VARCHAR(32) NOT NULL FIRST,
CHANGE COLUMN `flags` `flags` SMALLINT UNSIGNED NOT NULL AFTER `rank`,
ADD COLUMN `exclude_flags` SMALLINT UNSIGNED NOT NULL AFTER `flags`,
ADD COLUMN `can_edit_flags` SMALLINT(5) UNSIGNED NOT NULL AFTER `exclude_flags`,
DROP COLUMN `id`,
DROP PRIMARY KEY,
ADD PRIMARY KEY (`rank`);
----------------------------------------------------
Version 4.0, 12 November 2017, by Jordie0608
Modified feedback table to use json, a python script is used to migrate data to this new format.
See the file 'feedback_conversion_2017-11-12.py' for instructions on how to use the script.
@@ -29,7 +63,7 @@ CREATE TABLE `feedback` (
----------------------------------------------------
28 August 2017, by MrStonedOne
Version 3.4, 28 August 2017, by MrStonedOne
Modified table 'messages', adding a deleted column and editing all indexes to include it
ALTER TABLE `messages`
@@ -43,7 +77,7 @@ ADD INDEX `idx_msg_type_ckey_time_odr` (`type`,`targetckey`,`timestamp`, `delete
----------------------------------------------------
25 August 2017, by Jordie0608
Version 3.3, 25 August 2017, by Jordie0608
Modified tables 'connection_log', 'legacy_population', 'library', 'messages' and 'player' to add additional 'round_id' tracking in various forms and 'server_ip' and 'server_port' to the table 'messages'.
@@ -55,7 +89,7 @@ ALTER TABLE `player` ADD COLUMN `firstseen_round_id` INT(11) UNSIGNED NOT NULL A
----------------------------------------------------
18 August 2017, by Cyberboss and nfreader
Version 3.2, 18 August 2017, by Cyberboss and nfreader
Modified table 'death', adding the columns `last_words` and 'suicide'.
@@ -67,7 +101,7 @@ Remember to add a prefix to the table name if you use them.
----------------------------------------------------
20th July 2017, by Shadowlight213
Version 3.1, 20th July 2017, by Shadowlight213
Added role_time table to track time spent playing departments.
Also, added flags column to the player table.
@@ -79,7 +113,7 @@ Remember to add a prefix to the table name if you use them.
----------------------------------------------------
28 June 2017, by oranges
Version 3.0, 28 June 2017, by oranges
Added schema_revision to store the current db revision, why start at 3.0?
because:
@@ -319,7 +353,7 @@ Remember to add prefix to the table name if you use them.
Modified table 'memo', removing 'id' column and making 'ckey' primary.
ALTER TABLE `memo` DROP COLUMN `id`, DROP PRIMARY KEY, ADD PRIMARY KEY (`ckey`)
ALTER TABLE `memo` DROP COLUMN `id`, DROP PRIMARY KEY, ADD PRIMARY KEY (`ckey`)
Remember to add prefix to the table name if you use them.

View File

@@ -17,13 +17,9 @@ DROP TABLE IF EXISTS `admin`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `admin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ckey` varchar(32) NOT NULL,
`rank` varchar(32) NOT NULL DEFAULT 'Administrator',
`level` int(2) NOT NULL DEFAULT '0',
`flags` int(16) NOT NULL DEFAULT '0',
`email` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
`rank` varchar(32) NOT NULL,
PRIMARY KEY (`ckey`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
@@ -38,8 +34,9 @@ CREATE TABLE `admin_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`datetime` datetime NOT NULL,
`adminckey` varchar(32) NOT NULL,
`adminip` varchar(18) NOT NULL,
`log` text NOT NULL,
`adminip` int(10) unsigned NOT NULL,
`operation` enum('add admin','remove admin','change admin rank','add rank','remove rank','change rank flags') NOT NULL,
`log` varchar(1000) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
@@ -52,11 +49,12 @@ DROP TABLE IF EXISTS `admin_ranks`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `admin_ranks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`rank` varchar(40) NOT NULL,
`flags` int(16) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
`rank` varchar(32) NOT NULL,
`flags` smallint(5) unsigned NOT NULL,
`exclude_flags` smallint(5) unsigned NOT NULL,
`can_edit_flags` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`rank`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@@ -271,11 +269,11 @@ DROP TABLE IF EXISTS `role_time`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `role_time`
CREATE TABLE `role_time`
( `ckey` VARCHAR(32) NOT NULL ,
`job` VARCHAR(32) NOT NULL ,
`minutes` INT UNSIGNED NOT NULL,
PRIMARY KEY (`ckey`, `job`)
PRIMARY KEY (`ckey`, `job`)
) ENGINE = InnoDB;
--

View File

@@ -17,13 +17,9 @@ DROP TABLE IF EXISTS `SS13_admin`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `SS13_admin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ckey` varchar(32) NOT NULL,
`rank` varchar(32) NOT NULL DEFAULT 'Administrator',
`level` int(2) NOT NULL DEFAULT '0',
`flags` int(16) NOT NULL DEFAULT '0',
`email` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
`rank` varchar(32) NOT NULL,
PRIMARY KEY (`ckey`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
@@ -38,8 +34,9 @@ CREATE TABLE `SS13_admin_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`datetime` datetime NOT NULL,
`adminckey` varchar(32) NOT NULL,
`adminip` varchar(18) NOT NULL,
`log` text NOT NULL,
`adminip` int(10) unsigned NOT NULL,
`operation` enum('add admin','remove admin','change admin rank','add rank','remove rank','change rank flags') NOT NULL,
`log` varchar(1000) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
@@ -52,11 +49,12 @@ DROP TABLE IF EXISTS `SS13_admin_ranks`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `SS13_admin_ranks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`rank` varchar(40) NOT NULL,
`flags` int(16) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
`rank` varchar(32) NOT NULL,
`flags` smallint(5) unsigned NOT NULL,
`exclude_flags` smallint(5) unsigned NOT NULL,
`can_edit_flags` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`rank`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@@ -271,11 +269,11 @@ DROP TABLE IF EXISTS `SS13_role_time`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `SS13_role_time`
CREATE TABLE `SS13_role_time`
( `ckey` VARCHAR(32) NOT NULL ,
`job` VARCHAR(32) NOT NULL ,
`minutes` INT UNSIGNED NOT NULL,
PRIMARY KEY (`ckey`, `job`)
PRIMARY KEY (`ckey`, `job`)
) ENGINE = InnoDB;
--