2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local modulemanager
= require
"core.modulemanager";
10 local log = require
"util.logger".init("usermanager");
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 sasl_new
= require
"util.sasl".new
;
17 local storagemanager
= require
"core.storagemanager";
19 local prosody
= _G
.prosody
;
20 local hosts
= prosody
.hosts
;
22 local setmetatable
= setmetatable
;
24 local default_provider
= "internal_plain";
29 local function new_null_provider()
30 local function dummy() return nil, "method not implemented"; end;
31 local function dummy_get_sasl_handler() return sasl_new(nil, {}); end
32 return setmetatable({name
= "null", get_sasl_handler
= dummy_get_sasl_handler
}, {
33 __index
= function(self
, method
) return dummy
; end --luacheck: ignore 212
37 local provider_mt
= { __index
= new_null_provider() };
39 local function initialize_host(host
)
40 local host_session
= hosts
[host
];
41 if host_session
.type ~= "local" then return; end
43 host_session
.events
.add_handler("item-added/auth-provider", function (event
)
44 local provider
= event
.item
;
45 local auth_provider
= config
.get(host
, "authentication") or default_provider
;
46 if config
.get(host
, "anonymous_login") then
47 log("error", "Deprecated config option 'anonymous_login'. Use authentication = 'anonymous' instead.");
48 auth_provider
= "anonymous";
50 if provider
.name
== auth_provider
then
51 host_session
.users
= setmetatable(provider
, provider_mt
);
53 if host_session
.users
~= nil and host_session
.users
.name
~= nil then
54 log("debug", "Host '%s' now set to use user provider '%s'", host
, host_session
.users
.name
);
57 host_session
.events
.add_handler("item-removed/auth-provider", function (event
)
58 local provider
= event
.item
;
59 if host_session
.users
== provider
then
60 host_session
.users
= new_null_provider();
63 host_session
.users
= new_null_provider(); -- Start with the default usermanager provider
64 local auth_provider
= config
.get(host
, "authentication") or default_provider
;
65 if config
.get(host
, "anonymous_login") then auth_provider
= "anonymous"; end -- COMPAT 0.7
66 if auth_provider
~= "null" then
67 modulemanager
.load(host
, "auth_"..auth_provider
);
70 prosody
.events
.add_handler("host-activated", initialize_host
, 100);
72 local function test_password(username
, host
, password
)
73 return hosts
[host
].users
.test_password(username
, password
);
76 local function get_password(username
, host
)
77 return hosts
[host
].users
.get_password(username
);
80 local function set_password(username
, password
, host
, resource
)
81 local ok
, err
= hosts
[host
].users
.set_password(username
, password
);
83 prosody
.events
.fire_event("user-password-changed", { username
= username
, host
= host
, resource
= resource
});
88 local function user_exists(username
, host
)
89 if hosts
[host
].sessions
[username
] then return true; end
90 return hosts
[host
].users
.user_exists(username
);
93 local function create_user(username
, password
, host
)
94 return hosts
[host
].users
.create_user(username
, password
);
97 local function delete_user(username
, host
)
98 local ok
, err
= hosts
[host
].users
.delete_user(username
);
99 if not ok
then return nil, err
; end
100 prosody
.events
.fire_event("user-deleted", { username
= username
, host
= host
});
101 return storagemanager
.purge(username
, host
);
104 local function users(host
)
105 return hosts
[host
].users
.users();
108 local function get_sasl_handler(host
, session
)
109 return hosts
[host
].users
.get_sasl_handler(session
);
112 local function get_provider(host
)
113 return hosts
[host
].users
;
116 local function is_admin(jid
, host
)
117 if host
and not hosts
[host
] then return false; end
118 if type(jid
) ~= "string" then return false; end
123 local host_admins
= config
.get(host
, "admins");
124 local global_admins
= config
.get("*", "admins");
126 if host_admins
and host_admins
~= global_admins
then
127 if type(host_admins
) == "table" then
128 for _
,admin
in ipairs(host_admins
) do
129 if jid_prep(admin
) == jid
then
133 elseif host_admins
then
134 log("error", "Option 'admins' for host '%s' is not a list", host
);
138 if global_admins
then
139 if type(global_admins
) == "table" then
140 for _
,admin
in ipairs(global_admins
) do
141 if jid_prep(admin
) == jid
then
145 elseif global_admins
then
146 log("error", "Global option 'admins' is not a list");
150 -- Still not an admin, check with auth provider
151 if host
~= "*" and hosts
[host
].users
and hosts
[host
].users
.is_admin
then
152 return hosts
[host
].users
.is_admin(jid
);
158 new_null_provider
= new_null_provider
;
159 initialize_host
= initialize_host
;
160 test_password
= test_password
;
161 get_password
= get_password
;
162 set_password
= set_password
;
163 user_exists
= user_exists
;
164 create_user
= create_user
;
165 delete_user
= delete_user
;
167 get_sasl_handler
= get_sasl_handler
;
168 get_provider
= get_provider
;