proto.io 0.0.171 → 0.0.172

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/dist/index.mjs CHANGED
@@ -848,7 +848,7 @@ class ProtoInternal {
848
848
  validateSchemaName(options.schema);
849
849
  const schema = mergeSchema(defaultSchema, options.fileStorage.schema, options.schema);
850
850
  validateSchema(schema);
851
- if (!_.every(options.roleInheritKeys, k => {
851
+ if (!_.every(options.roleResolver?.inheritKeys, k => {
852
852
  const type = resolveDataType(schema, 'Role', k);
853
853
  return type && isRelation(type) && _.includes(['User', 'Role'], type.target);
854
854
  })) {
@@ -1354,7 +1354,7 @@ class ProtoService extends ProtoType {
1354
1354
  constructor(options) {
1355
1355
  super();
1356
1356
  this[PVK] = new ProtoInternal({
1357
- roleInheritKeys: [],
1357
+ roleResolver: {},
1358
1358
  objectIdSize: 10,
1359
1359
  maxFetchLimit: 1000,
1360
1360
  maxUploadSize: 20 * 1024 * 1024,
@@ -1441,30 +1441,37 @@ class ProtoService extends ProtoType {
1441
1441
  return _.assign(payload, _.isFunction(attrs) ? attrs(payload) : attrs);
1442
1442
  }
1443
1443
  async userRoles(user) {
1444
- const roleInheritKeys = this[PVK].options.roleInheritKeys;
1445
- const schema = this.schema;
1446
- const userKeys = _.filter(roleInheritKeys, k => {
1447
- const type = resolveDataType(schema, 'Role', k);
1448
- return !!type && isRelation(type) && type.target === 'User';
1449
- });
1450
- const roleKeys = _.filter(roleInheritKeys, k => {
1451
- const type = resolveDataType(schema, 'Role', k);
1452
- return !!type && isRelation(type) && type.target === 'Role';
1453
- });
1454
- let queue = await this.Query('Role')
1455
- .or(_.map(_.uniq(['users', ...userKeys]), k => q => q.isIntersect(k, [user])))
1456
- .includes('name')
1457
- .find({ master: true });
1458
- let roles = queue;
1459
- while (!_.isEmpty(queue)) {
1460
- queue = await this.Query('Role')
1461
- .or(_.map(_.uniq(['roles', ...roleKeys]), k => q => q.isIntersect(k, queue)))
1462
- .notContainsIn('_id', _.compact(_.map(roles, x => x.objectId)))
1444
+ const self = this;
1445
+ const defaultResolver = async () => {
1446
+ const inheritKeys = self[PVK].options.roleResolver?.inheritKeys ?? [];
1447
+ const schema = self.schema;
1448
+ const userKeys = _.filter(inheritKeys, k => {
1449
+ const type = resolveDataType(schema, 'Role', k);
1450
+ return !!type && isRelation(type) && type.target === 'User';
1451
+ });
1452
+ const roleKeys = _.filter(inheritKeys, k => {
1453
+ const type = resolveDataType(schema, 'Role', k);
1454
+ return !!type && isRelation(type) && type.target === 'Role';
1455
+ });
1456
+ let queue = await self.Query('Role')
1457
+ .or(_.map(_.uniq(['users', ...userKeys]), k => q => q.isIntersect(k, [user])))
1463
1458
  .includes('name')
1464
1459
  .find({ master: true });
1465
- roles = _.uniqBy([...roles, ...queue], x => x.objectId);
1466
- }
1467
- return roles;
1460
+ let roles = queue;
1461
+ while (!_.isEmpty(queue)) {
1462
+ queue = await self.Query('Role')
1463
+ .or(_.map(_.uniq(['roles', ...roleKeys]), k => q => q.isIntersect(k, queue)))
1464
+ .notContainsIn('_id', _.compact(_.map(roles, x => x.objectId)))
1465
+ .includes('name')
1466
+ .find({ master: true });
1467
+ roles = _.uniqBy([...roles, ...queue], x => x.objectId);
1468
+ }
1469
+ return roles;
1470
+ };
1471
+ const resolver = self[PVK].options.roleResolver?.resolver;
1472
+ if (resolver)
1473
+ return resolver(user, defaultResolver);
1474
+ return defaultResolver();
1468
1475
  }
1469
1476
  async becomeUser(req, user, options) {
1470
1477
  if (!user.objectId)