Comparison

plugins/muc/muc.lib.lua @ 9830:d935a0f0de24

MUC: Factor out role change permission check into its own method I would like to invert this logic so that it checks if the role change is allowed instead of checking if it is not allowed as it does now, in order to make it easier to understand.
author Kim Alvefur <zash@zash.se>
date Sun, 24 Feb 2019 16:18:30 +0100
parent 9820:87a1742f928d
child 9831:43f81e85cec2
comparison
equal deleted inserted replaced
9829:fdc42f685557 9830:d935a0f0de24
1366 function room_mt:get_role(nick) 1366 function room_mt:get_role(nick)
1367 local occupant = self:get_occupant_by_nick(nick); 1367 local occupant = self:get_occupant_by_nick(nick);
1368 return occupant and occupant.role or nil; 1368 return occupant and occupant.role or nil;
1369 end 1369 end
1370 1370
1371 function room_mt:may_set_role(actor, occupant, role)
1372 -- Can't do anything to other owners or admins
1373 local occupant_affiliation = self:get_affiliation(occupant.bare_jid);
1374 if occupant_affiliation == "owner" or occupant_affiliation == "admin" then
1375 return nil, "cancel", "not-allowed";
1376 end
1377
1378 -- If you are trying to give or take moderator role you need to be an owner or admin
1379 if occupant.role == "moderator" or role == "moderator" then
1380 local actor_affiliation = self:get_affiliation(actor);
1381 if actor_affiliation ~= "owner" and actor_affiliation ~= "admin" then
1382 return nil, "cancel", "not-allowed";
1383 end
1384 end
1385
1386 -- Need to be in the room and a moderator
1387 local actor_occupant = self:get_occupant_by_real_jid(actor);
1388 if not actor_occupant or actor_occupant.role ~= "moderator" then
1389 return nil, "cancel", "not-allowed";
1390 end
1391
1392 return true;
1393 end
1394
1371 function room_mt:set_role(actor, occupant_jid, role, reason) 1395 function room_mt:set_role(actor, occupant_jid, role, reason)
1372 if not actor then return nil, "modify", "not-acceptable"; end 1396 if not actor then return nil, "modify", "not-acceptable"; end
1373 1397
1374 local occupant = self:get_occupant_by_nick(occupant_jid); 1398 local occupant = self:get_occupant_by_nick(occupant_jid);
1375 if not occupant then return nil, "modify", "item-not-found"; end 1399 if not occupant then return nil, "modify", "item-not-found"; end
1380 role = role ~= "none" and role or nil; -- coerces `role == false` to `nil` 1404 role = role ~= "none" and role or nil; -- coerces `role == false` to `nil`
1381 1405
1382 if actor == true then 1406 if actor == true then
1383 actor = nil -- So we can pass it safely to 'publicise_occupant_status' below 1407 actor = nil -- So we can pass it safely to 'publicise_occupant_status' below
1384 else 1408 else
1385 -- Can't do anything to other owners or admins 1409 local allowed, err, condition = self:may_set_role(actor, occupant, role)
1386 local occupant_affiliation = self:get_affiliation(occupant.bare_jid); 1410 if not allowed then
1387 if occupant_affiliation == "owner" or occupant_affiliation == "admin" then 1411 return allowed, err, condition;
1388 return nil, "cancel", "not-allowed";
1389 end
1390
1391 -- If you are trying to give or take moderator role you need to be an owner or admin
1392 if occupant.role == "moderator" or role == "moderator" then
1393 local actor_affiliation = self:get_affiliation(actor);
1394 if actor_affiliation ~= "owner" and actor_affiliation ~= "admin" then
1395 return nil, "cancel", "not-allowed";
1396 end
1397 end
1398
1399 -- Need to be in the room and a moderator
1400 local actor_occupant = self:get_occupant_by_real_jid(actor);
1401 if not actor_occupant or actor_occupant.role ~= "moderator" then
1402 return nil, "cancel", "not-allowed";
1403 end 1412 end
1404 end 1413 end
1405 1414
1406 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"}); 1415 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"});
1407 if not role then 1416 if not role then