Skip to content

Commit

Permalink
feat: add isGlobal to Context
Browse files Browse the repository at this point in the history
  • Loading branch information
loopingz committed Oct 7, 2023
1 parent 850919b commit 6423ba7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
1 change: 0 additions & 1 deletion packages/core/src/core.ts
Expand Up @@ -1046,7 +1046,6 @@ export class Core<E extends CoreEvents = CoreEvents> extends events.EventEmitter
*/
public setGlobalContext(context: GlobalContext): void {
this.globalContext = context;
context.getSession().login("system", "system");
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/models/aclmodel.ts
Expand Up @@ -50,8 +50,9 @@ export default class AclModel extends CoreModel {
* Add the permissions for current user
*/
async _onGet() {
if (this.getContext()) {
this._permissions = await this.getPermissions(this.getContext());
const ctx = this.getContext();
if (!ctx.isGlobal()) {
this._permissions = await this.getPermissions(ctx);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/models/coremodel.ts
Expand Up @@ -336,8 +336,8 @@ export class ModelRef<T extends CoreModel> {
this.uuid = uuid === "" ? undefined : model.completeUid(uuid);
this.store = Core.get().getModelStore(model);
}
async get(): Promise<T> {
return this.store.get(this.uuid);
async get(context?: OperationContext): Promise<T> {
return (await this.store.get(this.uuid)).setContext(context);
}
set(id: string | T) {
this.uuid = id instanceof CoreModel ? id.getUuid() : id;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/services/invitationservice.ts
Expand Up @@ -371,7 +371,7 @@ export default class InvitationService<T extends InvitationParameters = Invitati
* @returns
*/
async invite(ctx: WebContext) {
let model = await this.model.ref(ctx.getParameters().uuid).get();
let model = await this.model.ref(ctx.getParameters().uuid).get(ctx);
if (ctx.getHttpContext().getMethod() === "PUT") {
return this.answerInvitation(ctx, model);
}
Expand Down
21 changes: 19 additions & 2 deletions packages/core/src/utils/context.ts
Expand Up @@ -358,8 +358,7 @@ export class OperationContext<T = any, U = any> extends EventEmitter {
}
// Caching the answer
if (!this.user || refresh) {
this.user = <User>await this._webda.getApplication().getModel("User").ref(this.getCurrentUserId()).get();
//this.user = <any>User; //await User.ref(this.getCurrentUserId()).get();
this.user = <User>await this._webda.getApplication().getModel("User").ref(this.getCurrentUserId()).get(this);
}
return <K>this.user;
}
Expand All @@ -370,6 +369,17 @@ export class OperationContext<T = any, U = any> extends EventEmitter {
getCurrentUserId() {
return undefined;
}

/**
* Global context is the default Context
*
* Whenever a request is internal to the system
* or not linked to a user request
* @returns
*/
isGlobal() {
return false;
}
}

export class GlobalContext extends OperationContext {
Expand All @@ -381,6 +391,13 @@ export class GlobalContext extends OperationContext {
// Disable logout
this.session.logout = () => {};
}

/**
* @override
*/
isGlobal() {
return true;
}
}
/**
* Simple Operation Context with custom input
Expand Down

0 comments on commit 6423ba7

Please sign in to comment.