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

Remove CurrentUserLoader and CurrentUserInterface #1683

Merged
merged 10 commits into from
Feb 15, 2024
7 changes: 7 additions & 0 deletions .changeset/plenty-humans-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@comet/cms-api": minor
---

Remove `CurrentUserLoader` and `CurrentUserInterface`

Overriding the the current user in the application isn't supported anymore when using the new `UserPermissionsModule`, which provides the current user DTO itself.
82 changes: 41 additions & 41 deletions demo/api/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,47 @@
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
# ------------------------------------------------------

type CurrentUserPermission {
permission: String!
contentScopes: [JSONObject!]!
}

"""
The `JSONObject` scalar type represents JSON objects as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
"""
scalar JSONObject @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")

type CurrentUser {
id: String!
name: String!
email: String!
language: String!
permissions: [CurrentUserPermission!]!
}

type UserPermission {
id: ID!
source: UserPermissionSource!
permission: String!
validFrom: DateTime
validTo: DateTime
reason: String
requestedBy: String
approvedBy: String
overrideContentScopes: Boolean!
contentScopes: [JSONObject!]!
}

enum UserPermissionSource {
MANUAL
BY_RULE
}

"""
A date-time string at UTC, such as 2019-12-03T09:54:33Z, compliant with the date-time format.
"""
scalar DateTime

type Dependency {
rootId: String!
rootGraphqlObjectType: String!
Expand Down Expand Up @@ -44,11 +85,6 @@ type DamFileImage {
url(width: Int!, height: Int!): String
}

"""
The `JSONObject` scalar type represents JSON objects as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
"""
scalar JSONObject @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")

type DamFileLicense {
type: LicenseType
details: String
Expand All @@ -69,11 +105,6 @@ enum LicenseType {
RIGHTS_MANAGED
}

"""
A date-time string at UTC, such as 2019-12-03T09:54:33Z, compliant with the date-time format.
"""
scalar DateTime

type BuildTemplate {
id: ID!
name: String!
Expand Down Expand Up @@ -144,44 +175,13 @@ type FilenameResponse {
isOccupied: Boolean!
}

type CurrentUserPermission {
permission: String!
contentScopes: [JSONObject!]!
}

type CurrentUser {
id: String!
name: String!
email: String!
language: String!
permissions: [CurrentUserPermission!]!
}

type User {
id: String!
name: String!
email: String!
language: String!
}

type UserPermission {
id: ID!
source: UserPermissionSource!
permission: String!
validFrom: DateTime
validTo: DateTime
reason: String
requestedBy: String
approvedBy: String
overrideContentScopes: Boolean!
contentScopes: [JSONObject!]!
}

enum UserPermissionSource {
MANUAL
BY_RULE
}

type PaginatedUserList {
nodes: [User!]!
totalCount: Int!
Expand Down
6 changes: 2 additions & 4 deletions demo/api/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createAuthResolver, createCometAuthGuard, createStaticAuthedUserStrategy, CurrentUser } from "@comet/cms-api";
import { createAuthResolver, createCometAuthGuard, createStaticAuthedUserStrategy } from "@comet/cms-api";
import { Module } from "@nestjs/common";
import { APP_GUARD } from "@nestjs/core";

Expand All @@ -11,9 +11,7 @@ import { UserService } from "./user.service";
createStaticAuthedUserStrategy({
staticAuthedUser: staticUsers[0].id,
}),
createAuthResolver({
currentUser: CurrentUser,
}),
createAuthResolver(),
{
provide: APP_GUARD,
useClass: createCometAuthGuard(["static-authed-user"]),
Expand Down
40 changes: 29 additions & 11 deletions docs/docs/migration/migration-from-v5-to-v6.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ sidebar_position: 1

# Migrating from v5 to v6

First, execute `npx @comet/upgrade@latest v6` in the root of your project.
First, execute `npx @comet/upgrade@latest v6` in the root of your project.
It automatically installs the new versions of all `@comet` libraries, runs an ESLint autofix and handles some of the necessary renames.

<details>

<summary>Renames handled by @comet/upgrade</summary>

- `JobStatus` -> `KubernetesJobStatus` in API
- `@SubjectEntity` -> `@AffectedEntity` in API
- `BuildRuntime` -> `JobRuntime` in Admin
- `JobStatus` -> `KubernetesJobStatus` in API
- `@SubjectEntity` -> `@AffectedEntity` in API
- `BuildRuntime` -> `JobRuntime` in Admin

</details>



## API

### User Permissions
Expand Down Expand Up @@ -54,14 +52,28 @@ It automatically installs the new versions of all `@comet` libraries, runs an ES
}),
```

Furthermore, it's not necessary anymore to provide the CurrentUser

```diff
createAuthResolver({
- currentUser: CurrentUser,
```

Change imports of removed classes

```diff
- import { CurrentUser, CurrentUserLoader } from "@src/auth/current-user";
+ import { CurrentUser } from "@comet/cms-api";
```

It shouldn't be necessary to override these classes anymore. However, if you really need it, provide the CurrentUserLoader with `CURRENT_USER_LOADER`.
Replace occurrences of CurrentUserInterface

```diff
- @GetCurrentUser() user: CurrentUserInterface;
+ @GetCurrentUser() user: CurrentUser;
```

It is not possible anymore to use a custom CurrentUserLoader neither to augment/use the CurrentUserInterface.

3. Create interface for `availablePermissions` similar to the already existing interface `interface ContentScope`

Expand Down Expand Up @@ -103,15 +115,15 @@ It automatically installs the new versions of all `@comet` libraries, runs an ES

5. Replace `ContentScopeModule` with `UserPermissionsModule`

Remove `ContentScopeModule`:
Remove `ContentScopeModule`:

```diff
- ContentScopeModule.forRoot({
- ...
- }),
```

Add `UserPermissionsModule`:
Add `UserPermissionsModule`:

```ts
UserPermissionsModule.forRootAsync({
Expand Down Expand Up @@ -176,14 +188,20 @@ It automatically installs the new versions of all `@comet` libraries, runs an ES

```tsx
<MenuItemRouterLink
primary={intl.formatMessage({ id: "menu.userPermissions", defaultMessage: "User Permissions" })}
primary={intl.formatMessage({
id: "menu.userPermissions",
defaultMessage: "User Permissions",
})}
icon={<Snips />}
to={`${match.url}/user-permissions`}
/>
```

```tsx
<RouteWithErrorBoundary path={`${match.path}/user-permissions`} component={UserPermissionsPage} />
<RouteWithErrorBoundary
path={`${match.path}/user-permissions`}
component={UserPermissionsPage}
/>
```

### Sites Config
Expand Down
18 changes: 1 addition & 17 deletions packages/api/cms-api/generate-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
createAuthResolver,
createPageTreeResolver,
createRedirectsResolver,
CurrentUserInterface,
DependenciesResolverFactory,
DependentsResolverFactory,
DocumentInterface,
Expand All @@ -29,7 +28,6 @@ import { createFilesResolver } from "./src/dam/files/files.resolver";
import { createFoldersResolver } from "./src/dam/files/folders.resolver";
import { RedirectInputFactory } from "./src/redirects/dto/redirect-input.factory";
import { RedirectEntityFactory } from "./src/redirects/entities/redirect-entity.factory";
import { CurrentUserPermission } from "./src/user-permissions/dto/current-user";
import { UserResolver } from "./src/user-permissions/user.resolver";
import { UserContentScopesResolver } from "./src/user-permissions/user-content-scopes.resolver";
import { UserPermissionResolver } from "./src/user-permissions/user-permission.resolver";
Expand All @@ -48,20 +46,6 @@ class Page implements DocumentInterface {
updatedAt: Date;
}

@ObjectType()
class CurrentUser implements CurrentUserInterface {
@Field()
id: string;
@Field()
name: string;
@Field()
email: string;
@Field()
language: string;
@Field(() => [CurrentUserPermission])
permissions: CurrentUserPermission[];
}

async function generateSchema(): Promise<void> {
console.info("Generating schema.gql...");

Expand All @@ -84,7 +68,7 @@ async function generateSchema(): Promise<void> {
}); // no scope
const PageTreeDependentsResolver = DependentsResolverFactory.create(PageTreeNode);

const AuthResolver = createAuthResolver({ currentUser: CurrentUser });
const AuthResolver = createAuthResolver({});
const RedirectsDependenciesResolver = DependenciesResolverFactory.create(RedirectEntity);

const Folder = createFolderEntity();
Expand Down
Loading
Loading