Skip to content

Commit 7413283

Browse files
committed
feat: add AV.User#getRoles()
Conflicts: test/query.js
1 parent b0536b7 commit 7413283

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

src/user.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,16 @@ module.exports = function(AV) {
531531
return this._sessionToken;
532532
},
533533

534+
/**
535+
* Get this user's Roles.
536+
* @param {AuthOptions} [options]
537+
* @return {Promise} A promise that is fulfilled with the roles when
538+
* the query is complete.
539+
*/
540+
getRoles(options) {
541+
return AV.Relation.reverseQuery("_Role", "users", this).find(options);
542+
},
543+
534544
}, /** @lends AV.User */ {
535545
// Class Variables
536546

test/query.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,12 @@ describe('Queries', function () {
232232
test.set('number', i);
233233
promises.unshift(test.save());
234234
}
235-
// AV.Promise.when(promises).then(function() {done();});
236235
return AV.Promise.all(promises).then(function () {
237-
var query = new AV.Query('deletedAll');
238-
return query.limit(1000).find();
239-
}).then(function (results) {
240-
// expect(results.length).to.be(10);
241-
return query.destroyAll();
242-
}).then(function () {
243-
return query.find();
244-
}).then(function (results) {
245-
expect(results.length).to.be(0);
236+
return new AV.Query('deletedAll').limit(300).destroyAll();
237+
}).then(function() {
238+
return new AV.Query('deletedAll').count();
239+
}).then(function(count) {
240+
expect(count).to.be(0);
246241
});
247242
});
248243
});

test/user.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ describe("User", function() {
105105
});
106106
});
107107

108+
describe('User#getRoles', function() {
109+
it('Should get the current user\'s role', function() {
110+
var user = AV.User.current();
111+
return new AV.Query('_Role').equalTo('name', 'testRole').first()
112+
.then(function(role) {
113+
return role.destroy();
114+
}).catch(function() {
115+
// already destroyed
116+
}).then(function() {
117+
var role = new AV.Role("testRole");
118+
role.getUsers().add(user);
119+
return role.save()
120+
}).then(function() {
121+
return user.getRoles()
122+
}).then(function(roles) {
123+
expect(roles.length).to.be(1);
124+
expect(roles[0].getName()).to.be('testRole');
125+
});
126+
});
127+
});
108128

109129
describe("Associations", function() {
110130
it("return post relation to user", function() {

0 commit comments

Comments
 (0)