Skip to content

Commit cefb644

Browse files
committed
fix: create api key permissions
1 parent 79f26ef commit cefb644

File tree

6 files changed

+39
-40
lines changed

6 files changed

+39
-40
lines changed

api/src/graphql/generated/api/operations.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@ export function AccessUrlInputSchema(): z.ZodObject<Properties<AccessUrlInput>>
9494

9595
export function AddPermissionInputSchema(): z.ZodObject<Properties<AddPermissionInput>> {
9696
return z.object({
97-
action: z.string(),
98-
possession: z.string(),
99-
resource: ResourceSchema,
100-
role: RoleSchema
97+
actions: z.array(z.string()),
98+
resource: ResourceSchema
10199
})
102100
}
103101

@@ -326,7 +324,8 @@ export function CreateApiKeyInputSchema(): z.ZodObject<Properties<CreateApiKeyIn
326324
return z.object({
327325
description: z.string().nullish(),
328326
name: z.string(),
329-
roles: z.array(RoleSchema)
327+
permissions: z.array(z.lazy(() => AddPermissionInputSchema())).nullish(),
328+
roles: z.array(RoleSchema).nullish()
330329
})
331330
}
332331

@@ -1230,7 +1229,8 @@ export function VmDomainSchema(): z.ZodObject<Properties<VmDomain>> {
12301229
export function VmsSchema(): z.ZodObject<Properties<Vms>> {
12311230
return z.object({
12321231
__typename: z.literal('Vms').optional(),
1233-
domain: z.array(VmDomainSchema()).nullish()
1232+
domain: z.array(VmDomainSchema()).nullish(),
1233+
id: z.string()
12341234
})
12351235
}
12361236

api/src/graphql/generated/api/types.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@ export type AccessUrlInput = {
4040
};
4141

4242
export type AddPermissionInput = {
43-
action: Scalars['String']['input'];
44-
possession: Scalars['String']['input'];
43+
actions: Array<Scalars['String']['input']>;
4544
resource: Resource;
46-
role: Role;
4745
};
4846

4947
export type AddRoleForApiKeyInput = {
@@ -350,7 +348,8 @@ export enum ContainerState {
350348
export type CreateApiKeyInput = {
351349
description?: InputMaybe<Scalars['String']['input']>;
352350
name: Scalars['String']['input'];
353-
roles: Array<Role>;
351+
permissions?: InputMaybe<Array<AddPermissionInput>>;
352+
roles?: InputMaybe<Array<Role>>;
354353
};
355354

356355
export type Devices = {
@@ -1682,6 +1681,7 @@ export enum VmState {
16821681
export type Vms = {
16831682
__typename?: 'Vms';
16841683
domain?: Maybe<Array<VmDomain>>;
1684+
id: Scalars['ID']['output'];
16851685
};
16861686

16871687
export enum WAN_ACCESS_TYPE {
@@ -3090,6 +3090,7 @@ export type VmDomainResolvers<ContextType = Context, ParentType extends Resolver
30903090

30913091
export type VmsResolvers<ContextType = Context, ParentType extends ResolversParentTypes['Vms'] = ResolversParentTypes['Vms']> = ResolversObject<{
30923092
domain?: Resolver<Maybe<Array<ResolversTypes['VmDomain']>>, ParentType, ContextType>;
3093+
id?: Resolver<ResolversTypes['ID'], ParentType, ContextType>;
30933094
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
30943095
}>;
30953096

api/src/graphql/schema/types/auth/auth.graphql

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ type ApiKeyWithSecret {
2525
input CreateApiKeyInput {
2626
name: String!
2727
description: String
28-
roles: [Role!]!
28+
roles: [Role!]
29+
permissions: [AddPermissionInput!]
2930
}
3031

3132
input AddPermissionInput {
32-
role: Role!
3333
resource: Resource!
34-
action: String!
35-
possession: String!
34+
actions: [String!]!
3635
}
3736

3837
input AddRoleForUserInput {

api/src/unraid-api/auth/api-key.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import { ZodError } from 'zod';
1313
import { environment } from '@app/environment';
1414
import { ApiKeySchema, ApiKeyWithSecretSchema } from '@app/graphql/generated/api/operations';
1515
import {
16+
AddPermissionInput,
1617
ApiKey,
1718
ApiKeyWithSecret,
1819
Permission,
1920
Resource,
2021
Role,
21-
UserAccount,
2222
} from '@app/graphql/generated/api/types';
2323
import { getters, store } from '@app/store';
2424
import { updateUserConfig } from '@app/store/modules/config';
@@ -107,7 +107,7 @@ export class ApiKeyService implements OnModuleInit {
107107
name: string;
108108
description: string | undefined;
109109
roles?: Role[];
110-
permissions?: Permission[];
110+
permissions?: Permission[] | AddPermissionInput[];
111111
overwrite?: boolean;
112112
}): Promise<ApiKeyWithSecret> {
113113
const trimmedName = name?.trim();
Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
import {
2-
Catch,
3-
type ArgumentsHost,
4-
type ExceptionFilter,
5-
} from '@nestjs/common';
6-
import { GraphQLError } from 'graphql';
1+
import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
2+
import { Catch } from '@nestjs/common';
3+
74
import { type FastifyReply } from 'fastify';
5+
import { GraphQLError } from 'graphql';
86

97
@Catch(GraphQLError)
10-
export class GraphQLExceptionsFilter<T extends GraphQLError>
11-
implements ExceptionFilter
12-
{
8+
export class GraphQLExceptionsFilter<T extends GraphQLError> implements ExceptionFilter {
139
catch(exception: T, host: ArgumentsHost) {
1410
const ctx = host.switchToHttp();
1511
const response: FastifyReply<any> = ctx.getResponse<FastifyReply>();
1612

17-
response.code(200).send({
18-
data: null,
19-
errors: [
20-
{
21-
message: exception.message,
22-
locations: exception.locations,
23-
path: exception.path,
24-
},
25-
],
26-
});
13+
if (response.code) {
14+
response.code(200).send({
15+
data: null,
16+
errors: [
17+
{
18+
message: exception.message,
19+
locations: exception.locations,
20+
path: exception.path,
21+
},
22+
],
23+
});
24+
}
2725
}
2826
}

api/src/unraid-api/graph/resolvers/auth/auth.resolver.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ export class AuthResolver {
5656
@Args('input')
5757
input: CreateApiKeyInput
5858
): Promise<ApiKeyWithSecret> {
59-
const apiKey = await this.apiKeyService.create(
60-
input.name,
61-
input.description ?? undefined,
62-
input.roles
63-
);
59+
const apiKey = await this.apiKeyService.create({
60+
name: input.name,
61+
description: input.description ?? undefined,
62+
roles: input.roles ?? [],
63+
permissions: input.permissions ?? [],
64+
});
6465

6566
await this.authService.syncApiKeyRoles(apiKey.id, apiKey.roles);
6667

0 commit comments

Comments
 (0)