Software /
code /
prosody
Annotate
spec/util_rsm_spec.lua @ 12642:9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
We began moving away from simple "is this user an admin?" permission checks
before 0.12, with the introduction of mod_authz_internal and the ability to
dynamically change the roles of individual users.
The approach in 0.12 still had various limitations however, and apart from
the introduction of roles other than "admin" and the ability to pull that info
from storage, not much actually changed.
This new framework shakes things up a lot, though aims to maintain the same
functionality and behaviour on the surface for a default Prosody
configuration. That is, if you don't take advantage of any of the new
features, you shouldn't notice any change.
The biggest change visible to developers is that usermanager.is_admin() (and
the auth provider is_admin() method) have been removed. Gone. Completely.
Permission checks should now be performed using a new module API method:
module:may(action_name, context)
This method accepts an action name, followed by either a JID (string) or
(preferably) a table containing 'origin'/'session' and 'stanza' fields (e.g.
the standard object passed to most events). It will return true if the action
should be permitted, or false/nil otherwise.
Modules should no longer perform permission checks based on the role name.
E.g. a lot of code previously checked if the user's role was prosody:admin
before permitting some action. Since many roles might now exist with similar
permissions, and the permissions of prosody:admin may be redefined
dynamically, it is no longer suitable to use this method for permission
checks. Use module:may().
If you start an action name with ':' (recommended) then the current module's
name will automatically be used as a prefix.
To define a new permission, use the new module API:
module:default_permission(role_name, action_name)
module:default_permissions(role_name, { action_name[, action_name...] })
This grants the specified role permission to execute the named action(s) by
default. This may be overridden via other mechanisms external to your module.
The built-in roles that developers should use are:
- prosody:user (normal user)
- prosody:admin (host admin)
- prosody:operator (global admin)
The new prosody:operator role is intended for server-wide actions (such as
shutting down Prosody).
Finally, all usage of is_admin() in modules has been fixed by this commit.
Some of these changes were trickier than others, but no change is expected to
break existing deployments.
EXCEPT: mod_auth_ldap no longer supports the ldap_admin_filter option. It's
very possible nobody is using this, but if someone is then we can later update
it to pull roles from LDAP somehow.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 15 Jun 2022 12:15:01 +0100 |
parent | 11427:83f5499d1f10 |
rev | line source |
---|---|
10760 | 1 local rsm = require "util.rsm"; |
2 local xml = require "util.xml"; | |
3 | |
4 local function strip(s) | |
5 return (s:gsub(">%s+<", "><")); | |
6 end | |
7 | |
8 describe("util.rsm", function () | |
9 describe("parse", function () | |
10 it("works", function () | |
11 local test = xml.parse(strip([[ | |
12 <set xmlns='http://jabber.org/protocol/rsm'> | |
13 <max>10</max> | |
14 </set> | |
15 ]])); | |
16 assert.same({ max = 10 }, rsm.parse(test)); | |
17 end); | |
18 | |
19 it("works", function () | |
20 local test = xml.parse(strip([[ | |
21 <set xmlns='http://jabber.org/protocol/rsm'> | |
22 <first index='0'>saint@example.org</first> | |
23 <last>peterpan@neverland.lit</last> | |
24 <count>800</count> | |
25 </set> | |
26 ]])); | |
27 assert.same({ first = { index = 0, "saint@example.org" }, last = "peterpan@neverland.lit", count = 800 }, rsm.parse(test)); | |
28 end); | |
29 | |
30 it("works", function () | |
31 local test = xml.parse(strip([[ | |
32 <set xmlns='http://jabber.org/protocol/rsm'> | |
33 <max>10</max> | |
34 <before>peter@pixyland.org</before> | |
35 </set> | |
36 ]])); | |
37 assert.same({ max = 10, before = "peter@pixyland.org" }, rsm.parse(test)); | |
38 end); | |
39 | |
11427
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
40 it("all fields works", function() |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
41 local test = assert(xml.parse(strip([[ |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
42 <set xmlns='http://jabber.org/protocol/rsm'> |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
43 <after>a</after> |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
44 <before>b</before> |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
45 <count>10</count> |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
46 <first index='1'>f</first> |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
47 <index>5</index> |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
48 <last>z</last> |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
49 <max>100</max> |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
50 </set> |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
51 ]]))); |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
52 assert.same({ |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
53 after = "a"; |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
54 before = "b"; |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
55 count = 10; |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
56 first = {index = 1; "f"}; |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
57 index = 5; |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
58 last = "z"; |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
59 max = 100; |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
60 }, rsm.parse(test)); |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
61 end); |
10760 | 62 end); |
63 | |
64 describe("generate", function () | |
65 it("works", function () | |
66 local test = xml.parse(strip([[ | |
67 <set xmlns='http://jabber.org/protocol/rsm'> | |
68 <max>10</max> | |
69 </set> | |
70 ]])); | |
71 local res = rsm.generate({ max = 10 }); | |
72 assert.same(test:get_child_text("max"), res:get_child_text("max")); | |
73 end); | |
74 | |
75 it("works", function () | |
76 local test = xml.parse(strip([[ | |
77 <set xmlns='http://jabber.org/protocol/rsm'> | |
78 <first index='0'>saint@example.org</first> | |
79 <last>peterpan@neverland.lit</last> | |
80 <count>800</count> | |
81 </set> | |
82 ]])); | |
83 local res = rsm.generate({ first = { index = 0, "saint@example.org" }, last = "peterpan@neverland.lit", count = 800 }); | |
84 assert.same(test:get_child("first").attr.index, res:get_child("first").attr.index); | |
85 assert.same(test:get_child_text("first"), res:get_child_text("first")); | |
86 assert.same(test:get_child_text("last"), res:get_child_text("last")); | |
87 assert.same(test:get_child_text("count"), res:get_child_text("count")); | |
88 end); | |
89 | |
90 it("works", function () | |
91 local test = xml.parse(strip([[ | |
92 <set xmlns='http://jabber.org/protocol/rsm'> | |
93 <max>10</max> | |
94 <before>peter@pixyland.org</before> | |
95 </set> | |
96 ]])); | |
97 local res = rsm.generate({ max = 10, before = "peter@pixyland.org" }); | |
98 assert.same(test:get_child_text("max"), res:get_child_text("max")); | |
99 assert.same(test:get_child_text("before"), res:get_child_text("before")); | |
100 end); | |
101 | |
10762
4fc224c97986
util.rsm: Test that Lua 5.3 floats are not encoded with decimal point
Kim Alvefur <zash@zash.se>
parents:
10760
diff
changeset
|
102 it("handles floats", function () |
4fc224c97986
util.rsm: Test that Lua 5.3 floats are not encoded with decimal point
Kim Alvefur <zash@zash.se>
parents:
10760
diff
changeset
|
103 local r1 = rsm.generate({ max = 10.0, count = 100.0, first = { index = 1.0, "foo" } }); |
4fc224c97986
util.rsm: Test that Lua 5.3 floats are not encoded with decimal point
Kim Alvefur <zash@zash.se>
parents:
10760
diff
changeset
|
104 assert.equal("10", r1:get_child_text("max")); |
4fc224c97986
util.rsm: Test that Lua 5.3 floats are not encoded with decimal point
Kim Alvefur <zash@zash.se>
parents:
10760
diff
changeset
|
105 assert.equal("100", r1:get_child_text("count")); |
4fc224c97986
util.rsm: Test that Lua 5.3 floats are not encoded with decimal point
Kim Alvefur <zash@zash.se>
parents:
10760
diff
changeset
|
106 assert.equal("1", r1:get_child("first").attr.index); |
4fc224c97986
util.rsm: Test that Lua 5.3 floats are not encoded with decimal point
Kim Alvefur <zash@zash.se>
parents:
10760
diff
changeset
|
107 end); |
4fc224c97986
util.rsm: Test that Lua 5.3 floats are not encoded with decimal point
Kim Alvefur <zash@zash.se>
parents:
10760
diff
changeset
|
108 |
11427
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
109 |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
110 it("all fields works", function () |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
111 local res = rsm.generate({ |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
112 after = "a"; |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
113 before = "b"; |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
114 count = 10; |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
115 first = {index = 1; "f"}; |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
116 index = 5; |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
117 last = "z"; |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
118 max = 100; |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
119 }); |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
120 assert.equal("a", res:get_child_text("after")); |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
121 assert.equal("b", res:get_child_text("before")); |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
122 assert.equal("10", res:get_child_text("count")); |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
123 assert.equal("f", res:get_child_text("first")); |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
124 assert.equal("1", res:get_child("first").attr.index); |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
125 assert.equal("5", res:get_child_text("index")); |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
126 assert.equal("z", res:get_child_text("last")); |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
127 assert.equal("100", res:get_child_text("max")); |
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
128 end); |
10760 | 129 end); |
11427
83f5499d1f10
util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents:
10762
diff
changeset
|
130 |
10760 | 131 end); |
132 |