changeset 4387:e5b4ebacbf7a

mod_groups_migration: module helping migration to groups Creates a default group with well-known ID at startup if that group does not exist yet. Adds all users to it.
author Jonas Schäfer <jonas@wielicki.name>
date Mon, 25 Jan 2021 21:34:32 +0100 (2021-01-25)
parents 6357ac65b4eb
children 7de3c955cfe2
files mod_groups_migration/mod_groups_migration.lua
diffstat 1 files changed, 30 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_groups_migration/mod_groups_migration.lua	Mon Jan 25 21:34:32 2021 +0100
@@ -0,0 +1,30 @@
+local host = module.host;
+local usermanager = require"core.usermanager";
+
+local mod_groups = module:depends("groups_internal");
+local default_group_id = module:get_option("group_default_id", "default");
+
+local function trigger_migration()
+	if mod_groups.exists(default_group_id) then
+		module:log("debug", "skipping migration, group exists already")
+		return
+	end
+	module:log("info", "migrating to mod_groups!")
+
+	local group_id = default_group_id;
+	local ok, err = mod_groups.create({name="default"}, false, group_id);
+	if not ok then
+		module:log("error", "failed to create group: %s", err)
+		return
+	end
+
+	for user in usermanager.users(host) do
+		mod_groups.add_member(group_id, user, true);
+		module:log("debug", "added %s to %s", user, group_id)
+	end
+	module:log("debug", "synchronising group %s", group_id)
+	mod_groups.sync(group_id)
+	module:log("info", "added all users to group %s", group_id)
+end
+
+module:hook_global("server-started", trigger_migration, -100)