diff --git a/app/javascript/dashboard/helper/permissionsHelper.js b/app/javascript/dashboard/helper/permissionsHelper.js
index 135238371..156e5ef4f 100644
--- a/app/javascript/dashboard/helper/permissionsHelper.js
+++ b/app/javascript/dashboard/helper/permissionsHelper.js
@@ -7,6 +7,15 @@ export const hasPermissions = (
);
};
+export const getCurrentAccount = ({ accounts } = {}, accountId = null) => {
+ return accounts.find(account => Number(account.id) === Number(accountId));
+};
+
+export const getUserPermissions = (user, accountId) => {
+ const currentAccount = getCurrentAccount(user, accountId) || {};
+ return currentAccount.permissions || [];
+};
+
const isPermissionsPresentInRoute = route =>
route.meta && route.meta.permissions;
diff --git a/app/javascript/dashboard/helper/routeHelpers.js b/app/javascript/dashboard/helper/routeHelpers.js
index 6c4036dd1..0a7615472 100644
--- a/app/javascript/dashboard/helper/routeHelpers.js
+++ b/app/javascript/dashboard/helper/routeHelpers.js
@@ -1,9 +1,8 @@
-import { hasPermissions } from './permissionsHelper';
-
-// eslint-disable-next-line default-param-last
-export const getCurrentAccount = ({ accounts } = {}, accountId) => {
- return accounts.find(account => account.id === accountId);
-};
+import {
+ hasPermissions,
+ getUserPermissions,
+ getCurrentAccount,
+} from './permissionsHelper';
export const routeIsAccessibleFor = (route, userPermissions = []) => {
const { meta: { permissions: routePermissions = [] } = {} } = route;
@@ -19,7 +18,9 @@ const validateActiveAccountRoutes = (to, user) => {
return accountDashboardURL;
}
- const isAccessible = routeIsAccessibleFor(to, user.permissions);
+ const userPermissions = getUserPermissions(user, to.params.accountId);
+
+ const isAccessible = routeIsAccessibleFor(to, userPermissions);
// If the route is not accessible for the user, return to dashboard screen
return isAccessible ? null : accountDashboardURL;
};
diff --git a/app/javascript/dashboard/helper/specs/permissionsHelper.spec.js b/app/javascript/dashboard/helper/specs/permissionsHelper.spec.js
index 34f434f7d..ed8511862 100644
--- a/app/javascript/dashboard/helper/specs/permissionsHelper.spec.js
+++ b/app/javascript/dashboard/helper/specs/permissionsHelper.spec.js
@@ -1,8 +1,31 @@
import {
buildPermissionsFromRouter,
+ getCurrentAccount,
+ getUserPermissions,
hasPermissions,
} from '../permissionsHelper';
+describe('#getCurrentAccount', () => {
+ it('should return the current account', () => {
+ expect(getCurrentAccount({ accounts: [{ id: 1 }] }, 1)).toEqual({ id: 1 });
+ expect(getCurrentAccount({ accounts: [] }, 1)).toEqual(undefined);
+ });
+});
+
+describe('#getUserPermissions', () => {
+ it('should return the correct permissions', () => {
+ const user = {
+ accounts: [
+ { id: 1, permissions: ['conversations_manage'] },
+ { id: 3, permissions: ['contacts_manage'] },
+ ],
+ };
+ expect(getUserPermissions(user, 1)).toEqual(['conversations_manage']);
+ expect(getUserPermissions(user, '3')).toEqual(['contacts_manage']);
+ expect(getUserPermissions(user, 2)).toEqual([]);
+ });
+});
+
describe('hasPermissions', () => {
it('returns true if permission is present', () => {
expect(
diff --git a/app/javascript/dashboard/helper/specs/routeHelpers.spec.js b/app/javascript/dashboard/helper/specs/routeHelpers.spec.js
index 5aa9c4ee2..dc5a01de1 100644
--- a/app/javascript/dashboard/helper/specs/routeHelpers.spec.js
+++ b/app/javascript/dashboard/helper/specs/routeHelpers.spec.js
@@ -1,19 +1,11 @@
import {
getConversationDashboardRoute,
- getCurrentAccount,
isAConversationRoute,
routeIsAccessibleFor,
validateLoggedInRoutes,
isAInboxViewRoute,
} from '../routeHelpers';
-describe('#getCurrentAccount', () => {
- it('should return the current account', () => {
- expect(getCurrentAccount({ accounts: [{ id: 1 }] }, 1)).toEqual({ id: 1 });
- expect(getCurrentAccount({ accounts: [] }, 1)).toEqual(undefined);
- });
-});
-
describe('#routeIsAccessibleFor', () => {
it('should return the correct access', () => {
let route = { meta: { permissions: ['administrator'] } };
diff --git a/app/javascript/dashboard/routes/index.spec.js b/app/javascript/dashboard/routes/index.spec.js
index a169f8b76..ac67e8f2e 100644
--- a/app/javascript/dashboard/routes/index.spec.js
+++ b/app/javascript/dashboard/routes/index.spec.js
@@ -34,8 +34,14 @@ describe('#validateAuthenticateRoutePermission', () => {
getCurrentUser: {
account_id: 1,
id: 1,
- permissions: ['agent'],
- accounts: [{ id: 1, role: 'agent', status: 'active' }],
+ accounts: [
+ {
+ permissions: ['agent'],
+ id: 1,
+ role: 'agent',
+ status: 'active',
+ },
+ ],
},
};
validateAuthenticateRoutePermission(to, next, { getters });
@@ -55,8 +61,14 @@ describe('#validateAuthenticateRoutePermission', () => {
getCurrentUser: {
account_id: 1,
id: 1,
- permissions: ['administrator'],
- accounts: [{ id: 1, role: 'administrator', status: 'active' }],
+ accounts: [
+ {
+ id: 1,
+ role: 'administrator',
+ permissions: ['administrator'],
+ status: 'active',
+ },
+ ],
},
};
validateAuthenticateRoutePermission(to, next, { getters });
diff --git a/app/views/api/v1/models/_user.json.jbuilder b/app/views/api/v1/models/_user.json.jbuilder
index 0e8c95adb..3d8131f60 100644
--- a/app/views/api/v1/models/_user.json.jbuilder
+++ b/app/views/api/v1/models/_user.json.jbuilder
@@ -14,7 +14,6 @@ json.provider resource.provider
json.pubsub_token resource.pubsub_token
json.custom_attributes resource.custom_attributes if resource.custom_attributes.present?
json.role resource.active_account_user&.role
-json.permissions resource.active_account_user&.permissions
json.ui_settings resource.ui_settings
json.uid resource.uid
json.type resource.type
diff --git a/spec/controllers/devise/session_controller_spec.rb b/spec/controllers/devise/session_controller_spec.rb
index ad6d76bf9..f9c2ad1fd 100644
--- a/spec/controllers/devise/session_controller_spec.rb
+++ b/spec/controllers/devise/session_controller_spec.rb
@@ -50,7 +50,7 @@ RSpec.describe 'Session', type: :request do
as: :json
expect(response).to have_http_status(:success)
- expect(response.parsed_body['data']['permissions']).to eq(['agent'])
+ expect(response.parsed_body['data']['accounts'].first['permissions']).to eq(['agent'])
end
end