biz-email-builder-shared 1.0.17 → 1.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  export * from "./user.entity";
2
2
  export * from "./template.entity";
3
- export * from "./role.entitty";
3
+ export * from "./user-group.entitty";
4
4
  export * from "./feature.entitiy";
5
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/entity/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/entity/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC"}
@@ -16,5 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./user.entity"), exports);
18
18
  __exportStar(require("./template.entity"), exports);
19
- __exportStar(require("./role.entitty"), exports);
19
+ __exportStar(require("./user-group.entitty"), exports);
20
20
  __exportStar(require("./feature.entitiy"), exports);
@@ -0,0 +1,14 @@
1
+ import { Types } from "mongoose";
2
+ interface IUserGroup {
3
+ name: string;
4
+ isDeleted: boolean;
5
+ createdAt: Date;
6
+ updatedAt: Date;
7
+ deletedAt: Date;
8
+ features: Types.ObjectId[];
9
+ }
10
+ export declare const UserGroupModel: import("mongoose").Model<IUserGroup, {}, {}, {}, import("mongoose").Document<unknown, {}, IUserGroup> & Omit<IUserGroup & {
11
+ _id: Types.ObjectId;
12
+ }, never>, any>;
13
+ export {};
14
+ //# sourceMappingURL=user-group.entitty.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user-group.entitty.d.ts","sourceRoot":"","sources":["../../src/entity/user-group.entitty.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,EAAE,MAAM,UAAU,CAAC;AAEhD,UAAU,UAAU;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,OAAO,CAAC;IACnB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,QAAQ,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC;CAC5B;AAcD,eAAO,MAAM,cAAc;SACmkI,MAAO,QAAQ;eAD/hI,CAAC"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UserGroupModel = void 0;
4
+ const mongoose_1 = require("mongoose");
5
+ const UserGroupSchema = new mongoose_1.Schema({
6
+ name: { type: String, unique: true, sparse: true },
7
+ isDeleted: { type: Boolean, default: false },
8
+ deletedAt: { type: Date, default: null },
9
+ features: { type: [{ type: mongoose_1.Schema.Types.ObjectId, ref: 'feature' }], default: [], _id: false },
10
+ }, {
11
+ timestamps: true,
12
+ });
13
+ exports.UserGroupModel = (0, mongoose_1.model)("user-group", UserGroupSchema);
@@ -1,7 +1,10 @@
1
+ import { Types } from "mongoose";
2
+ import { ROLES } from "../utilities";
1
3
  interface IUser {
2
4
  email: string | null;
3
5
  password: string;
4
- role: string;
6
+ role: ROLES;
7
+ groups: Array<Types.ObjectId>;
5
8
  isVerified: boolean;
6
9
  verificationToken: string;
7
10
  createdAt: Date;
@@ -10,7 +13,7 @@ interface IUser {
10
13
  isDeleted: boolean;
11
14
  }
12
15
  export declare const UserModel: import("mongoose").Model<IUser, {}, {}, {}, import("mongoose").Document<unknown, {}, IUser> & Omit<IUser & {
13
- _id: import("mongoose").Types.ObjectId;
16
+ _id: Types.ObjectId;
14
17
  }, never>, any>;
15
18
  export {};
16
19
  //# sourceMappingURL=user.entity.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"user.entity.d.ts","sourceRoot":"","sources":["../../src/entity/user.entity.ts"],"names":[],"mappings":"AAGA,UAAU,KAAK;IACb,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,OAAO,CAAC;IACpB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,OAAO,CAAC;CACpB;AAiBD,eAAO,MAAM,SAAS;;eAAmC,CAAC"}
1
+ {"version":3,"file":"user.entity.d.ts","sourceRoot":"","sources":["../../src/entity/user.entity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,EAAE,MAAM,UAAU,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAErC,UAAU,KAAK;IACb,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,KAAK,CAAC;IACZ,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;IAC7B,UAAU,EAAE,OAAO,CAAC;IACpB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,OAAO,CAAC;CACpB;AAkBD,eAAO,MAAM,SAAS;SACwyH,MAAO,QAAQ;eADpxH,CAAC"}
@@ -10,7 +10,8 @@ const UserSchema = new mongoose_1.Schema({
10
10
  isVerified: { type: Boolean, default: false },
11
11
  verificationToken: { type: String },
12
12
  deletedAt: { type: Date, default: null },
13
- isDeleted: { type: Boolean, default: false }
13
+ isDeleted: { type: Boolean, default: false },
14
+ groups: { type: [{ type: mongoose_1.Schema.Types.ObjectId, ref: 'user-group' }], default: [], _id: false }
14
15
  }, {
15
16
  timestamps: true,
16
17
  });
@@ -1,5 +1,8 @@
1
1
  import { Response, NextFunction } from "express";
2
+ import { FEATURE_TYPE, ROLES } from "../utilities";
2
3
  import { IRequest } from "../types";
3
- declare const authorize: (roles: string[]) => (req: IRequest, res: Response, next: NextFunction) => Promise<Response<any, Record<string, any>> | undefined>;
4
- export { authorize };
4
+ declare const authorize: () => (req: IRequest, res: Response, next: NextFunction) => Promise<Response<any, Record<string, any>> | undefined>;
5
+ declare const checkRoleAccess: (roles: ROLES[]) => (req: IRequest, res: Response, next: NextFunction) => Promise<Response<any, Record<string, any>> | undefined>;
6
+ declare const checkFeatureAccess: (features: FEATURE_TYPE[]) => (req: IRequest, res: Response, next: NextFunction) => Promise<Response<any, Record<string, any>> | undefined>;
7
+ export { authorize, checkFeatureAccess, checkRoleAccess };
5
8
  //# sourceMappingURL=authentication.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"authentication.d.ts","sourceRoot":"","sources":["../../src/middleware/authentication.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEjD,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAIpC,QAAA,MAAM,SAAS,UAAW,MAAM,EAAE,WACJ,QAAQ,OAAO,QAAQ,QAAQ,YAAY,4DA+BxE,CAAA;AAGD,OAAO,EAAC,SAAS,EAAC,CAAC"}
1
+ {"version":3,"file":"authentication.d.ts","sourceRoot":"","sources":["../../src/middleware/authentication.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,KAAK,EAAa,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAIpC,QAAA,MAAM,SAAS,cACe,QAAQ,OAAO,QAAQ,QAAQ,YAAY,4DA0BxE,CAAA;AAED,QAAA,MAAM,eAAe,UAAW,KAAK,EAAE,WACT,QAAQ,OAAO,QAAQ,QAAQ,YAAY,4DAmBxE,CAAA;AAED,QAAA,MAAM,kBAAkB,aAAc,YAAY,EAAE,WACtB,QAAQ,OAAO,QAAQ,QAAQ,YAAY,4DAmBxE,CAAA;AAGD,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAE,eAAe,EAAE,CAAC"}
@@ -11,30 +11,26 @@ var __rest = (this && this.__rest) || function (s, e) {
11
11
  return t;
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.authorize = void 0;
14
+ exports.checkRoleAccess = exports.checkFeatureAccess = exports.authorize = void 0;
15
15
  const utilities_1 = require("../utilities");
16
16
  const entity_1 = require("../entity");
17
- const authorize = (roles) => {
17
+ const authorize = () => {
18
18
  return async function (req, res, next) {
19
19
  if (!req.headers.authorization) {
20
20
  return res.status(401).json({ message: 'Unauthorized' });
21
21
  }
22
- if (!roles.length) {
23
- return res.status(401).json({ message: 'Unauthorized' });
24
- }
25
22
  if (req.headers.authorization) {
26
23
  const token = await (0, utilities_1.verifyUid)(req.headers.authorization);
27
24
  if (!token) {
28
25
  return res.status(401).json({ message: 'Session Expired' });
29
26
  }
30
- let user = await entity_1.UserModel.findById(token.value.id);
27
+ let user = await entity_1.UserModel.findById(token.value.id).populate({
28
+ path: "groups",
29
+ populate: { path: "features", select: "name" },
30
+ }).lean();
31
31
  if (!user) {
32
32
  return res.status(401).json({ message: 'No User Found' });
33
33
  }
34
- const hasAccess = roles.find(role => role === user.role);
35
- if (!hasAccess) {
36
- return res.status(403).json({ message: 'Forbbiden' });
37
- }
38
34
  const _a = user.toObject(), { createdAt, updatedAt, deletedAt, password } = _a, rest = __rest(_a, ["createdAt", "updatedAt", "deletedAt", "password"]);
39
35
  req.user = rest;
40
36
  next();
@@ -42,3 +38,36 @@ const authorize = (roles) => {
42
38
  };
43
39
  };
44
40
  exports.authorize = authorize;
41
+ const checkRoleAccess = (roles) => {
42
+ return async function (req, res, next) {
43
+ const { user } = req;
44
+ if (!user) {
45
+ return res.status(401).json({ message: 'Unauthorized' });
46
+ }
47
+ const hasAccess = roles.find(role => role === user.role);
48
+ if (!hasAccess) {
49
+ return res.status(403).json({ message: 'Forbbiden: Role access denied' });
50
+ }
51
+ user.isAdmin = user.role === utilities_1.ROLES.ADMIN;
52
+ if (user.isAdmin) {
53
+ user.groups.push({ name: "DEFAULT", features: Object.values(utilities_1.FEATURE_TYPE).map(value => ({ name: value })) });
54
+ }
55
+ next();
56
+ };
57
+ };
58
+ exports.checkRoleAccess = checkRoleAccess;
59
+ const checkFeatureAccess = (features) => {
60
+ return async function (req, res, next) {
61
+ const { user } = req;
62
+ if (!user) {
63
+ return res.status(401).json({ message: 'Unauthorized' });
64
+ }
65
+ const userFeatureNames = user.groups.flatMap(group => group.features ? group.features.map((feature) => feature.name) : []);
66
+ const hasAccess = features.some((feature) => userFeatureNames.includes(feature));
67
+ if (!hasAccess) {
68
+ return res.status(403).json({ message: "Forbidden: Feature access denied" });
69
+ }
70
+ next();
71
+ };
72
+ };
73
+ exports.checkFeatureAccess = checkFeatureAccess;
@@ -1,8 +1,20 @@
1
1
  import { Request } from "express";
2
+ import { FEATURE_TYPE, ROLES } from "../utilities";
3
+ export interface IRequestUser {
4
+ email: string | null;
5
+ role: ROLES;
6
+ groups: Array<{
7
+ name: string;
8
+ features: Array<{
9
+ name: FEATURE_TYPE;
10
+ }>;
11
+ }>;
12
+ isVerified: boolean;
13
+ deletedAt: Date;
14
+ isDeleted: boolean;
15
+ isAdmin: boolean;
16
+ }
2
17
  export interface IRequest extends Request {
3
- user?: any;
4
- role?: any;
5
- file?: any;
6
- features?: Array<any>;
18
+ user?: IRequestUser;
7
19
  }
8
20
  //# sourceMappingURL=IRequest.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"IRequest.d.ts","sourceRoot":"","sources":["../../src/types/IRequest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,MAAM,WAAW,QAAS,SAAQ,OAAO;IACrC,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,IAAI,CAAC,EAAC,GAAG,CAAC;IACV,QAAQ,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,CAAA;CACxB"}
1
+ {"version":3,"file":"IRequest.d.ts","sourceRoot":"","sources":["../../src/types/IRequest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAEnD,MAAM,WAAW,YAAY;IACzB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,IAAI,EAAE,KAAK,CAAC;IACZ,MAAM,EAAE,KAAK,CAAC;QACV,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,EAAE,KAAK,CAAC;YAAE,IAAI,EAAE,YAAY,CAAA;SAAE,CAAC,CAAA;KAC1C,CAAC,CAAA;IACF,UAAU,EAAE,OAAO,CAAC;IACpB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,OAAO,CAAC;IACnB,OAAO,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,WAAW,QAAS,SAAQ,OAAO;IACrC,IAAI,CAAC,EAAE,YAAY,CAAA;CACtB"}
@@ -17,4 +17,21 @@ export declare enum OWNERSHIP_TYPE {
17
17
  VIEWER = "VIEWER",
18
18
  EDITOR = "EDITOR"
19
19
  }
20
+ export declare enum FEATURE_TYPE {
21
+ ADD_USER = "ADD_USER",
22
+ DELETE_USER = "DELETE_USER",
23
+ MODIFY_USER = "MODIFY_USER",
24
+ LIST_USER = "LIST_USER",
25
+ ADD_USER_GROUP = "ADD_USER_GROUP",
26
+ DELETE_USER_GROUP = "DELETE_USER_GROUP",
27
+ MODIFY_USER_GROUP = "MODIFY_USER_GROUP",
28
+ LIST_USER_GROUP = "LIST_USER_GROUP",
29
+ PREVIEW_PRE_BUILT_TEMPLATE = "PREVIEW_PRE_BUILT_TEMPLATE",
30
+ CLONE_TEMPLATE = "CLONE_TEMPLATE",
31
+ CREATE_TEMPLATE = "CREATE_TEMPLATE",
32
+ DELETE_TEMPLATE = "DELETE_TEMPLATE",
33
+ SHARE_TEMPLATE = "SHARE_TEMPLATE",
34
+ UPDATE_TEAMPLATE = "UPDATE_TEAMPLATE",
35
+ LIST_TEMPLATE = "LIST_TEMPLATE"
36
+ }
20
37
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utilities/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AAEnC,oBAAY,KAAK;IACf,KAAK,UAAU;IACf,IAAI,SAAS;CACd;AAED,oBAAY,WAAW;IACrB,GAAG,QAAQ;IACX,OAAO,YAAY;IACnB,WAAW,gBAAgB;CAC5B;AAED,oBAAY,cAAc;IACxB,KAAK,UAAU;IACf,QAAQ,aAAa;IACrB,MAAM,WAAW;IACjB,MAAM,WAAW;CAClB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utilities/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AAEnC,oBAAY,KAAK;IACf,KAAK,UAAU;IACf,IAAI,SAAS;CACd;AAED,oBAAY,WAAW;IACrB,GAAG,QAAQ;IACX,OAAO,YAAY;IACnB,WAAW,gBAAgB;CAC5B;AAED,oBAAY,cAAc;IACxB,KAAK,UAAU;IACf,QAAQ,aAAa;IACrB,MAAM,WAAW;IACjB,MAAM,WAAW;CAClB;AAED,oBAAY,YAAY;IAEtB,QAAQ,aAAW;IACnB,WAAW,gBAAc;IACzB,WAAW,gBAAc;IACzB,SAAS,cAAY;IAGrB,cAAc,mBAAiB;IAC/B,iBAAiB,sBAAoB;IACrC,iBAAiB,sBAAoB;IACrC,eAAe,oBAAkB;IAGjC,0BAA0B,+BAA6B;IACvD,cAAc,mBAAiB;IAC/B,eAAe,oBAAkB;IACjC,eAAe,oBAAkB;IACjC,cAAc,mBAAiB;IAC/B,gBAAgB,qBAAmB;IACnC,aAAa,kBAAgB;CAC9B"}
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.OWNERSHIP_TYPE = exports.RECORD_TYPE = exports.ROLES = void 0;
17
+ exports.FEATURE_TYPE = exports.OWNERSHIP_TYPE = exports.RECORD_TYPE = exports.ROLES = void 0;
18
18
  __exportStar(require("./callWithRetries"), exports);
19
19
  __exportStar(require("./createFolder"), exports);
20
20
  __exportStar(require("./encryptionUtils"), exports);
@@ -37,3 +37,24 @@ var OWNERSHIP_TYPE;
37
37
  OWNERSHIP_TYPE["VIEWER"] = "VIEWER";
38
38
  OWNERSHIP_TYPE["EDITOR"] = "EDITOR";
39
39
  })(OWNERSHIP_TYPE || (exports.OWNERSHIP_TYPE = OWNERSHIP_TYPE = {}));
40
+ var FEATURE_TYPE;
41
+ (function (FEATURE_TYPE) {
42
+ // MANAGE USER
43
+ FEATURE_TYPE["ADD_USER"] = "ADD_USER";
44
+ FEATURE_TYPE["DELETE_USER"] = "DELETE_USER";
45
+ FEATURE_TYPE["MODIFY_USER"] = "MODIFY_USER";
46
+ FEATURE_TYPE["LIST_USER"] = "LIST_USER";
47
+ // MANAGE GROUPS
48
+ FEATURE_TYPE["ADD_USER_GROUP"] = "ADD_USER_GROUP";
49
+ FEATURE_TYPE["DELETE_USER_GROUP"] = "DELETE_USER_GROUP";
50
+ FEATURE_TYPE["MODIFY_USER_GROUP"] = "MODIFY_USER_GROUP";
51
+ FEATURE_TYPE["LIST_USER_GROUP"] = "LIST_USER_GROUP";
52
+ // MANAGE TEMPLATE
53
+ FEATURE_TYPE["PREVIEW_PRE_BUILT_TEMPLATE"] = "PREVIEW_PRE_BUILT_TEMPLATE";
54
+ FEATURE_TYPE["CLONE_TEMPLATE"] = "CLONE_TEMPLATE";
55
+ FEATURE_TYPE["CREATE_TEMPLATE"] = "CREATE_TEMPLATE";
56
+ FEATURE_TYPE["DELETE_TEMPLATE"] = "DELETE_TEMPLATE";
57
+ FEATURE_TYPE["SHARE_TEMPLATE"] = "SHARE_TEMPLATE";
58
+ FEATURE_TYPE["UPDATE_TEAMPLATE"] = "UPDATE_TEAMPLATE";
59
+ FEATURE_TYPE["LIST_TEMPLATE"] = "LIST_TEMPLATE";
60
+ })(FEATURE_TYPE || (exports.FEATURE_TYPE = FEATURE_TYPE = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "biz-email-builder-shared",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [