Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] API to fetch permissions & user roles #9519

Merged
merged 6 commits into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/rocketchat-api/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Package.onUse(function(api) {
api.addFiles('server/v1/im.js', 'server');
api.addFiles('server/v1/integrations.js', 'server');
api.addFiles('server/v1/misc.js', 'server');
api.addFiles('server/v1/permissions.js', 'server');
api.addFiles('server/v1/push.js', 'server');
api.addFiles('server/v1/settings.js', 'server');
api.addFiles('server/v1/stats.js', 'server');
Expand Down
14 changes: 14 additions & 0 deletions packages/rocketchat-api/server/v1/permissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
This API returns all permissions that exists
on the server, with respective roles.

Method: GET
Route: api/v1/permissions
*/
RocketChat.API.v1.addRoute('permissions', { authRequired: true }, {
get() {
const result = Meteor.runAsUser(this.userId, () => Meteor.call('permissions/get'));

return RocketChat.API.v1.success(result);
}
});
21 changes: 21 additions & 0 deletions packages/rocketchat-api/server/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,24 @@ RocketChat.API.v1.addRoute('users.createToken', { authRequired: true }, {
return data ? RocketChat.API.v1.success({data}) : RocketChat.API.v1.unauthorized();
}
});

/**
This API returns the logged user roles.

Method: GET
Route: api/v1/user.roles
*/
RocketChat.API.v1.addRoute('user.roles', { authRequired: true }, {
get() {
let currentUserRoles = {};

const result = Meteor.runAsUser(this.userId, () => Meteor.call('getUserRoles'));

if (Array.isArray(result) && result.length > 0) {
currentUserRoles = result[0];
}

return RocketChat.API.v1.success(currentUserRoles);
}
});

17 changes: 17 additions & 0 deletions tests/end-to-end/api/01-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,21 @@ describe('[Users]', function() {
});
});
});

describe('[/user.roles]', () => {

it('should return id and name of user, and an array of roles', (done) => {
request.get(api('user.roles'))
.set(credentials)
.expect(200)
.expect('Content-Type', 'application/json')
.expect((res) => {
expect(res.body).to.have.property('username');
expect(res.body).to.have.property('roles').and.to.be.a('array');
expect(res.body).to.have.property('_id');
expect(res.body).to.have.property('success', true);
})
.end(done);
});
});
});
33 changes: 33 additions & 0 deletions tests/end-to-end/api/11-permissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-env mocha */
/* globals expect */

import { getCredentials, api, request, credentials } from '../../data/api-data.js';

describe('[Permissions]', function() {
this.retries(0);

before(done => getCredentials(done));

describe('[/permissions]', () => {
it('should return all permissions that exists on the server, with respective roles', (done) => {
request.get(api('permissions'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.be.a('array');

const firstElement = res.body[0];
expect(firstElement).to.have.property('_id');
expect(firstElement).to.have.property('roles').and.to.be.a('array');
expect(firstElement).to.have.property('_updatedAt');
expect(firstElement).to.have.property('meta');
expect(firstElement.meta).to.have.property('revision');
expect(firstElement.meta).to.have.property('created');
expect(firstElement.meta).to.have.property('version');
expect(firstElement).to.have.property('$loki');
})
.end(done);
});
});
});