From d702b7e106ed2f4227aeef78ab85bbdbf43701cd Mon Sep 17 00:00:00 2001 From: Rafael Kellermann Streit Date: Fri, 26 Jan 2018 10:21:36 -0500 Subject: [PATCH 1/6] Add a new Permissions REST API endpoint --- packages/rocketchat-api/package.js | 1 + packages/rocketchat-api/server/v1/permissions.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 packages/rocketchat-api/server/v1/permissions.js diff --git a/packages/rocketchat-api/package.js b/packages/rocketchat-api/package.js index 859ca35ffdf0..8bee05c46cae 100644 --- a/packages/rocketchat-api/package.js +++ b/packages/rocketchat-api/package.js @@ -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'); diff --git a/packages/rocketchat-api/server/v1/permissions.js b/packages/rocketchat-api/server/v1/permissions.js new file mode 100644 index 000000000000..83139fe3c9a6 --- /dev/null +++ b/packages/rocketchat-api/server/v1/permissions.js @@ -0,0 +1,16 @@ +/** + This API returns logged user permissions. + + Method: GET + Route: api/v1/permissions + */ +RocketChat.API.v1.addRoute('permissions', { authRequired: true }, { + get() { + let result; + Meteor.runAsUser(this.userId, () => + result = Meteor.call('permissions/get') + ); + + return RocketChat.API.v1.success(result); + } +}); From ecf1ece59887f095fdb9a73645a042bd066533d2 Mon Sep 17 00:00:00 2001 From: Rafael Kellermann Streit Date: Fri, 26 Jan 2018 10:29:17 -0500 Subject: [PATCH 2/6] Create a new API that returns all logged user roles --- .../rocketchat-api/server/v1/permissions.js | 3 ++- packages/rocketchat-api/server/v1/users.js | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/rocketchat-api/server/v1/permissions.js b/packages/rocketchat-api/server/v1/permissions.js index 83139fe3c9a6..542675c419c7 100644 --- a/packages/rocketchat-api/server/v1/permissions.js +++ b/packages/rocketchat-api/server/v1/permissions.js @@ -1,5 +1,6 @@ /** - This API returns logged user permissions. + This API returns all permissions that exists + on the server, with respective roles. Method: GET Route: api/v1/permissions diff --git a/packages/rocketchat-api/server/v1/users.js b/packages/rocketchat-api/server/v1/users.js index 234e73e83ac6..d9606b219951 100644 --- a/packages/rocketchat-api/server/v1/users.js +++ b/packages/rocketchat-api/server/v1/users.js @@ -270,3 +270,27 @@ 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 result; + var currentUserRoles = {}; + + Meteor.runAsUser(this.userId, () => + result = Meteor.call('getUserRoles') + ); + + if (Array.isArray(result) && result.length > 0) { + currentUserRoles = result[0] + } + + return RocketChat.API.v1.success(currentUserRoles); + } +}); + From 525cb92d221946d849d1adebc4d67b6ad74c60ea Mon Sep 17 00:00:00 2001 From: Rafael Kellermann Streit Date: Fri, 26 Jan 2018 10:39:30 -0500 Subject: [PATCH 3/6] Fixed some lint issues --- packages/rocketchat-api/server/v1/users.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rocketchat-api/server/v1/users.js b/packages/rocketchat-api/server/v1/users.js index d9606b219951..7e12cf616f7f 100644 --- a/packages/rocketchat-api/server/v1/users.js +++ b/packages/rocketchat-api/server/v1/users.js @@ -280,14 +280,14 @@ RocketChat.API.v1.addRoute('users.createToken', { authRequired: true }, { RocketChat.API.v1.addRoute('user.roles', { authRequired: true }, { get() { let result; - var currentUserRoles = {}; + let currentUserRoles = {}; Meteor.runAsUser(this.userId, () => result = Meteor.call('getUserRoles') ); if (Array.isArray(result) && result.length > 0) { - currentUserRoles = result[0] + currentUserRoles = result[0]; } return RocketChat.API.v1.success(currentUserRoles); From 962300c1b74f7df48c594dab048d6242d8a8a88e Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Tue, 6 Feb 2018 09:34:21 -0200 Subject: [PATCH 4/6] Added tests for thiis endpoints --- tests/end-to-end/api/01-users.js | 17 +++++++++++++ tests/end-to-end/api/11-permissions.js | 33 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tests/end-to-end/api/11-permissions.js diff --git a/tests/end-to-end/api/01-users.js b/tests/end-to-end/api/01-users.js index b92fa23b7e87..77bf902518cb 100644 --- a/tests/end-to-end/api/01-users.js +++ b/tests/end-to-end/api/01-users.js @@ -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); + }); + }); }); diff --git a/tests/end-to-end/api/11-permissions.js b/tests/end-to-end/api/11-permissions.js new file mode 100644 index 000000000000..c0d956187fd7 --- /dev/null +++ b/tests/end-to-end/api/11-permissions.js @@ -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); + }); + }); +}); From b93615628774487c0abe704d105bffee54dda30f Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Tue, 6 Feb 2018 09:43:52 -0200 Subject: [PATCH 5/6] Fixed lint problem --- tests/end-to-end/api/11-permissions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/end-to-end/api/11-permissions.js b/tests/end-to-end/api/11-permissions.js index c0d956187fd7..86b702342c01 100644 --- a/tests/end-to-end/api/11-permissions.js +++ b/tests/end-to-end/api/11-permissions.js @@ -3,7 +3,7 @@ import { getCredentials, api, request, credentials } from '../../data/api-data.js'; -describe('[Permissions]', function () { +describe('[Permissions]', function() { this.retries(0); before(done => getCredentials(done)); From a32b80a83d08aa05b360cd5fa64e551d0c334e14 Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Fri, 9 Feb 2018 10:46:12 -0200 Subject: [PATCH 6/6] small fixes --- packages/rocketchat-api/server/v1/permissions.js | 5 +---- packages/rocketchat-api/server/v1/users.js | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/rocketchat-api/server/v1/permissions.js b/packages/rocketchat-api/server/v1/permissions.js index 542675c419c7..68ccb81c01e7 100644 --- a/packages/rocketchat-api/server/v1/permissions.js +++ b/packages/rocketchat-api/server/v1/permissions.js @@ -7,10 +7,7 @@ */ RocketChat.API.v1.addRoute('permissions', { authRequired: true }, { get() { - let result; - Meteor.runAsUser(this.userId, () => - result = Meteor.call('permissions/get') - ); + const result = Meteor.runAsUser(this.userId, () => Meteor.call('permissions/get')); return RocketChat.API.v1.success(result); } diff --git a/packages/rocketchat-api/server/v1/users.js b/packages/rocketchat-api/server/v1/users.js index 7e12cf616f7f..922beea7224e 100644 --- a/packages/rocketchat-api/server/v1/users.js +++ b/packages/rocketchat-api/server/v1/users.js @@ -279,12 +279,9 @@ RocketChat.API.v1.addRoute('users.createToken', { authRequired: true }, { */ RocketChat.API.v1.addRoute('user.roles', { authRequired: true }, { get() { - let result; let currentUserRoles = {}; - Meteor.runAsUser(this.userId, () => - result = Meteor.call('getUserRoles') - ); + const result = Meteor.runAsUser(this.userId, () => Meteor.call('getUserRoles')); if (Array.isArray(result) && result.length > 0) { currentUserRoles = result[0];