0920eb62951896bccace861a86127a36dfda8613
[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 datamanager = require "util.datamanager";
10 local log = require "util.logger".init("usermanager");
11 local type = type;
12 local error = error;
13 local ipairs = ipairs;
14 local hashes = require "util.hashes";
15 local jid_bare = require "util.jid".bare;
16 local config = require "core.configmanager";
17 local hosts = hosts;
18
19 local require_provisioning = config.get("*", "core", "cyrus_require_provisioning") or false;
20
21 local prosody = _G.prosody;
22
23 local setmetatable = setmetatable;
24
25 module "usermanager"
26
27 function new_null_provider()
28         local function dummy() end;
29         return setmetatable({name = "dummyauth"}, { __index = function() return dummy; end });
30 end
31
32 local function host_handler(host)
33         log("debug", "host_handler called with host '%s'", host);
34         local host_session = hosts[host];
35         host_session.events.add_handler("item-added/auth-provider", function (event)
36                 local provider = event.item;
37                 if provider == nil then
38                         log("debug", "auth provider is nil");
39                 else
40                         log("debug", "auth provider is not nil");
41                 end
42                 if provider.name == nil then
43                         log("debug", "authentication provider name is nil");
44                 else
45                         log("debug", "authentication provider name = '%s'", provider.name);
46                 end
47                 if config.get(host, "core", "authentication") == nil and provider.name == "default" then
48                         host_session.users = provider;
49                 elseif config.get(host, "core", "authentication") == provider.name then
50                         host_session.users = provider;
51                 end
52                 if host_session.users ~= nil and host_session.users.name ~= nil then
53                         log("debug", "host_session.users.name for host '%s' now '%s'", host, host_session.users.name);
54                 end
55         end);
56         host_session.events.add_handler("item-removed/auth-provider", function (event)
57                 local provider = event.item;
58                 if host_session.users == provider then
59                         host_session.users = new_null_provider();
60                 end
61         end);
62 end
63 prosody.events.add_handler("host-activated", host_handler, 100);
64 prosody.events.add_handler("component-activated", host_handler, 100);
65
66 function is_cyrus(host) return config.get(host, "core", "sasl_backend") == "cyrus"; end
67
68 function test_password(username, password, host)
69         return hosts[host].users.test_password(username, password);
70 end
71
72 function get_password(username, host)
73         return hosts[host].users.get_password(username);
74 end
75
76 function set_password(username, password, host)
77         return hosts[host].users.set_password(username, password);
78 end
79
80 function user_exists(username, host)
81         return hosts[host].users.user_exists(username);
82 end
83
84 function create_user(username, password, host)
85         return hosts[host].users.create_user(username, password);
86 end
87
88 function get_supported_methods(host)
89         return hosts[host].users.get_supported_methods();
90 end
91
92 function is_admin(jid, host)
93         if host and host ~= "*" then
94                 return hosts[host].users.is_admin(jid);
95         else -- Test only whether this JID is a global admin
96                 local admins = config.get("*", "core", "admins");
97                 if type(admins) == "table" then
98                         jid = jid_bare(jid);
99                         for _,admin in ipairs(admins) do
100                                 if admin == jid then return true; end
101                         end
102                 elseif admins then
103                         log("error", "Option 'admins' for host '%s' is not a table", host);
104                 end
105                 return nil;
106         end
107 end
108
109 return _M;