Comparison

util/pubsub.lua @ 9095:5639dc1a3f85

util.pubsub: Add initial support for configurable access models
author Kim Alvefur <zash@zash.se>
date Sat, 04 Aug 2018 03:38:20 +0200
parent 9075:46d4322f7eed
child 9098:d5bc306e93aa
comparison
equal deleted inserted replaced
9094:05979ae1e38a 9095:5639dc1a3f85
14 local default_config_mt = { __index = default_config }; 14 local default_config_mt = { __index = default_config };
15 15
16 local default_node_config = { 16 local default_node_config = {
17 ["persist_items"] = false; 17 ["persist_items"] = false;
18 ["max_items"] = 20; 18 ["max_items"] = 20;
19 ["access_model"] = "open";
19 }; 20 };
20 local default_node_config_mt = { __index = default_node_config }; 21 local default_node_config_mt = { __index = default_node_config };
21 22
22 -- Storage helper functions 23 -- Storage helper functions
23 24
80 81
81 local node_obj = self.nodes[node]; 82 local node_obj = self.nodes[node];
82 local node_aff = node_obj and (node_obj.affiliations[actor] 83 local node_aff = node_obj and (node_obj.affiliations[actor]
83 or node_obj.affiliations[self.config.normalize_jid(actor)]); 84 or node_obj.affiliations[self.config.normalize_jid(actor)]);
84 local service_aff = self.affiliations[actor] 85 local service_aff = self.affiliations[actor]
85 or self.config.get_affiliation(actor, node, action) 86 or self.config.get_affiliation(actor, node, action);
86 or "none"; 87 local default_aff = self:get_default_affiliation(node, actor) or "none";
87 88
88 -- Check if node allows/forbids it 89 -- Check if node allows/forbids it
89 local node_capabilities = node_obj and node_obj.capabilities; 90 local node_capabilities = node_obj and node_obj.capabilities;
90 if node_capabilities then 91 if node_capabilities then
91 local caps = node_capabilities[node_aff or service_aff]; 92 local caps = node_capabilities[node_aff or service_aff or default_aff];
92 if caps then 93 if caps then
93 local can = caps[action]; 94 local can = caps[action];
94 if can ~= nil then 95 if can ~= nil then
95 return can; 96 return can;
96 end 97 end
97 end 98 end
98 end 99 end
99 100
100 -- Check service-wide capabilities instead 101 -- Check service-wide capabilities instead
101 local service_capabilities = self.config.capabilities; 102 local service_capabilities = self.config.capabilities;
102 local caps = service_capabilities[node_aff or service_aff]; 103 local caps = service_capabilities[node_aff or service_aff or default_aff];
103 if caps then 104 if caps then
104 local can = caps[action]; 105 local can = caps[action];
105 if can ~= nil then 106 if can ~= nil then
106 return can; 107 return can;
107 end 108 end
108 end 109 end
109 110
110 return false; 111 return false;
112 end
113
114 function service:get_default_affiliation(node, actor, action) -- luacheck: ignore 212
115 local node_obj = self.nodes[node];
116 local access_model = node_obj and node_obj.config.access_model
117 or self.config.node_defaults.access_model;
118
119 if access_model == "open" then
120 return "subscriber";
121 elseif access_model == "whitelist" then
122 return "none";
123 end
111 end 124 end
112 125
113 function service:set_affiliation(node, actor, jid, affiliation) 126 function service:set_affiliation(node, actor, jid, affiliation)
114 -- Access checking 127 -- Access checking
115 if not self:may(node, actor, "set_affiliation") then 128 if not self:may(node, actor, "set_affiliation") then