chore: snippet name replacement (#4346)

* chore: snippet name replacement

* chore: test
This commit is contained in:
ChengLei Shao 2024-05-14 13:23:02 +08:00 committed by GitHub
parent caa1dc37f6
commit c0e1260189
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 4 deletions

View File

@ -65,6 +65,15 @@ describe('acl snippet', () => {
acl = new ACL();
});
it('should replace invalid snippet name', () => {
acl.registerSnippet({
name: 'pm.users.*',
actions: ['users:list'],
});
expect(acl.snippetManager.snippets.get('pm.users')).toBeDefined();
});
it('should get effective snipptes', () => {
acl.registerSnippet({
name: 'sc.collection-manager.fields',
@ -219,7 +228,7 @@ describe('snippet manager', () => {
try {
snippetManager.register({
name: 'sc.collection-manager.*',
name: 'sc.collection-.*.manager.*',
actions: ['collections:*'],
});
} catch (e) {

View File

@ -30,10 +30,11 @@ class SnippetManager {
public snippets: Map<string, Snippet> = new Map();
register(snippet: SnippetOptions) {
const name = snippet.name;
snippet.name = snippet.name.replace('.*', '');
// throw error if name include * or end with dot
if (name.includes('*') || name.endsWith('.')) {
throw new Error(`Invalid snippet name: ${name}, name should not include * or end with dot.`);
if (snippet.name.includes('*') || snippet.name.endsWith('.')) {
throw new Error(`Invalid snippet name: ${snippet.name}, name should not include * or end with dot.`);
}
this.snippets.set(snippet.name, snippet);