-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support multiple
@AffectedEntity()
-decorators (#1733)
Notable changes: 1. AffectedEntity()-decorator is only valid for Methods, not for classes anymore 2. Every (not at least one) affected ContentScope must apply to user-permissions (was a security hole before) 3. In entities without scope-field the @ScopedEntity-decorator is now mandatory 4. The PageTreeNode must have a scope-field when AffectedEntity is used 5. Removed equality-check when using AffectedEntity() and at the same time submitting a scope argument 6. Support array of ids submitted in args 1-4 are breaking changes, but mainly in theory. Practically no project will be affected. --------- Co-authored-by: Johannes Obermair <[email protected]>
- Loading branch information
1 parent
ad153c9
commit 151e121
Showing
5 changed files
with
101 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@comet/cms-api": minor | ||
--- | ||
|
||
Support multiple `@AffectedEntity()`-decorators for a single function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 11 additions & 7 deletions
18
packages/api/cms-api/src/user-permissions/decorators/affected-entity.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
import { EntityName } from "@mikro-orm/core"; | ||
import { CustomDecorator, SetMetadata } from "@nestjs/common"; | ||
import { EntityClass, EntityName } from "@mikro-orm/core"; | ||
|
||
export interface AffectedEntityOptions { | ||
idArg?: string; | ||
pageTreeNodeIdArg?: string; | ||
} | ||
export interface AffectedEntityMeta { | ||
export type AffectedEntityMeta = { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
entity: EntityName<any>; //TODO | ||
entity: EntityClass<object>; | ||
options: AffectedEntityOptions; | ||
} | ||
}; | ||
|
||
export const AffectedEntity = ( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
entity: EntityName<any>, | ||
{ idArg, pageTreeNodeIdArg }: AffectedEntityOptions = { idArg: "id" }, | ||
): CustomDecorator<string> => { | ||
return SetMetadata("affectedEntity", { entity, options: { idArg, pageTreeNodeIdArg } }); | ||
): MethodDecorator => { | ||
return (target: object, key: string | symbol, descriptor: PropertyDescriptor) => { | ||
const metadata = Reflect.getOwnMetadata("affectedEntities", descriptor.value) || []; | ||
metadata.push({ entity, options: { idArg, pageTreeNodeIdArg } }); | ||
Reflect.defineMetadata("affectedEntities", metadata, descriptor.value); | ||
return descriptor; | ||
}; | ||
}; |