diff --git a/src/components/Authorized/CheckPermissions.js b/src/components/Authorized/CheckPermissions.js index df6fdb7f0ec7504356c28f3868b3e7f3fadc2b3d..8aaad1ce5b01a913aa38e1954d8fd149c23bd406 100644 --- a/src/components/Authorized/CheckPermissions.js +++ b/src/components/Authorized/CheckPermissions.js @@ -45,6 +45,14 @@ const checkPermissions = (authority, currentAuthority, target, Exception) => { if (authority === currentAuthority) { return target; } + if (Array.isArray(currentAuthority)) { + for (let i = 0; i < currentAuthority.length; i += 1) { + const element = currentAuthority[i]; + if (authority.indexOf(element) >= 0) { + return target; + } + } + } return Exception; } diff --git a/src/components/Authorized/CheckPermissions.test.js b/src/components/Authorized/CheckPermissions.test.js index b4b5e2ce4abac1bd691c0a6da339b4fccceac447..1e66cb9edac708ba803cf3a916e54ca36e54b329 100644 --- a/src/components/Authorized/CheckPermissions.test.js +++ b/src/components/Authorized/CheckPermissions.test.js @@ -34,4 +34,22 @@ describe('test CheckPermissions', () => { it('Correct Function permission authentication', () => { expect(checkPermissions(() => true, 'guest', target, error)).toEqual('ok'); }); + it('authority is string, currentAuthority is array, return ok', () => { + expect(checkPermissions('user', ['user'], target, error)).toEqual('ok'); + }); + it('authority is string, currentAuthority is array, return ok', () => { + expect(checkPermissions('user', ['user', 'admin'], target, error)).toEqual('ok'); + }); + it('authority is array, currentAuthority is array, return ok', () => { + expect(checkPermissions(['user', 'admin'], ['user', 'admin'], target, error)).toEqual('ok'); + }); + it('Wrong Function permission authentication', () => { + expect(checkPermissions(() => false, ['user'], target, error)).toEqual('error'); + }); + it('Correct Function permission authentication', () => { + expect(checkPermissions(() => true, ['user'], target, error)).toEqual('ok'); + }); + it('authority is undefined , return ok', () => { + expect(checkPermissions(null, ['user'], target, error)).toEqual('ok'); + }); });