util.stanza: Change get_error() to return nil rather than '' for no text
[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 config = require "core.configmanager";
15 local hosts = hosts;
16 local sasl_new = require "util.sasl".new;
17
18 local prosody = _G.prosody;
19
20 local setmetatable = setmetatable;
21
22 local default_provider = "internal_plain";
23
24 module "usermanager"
25
26 function new_null_provider()
27         local function dummy() end;
28         local function dummy_get_sasl_handler() return sasl_new(nil, {}); end
29         return setmetatable({name = "null", get_sasl_handler = dummy_get_sasl_handler}, { __index = function() return dummy; end });
30 end
31
32 function initialize_host(host)
33         local host_session = hosts[host];
34         if host_session.type ~= "local" then return; end
35         
36         host_session.events.add_handler("item-added/auth-provider", function (event)
37                 local provider = event.item;
38                 local auth_provider = config.get(host, "core", "authentication") or default_provider;
39                 if provider.name == auth_provider then
40                         host_session.users = provider;
41                 end
42                 if host_session.users ~= nil and host_session.users.name ~= nil then
43                         log("debug", "host '%s' now set to use user provider '%s'", host, host_session.users.name);
44                 end
45         end);
46         host_session.events.add_handler("item-removed/auth-provider", function (event)
47                 local provider = event.item;
48                 if host_session.users == provider then
49                         host_session.users = new_null_provider();
50                 end
51         end);
52         host_session.users = new_null_provider(); -- Start with the default usermanager provider
53         local auth_provider = config.get(host, "core", "authentication") or default_provider;
54         if auth_provider ~= "null" then
55                 modulemanager.load(host, "auth_"..auth_provider);
56         end
57 end;
58 prosody.events.add_handler("host-activated", initialize_host, 100);
59
60 function test_password(username, host, password)
61         return hosts[host].users.test_password(username, password);
62 end
63
64 function get_password(username, host)
65         return hosts[host].users.get_password(username);
66 end
67
68 function set_password(username, password, host)
69         return hosts[host].users.set_password(username, password);
70 end
71
72 function user_exists(username, host)
73         return hosts[host].users.user_exists(username);
74 end
75
76 function create_user(username, password, host)
77         return hosts[host].users.create_user(username, password);
78 end
79
80 function get_sasl_handler(host)
81         return hosts[host].users.get_sasl_handler();
82 end
83
84 function get_provider(host)
85         return hosts[host].users;
86 end
87
88 function is_admin(jid, host)
89         local is_admin;
90         jid = jid_bare(jid);
91         host = host or "*";
92         
93         local host_admins = config.get(host, "core", "admins");
94         local global_admins = config.get("*", "core", "admins");
95         
96         if host_admins and host_admins ~= global_admins then
97                 if type(host_admins) == "table" then
98                         for _,admin in ipairs(host_admins) do
99                                 if admin == jid then
100                                         is_admin = true;
101                                         break;
102                                 end
103                         end
104                 elseif host_admins then
105                         log("error", "Option 'admins' for host '%s' is not a list", host);
106                 end
107         end
108         
109         if not is_admin and global_admins then
110                 if type(global_admins) == "table" then
111                         for _,admin in ipairs(global_admins) do
112                                 if admin == jid then
113                                         is_admin = true;
114                                         break;
115                                 end
116                         end
117                 elseif global_admins then
118                         log("error", "Global option 'admins' is not a list");
119                 end
120         end
121         
122         -- Still not an admin, check with auth provider
123         if not is_admin and host ~= "*" and hosts[host].users.is_admin then
124                 is_admin = hosts[host].users.is_admin(jid);
125         end
126         return is_admin or false;
127 end
128
129 return _M;