util.datamanager: Add function for listing stores
[prosody.git] / core / usermanager.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local modulemanager = require "core.modulemanager";
10 local log = require "util.logger".init("usermanager");
11 local type = type;
12 local ipairs = ipairs;
13 local jid_bare = require "util.jid".bare;
14 local jid_prep = require "util.jid".prep;
15 local config = require "core.configmanager";
16 local hosts = hosts;
17 local sasl_new = require "util.sasl".new;
18
19 local prosody = _G.prosody;
20
21 local setmetatable = setmetatable;
22
23 local default_provider = "internal_plain";
24
25 module "usermanager"
26
27 function new_null_provider()
28         local function dummy() return nil, "method not implemented"; end;
29         local function dummy_get_sasl_handler() return sasl_new(nil, {}); end
30         return setmetatable({name = "null", get_sasl_handler = dummy_get_sasl_handler}, {
31                 __index = function(self, method) return dummy; end
32         });
33 end
34
35 local provider_mt = { __index = new_null_provider() };
36
37 function initialize_host(host)
38         local host_session = hosts[host];
39         if host_session.type ~= "local" then return; end
40         
41         host_session.events.add_handler("item-added/auth-provider", function (event)
42                 local provider = event.item;
43                 local auth_provider = config.get(host, "core", "authentication") or default_provider;
44                 if config.get(host, "core", "anonymous_login") then
45                         log("error", "Deprecated config option 'anonymous_login'. Use authentication = 'anonymous' instead.");
46                         auth_provider = "anonymous";
47                 end -- COMPAT 0.7
48                 if provider.name == auth_provider then
49                         host_session.users = setmetatable(provider, provider_mt);
50                 end
51                 if host_session.users ~= nil and host_session.users.name ~= nil then
52                         log("debug", "host '%s' now set to use user provider '%s'", host, host_session.users.name);
53                 end
54         end);
55         host_session.events.add_handler("item-removed/auth-provider", function (event)
56                 local provider = event.item;
57                 if host_session.users == provider then
58                         host_session.users = new_null_provider();
59                 end
60         end);
61         host_session.users = new_null_provider(); -- Start with the default usermanager provider
62         local auth_provider = config.get(host, "core", "authentication") or default_provider;
63         if config.get(host, "core", "anonymous_login") then auth_provider = "anonymous"; end -- COMPAT 0.7
64         if auth_provider ~= "null" then
65                 modulemanager.load(host, "auth_"..auth_provider);
66         end
67 end;
68 prosody.events.add_handler("host-activated", initialize_host, 100);
69
70 function test_password(username, host, password)
71         return hosts[host].users.test_password(username, password);
72 end
73
74 function get_password(username, host)
75         return hosts[host].users.get_password(username);
76 end
77
78 function set_password(username, password, host)
79         return hosts[host].users.set_password(username, password);
80 end
81
82 function user_exists(username, host)
83         return hosts[host].users.user_exists(username);
84 end
85
86 function create_user(username, password, host)
87         return hosts[host].users.create_user(username, password);
88 end
89
90 function delete_user(username, host)
91         return hosts[host].users.delete_user(username);
92 end
93
94 function get_sasl_handler(host, session)
95         return hosts[host].users.get_sasl_handler(session);
96 end
97
98 function get_provider(host)
99         return hosts[host].users;
100 end
101
102 function is_admin(jid, host)
103         if host and not hosts[host] then return false; end
104         if type(jid) ~= "string" then return false; end
105
106         local is_admin;
107         jid = jid_bare(jid);
108         host = host or "*";
109         
110         local host_admins = config.get(host, "core", "admins");
111         local global_admins = config.get("*", "core", "admins");
112         
113         if host_admins and host_admins ~= global_admins then
114                 if type(host_admins) == "table" then
115                         for _,admin in ipairs(host_admins) do
116                                 if jid_prep(admin) == jid then
117                                         is_admin = true;
118                                         break;
119                                 end
120                         end
121                 elseif host_admins then
122                         log("error", "Option 'admins' for host '%s' is not a list", host);
123                 end
124         end
125         
126         if not is_admin and global_admins then
127                 if type(global_admins) == "table" then
128                         for _,admin in ipairs(global_admins) do
129                                 if jid_prep(admin) == jid then
130                                         is_admin = true;
131                                         break;
132                                 end
133                         end
134                 elseif global_admins then
135                         log("error", "Global option 'admins' is not a list");
136                 end
137         end
138         
139         -- Still not an admin, check with auth provider
140         if not is_admin and host ~= "*" and hosts[host].users and hosts[host].users.is_admin then
141                 is_admin = hosts[host].users.is_admin(jid);
142         end
143         return is_admin or false;
144 end
145
146 return _M;