Skip to content

Commit

Permalink
fix: add groupPolicy for GetPermissionForUser in frontend.ts (#415)
Browse files Browse the repository at this point in the history
* test: add test for getPolicy for group

* fix: add groupPolicy for GetPermissionForUser
  • Loading branch information
imp2002 committed Jan 20, 2023
1 parent a704ebb commit 5d1a679
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { concat } from 'lodash';
import { Enforcer } from './enforcer';
import { deepCopy } from './util';

Expand Down Expand Up @@ -40,10 +41,19 @@ export async function casbinJsGetPermissionForUser(e: Enforcer, user?: string):
s += '[matchers]\n';
s += `m = ${m.get('m')?.get('m')?.value.replace(/_/g, '.')}`;
obj['m'] = s;
obj['p'] = deepCopy(await e.getPolicy());
for (const arr of obj['p']) {
arr.splice(0, 0, 'p');
}

const policy = deepCopy(await e.getPolicy());
const groupPolicy = deepCopy(await e.getGroupingPolicy());

policy.forEach((item: string[]) => {
item.unshift('p');
});

groupPolicy.forEach((item: string[]) => {
item.unshift('g');
});

obj['p'] = concat(policy, groupPolicy);

return JSON.stringify(obj);
}
5 changes: 4 additions & 1 deletion test/frontend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ test('TestCasbinJsGetPermissionForUser', async () => {
// If you enable CR_LF auto transfer on Windows platform, this can lead to some unexpected behavior.
expect(received['m']).toBe(expectedModelStr.replace(/\n\n/g, '\n'));
const expectedPoliciesStr = readFileSync('examples/rbac_with_hierarchy_policy.csv').toString();
const expectedPolicyItem = expectedPoliciesStr.split(RegExp(',|\n'));

let expectedPolicyItem = expectedPoliciesStr.split(RegExp(',|\n'));
expectedPolicyItem = expectedPolicyItem.filter((item) => item !== null && item.trim() !== '');

let i = 0;
for (const sArr of received['p']) {
for (const s of sArr) {
Expand Down

0 comments on commit 5d1a679

Please sign in to comment.