Skip to content

Commit 25ed2f8

Browse files
committed
Improve handling of CLP
1 parent 4d58b6d commit 25ed2f8

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

src/SchemaMigrations/Migrations.js

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,8 @@ export interface FieldType {
2020
targetClass?: string;
2121
}
2222

23-
export type CLPType =
24-
| '*'
25-
| ('find' | 'count' | 'get' | 'update' | 'create' | 'delete') /*| 'addField'*/[];
2623
type ClassNameType = '_User' | '_Role' | string;
2724

28-
export interface CLPInterface {
29-
requiresAuthentication?: boolean;
30-
'*'?: boolean;
31-
}
32-
3325
export interface ProtectedFieldsInterface {
3426
[key: string]: string[];
3527
}
@@ -49,43 +41,47 @@ export interface MigrationsOptions {
4941
recreateModifiedFields: ?boolean;
5042
}
5143

44+
export type CLPOperation =
45+
| 'find'
46+
| 'count'
47+
| 'get'
48+
| 'update'
49+
| 'create'
50+
| 'delete' /*| 'addField'*/;
51+
// @Typescript 4.1+ // type CLPPermission = 'requiresAuthentication' | '*' | `user:${string}` | `role:${string}`
52+
53+
type CLPValue = { [key: string]: boolean };
54+
type CLPData = { [key: string]: CLPOperation[] };
55+
type CLPInterface = { [key: string]: CLPValue };
56+
5257
export interface JSONSchema {
5358
className: ClassNameType;
5459
fields?: { [key: string]: FieldType };
5560
indexes?: IndexesInterface;
5661
classLevelPermissions?: {
57-
find?: CLPInterface,
58-
count?: CLPInterface,
59-
get?: CLPInterface,
60-
update?: CLPInterface,
61-
create?: CLPInterface,
62-
delete?: CLPInterface,
63-
addField?: CLPInterface,
62+
find?: CLPValue,
63+
count?: CLPValue,
64+
get?: CLPValue,
65+
update?: CLPValue,
66+
create?: CLPValue,
67+
delete?: CLPValue,
68+
addField?: CLPValue,
6469
protectedFields?: ProtectedFieldsInterface,
6570
};
6671
}
6772

68-
function CLP(ops: CLPType, value: CLPInterface): CLPInterface {
69-
const v: CLPInterface = {};
70-
71-
if (ops === '*') {
72-
ops = ['find', 'count', 'get', 'update', 'create', 'delete'];
73-
}
74-
75-
ops.forEach(op => {
76-
v[op] = value;
77-
});
73+
export class CLP {
74+
static allow(perms: CLPData): CLPInterface {
75+
const out = {};
7876

79-
return v;
80-
}
81-
82-
export class CLPHelper {
83-
static requiresAuthentication(ops: CLPType): CLPInterface {
84-
return CLP(ops, { requiresAuthentication: true });
85-
}
77+
for (const [perm, ops] of Object.entries(perms)) {
78+
for (const op of ops) {
79+
out[op] = out[op] || {};
80+
out[op][perm] = true;
81+
}
82+
}
8683

87-
static requiresAnonymous(ops: CLPType): CLPInterface {
88-
return CLP(ops, { '*': true });
84+
return out;
8985
}
9086
}
9187

0 commit comments

Comments
 (0)