Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export UserPermissionsService #3559

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/silly-buses-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@comet/cms-api": minor
---

Export `UserPermissionsService` from `@comet/cms-api`

This allows the usage of `getPermissionsAndContentScopes` if projects want to get all rule based
and admin based permissions for specific users.
2 changes: 2 additions & 0 deletions packages/api/cms-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,12 @@ export { RequiredPermission } from "./user-permissions/decorators/required-permi
export { DisablePermissionCheck } from "./user-permissions/decorators/required-permission.decorator";
export { ScopedEntity, ScopedEntityMeta } from "./user-permissions/decorators/scoped-entity.decorator";
export { CurrentUser } from "./user-permissions/dto/current-user";
export { CurrentUserPermission } from "./user-permissions/dto/current-user";
export { FindUsersArgs } from "./user-permissions/dto/paginated-user-list";
export { ContentScope } from "./user-permissions/interfaces/content-scope.interface";
export { User } from "./user-permissions/interfaces/user";
export { UserPermissionsModule } from "./user-permissions/user-permissions.module";
export { UserPermissionsPublicService as UserPermissionsService } from "./user-permissions/user-permissions.public.service";
export {
AccessControlServiceInterface,
ContentScopesForUser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { UserResolver } from "./user.resolver";
import { UserContentScopesResolver } from "./user-content-scopes.resolver";
import { UserPermissionResolver } from "./user-permission.resolver";
import { ACCESS_CONTROL_SERVICE, USER_PERMISSIONS_OPTIONS, USER_PERMISSIONS_USER_SERVICE } from "./user-permissions.constants";
import { UserPermissionsPublicService } from "./user-permissions.public.service";
import { UserPermissionsService } from "./user-permissions.service";
import {
UserPermissionsAsyncOptions,
Expand All @@ -24,6 +25,7 @@ import {
imports: [MikroOrmModule.forFeature([UserPermission, UserContentScopes]), DiscoveryModule],
providers: [
UserPermissionsService,
UserPermissionsPublicService,
UserResolver,
UserPermissionResolver,
UserContentScopesResolver,
Expand All @@ -33,7 +35,7 @@ import {
useClass: UserPermissionsGuard,
},
],
exports: [ContentScopeService, ACCESS_CONTROL_SERVICE, UserPermissionsService],
exports: [ContentScopeService, ACCESS_CONTROL_SERVICE, UserPermissionsService, UserPermissionsPublicService],
})
export class UserPermissionsModule {
static forRoot(options: UserPermissionsModuleSyncOptions): DynamicModule {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from "@nestjs/common";

import { User } from "./interfaces/user";
import { UserPermissionsService } from "./user-permissions.service";

@Injectable()
export class UserPermissionsPublicService {
constructor(private readonly service: UserPermissionsService) {}

async getAvailableContentScopes() {
return this.service.getAvailableContentScopes();
}

async getAvailablePermissions() {
return this.service.getAvailablePermissions();
}

async getPermissionsAndContentScopes(user: User) {
return this.service.getPermissionsAndContentScopes(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import isEqual from "lodash.isequal";
import getUuid from "uuid-by-string";

import { DisablePermissionCheck, RequiredPermissionMetadata } from "./decorators/required-permission.decorator";
import { CurrentUser } from "./dto/current-user";
import { CurrentUser, CurrentUserPermission } from "./dto/current-user";
import { FindUsersArgs } from "./dto/paginated-user-list";
import { UserContentScopes } from "./entities/user-content-scopes.entity";
import { UserPermission, UserPermissionSource } from "./entities/user-permission.entity";
Expand Down Expand Up @@ -172,9 +172,18 @@ export class UserPermissionsService {
const impersonatedUser = request && (await this.getImpersonatedUser(authenticatedUser, request));
const user = impersonatedUser || authenticatedUser;

return {
...user,
permissions: await this.getPermissionsAndContentScopes(user),
impersonated: !!impersonatedUser,
authenticatedUser: impersonatedUser ? authenticatedUser : undefined,
};
}

async getPermissionsAndContentScopes(user: User): Promise<CurrentUserPermission[]> {
const availableContentScopes = await this.getAvailableContentScopes();
const userContentScopes = await this.getContentScopes(user);
const permissions = (await this.getPermissions(user))
return (await this.getPermissions(user))
.filter((p) => (!p.validFrom || isPast(p.validFrom)) && (!p.validTo || isFuture(p.validTo)))
.reduce((acc: CurrentUser["permissions"], userPermission) => {
const contentScopes = userPermission.overrideContentScopes ? userPermission.contentScopes : userContentScopes;
Expand All @@ -193,12 +202,5 @@ export class UserPermissionsService {
p.contentScopes = this.normalizeContentScopes(p.contentScopes, availableContentScopes);
return p;
});

return {
...user,
permissions,
impersonated: !!impersonatedUser,
authenticatedUser: impersonatedUser ? authenticatedUser : undefined,
};
}
}