Comparison

mod_groups_migration/mod_groups_migration.lua @ 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
child 4391:679a0c9d365d
comparison
equal deleted inserted replaced
4386:6357ac65b4eb 4387:e5b4ebacbf7a
1 local host = module.host;
2 local usermanager = require"core.usermanager";
3
4 local mod_groups = module:depends("groups_internal");
5 local default_group_id = module:get_option("group_default_id", "default");
6
7 local function trigger_migration()
8 if mod_groups.exists(default_group_id) then
9 module:log("debug", "skipping migration, group exists already")
10 return
11 end
12 module:log("info", "migrating to mod_groups!")
13
14 local group_id = default_group_id;
15 local ok, err = mod_groups.create({name="default"}, false, group_id);
16 if not ok then
17 module:log("error", "failed to create group: %s", err)
18 return
19 end
20
21 for user in usermanager.users(host) do
22 mod_groups.add_member(group_id, user, true);
23 module:log("debug", "added %s to %s", user, group_id)
24 end
25 module:log("debug", "synchronising group %s", group_id)
26 mod_groups.sync(group_id)
27 module:log("info", "added all users to group %s", group_id)
28 end
29
30 module:hook_global("server-started", trigger_migration, -100)