Software /
code /
prosody
Comparison
plugins/adhoc/mod_adhoc.lua @ 5762:785da1854eb9
mod_adhoc: Add local_user permission
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Wed, 24 Jul 2013 23:30:32 +0200 |
parent | 5761:91f8cd53584c |
child | 6841:be87ab2d611c |
comparison
equal
deleted
inserted
replaced
5761:91f8cd53584c | 5762:785da1854eb9 |
---|---|
7 | 7 |
8 local st = require "util.stanza"; | 8 local st = require "util.stanza"; |
9 local keys = require "util.iterators".keys; | 9 local keys = require "util.iterators".keys; |
10 local array_collect = require "util.array".collect; | 10 local array_collect = require "util.array".collect; |
11 local is_admin = require "core.usermanager".is_admin; | 11 local is_admin = require "core.usermanager".is_admin; |
12 local jid_split = require "util.jid".split; | |
12 local adhoc_handle_cmd = module:require "adhoc".handle_cmd; | 13 local adhoc_handle_cmd = module:require "adhoc".handle_cmd; |
13 local xmlns_cmd = "http://jabber.org/protocol/commands"; | 14 local xmlns_cmd = "http://jabber.org/protocol/commands"; |
14 local commands = {}; | 15 local commands = {}; |
15 | 16 |
16 module:add_feature(xmlns_cmd); | 17 module:add_feature(xmlns_cmd); |
17 | 18 |
18 module:hook("host-disco-info-node", function (event) | 19 module:hook("host-disco-info-node", function (event) |
19 local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node; | 20 local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node; |
20 if commands[node] then | 21 if commands[node] then |
21 local privileged = is_admin(stanza.attr.from, stanza.attr.to); | 22 local from = stanza.attr.from; |
22 local global_admin = is_admin(stanza.attr.from); | 23 local privileged = is_admin(from, stanza.attr.to); |
24 local global_admin = is_admin(from); | |
25 local username, hostname = jid_split(from); | |
23 local command = commands[node]; | 26 local command = commands[node]; |
24 if (command.permission == "admin" and privileged) | 27 if (command.permission == "admin" and privileged) |
25 or (command.permission == "global_admin" and global_admin) | 28 or (command.permission == "global_admin" and global_admin) |
29 or (command.permission == "local_user" and hostname == module.host) | |
26 or (command.permission == "user") then | 30 or (command.permission == "user") then |
27 reply:tag("identity", { name = command.name, | 31 reply:tag("identity", { name = command.name, |
28 category = "automation", type = "command-node" }):up(); | 32 category = "automation", type = "command-node" }):up(); |
29 reply:tag("feature", { var = xmlns_cmd }):up(); | 33 reply:tag("feature", { var = xmlns_cmd }):up(); |
30 reply:tag("feature", { var = "jabber:x:data" }):up(); | 34 reply:tag("feature", { var = "jabber:x:data" }):up(); |
43 local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node; | 47 local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node; |
44 if node ~= xmlns_cmd then | 48 if node ~= xmlns_cmd then |
45 return; | 49 return; |
46 end | 50 end |
47 | 51 |
48 local admin = is_admin(stanza.attr.from, stanza.attr.to); | 52 local from = stanza.attr.from; |
49 local global_admin = is_admin(stanza.attr.from); | 53 local admin = is_admin(from, stanza.attr.to); |
54 local global_admin = is_admin(from); | |
55 local username, hostname = jid_split(from); | |
50 local nodes = array_collect(keys(commands)):sort(); | 56 local nodes = array_collect(keys(commands)):sort(); |
51 for _, node in ipairs(nodes) do | 57 for _, node in ipairs(nodes) do |
52 local command = commands[node]; | 58 local command = commands[node]; |
53 if (command.permission == "admin" and admin) | 59 if (command.permission == "admin" and admin) |
54 or (command.permission == "global_admin" and global_admin) | 60 or (command.permission == "global_admin" and global_admin) |
61 or (command.permission == "local_user" and hostname == module.host) | |
55 or (command.permission == "user") then | 62 or (command.permission == "user") then |
56 reply:tag("item", { name = command.name, | 63 reply:tag("item", { name = command.name, |
57 node = node, jid = module:get_host() }); | 64 node = node, jid = module:get_host() }); |
58 reply:up(); | 65 reply:up(); |
59 end | 66 end |
63 | 70 |
64 module:hook("iq/host/"..xmlns_cmd..":command", function (event) | 71 module:hook("iq/host/"..xmlns_cmd..":command", function (event) |
65 local origin, stanza = event.origin, event.stanza; | 72 local origin, stanza = event.origin, event.stanza; |
66 if stanza.attr.type == "set" then | 73 if stanza.attr.type == "set" then |
67 local node = stanza.tags[1].attr.node | 74 local node = stanza.tags[1].attr.node |
68 if commands[node] then | 75 local command = commands[node]; |
69 local admin = is_admin(stanza.attr.from, stanza.attr.to); | 76 if command then |
70 local global_admin = is_admin(stanza.attr.from); | 77 local from = stanza.attr.from; |
71 if (commands[node].permission == "admin" and not admin) | 78 local admin = is_admin(from, stanza.attr.to); |
72 or (commands[node].permission == "global_admin" and not global_admin) then | 79 local global_admin = is_admin(from); |
80 local username, hostname = jid_split(from); | |
81 if (command.permission == "admin" and not admin) | |
82 or (command.permission == "global_admin" and not global_admin) | |
83 or (command.permission == "local_user" and hostname ~= module.host) then | |
73 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up() | 84 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up() |
74 :add_child(commands[node]:cmdtag("canceled") | 85 :add_child(commands[node]:cmdtag("canceled") |
75 :tag("note", {type="error"}):text("You don't have permission to execute this command"))); | 86 :tag("note", {type="error"}):text("You don't have permission to execute this command"))); |
76 return true | 87 return true |
77 end | 88 end |