forked from mastepanoski/keycloak-authz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUMAResource.js
More file actions
134 lines (114 loc) · 2.73 KB
/
UMAResource.js
File metadata and controls
134 lines (114 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const UMAScope = require('./UMAScope');
class UMAResource {
constructor({
name,
_id = null,
uri = null,
type = null,
icon_uri = null,
owner = null,
scopes = []
}) {
if (!name) throw new Error('ResourceSetName is required');
this._name = name;
this._uri = uri;
this._type = type;
this._scopes = [];
this._iconUri = icon_uri;
this._owner = owner;
this._id = _id;
this.setScopes(scopes);
}
get name() {
return this._name;
}
setName(newName) {
if (!newName) throw new Error('ResourceSetName is required');
this._name = newName;
return this;
}
get uri() {
return this._uri;
}
setUri(newUri) {
if (!newUri) throw new Error('Uri is required');
this._uri = newUri;
return this;
}
get type() {
return this._type;
}
setType(newType) {
if (!newType) throw new Error('Type is required');
this._type = newType;
return this;
}
get scopes() {
return this._scopes;
}
setScopes(newScopes) {
newScopes = newScopes || [];
this._scopes = [];
newScopes.forEach(scope => {
if (typeof scope === 'string') this._scopes.push(new UMAScope({ name: scope }));
if (typeof scope === 'object') this._scopes.push(new UMAScope(scope));
});
return this;
}
addScope(scopeName) {
if (!scopeName) throw new Error('Scope name is required');
if (!this.hasScope(scopeName)) this._scopes.push(new UMAScope({ name: scopeName }));
return this;
}
hasScope(scopeName) {
return !!this._scopes.filter(scope => {
return scope.name === scopeName;
}).length;
}
get owner() {
return this._owner;
}
setOwner(newOwner) {
if (!newOwner) throw new Error('Owner is required');
this._owner = newOwner;
return this;
}
get id() {
return this._id;
}
setId(newId) {
if (!newId) throw new Error('Id is required');
this._id = newId;
return this;
}
get iconUri() {
return this._iconUri;
}
setIconUri(newUri) {
if (!newUri) throw new Error('IconUri is required');
this._iconUri = newUri;
return this;
}
serialize() {
let object = {
name: this.name
};
if (this.uri) object.uri = this.uri;
if (this.type) object.type = this.type;
if (this.iconUri) object.icon_uri = this.iconUri;
if (this.scopes) object.scopes = this._scopes.map(scope => scope.serialize());
if (this.owner) object.owner = this.owner;
if (this.id) object._id = this.id;
return object;
}
equal(object) {
return (
object.name === this.name &&
object.id === this.id &&
object.iconUri === this.iconUri &&
object.uri === this.uri &&
object.type === this.type
);
}
}
module.exports = UMAResource;