forked from leancloud/javascript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacl.js
More file actions
265 lines (245 loc) · 8.24 KB
/
Copy pathacl.js
File metadata and controls
265 lines (245 loc) · 8.24 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* 每位工程师都有保持代码优雅的义务
* Each engineer has a duty to keep the code elegant
**/
'use strict';
var _ = require('underscore');
/*global navigator: false */
module.exports = function(AV) {
var PUBLIC_KEY = "*";
/**
* Creates a new ACL.
* If no argument is given, the ACL has no permissions for anyone.
* If the argument is a AV.User, the ACL will have read and write
* permission for only that user.
* If the argument is any other JSON object, that object will be interpretted
* as a serialized ACL created with toJSON().
* @see AV.Object#setACL
* @class
*
* <p>An ACL, or Access Control List can be added to any
* <code>AV.Object</code> to restrict access to only a subset of users
* of your application.</p>
*/
AV.ACL = function(arg1) {
var self = this;
self.permissionsById = {};
if (_.isObject(arg1)) {
if (arg1 instanceof AV.User) {
self.setReadAccess(arg1, true);
self.setWriteAccess(arg1, true);
} else {
if (_.isFunction(arg1)) {
throw "AV.ACL() called with a function. Did you forget ()?";
}
AV._objectEach(arg1, function(accessList, userId) {
if (!_.isString(userId)) {
throw "Tried to create an ACL with an invalid userId.";
}
self.permissionsById[userId] = {};
AV._objectEach(accessList, function(allowed, permission) {
if (permission !== "read" && permission !== "write") {
throw "Tried to create an ACL with an invalid permission type.";
}
if (!_.isBoolean(allowed)) {
throw "Tried to create an ACL with an invalid permission value.";
}
self.permissionsById[userId][permission] = allowed;
});
});
}
}
};
/**
* Returns a JSON-encoded version of the ACL.
* @return {Object}
*/
AV.ACL.prototype.toJSON = function() {
return _.clone(this.permissionsById);
};
AV.ACL.prototype._setAccess = function(accessType, userId, allowed) {
if (userId instanceof AV.User) {
userId = userId.id;
} else if (userId instanceof AV.Role) {
userId = "role:" + userId.getName();
}
if (!_.isString(userId)) {
throw "userId must be a string.";
}
if (!_.isBoolean(allowed)) {
throw "allowed must be either true or false.";
}
var permissions = this.permissionsById[userId];
if (!permissions) {
if (!allowed) {
// The user already doesn't have this permission, so no action needed.
return;
} else {
permissions = {};
this.permissionsById[userId] = permissions;
}
}
if (allowed) {
this.permissionsById[userId][accessType] = true;
} else {
delete permissions[accessType];
if (_.isEmpty(permissions)) {
delete permissions[userId];
}
}
};
AV.ACL.prototype._getAccess = function(accessType, userId) {
if (userId instanceof AV.User) {
userId = userId.id;
} else if (userId instanceof AV.Role) {
userId = "role:" + userId.getName();
}
var permissions = this.permissionsById[userId];
if (!permissions) {
return false;
}
return permissions[accessType] ? true : false;
};
/**
* Set whether the given user is allowed to read this object.
* @param userId An instance of AV.User or its objectId.
* @param {Boolean} allowed Whether that user should have read access.
*/
AV.ACL.prototype.setReadAccess = function(userId, allowed) {
this._setAccess("read", userId, allowed);
};
/**
* Get whether the given user id is *explicitly* allowed to read this object.
* Even if this returns false, the user may still be able to access it if
* getPublicReadAccess returns true or a role that the user belongs to has
* write access.
* @param userId An instance of AV.User or its objectId, or a AV.Role.
* @return {Boolean}
*/
AV.ACL.prototype.getReadAccess = function(userId) {
return this._getAccess("read", userId);
};
/**
* Set whether the given user id is allowed to write this object.
* @param userId An instance of AV.User or its objectId, or a AV.Role..
* @param {Boolean} allowed Whether that user should have write access.
*/
AV.ACL.prototype.setWriteAccess = function(userId, allowed) {
this._setAccess("write", userId, allowed);
};
/**
* Get whether the given user id is *explicitly* allowed to write this object.
* Even if this returns false, the user may still be able to write it if
* getPublicWriteAccess returns true or a role that the user belongs to has
* write access.
* @param userId An instance of AV.User or its objectId, or a AV.Role.
* @return {Boolean}
*/
AV.ACL.prototype.getWriteAccess = function(userId) {
return this._getAccess("write", userId);
};
/**
* Set whether the public is allowed to read this object.
* @param {Boolean} allowed
*/
AV.ACL.prototype.setPublicReadAccess = function(allowed) {
this.setReadAccess(PUBLIC_KEY, allowed);
};
/**
* Get whether the public is allowed to read this object.
* @return {Boolean}
*/
AV.ACL.prototype.getPublicReadAccess = function() {
return this.getReadAccess(PUBLIC_KEY);
};
/**
* Set whether the public is allowed to write this object.
* @param {Boolean} allowed
*/
AV.ACL.prototype.setPublicWriteAccess = function(allowed) {
this.setWriteAccess(PUBLIC_KEY, allowed);
};
/**
* Get whether the public is allowed to write this object.
* @return {Boolean}
*/
AV.ACL.prototype.getPublicWriteAccess = function() {
return this.getWriteAccess(PUBLIC_KEY);
};
/**
* Get whether users belonging to the given role are allowed
* to read this object. Even if this returns false, the role may
* still be able to write it if a parent role has read access.
*
* @param role The name of the role, or a AV.Role object.
* @return {Boolean} true if the role has read access. false otherwise.
* @throws {String} If role is neither a AV.Role nor a String.
*/
AV.ACL.prototype.getRoleReadAccess = function(role) {
if (role instanceof AV.Role) {
// Normalize to the String name
role = role.getName();
}
if (_.isString(role)) {
return this.getReadAccess("role:" + role);
}
throw "role must be a AV.Role or a String";
};
/**
* Get whether users belonging to the given role are allowed
* to write this object. Even if this returns false, the role may
* still be able to write it if a parent role has write access.
*
* @param role The name of the role, or a AV.Role object.
* @return {Boolean} true if the role has write access. false otherwise.
* @throws {String} If role is neither a AV.Role nor a String.
*/
AV.ACL.prototype.getRoleWriteAccess = function(role) {
if (role instanceof AV.Role) {
// Normalize to the String name
role = role.getName();
}
if (_.isString(role)) {
return this.getWriteAccess("role:" + role);
}
throw "role must be a AV.Role or a String";
};
/**
* Set whether users belonging to the given role are allowed
* to read this object.
*
* @param role The name of the role, or a AV.Role object.
* @param {Boolean} allowed Whether the given role can read this object.
* @throws {String} If role is neither a AV.Role nor a String.
*/
AV.ACL.prototype.setRoleReadAccess = function(role, allowed) {
if (role instanceof AV.Role) {
// Normalize to the String name
role = role.getName();
}
if (_.isString(role)) {
this.setReadAccess("role:" + role, allowed);
return;
}
throw "role must be a AV.Role or a String";
};
/**
* Set whether users belonging to the given role are allowed
* to write this object.
*
* @param role The name of the role, or a AV.Role object.
* @param {Boolean} allowed Whether the given role can write this object.
* @throws {String} If role is neither a AV.Role nor a String.
*/
AV.ACL.prototype.setRoleWriteAccess = function(role, allowed) {
if (role instanceof AV.Role) {
// Normalize to the String name
role = role.getName();
}
if (_.isString(role)) {
this.setWriteAccess("role:" + role, allowed);
return;
}
throw "role must be a AV.Role or a String";
};
};