Software /
code /
prosody
Annotate
spec/util_array_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 | 11787:3ae6fa901a8b |
child | 13245:ffe4adbd2af9 |
rev | line source |
---|---|
10100 | 1 local array = require "util.array"; |
2 describe("util.array", function () | |
3 describe("creation", function () | |
10397 | 4 describe("from table", function () |
10100 | 5 it("works", function () |
6 local a = array({"a", "b", "c"}); | |
7 assert.same({"a", "b", "c"}, a); | |
8 end); | |
9 end); | |
10 | |
11 describe("from iterator", function () | |
12 it("works", function () | |
13 -- collects the first value, ie the keys | |
14 local a = array(ipairs({true, true, true})); | |
15 assert.same({1, 2, 3}, a); | |
16 end); | |
17 end); | |
18 | |
19 describe("collect", function () | |
20 it("works", function () | |
21 -- collects the first value, ie the keys | |
22 local a = array.collect(ipairs({true, true, true})); | |
23 assert.same({1, 2, 3}, a); | |
24 end); | |
25 end); | |
26 | |
27 end); | |
28 | |
29 describe("metatable", function () | |
30 describe("operator", function () | |
31 describe("addition", function () | |
32 it("works", function () | |
33 local a = array({ "a", "b" }); | |
34 local b = array({ "c", "d" }); | |
35 assert.same({"a", "b", "c", "d"}, a + b); | |
36 end); | |
37 end); | |
38 | |
39 describe("equality", function () | |
40 it("works", function () | |
41 local a1 = array({ "a", "b" }); | |
42 local a2 = array({ "a", "b" }); | |
43 local b = array({ "c", "d" }); | |
44 assert.truthy(a1 == a2); | |
45 assert.falsy(a1 == b); | |
10590
257dc26e8e65
util.array: Add a test case for a behavior change in Lua 5.3
Kim Alvefur <zash@zash.se>
parents:
10397
diff
changeset
|
46 assert.falsy(a1 == { "a", "b" }, "Behavior of metatables changed in Lua 5.3"); |
10100 | 47 end); |
48 end); | |
49 | |
50 describe("division", function () | |
51 it("works", function () | |
52 local a = array({ "a", "b", "c" }); | |
53 local b = a / function (i) if i ~= "b" then return i .. "x" end end; | |
54 assert.same({ "ax", "cx" }, b); | |
55 end); | |
56 end); | |
57 | |
58 end); | |
59 end); | |
60 | |
61 describe("methods", function () | |
62 describe("map", function () | |
63 it("works", function () | |
64 local a = array({ "a", "b", "c" }); | |
65 local b = a:map(string.upper); | |
66 assert.same({ "A", "B", "C" }, b); | |
67 end); | |
68 end); | |
69 | |
70 describe("filter", function () | |
71 it("works", function () | |
72 local a = array({ "a", "b", "c" }); | |
73 a:filter(function (i) return i ~= "b" end); | |
74 assert.same({ "a", "c" }, a); | |
75 end); | |
76 end); | |
77 | |
78 describe("sort", function () | |
79 it("works", function () | |
80 local a = array({ 5, 4, 3, 1, 2, }); | |
81 a:sort(); | |
82 assert.same({ 1, 2, 3, 4, 5, }, a); | |
83 end); | |
84 end); | |
85 | |
86 describe("unique", function () | |
87 it("works", function () | |
88 local a = array({ "a", "b", "c", "c", "a", "b" }); | |
89 a:unique(); | |
90 assert.same({ "a", "b", "c" }, a); | |
91 end); | |
92 end); | |
93 | |
94 describe("pluck", function () | |
95 it("works", function () | |
96 local a = array({ { a = 1, b = -1 }, { a = 2, b = -2 }, }); | |
97 a:pluck("a"); | |
98 assert.same({ 1, 2 }, a); | |
99 end); | |
100 end); | |
101 | |
102 | |
103 describe("reverse", function () | |
104 it("works", function () | |
105 local a = array({ "a", "b", "c" }); | |
106 a:reverse(); | |
107 assert.same({ "c", "b", "a" }, a); | |
108 end); | |
109 end); | |
110 | |
111 -- TODO :shuffle | |
112 | |
113 describe("append", function () | |
114 it("works", function () | |
115 local a = array({ "a", "b", "c" }); | |
116 a:append(array({ "d", "e", })); | |
117 assert.same({ "a", "b", "c", "d", "e" }, a); | |
118 end); | |
119 end); | |
120 | |
121 describe("push", function () | |
122 it("works", function () | |
123 local a = array({ "a", "b", "c" }); | |
124 a:push("d"):push("e"); | |
125 assert.same({ "a", "b", "c", "d", "e" }, a); | |
126 end); | |
127 end); | |
128 | |
129 describe("pop", function () | |
130 it("works", function () | |
131 local a = array({ "a", "b", "c" }); | |
132 assert.equal("c", a:pop()); | |
133 assert.same({ "a", "b", }, a); | |
134 end); | |
135 end); | |
136 | |
137 describe("concat", function () | |
138 it("works", function () | |
139 local a = array({ "a", "b", "c" }); | |
140 assert.equal("a,b,c", a:concat(",")); | |
141 end); | |
142 end); | |
143 | |
144 describe("length", function () | |
145 it("works", function () | |
146 local a = array({ "a", "b", "c" }); | |
147 assert.equal(3, a:length()); | |
148 end); | |
149 end); | |
150 | |
11787
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
151 describe("slice", function () |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
152 it("works", function () |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
153 local a = array({ "a", "b", "c" }); |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
154 assert.equal(array.slice(a, 1, 2), array{ "a", "b" }); |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
155 assert.equal(array.slice(a, 1, 3), array{ "a", "b", "c" }); |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
156 assert.equal(array.slice(a, 2, 3), array{ "b", "c" }); |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
157 assert.equal(array.slice(a, 2), array{ "b", "c" }); |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
158 assert.equal(array.slice(a, -4), array{ "a", "b", "c" }); |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
159 assert.equal(array.slice(a, -3), array{ "a", "b", "c" }); |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
160 assert.equal(array.slice(a, -2), array{ "b", "c" }); |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
161 assert.equal(array.slice(a, -1), array{ "c" }); |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
162 end); |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
163 |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
164 it("can mutate", function () |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
165 local a = array({ "a", "b", "c" }); |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
166 assert.equal(a:slice(-1), array{"c"}); |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
167 assert.equal(a, array{"c"}); |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
168 end); |
3ae6fa901a8b
util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents:
10590
diff
changeset
|
169 end); |
10100 | 170 end); |
171 | |
172 -- TODO The various array.foo(array ina, array outa) functions | |
173 end); | |
174 |