Skip to content

Commit

Permalink
User API ability to filter users by role
Browse files Browse the repository at this point in the history
refs #3446

- This only covers the API changes needed for #3446
  • Loading branch information
ErisDS committed Jul 30, 2014
1 parent fe2e3f4 commit 57a5444
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 11 deletions.
41 changes: 34 additions & 7 deletions core/server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ User = ghostBookshelf.Model.extend({
options = options || {};

var userCollection = Users.forge(),
userQuery;
roleInstance = options.role !== undefined ? Role.forge({name: options.role}) : false;

if (options.limit && options.limit !== 'all') {
options.limit = parseInt(options.limit) || 15;
Expand Down Expand Up @@ -213,14 +213,28 @@ User = ghostBookshelf.Model.extend({
.query('offset', options.limit * (options.page - 1));
}

userQuery = userCollection
.query('orderBy', 'last_login', 'DESC')
.query('orderBy', 'name', 'ASC')
.query('orderBy', 'created_at', 'DESC')
.fetch(_.omit(options, 'page', 'limit'));
function fetchRoleQuery() {
if (roleInstance) {
return roleInstance.fetch();
}
return false;
}

return when(fetchRoleQuery())
.then(function () {

if (roleInstance) {
userCollection
.query('join', 'roles_users', 'roles_users.user_id', '=', 'users.id')
.query('where', 'roles_users.role_id', '=', roleInstance.id);
}

return when(userQuery)
return userCollection
.query('orderBy', 'last_login', 'DESC')
.query('orderBy', 'name', 'ASC')
.query('orderBy', 'created_at', 'DESC')
.fetch(_.omit(options, 'page', 'limit'));
})

// Fetch pagination information
.then(function () {
Expand All @@ -236,6 +250,11 @@ User = ghostBookshelf.Model.extend({
qb.where(options.where);
}

if (roleInstance) {
qb.join('roles_users', 'roles_users.user_id', '=', 'users.id');
qb.where('roles_users.role_id', '=', roleInstance.id);
}

return qb.count(tableName + '.' + idAttribute + ' as aggregate');
})

Expand Down Expand Up @@ -276,6 +295,14 @@ User = ghostBookshelf.Model.extend({
}
}

if (roleInstance) {
meta.filters = {};
if (!roleInstance.isNew()) {
meta.filters.roles = [roleInstance.toJSON()];
}
}


return data;
})
.catch(errors.logAndThrowError);
Expand Down
50 changes: 46 additions & 4 deletions core/test/integration/model/model_users_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('User Model', function run() {
});

describe('Basic Operations', function () {
beforeEach(testUtils.setup('owner', 'role'));
beforeEach(testUtils.setup('users:roles'));

it('sets last login time on successful login', function (done) {
var userData = testUtils.DataGenerator.forModel.users[0];
Expand Down Expand Up @@ -163,17 +163,59 @@ describe('User Model', function run() {
});

it('can findAll', function (done) {

UserModel.findAll().then(function (results) {
should.exist(results);
results.length.should.equal(4);

results.length.should.be.above(0);
done();

}).catch(done);
});

it('can findPage (default)', function (done) {
UserModel.findPage().then(function (results) {
should.exist(results);

results.meta.pagination.page.should.equal(1);
results.meta.pagination.limit.should.equal(15);
results.meta.pagination.pages.should.equal(1);
results.users.length.should.equal(4);

done();
}).catch(done);
});

it('can findPage by role', function (done) {
return testUtils.fixtures.createExtraUsers().then(function () {
return UserModel.findPage({role: 'Administrator'});
}).then(function (results) {
results.meta.pagination.page.should.equal(1);
results.meta.pagination.limit.should.equal(15);
results.meta.pagination.pages.should.equal(1);
results.meta.pagination.total.should.equal(2);
results.users.length.should.equal(2);

return UserModel.findPage({role: 'Owner'});
}).then(function (results) {
results.meta.pagination.page.should.equal(1);
results.meta.pagination.limit.should.equal(15);
results.meta.pagination.pages.should.equal(1);
results.meta.pagination.total.should.equal(1);
results.users.length.should.equal(1);

return UserModel.findPage({role: 'Editor', limit: 1});
}).then(function (results) {
results.meta.pagination.page.should.equal(1);
results.meta.pagination.limit.should.equal(1);
results.meta.pagination.pages.should.equal(2);
results.meta.pagination.total.should.equal(2);
results.users.length.should.equal(1);

done();
}).catch(done);
});


it('can findOne', function (done) {
var firstUser;

Expand Down Expand Up @@ -214,7 +256,7 @@ describe('User Model', function run() {
});

it('can add', function (done) {
var userData = testUtils.DataGenerator.forModel.users[2];
var userData = testUtils.DataGenerator.forModel.users[4];

sandbox.stub(UserModel, 'gravatarLookup', function (userData) {
return when.resolve(userData);
Expand Down

0 comments on commit 57a5444

Please sign in to comment.