Comparison

plugins/mod_admin_shell.lua @ 13684:026a75a443de 13.0

mod_admin_shell: Hide secondary role commands, focus on primary roles Secondary roles are an advanced feature without any strong use cases currently. Having multiple ways to manage roles is confusing. Now the 'user:role' command will just show the primary role if that is all there is, but will list secondary roles too if there are any (which in 99.9% of cases there won't be).
author Matthew Wild <mwild1@gmail.com>
date Thu, 13 Feb 2025 16:18:59 +0000
parent 13683:4c1f26b4883b
child 13685:b9fce1651699
comparison
equal deleted inserted replaced
13683:4c1f26b4883b 13684:026a75a443de
360 args = array.collect(args:gmatch("[%w_]+")):map(function (arg_name) 360 args = array.collect(args:gmatch("[%w_]+")):map(function (arg_name)
361 return { name = arg_name }; 361 return { name = arg_name };
362 end); 362 end);
363 hidden = hidden; 363 hidden = hidden;
364 }; 364 };
365 end
366
367 local function hidden_command(s)
368 return describe_command(s, true);
365 end 369 end
366 370
367 -- Console commands -- 371 -- Console commands --
368 -- These are simple commands, not valid standalone in Lua 372 -- These are simple commands, not valid standalone in Lua
369 373
1801 return promise.reject("Could not change password for user: "..err); 1805 return promise.reject("Could not change password for user: "..err);
1802 end 1806 end
1803 end); 1807 end);
1804 end 1808 end
1805 1809
1806 describe_command [[user:roles(jid, host) - Show current roles for an user]] 1810 describe_command [[user:role(jid, host) - Show primary role for a user]]
1807 function def_env.user:role(jid, host) 1811 function def_env.user:role(jid, host)
1808 local print = self.session.print;
1809 local username, userhost = jid_split(jid); 1812 local username, userhost = jid_split(jid);
1810 if host == nil then host = userhost; end 1813 if host == nil then host = userhost; end
1811 if not prosody.hosts[host] then 1814 if not prosody.hosts[host] then
1812 return nil, "No such host: "..host; 1815 return nil, "No such host: "..host;
1813 elseif prosody.hosts[userhost] and not um.user_exists(username, userhost) then 1816 elseif prosody.hosts[userhost] and not um.user_exists(username, userhost) then
1815 end 1818 end
1816 1819
1817 local primary_role = um.get_user_role(username, host); 1820 local primary_role = um.get_user_role(username, host);
1818 local secondary_roles = um.get_user_secondary_roles(username, host); 1821 local secondary_roles = um.get_user_secondary_roles(username, host);
1819 1822
1823 local primary_role_desc = primary_role and primary_role.name or "<none>";
1824
1825 local secondary_roles = um.get_user_secondary_roles(username, host);
1826
1820 print(primary_role and primary_role.name or "<none>"); 1827 print(primary_role and primary_role.name or "<none>");
1821 1828
1822 local count = primary_role and 1 or 0; 1829 local n_secondary = 0;
1823 for role_name in pairs(secondary_roles or {}) do 1830 for role_name in pairs(secondary_roles or {}) do
1824 count = count + 1; 1831 n_secondary = n_secondary + 1;
1825 print(role_name.." (secondary)"); 1832 print(role_name.." (secondary)");
1826 end 1833 end
1827 1834
1828 return true, count == 1 and "1 role" or count.." roles"; 1835 if n_secondary > 0 then
1836 return true, primary_role_desc.." (primary)";
1837 end
1838 return true, primary_role_desc;
1829 end 1839 end
1830 def_env.user.roles = def_env.user.role; 1840 def_env.user.roles = def_env.user.role;
1831 1841
1832 describe_command [[user:setrole(jid, host, role) - Set primary role of a user (see 'help roles')]] 1842 describe_command [[user:setrole(jid, host, role) - Set primary role of a user (see 'help roles')]]
1833 -- user:setrole("someone@example.com", "example.com", "prosody:admin") 1843 -- user:setrole("someone@example.com", "example.com", "prosody:admin")
1845 else 1855 else
1846 return um.set_jid_role(jid, host, new_role); 1856 return um.set_jid_role(jid, host, new_role);
1847 end 1857 end
1848 end 1858 end
1849 1859
1850 describe_command [[user:addrole(jid, host, role) - Add a secondary role to a user]] 1860 hidden_command [[user:addrole(jid, host, role) - Add a secondary role to a user]]
1851 function def_env.user:addrole(jid, host, new_role) 1861 function def_env.user:addrole(jid, host, new_role)
1852 local username, userhost = jid_split(jid); 1862 local username, userhost = jid_split(jid);
1853 if new_role == nil then host, new_role = userhost, host; end 1863 if new_role == nil then host, new_role = userhost, host; end
1854 if not prosody.hosts[host] then 1864 if not prosody.hosts[host] then
1855 return nil, "No such host: "..host; 1865 return nil, "No such host: "..host;
1863 return nil, err; 1873 return nil, err;
1864 end 1874 end
1865 return true, "Role added"; 1875 return true, "Role added";
1866 end 1876 end
1867 1877
1868 describe_command [[user:delrole(jid, host, role) - Remove a secondary role from a user]] 1878 hidden_command [[user:delrole(jid, host, role) - Remove a secondary role from a user]]
1869 function def_env.user:delrole(jid, host, role_name) 1879 function def_env.user:delrole(jid, host, role_name)
1870 local username, userhost = jid_split(jid); 1880 local username, userhost = jid_split(jid);
1871 if role_name == nil then host, role_name = userhost, host; end 1881 if role_name == nil then host, role_name = userhost, host; end
1872 if not prosody.hosts[host] then 1882 if not prosody.hosts[host] then
1873 return nil, "No such host: "..host; 1883 return nil, "No such host: "..host;