forked from leancloud/javascript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrole.js
More file actions
141 lines (133 loc) · 4.64 KB
/
Copy pathrole.js
File metadata and controls
141 lines (133 loc) · 4.64 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
/**
* 每位工程师都有保持代码优雅的义务
* Each engineer has a duty to keep the code elegant
**/
const _ = require('underscore');
const AVError = require('./error');
module.exports = function(AV) {
/**
* Represents a Role on the AV server. Roles represent groupings of
* Users for the purposes of granting permissions (e.g. specifying an ACL
* for an Object). Roles are specified by their sets of child users and
* child roles, all of which are granted any permissions that the parent
* role has.
*
* <p>Roles must have a name (which cannot be changed after creation of the
* role), and must specify an ACL.</p>
* @class
* A AV.Role is a local representation of a role persisted to the AV
* cloud.
*/
AV.Role = AV.Object.extend("_Role", /** @lends AV.Role.prototype */ {
// Instance Methods
/**
* Constructs a new AVRole with the given name and ACL.
*
* @param {String} name The name of the Role to create.
* @param {AV.ACL} [acl] The ACL for this role. if absent, the default ACL
* `{'*': { read: true }}` will be used.
*/
constructor: function(name, acl) {
if (_.isString(name)) {
AV.Object.prototype.constructor.call(this, null, null);
this.setName(name);
} else {
AV.Object.prototype.constructor.call(this, name, acl);
}
if (acl === undefined) {
var defaultAcl = new AV.ACL();
defaultAcl.setPublicReadAccess(true);
if(!this.getACL()) {
this.setACL(defaultAcl);
}
} else if (!(acl instanceof AV.ACL)) {
throw new TypeError('acl must be an instance of AV.ACL');
} else {
this.setACL(acl);
}
},
/**
* Gets the name of the role. You can alternatively call role.get("name")
*
* @return {String} the name of the role.
*/
getName: function() {
return this.get("name");
},
/**
* Sets the name for a role. This value must be set before the role has
* been saved to the server, and cannot be set once the role has been
* saved.
*
* <p>
* A role's name can only contain alphanumeric characters, _, -, and
* spaces.
* </p>
*
* <p>This is equivalent to calling role.set("name", name)</p>
*
* @param {String} name The name of the role.
* @param {Object} options Standard options object with success and error
* callbacks.
*/
setName: function(name, options) {
return this.set("name", name, options);
},
/**
* Gets the AV.Relation for the AV.Users that are direct
* children of this role. These users are granted any privileges that this
* role has been granted (e.g. read or write access through ACLs). You can
* add or remove users from the role through this relation.
*
* <p>This is equivalent to calling role.relation("users")</p>
*
* @return {AV.Relation} the relation for the users belonging to this
* role.
*/
getUsers: function() {
return this.relation("users");
},
/**
* Gets the AV.Relation for the AV.Roles that are direct
* children of this role. These roles' users are granted any privileges that
* this role has been granted (e.g. read or write access through ACLs). You
* can add or remove child roles from this role through this relation.
*
* <p>This is equivalent to calling role.relation("roles")</p>
*
* @return {AV.Relation} the relation for the roles belonging to this
* role.
*/
getRoles: function() {
return this.relation("roles");
},
/**
* @ignore
*/
validate: function(attrs, options) {
if ("name" in attrs && attrs.name !== this.getName()) {
var newName = attrs.name;
if (this.id && this.id !== attrs.objectId) {
// Check to see if the objectId being set matches this.id.
// This happens during a fetch -- the id is set before calling fetch.
// Let the name be set in this case.
return new AVError(AVError.OTHER_CAUSE,
"A role's name can only be set before it has been saved.");
}
if (!_.isString(newName)) {
return new AVError(AVError.OTHER_CAUSE,
"A role's name must be a String.");
}
if (!(/^[0-9a-zA-Z\-_ ]+$/).test(newName)) {
return new AVError(AVError.OTHER_CAUSE,
"A role's name can only contain alphanumeric characters, _," +
" -, and spaces.");
}
}
if (AV.Object.prototype.validate) {
return AV.Object.prototype.validate.call(this, attrs, options);
}
return false;
}
});
};