Skip to content

Commit d446179

Browse files
committed
11.4.8: S_VERIFIED role
1 parent c865a11 commit d446179

File tree

6 files changed

+31
-24
lines changed

6 files changed

+31
-24
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lenne.tech/nest-server",
3-
"version": "11.4.7",
3+
"version": "11.4.8",
44
"description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
55
"keywords": [
66
"node",

spectaql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ servers:
1111
info:
1212
title: lT Nest Server
1313
description: Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).
14-
version: 11.4.7
14+
version: 11.4.8
1515
contact:
1616
name: lenne.Tech GmbH
1717
url: https://lenne.tech

src/core/common/decorators/restricted.decorator.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const getRestricted = (object: unknown, propertyKey?: string): Restricted
6161
*/
6262
export const checkRestricted = (
6363
data: any,
64-
user: { hasRole: (roles: string[]) => boolean; id: any },
64+
user: { hasRole: (roles: string[]) => boolean; id: any; verified?: any; verifiedAt?: any },
6565
options: {
6666
allowCreatorOfParent?: boolean;
6767
checkObjectItself?: boolean;
@@ -108,9 +108,9 @@ export const checkRestricted = (
108108
// Array
109109
if (Array.isArray(data)) {
110110
// Check array items
111-
let result = data.map(item => checkRestricted(item, user, config, processedObjects));
111+
let result = data.map((item) => checkRestricted(item, user, config, processedObjects));
112112
if (!config.throwError && config.removeUndefinedFromResultArray) {
113-
result = result.filter(item => item !== undefined);
113+
result = result.filter((item) => item !== undefined);
114114
}
115115
return result;
116116
}
@@ -134,8 +134,8 @@ export const checkRestricted = (
134134
if (typeof item === 'string') {
135135
roles.push(item);
136136
} else if (
137-
item?.roles?.length
138-
&& (config.processType && item.processType ? config.processType === item.processType : true)
137+
item?.roles?.length &&
138+
(config.processType && item.processType ? config.processType === item.processType : true)
139139
) {
140140
if (Array.isArray(item.roles)) {
141141
roles.push(...item.roles);
@@ -156,13 +156,14 @@ export const checkRestricted = (
156156

157157
// Check access rights
158158
if (
159-
roles.includes(RoleEnum.S_EVERYONE)
160-
|| user?.hasRole?.(roles)
161-
|| (user?.id && roles.includes(RoleEnum.S_USER))
162-
|| (roles.includes(RoleEnum.S_SELF) && equalIds(data, user))
163-
|| (roles.includes(RoleEnum.S_CREATOR)
164-
&& (('createdBy' in data && equalIds(data.createdBy, user))
165-
|| (config.allowCreatorOfParent && !('createdBy' in data) && config.isCreatorOfParent)))
159+
roles.includes(RoleEnum.S_EVERYONE) ||
160+
user?.hasRole?.(roles) ||
161+
(user?.id && roles.includes(RoleEnum.S_USER)) ||
162+
(roles.includes(RoleEnum.S_SELF) && equalIds(data, user)) ||
163+
(roles.includes(RoleEnum.S_CREATOR) &&
164+
(('createdBy' in data && equalIds(data.createdBy, user)) ||
165+
(config.allowCreatorOfParent && !('createdBy' in data) && config.isCreatorOfParent))) ||
166+
(roles.includes(RoleEnum.S_VERIFIED) && (user?.verified || user?.verifiedAt))
166167
) {
167168
valid = true;
168169
}
@@ -172,11 +173,11 @@ export const checkRestricted = (
172173
// Get groups
173174
const groups = restricted.filter((item) => {
174175
return (
175-
typeof item === 'object'
176+
typeof item === 'object' &&
176177
// Check if object is valid
177-
&& item.memberOf?.length
178+
item.memberOf?.length &&
178179
// Check if processType is specified and is valid for current process
179-
&& (config.processType && item.processType ? config.processType === item.processType : true)
180+
(config.processType && item.processType ? config.processType === item.processType : true)
180181
);
181182
}) as { memberOf: string | string[] }[];
182183

@@ -252,8 +253,8 @@ export const checkRestricted = (
252253
// Check rights
253254
if (valid) {
254255
// Check if data is user or user is creator of data (for nested plain objects)
255-
config.isCreatorOfParent
256-
= equalIds(data, user) || ('createdBy' in data ? equalIds(data.createdBy, user) : config.isCreatorOfParent);
256+
config.isCreatorOfParent =
257+
equalIds(data, user) || ('createdBy' in data ? equalIds(data.createdBy, user) : config.isCreatorOfParent);
257258

258259
// Check deep
259260
data[propertyKey] = checkRestricted(data[propertyKey], user, config, processedObjects);

src/core/common/enums/role.enum.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable perfectionist/sort-enums */
12
/**
23
* Enums for Resolver @Role and Model @Restricted decorator and for roles property in ServiceOptions
34
*
@@ -42,9 +43,12 @@ export enum RoleEnum {
4243
// Everyone, including users who are not logged in, can access (see context user, e.g. @CurrentUser)
4344
S_EVERYONE = 's_everyone',
4445

45-
// No one has access, not even administrators
46+
// No one has access, not even administrators (regardless of which roles are still set, access will always be denied)
4647
S_NO_ONE = 's_no_one',
4748

49+
// User must be verified (see verified or verifiedAt property of user)
50+
S_VERIFIED = 's_verified',
51+
4852
// ===================================================================================================================
4953
// Special system roles that check rights for DB objects and can be used via @Restricted for Models
5054
// (classes and properties) and via ServiceOptions for Resolver methods. These roles should not be integrated in

src/core/common/helpers/input.helper.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export function assignPlain(target: Record<any, any>, ...args: Record<any, any>[
209209
*/
210210
export async function check(
211211
value: any,
212-
user: { hasRole: (roles: string[]) => boolean; id: any },
212+
user: { hasRole: (roles: string[]) => boolean; id: any; verified?: any; verifiedAt?: any },
213213
options?: {
214214
allowCreatorOfParent?: boolean;
215215
dbObject?: any;
@@ -262,7 +262,9 @@ export async function check(
262262
(config.allowCreatorOfParent &&
263263
config.dbObject &&
264264
!('createdBy' in config.dbObject) &&
265-
config.isCreatorOfParent)))
265+
config.isCreatorOfParent))) ||
266+
// check if the is verified
267+
(roles.includes(RoleEnum.S_VERIFIED) && (user?.verified || user?.verifiedAt))
266268
) {
267269
valid = true;
268270
}

0 commit comments

Comments
 (0)