Skip to content

Commit e14512b

Browse files
committedNov 9, 2023
break: Change permission messages to include user role
1 parent a126d8a commit e14512b

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed
 

‎src/client/queries.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export const getEntityQuery = (
183183
role: string,
184184
visibleRelationsByRole: VisibleRelationsByRole,
185185
typesWithSubRelations: string[]
186-
) => `query Admin${model.name} ($id: ID!) {
186+
) => `query Get${model.name}Entity ($id: ID!) {
187187
data: ${typeToField(model.name)}(where: { id: $id }) {
188188
${displayField(model)}
189189
${model.fields.filter(and(isSimpleField, isQueriableBy(role))).map(({ name }) => name)}

‎src/errors.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class UserInputError extends GraphQLError {
2626
}
2727

2828
export class PermissionError extends ForbiddenError {
29-
constructor(action: PermissionAction, what: string, why: string) {
30-
super(`You do not have sufficient permissions to ${action.toLowerCase()} ${what} (${why}).`);
29+
constructor(role: string, action: PermissionAction, what: string, why: string) {
30+
super(`Role ${role} does not have sufficient permissions to ${action.toLowerCase()} ${what} (${why}).`);
3131
}
3232
}

‎src/permissions/check.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export const getEntityToMutate = async (
113113
.map(([key, value]) => `${key}: ${value}`)
114114
.join(', ')}`
115115
);
116-
throw new PermissionError(action, `this ${model.name}`, 'no available permissions applied');
116+
throw new PermissionError(ctx.user.role, action, `this ${model.name}`, 'no available permissions applied');
117117
}
118118

119119
if (model.parent) {
@@ -139,7 +139,7 @@ export const checkCanWrite = async (
139139
return;
140140
}
141141
if (permissionStack === false) {
142-
throw new PermissionError(action, model.plural, 'no applicable permissions');
142+
throw new PermissionError(ctx.user.role, action, model.plural, 'no applicable permissions');
143143
}
144144

145145
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- using `select(1 as any)` to instantiate an "empty" query builder
@@ -157,7 +157,12 @@ export const checkCanWrite = async (
157157

158158
const fieldPermissions = field[action === 'CREATE' ? 'creatable' : 'updatable'];
159159
if (fieldPermissions && typeof fieldPermissions === 'object' && !fieldPermissions.roles?.includes(ctx.user.role)) {
160-
throw new PermissionError(action, `this ${model.name}'s ${field.name}`, 'field permission not available');
160+
throw new PermissionError(
161+
ctx.user.role,
162+
action,
163+
`this ${model.name}'s ${field.name}`,
164+
'field permission not available'
165+
);
161166
}
162167

163168
linked = true;
@@ -172,7 +177,12 @@ export const checkCanWrite = async (
172177
}
173178

174179
if (fieldPermissionStack === false || !fieldPermissionStack.length) {
175-
throw new PermissionError(action, `this ${model.name}'s ${field.name}`, 'no applicable permissions on data to link');
180+
throw new PermissionError(
181+
ctx.user.role,
182+
action,
183+
`this ${model.name}'s ${field.name}`,
184+
'no applicable permissions on data to link'
185+
);
176186
}
177187

178188
// eslint-disable-next-line @typescript-eslint/no-floating-promises -- we do not need to await knex here
@@ -187,10 +197,10 @@ export const checkCanWrite = async (
187197
if (linked) {
188198
const canMutate = await query;
189199
if (!canMutate) {
190-
throw new PermissionError(action, `this ${model.name}`, 'no linkable entities');
200+
throw new PermissionError(ctx.user.role, action, `this ${model.name}`, 'no linkable entities');
191201
}
192202
} else if (action === 'CREATE') {
193-
throw new PermissionError(action, `this ${model.name}`, 'no linkable entities');
203+
throw new PermissionError(ctx.user.role, action, `this ${model.name}`, 'no linkable entities');
194204
}
195205
};
196206

‎src/resolvers/selects.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const applySelects = (node: ResolverNode, query: Knex.QueryBuilder, joins
3030

3131
if (typeof field.queriable === 'object' && !field.queriable.roles?.includes(node.ctx.user.role)) {
3232
throw new PermissionError(
33+
node.ctx.user.role,
3334
'READ',
3435
`${node.model.name}'s field "${field.name}"`,
3536
'field permission not available'

0 commit comments

Comments
 (0)
Please sign in to comment.