Skip to content

Commit 2cd8ac1

Browse files
committed
Merge pull request #150 from leeyeh/fix-149
[feature] Use the default acl for AV.Role if absent.
2 parents 765c6ef + 3f67d5f commit 2cd8ac1

File tree

6 files changed

+68
-19
lines changed

6 files changed

+68
-19
lines changed

dist/av-core.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5735,16 +5735,23 @@ module.exports = function(AV) {
57355735
* Constructs a new AVRole with the given name and ACL.
57365736
*
57375737
* @param {String} name The name of the Role to create.
5738-
* @param {AV.ACL} acl The ACL for this role. Roles must have an ACL.
5738+
* @param {AV.ACL} [acl] The ACL for this role. if absent, the default ACL
5739+
* `{'*': { read: true }}` will be used.
57395740
*/
57405741
constructor: function(name, acl) {
5741-
if (_.isString(name) && (acl instanceof AV.ACL)) {
5742+
if (_.isString(name)) {
57425743
AV.Object.prototype.constructor.call(this, null, null);
57435744
this.setName(name);
5744-
this.setACL(acl);
5745-
} else {
5746-
AV.Object.prototype.constructor.call(this, name, acl);
57475745
}
5746+
if (acl === undefined) {
5747+
var defaultAcl = new AV.ACL();
5748+
defaultAcl.setPublicReadAccess(true);
5749+
acl = defaultAcl;
5750+
}
5751+
if (!(acl instanceof AV.ACL)) {
5752+
throw new TypeError('acl must be an instance of AV.ACL');
5753+
}
5754+
this.setACL(acl);
57485755
},
57495756

57505757
/**
@@ -8414,7 +8421,9 @@ function drainQueue() {
84148421
currentQueue = queue;
84158422
queue = [];
84168423
while (++queueIndex < len) {
8417-
currentQueue[queueIndex].run();
8424+
if (currentQueue) {
8425+
currentQueue[queueIndex].run();
8426+
}
84188427
}
84198428
queueIndex = -1;
84208429
len = queue.length;
@@ -8466,7 +8475,6 @@ process.binding = function (name) {
84668475
throw new Error('process.binding is not supported');
84678476
};
84688477

8469-
// TODO(shtylman)
84708478
process.cwd = function () { return '/' };
84718479
process.chdir = function (dir) {
84728480
throw new Error('process.chdir is not supported');

dist/av.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6603,16 +6603,23 @@ module.exports = function(AV) {
66036603
* Constructs a new AVRole with the given name and ACL.
66046604
*
66056605
* @param {String} name The name of the Role to create.
6606-
* @param {AV.ACL} acl The ACL for this role. Roles must have an ACL.
6606+
* @param {AV.ACL} [acl] The ACL for this role. if absent, the default ACL
6607+
* `{'*': { read: true }}` will be used.
66076608
*/
66086609
constructor: function(name, acl) {
6609-
if (_.isString(name) && (acl instanceof AV.ACL)) {
6610+
if (_.isString(name)) {
66106611
AV.Object.prototype.constructor.call(this, null, null);
66116612
this.setName(name);
6612-
this.setACL(acl);
6613-
} else {
6614-
AV.Object.prototype.constructor.call(this, name, acl);
66156613
}
6614+
if (acl === undefined) {
6615+
var defaultAcl = new AV.ACL();
6616+
defaultAcl.setPublicReadAccess(true);
6617+
acl = defaultAcl;
6618+
}
6619+
if (!(acl instanceof AV.ACL)) {
6620+
throw new TypeError('acl must be an instance of AV.ACL');
6621+
}
6622+
this.setACL(acl);
66166623
},
66176624

66186625
/**
@@ -9616,7 +9623,9 @@ function drainQueue() {
96169623
currentQueue = queue;
96179624
queue = [];
96189625
while (++queueIndex < len) {
9619-
currentQueue[queueIndex].run();
9626+
if (currentQueue) {
9627+
currentQueue[queueIndex].run();
9628+
}
96209629
}
96219630
queueIndex = -1;
96229631
len = queue.length;
@@ -9668,7 +9677,6 @@ process.binding = function (name) {
96689677
throw new Error('process.binding is not supported');
96699678
};
96709679

9671-
// TODO(shtylman)
96729680
process.cwd = function () { return '/' };
96739681
process.chdir = function (dir) {
96749682
throw new Error('process.chdir is not supported');

gulpfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ gulp.task('test', function() {
120120
'query.js',
121121
'geopoint.js',
122122
'acl.js',
123+
'role.js',
123124
'master_key.js',
124125
'status.js',
125126
'sms.js',

lib/role.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,23 @@ module.exports = function(AV) {
2323
* Constructs a new AVRole with the given name and ACL.
2424
*
2525
* @param {String} name The name of the Role to create.
26-
* @param {AV.ACL} acl The ACL for this role. Roles must have an ACL.
26+
* @param {AV.ACL} [acl] The ACL for this role. if absent, the default ACL
27+
* `{'*': { read: true }}` will be used.
2728
*/
2829
constructor: function(name, acl) {
29-
if (_.isString(name) && (acl instanceof AV.ACL)) {
30+
if (_.isString(name)) {
3031
AV.Object.prototype.constructor.call(this, null, null);
3132
this.setName(name);
32-
this.setACL(acl);
33-
} else {
34-
AV.Object.prototype.constructor.call(this, name, acl);
3533
}
34+
if (acl === undefined) {
35+
var defaultAcl = new AV.ACL();
36+
defaultAcl.setPublicReadAccess(true);
37+
acl = defaultAcl;
38+
}
39+
if (!(acl instanceof AV.ACL)) {
40+
throw new TypeError('acl must be an instance of AV.ACL');
41+
}
42+
this.setACL(acl);
3643
},
3744

3845
/**

test/role.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
describe("Role", function() {
2+
describe("constructor", function() {
3+
it("normal usage", function() {
4+
var acl = new AV.ACL();
5+
var role = new AV.Role('foo', acl);
6+
expect(role.getName()).to.be('foo');
7+
expect(role.getACL()).to.be(acl);
8+
});
9+
it("acl is optional", function() {
10+
var role = new AV.Role('foo');
11+
expect(role.getName()).to.be('foo');
12+
expect(role.getACL().toJSON()).to.eql({
13+
'*': {
14+
read: true
15+
}
16+
});
17+
});
18+
it("type check", function() {
19+
expect(function() {
20+
new AV.Role('foo', {});
21+
}).to.throwError();
22+
});
23+
});
24+
});

test/test.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<script src="query.js"></script>
3232
<script src="geopoints.js"></script>
3333
<script src="acl.js"></script>
34+
<script src="role.js"></script>
3435
<script src="master_key.js"></script>
3536
<script src="status.js"></script>
3637
<script src="sms.js"></script>

0 commit comments

Comments
 (0)