aloux-iam 0.0.147 → 0.0.148

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.
package/lib/middleware.js CHANGED
@@ -58,7 +58,13 @@ const auth = async (req, res, next) => {
58
58
  { _id: data._id, "tokens.token": token, status: "Activo" },
59
59
  { tokens: 0, pwd: 0 },
60
60
  )
61
- .populate({ path: "_functions", populate: [{ path: "_permissions" }] })
61
+ .populate({
62
+ path: "_functions",
63
+ populate: [
64
+ { path: "_permissions", model: "Permission" },
65
+ { path: "_menus", model: "Menu" }
66
+ ]
67
+ })
62
68
  .lean();
63
69
 
64
70
  if (!user) {
@@ -1,14 +1,19 @@
1
- const mongoose = require('mongoose')
1
+ const mongoose = require("mongoose");
2
2
 
3
- const functionsSchema = mongoose.Schema({
4
- name: { type: String, required: true, trim: true, unique: true },
5
- description: { type: String, trim: true },
6
- _permissions: [ { type: mongoose.Schema.Types.ObjectId, required: true, ref: 'Permission' } ],
7
- _menus: [ { type: mongoose.Schema.Types.ObjectId, required: true, ref: 'Menu' } ],
8
- status: { type: String, required: true, enum: ['Activo','Inactivo'] },
9
- createdAt: { type: Number },
10
- lastUpdate: { type: Number }
11
- })
3
+ const permissionSchema = mongoose.Schema({
4
+ description: { type: String, required: true, trim: true },
5
+ method: { type: String, required: true },
6
+ api: { type: String, required: true },
7
+ endpoint: { type: String, required: true },
8
+ auth: { type: Number, required: true, default: 1 },
9
+ default: { type: Boolean },
10
+ status: { type: String, required: true, enum: ["Activo", "Inactivo"] },
11
+ createdAt: { type: Number },
12
+ lastUpdate: { type: Number },
13
+ });
12
14
 
13
- const Functions = mongoose.model("Functions", functionsSchema)
14
- module.exports = Functions
15
+ // Índice compuesto: method + endpoint sí son únicos juntos
16
+ permissionSchema.index({ method: 1, endpoint: 1 }, { unique: true });
17
+
18
+ const Permission = mongoose.model("Permission", permissionSchema);
19
+ module.exports = Permission;
@@ -312,14 +312,8 @@ self.getMenu = (user) => {
312
312
  };
313
313
 
314
314
  self.me = async (req, res) => {
315
- // let user = await User.findOne({ _id: req.user._id }, { "tokens": 0, pwd: 0 }).populate([
316
- // { path: "_business" },
317
- // { path: "_functions", populate: [{ path: "_permissions" }, { path: "_menus" }] },
318
- // ]).lean()
319
-
320
315
  const _id = req.user._id;
321
316
 
322
- // Valida que los modelos existan hantes de hacer una consulta con populate
323
317
  if (
324
318
  mongoose.modelNames().includes("Business") &&
325
319
  mongoose.modelNames().includes("Client")
@@ -328,7 +322,10 @@ self.me = async (req, res) => {
328
322
  .populate([
329
323
  {
330
324
  path: "_functions",
331
- populate: [{ path: "_permissions" }, { path: "_menus" }],
325
+ populate: [
326
+ { path: "_permissions", model: "Permission" },
327
+ { path: "_menus", model: "Menu" }
328
+ ],
332
329
  },
333
330
  { path: "_business" },
334
331
  { path: "_client" },
@@ -340,7 +337,10 @@ self.me = async (req, res) => {
340
337
  .populate([
341
338
  {
342
339
  path: "_functions",
343
- populate: [{ path: "_permissions" }, { path: "_menus" }],
340
+ populate: [
341
+ { path: "_permissions", model: "Permission" },
342
+ { path: "_menus", model: "Menu" }
343
+ ],
344
344
  },
345
345
  { path: "_business" },
346
346
  ])
@@ -351,7 +351,10 @@ self.me = async (req, res) => {
351
351
  .populate([
352
352
  {
353
353
  path: "_functions",
354
- populate: [{ path: "_permissions" }, { path: "_menus" }],
354
+ populate: [
355
+ { path: "_permissions", model: "Permission" },
356
+ { path: "_menus", model: "Menu" }
357
+ ],
355
358
  },
356
359
  { path: "_client" },
357
360
  ])
@@ -362,14 +365,16 @@ self.me = async (req, res) => {
362
365
  .populate([
363
366
  {
364
367
  path: "_functions",
365
- populate: [{ path: "_permissions" }, { path: "_menus" }],
368
+ populate: [
369
+ { path: "_permissions", model: "Permission" },
370
+ { path: "_menus", model: "Menu" }
371
+ ],
366
372
  },
367
373
  ])
368
374
  .select("-pwd -tokens")
369
375
  .lean();
370
376
  }
371
377
 
372
- // Obtener menús y funciones sin repertir y activas
373
378
  user.menus = self.getMenu(user);
374
379
  user.permissions = self.getPermission(user);
375
380
  for (let i in user._functions) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aloux-iam",
3
- "version": "0.0.147",
3
+ "version": "0.0.148",
4
4
  "description": "Aloux IAM for APIs ",
5
5
  "main": "index.js",
6
6
  "scripts": {