SQL Admins (SourceMod)/zh

From AlliedModders Wiki
Revision as of 05:05, 20 September 2019 by MisakaSora (talk | contribs) (half translated)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Language: English  • 中文


SourceMod已经支持通过SQL来加载管理admin,现在支持是数据库是MySQL和SQLite。如果你有大量管理员、你想通过外部工具管理或者你想在不同服务器中 共享管理员,使用数据库是非常有效的。

本文会介绍如何配置服务器来使用SQL管理admin。基础管理不需要SQL知识,但是使用这个功能,你必须意识到SourceMod并不会为完整的SQL控制提供简单的功能。如果你需要更好的功能,你需要使用三方工具或者自学SQL。

不过,先假设你会:

  • 能访问一个数据库,
  • 明白如何访问一个数据库;
  • 明白如何在数据库中执行指令或者脚本。

配置

MySQL

打开服务器的configs/databases.cfg文件,你应该会看到有一个叫"default"的节点。你可以使用这个,如果你想分开连接也可以创建一个新的"admins"节点。你必须填充driverhostdatabaseuserpass的值,如果有值不需要填,填个双引号即可(""),每个'srcds'的实例都需要重启,否则改动不生效。

例子:

	"default"
	{
		"driver"			"mysql"
		"host"				"数据库"
		"database"			"数据库名字"
		"user"				"账号"
		"pass"				"密码"
	}

SQLite

在你服务器的configs/databases.cfg文件中添加一个如下的节点:

	"admins"
	{
		"driver"			"sqlite"
		"database"			"admins-sqlite"
	}

你可以使用任意合法的文件名作为数据库的名字,这里用“admins-sqlite”是因为这是预置的数据库,configs/sql-init-scripts/sqlite目录下可以找到admins-sqlite文件。


数据库设置

自动

确保sql-admin-manager.smx已加载,然后在服务器控制台中执行sm_create_adm_tables。如果使用的是MySQL,你需要有数据库的CREATE权限。

手动

MySQL

如果你还没有在你的MySQL中创建数据库里,这样做。

找到SourceMod中的configs/sql-init-scripts/mysql/create_admins.sql脚本。然后通过数据库命令行或者类似phpMyAdmin的工具执行里面的内容,这个脚本会创建必要的表。

SQLite

SourceMod发布有一个预置的SQLite数据库文件,里面已经建好所有的表。如果你想手动创建数据库或者添加表,使用configs/sql-init-scripts/sqlite/create_admins.sql脚本的内容。

把二进制文件configs/sql-init-scripts/sqlite/admins-sqlite.sq3拷贝至你服务器的data/sqlite目录下,如果你在databases.cfg中重命名了数据库,那么你应该也同时重命名这个文件。

启用插件

所有的SQL插件都在plugins/disabled目录下——默认不加载。要启用插件,只需要把他们从plugins/disabled目录移动到plugins目录下。

你会比较想启用sql-admin-manager.smx插件,因为它提供了一些非常有用的控制台指令来进行管理。但是,另外两个插件,你同时只能启用其中一个: However, you should only enable ONE of the other two SQL plugins:

  1. admin-sql-prefetch.smx: 这个插件是中小型低延迟数据库的理想选择,它在地图加载时加载权限(或者主动刷新时),如果数据库延迟很大,或者你希望增删改权限后不需要刷新每个服务器的缓存,那么这个插件不适合你。
    • 优点: 非常简单的插件,不复杂
    • 缺点: 当缓存刷新时服务器会有延迟(地图加载或者使用sm_reloadadmins)。如果你增删改权限表了,在缓存刷新前改动不会有体现。
  2. admin-sql-threaded.smx: 这个插件完全线程化了。因此它十分复杂,现在也是“实验”性质的(尽管测试运行良好)。它预加载所有的组权限和覆写信息,管理员查找也是动态完成的。
    • 优点:糟糕的数据库连接不会卡顿服务器。如果你增删改了管理员,变动会在玩家连接服务器时实时体现。
    • 缺点:代码非常复杂,不适合作自定义修改,同时它有一些副作用(比如,sm_reloadadmins指令会有轻微的延迟,因为插件是多线程的)

一旦你启用了管理插件,并且启用了admin-sql插件的其中一个,那么可以继续往下了。


更新表

自动

确保插件sql-admin-manager.smx已加载,然后在服务器控制台中执行sm_update_adm_tables指令,如果你使用的是MySQL,你必须就有数据库的CREATEALTER权限。

手动

如果你正在使用的是旧版SourceMod表结构,那么更新最新架构的表结构非常重要。

总结一下:

  • 先备份所有数据。
  • 要明确你的旧表是依据什么建立的,不幸的是,目前唯一的办法就是记住你当时使用的SourceMod版本。
  • 找到所有服务器上像update_admins-rXXXX这个格式的脚本,其中XXXX是修订的版本号,需要的是版本比第二步中的高,但比你现在使用的版本低或者相同的脚本。
  • 按照递增的修订号执行匹配的脚本(最低的先执行,最高的最后执行)。
  • 升级SQL插件,以及任何依赖于管理员表的第三方软件。

升级表结构的脚本在configs/sql-init-scripts/<driver>/update_admins-rXXXX,其中XXXX是修订版本号。

下面是一种可能的场景。(数据是编的,实际的修订版本号完全不同)。

你的表是在r2978版的SourceMod下建立的,现在的版本是3512,那么下面是可能存在的文件:

  • update_admins-r2500.sql
  • update_admins-r3243.sql
  • update_admins-r3444.sql

在这种情况下,你应当依次执行脚本32433444,来把你的表升级到最新版。所有的数据都会被保留,但是还是推荐你在作数据库变更时进行数据备份。

管理命令

出于方便考虑,SourceMod通过插件sql-admin-manager.smx提供了一些基础的数据库的管理员管理命令。所有的命令都需要root的管理flag。

下面是一些使用惯例:

  • 当<authtype>被请求时,它意味着下面三者之一的值,steam, ip, or name
  • 当<identity>被请求时,它是跟authtype匹配的唯一字符串。例如,steam代表Steam Id,ip代表IP地址,name代表半条命2中玩家的昵称。

注意: 如果字符串中有空格,那么它要用引号括起来!(译注:整个子串用引号括起来)

注意: 冒号是中断字符,所以Steam Id必须用引号括起来!

Command Format Description
sm_sql_addadmin <alias> <authtype> <identity> <flags> [immunity] [password] Adds a new entry to the sm_admins table. The alias can be any value and is usually used to assign a readable name to an IP/SteamID.
sm_sql_deladmin <authtype> <identity> Removes an admin.
sm_sql_addgroup <name> <flags> <immunity> Adds a new group with the specified flags and immunity
sm_sql_delgroup <name> Removes the specified group. Quotation marks are optional if the name has odd characters.
sm_sql_setadmingroups <authtype> <identity> [group1] ... [group N] Sets an admin's group list. The inheritance order is the order the groups are specified in. This sets, and does not add; thus specifying no groups removes the user from all groups. Example:

sm_sql_setadmingroups steam "STEAM_0:1:16" "Full Admins"

sm_create_adm_tables Creates the administration tables for SourceMod. Can only be run from the server console.
sm_update_adm_tables Updates the administration tables to the latest schema, preserving data as applicable. Can only be run from the server console.

Frequently Asked Questions

Q: Can I use admin-sql and admin-flatfile at the same time? A: Yes. The data will be merged together in SourceMod's cache.

Q: Can I use both the threaded and prefetch SQL plugins at the same time? A: No.

Q: Can duplicate groups/admins be in the flat files and the SQL database? A: Yup. They will be merged safely.


Schemas

This section documents the required portions of the admin table schema. You do not need to read or learn this unless you plan to use SQL directly.

The exact schemas for each driver are located in configs/sql-init-scripts. The purpose of this document is to explain the fields rather than list the exact structures.

sm_admins

This table is used to store administrators. Although the primary key is id, applications should enforce that authtype and identity have no combined duplicates.

Primary key: id (auto increments).

Field Type Purpose
id integer Unique integer identifying the row.
authtype string NOT NULL

Constrained to 'steam', 'name', or 'ip'

Authentication type the identity is against.
identity string NOT NULL Steam ID, name, or IP address.
password string Password, if any, the admin must use.
flags string NOT NULL Permission flag string.
name string NOT NULL Alias used for external tools (all but ignored in SourceMod).
immunity integer NOT NULL Immunity level value.

sm_groups

This table is used to store all groups entries. Although the primary key is id, applications should enforce that the name field stays unique.

Primary key: id (auto increments).

Field Type Purpose
id integer Unique integer identifying the row.
flags string NOT NULL Permissions flag string.
name string NOT NULL Unique name of the group.
immunity_level integer NOT NULL Immunity level value.

sm_admins_groups

This table is used to map admins to the groups they will inherit.

Primary key: admin_id, group_id

Field Type Purpose
admin_id integer Reference to the sm_admins.id field. Specifies the admin inheriting the group.
group_id integer Reference to the sm_groups.id field. Specifies the group the admin is inheriting.
inherit_order integer NOT NULL Order of inheritance for the given admin. Lower means earlier inheritance.

sm_group_immunity

This table is used to map which groups are immune from other groups.

Primary key: group_id, other_id

Field Type Purpose
group_id integer Reference to the sm_groups.id field. Specifies the group gaining immunity.
other_id integer Reference to the sm_groups.id field. Specifies who group_id is becoming immune from.


sm_group_overrides

This table is used to specify group-based command overrides.

Primary key: group_id, type, name

Field Type Purpose
group_id integer Reference to the sm_groups.id field. Specifies the group the override is for.
type string NOT NULL

Constrained to 'command' or 'group'.

Specifies whether the override is a command or a command group.
name string NOT NULL Command name.
access string NOT NULL

Constrained to 'allow' or 'deny'.

Whether the command is allowed or denied to this group.

sm_overrides

This table is used to specify global command overrides.

Primary key: type, name

Field Type Purpose
type string NOT NULL

Constrained to 'command' or 'group'.

Specifies whether the override is a command or a command group.
name string NOT NULL Command name.
flags string NOT NULL Permissions flag string.

sm_config

This table is used to specify configuration options.

Primary key: cfg_key.

Field Type Purpose
cfg_key string NOT NULL Configuration key.
cfg_value string NOT NULL Configuration value.

Note: As of this writing, the only configuration value is admin_version, and it specifies the revision the schema last changed.



友情翻译MisakaSora