Skip to content

Commit 6e3fed3

Browse files
committed
Simplified/shrunk configuration output, support loading explicitly defined user rights.
1 parent e622fc5 commit 6e3fed3

File tree

3 files changed

+94
-10
lines changed

3 files changed

+94
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@author.io/iam",
3-
"version": "1.0.0-alpha.4",
3+
"version": "1.0.0-alpha.5",
44
"description": "A Identification and Authorization Management library.",
55
"main": "src/index.js",
66
"module": "index.js",

src/lib/actors/user.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ export default class User extends Trace {
6767
})
6868

6969
if (this.#explicitResourceRights.size > 0) {
70-
result.assignedRights = {}
70+
result.rights = {}
7171
for (const [resource, rights] of this.#explicitResourceRights.entries()) {
72-
result.assignedRights[resource] = rights.map(r => r.data)
72+
result.rights[resource] = rights.map(r => r.data)
7373
}
7474
}
7575

src/lib/registry.js

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,91 @@ class Registry extends Base {
6060
}
6161

6262
get configuration () {
63+
function serializeRights (rights) {
64+
return rights.map(r => {
65+
return {
66+
name: r.right === 'all' ? '*' : r.right.replace(':all', ':*'),
67+
description: r.description
68+
}
69+
})
70+
}
71+
72+
const roles = this.#roles.data.roles.map(r => {
73+
const rights = {}
74+
Object.keys(r.rights).forEach(name => { rights[name] = serializeRights(r.rights[name]).map(rt => rt.name) })
75+
const result = {
76+
name: r.name,
77+
rights
78+
}
79+
80+
if (r.description.trim().length > 0) {
81+
result.description = r.description
82+
}
83+
84+
return result
85+
})
86+
6387
const result = Object.assign({}, super.data, {
64-
resources: this.#resources.data.resources,
65-
roles: this.#roles.data.roles,
66-
groups: this.#groups.data.groups,
67-
users: this.#users.data.users
88+
resources: this.#resources.data.resources.map(r => {
89+
delete r.type
90+
r.rights = serializeRights(r.rights)
91+
92+
if (!r.description || r.description.trim().length === 0) {
93+
delete r.description
94+
}
95+
96+
return r
97+
}),
98+
roles,
99+
groups: this.#groups.data.groups.map(g => {
100+
delete g.memberOf
101+
delete g.type
102+
103+
if (g.description.trim().length === 0) {
104+
delete g.description
105+
}
106+
107+
if (g.members.length === 0) {
108+
delete g.members
109+
} else {
110+
g.members = g.members.map(m => { return { name: m.name, type: m.type } })
111+
}
112+
113+
g.roles = g.roles.map(r => r.name)
114+
if (g.roles.length === 0) {
115+
delete g.roles
116+
}
117+
118+
return g
119+
}),
120+
users: this.#users.data.users.map(u => {
121+
delete u.type
122+
123+
if (u.description.trim().length === 0) {
124+
delete u.description
125+
}
126+
127+
if (u.roles.length === 0) {
128+
delete u.roles
129+
}
130+
131+
if (u.groups.length === 0) {
132+
delete u.groups
133+
}
134+
135+
if (u.rights) {
136+
const rights = {}
137+
Object.keys(u.rights).forEach(name => { rights[name] = serializeRights(u.rights[name]).map(rt => rt.name) })
138+
139+
u.rights = rights
140+
141+
if (Object.keys(u.rights).length === 0) {
142+
delete u.rights
143+
}
144+
}
145+
146+
return u
147+
})
68148
})
69149

70150
delete result.type
@@ -552,7 +632,7 @@ class Registry extends Base {
552632
if (cfg.resources) {
553633
for (const resource of cfg.resources) {
554634
const r = this.createResource(resource.name, resource.rights)
555-
r.description = resource.description
635+
r.description = resource.description || ''
556636
}
557637
}
558638

@@ -583,7 +663,7 @@ class Registry extends Base {
583663
}
584664

585665
if (Array.isArray(group.roles)) {
586-
group.roles.forEach(r => g.assign(r.name))
666+
group.roles.forEach(r => g.assign(r.name ? r.name : r))
587667
}
588668

589669
if (Array.isArray(group.members) && group.members.filter(m => m.type === 'group').length > 0) {
@@ -616,7 +696,11 @@ class Registry extends Base {
616696
}
617697

618698
if (Array.isArray(user.groups)) {
619-
user.groups.forEach(g => u.join(g))
699+
u.join(...user.groups)
700+
}
701+
702+
if (typeof user.rights === 'object') {
703+
u.setRight(user.rights)
620704
}
621705
}
622706
}

0 commit comments

Comments
 (0)