roboto-js 1.7.5 → 1.8.2

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.
@@ -292,6 +292,344 @@ export default class RbtObject {
292
292
  throw e;
293
293
  }
294
294
  }
295
+
296
+ /**
297
+ * Grants access to this object for specific users and/or user groups.
298
+ * Updates the IAC (Identity and Access Control) permissions.
299
+ *
300
+ * @param {Object} options - Access grant options
301
+ * @param {string[]} [options.userIds=[]] - Array of user IDs to grant read access
302
+ * @param {string[]} [options.groupIds=[]] - Array of user group IDs to grant read access
303
+ * @param {boolean} [options.write=false] - If true, grants write access instead of read access
304
+ * @param {boolean} [options.replace=false] - If true, replaces existing grants; if false, merges with existing
305
+ * @param {boolean} [options.save=true] - If true, automatically saves the object after updating permissions
306
+ * @returns {Promise<RbtObject>} - Returns this object (saved if options.save is true)
307
+ *
308
+ * @example
309
+ * // Grant read access to specific users
310
+ * await myObject.grantAccess({
311
+ * userIds: ['user123', 'user456']
312
+ * });
313
+ *
314
+ * @example
315
+ * // Grant read access to user groups
316
+ * await myObject.grantAccess({
317
+ * groupIds: ['grpRngAccount', 'grpAdmins']
318
+ * });
319
+ *
320
+ * @example
321
+ * // Grant write access to users and groups
322
+ * await myObject.grantAccess({
323
+ * userIds: ['user123'],
324
+ * groupIds: ['grpAdmins'],
325
+ * write: true
326
+ * });
327
+ *
328
+ * @example
329
+ * // Replace existing permissions instead of merging
330
+ * await myObject.grantAccess({
331
+ * userIds: ['user123'],
332
+ * replace: true
333
+ * });
334
+ *
335
+ * @example
336
+ * // Update permissions without auto-saving
337
+ * await myObject.grantAccess({
338
+ * userIds: ['user123'],
339
+ * save: false
340
+ * });
341
+ * // ... make other changes ...
342
+ * await myObject.save();
343
+ */
344
+ async grantAccess(options = {}) {
345
+ const {
346
+ userIds = [],
347
+ groupIds = [],
348
+ write = false,
349
+ replace = false,
350
+ save = true
351
+ } = options;
352
+
353
+ // Validate inputs
354
+ if (!Array.isArray(userIds)) {
355
+ throw new Error('userIds must be an array');
356
+ }
357
+ if (!Array.isArray(groupIds)) {
358
+ throw new Error('groupIds must be an array');
359
+ }
360
+
361
+ // Get current IAC settings
362
+ const iac = this.get('iac') || {};
363
+
364
+ // Determine which grant type to update (read or write)
365
+ const grantType = write ? 'writeGrants' : 'readGrants';
366
+
367
+ // Initialize grants if they don't exist
368
+ if (!iac[grantType]) {
369
+ iac[grantType] = {};
370
+ }
371
+
372
+ // Handle users
373
+ if (userIds.length > 0) {
374
+ if (replace) {
375
+ // Replace existing users
376
+ iac[grantType].users = [...userIds];
377
+ } else {
378
+ // Merge with existing users (avoiding duplicates)
379
+ const existingUsers = iac[grantType].users || [];
380
+ const mergedUsers = [...new Set([...existingUsers, ...userIds])];
381
+ iac[grantType].users = mergedUsers;
382
+ }
383
+ }
384
+
385
+ // Handle user groups
386
+ if (groupIds.length > 0) {
387
+ if (replace) {
388
+ // Replace existing groups
389
+ iac[grantType].userGroups = [...groupIds];
390
+ } else {
391
+ // Merge with existing groups (avoiding duplicates)
392
+ const existingGroups = iac[grantType].userGroups || [];
393
+ const mergedGroups = [...new Set([...existingGroups, ...groupIds])];
394
+ iac[grantType].userGroups = mergedGroups;
395
+ }
396
+ }
397
+
398
+ // Update the object
399
+ this.set('iac', iac);
400
+
401
+ // Save if requested
402
+ if (save) {
403
+ return await this.save();
404
+ }
405
+ return this;
406
+ }
407
+
408
+ /**
409
+ * Publishes this object to make it publicly accessible (or unpublishes it).
410
+ * Adds or removes 'public_user' from the IAC read permissions.
411
+ *
412
+ * @param {Object} options - Publishing options
413
+ * @param {boolean} [options.publish=true] - If true, publishes the object; if false, unpublishes it
414
+ * @param {boolean} [options.save=true] - If true, automatically saves the object after updating permissions
415
+ * @returns {Promise<RbtObject>} - Returns this object (saved if options.save is true)
416
+ *
417
+ * @example
418
+ * // Publish an object (make it public)
419
+ * await myObject.publishObject();
420
+ *
421
+ * @example
422
+ * // Unpublish an object (make it private)
423
+ * await myObject.publishObject({ publish: false });
424
+ *
425
+ * @example
426
+ * // Publish without auto-saving
427
+ * await myObject.publishObject({ save: false });
428
+ * // ... make other changes ...
429
+ * await myObject.save();
430
+ */
431
+ async publishObject(options = {}) {
432
+ const {
433
+ publish = true,
434
+ save = true
435
+ } = options;
436
+
437
+ // Get current IAC settings and create a deep clone to ensure change detection
438
+ const currentIac = this.get('iac') || {};
439
+ const iac = _.cloneDeep(currentIac);
440
+
441
+ // Initialize readGrants if it doesn't exist
442
+ if (!iac.readGrants) {
443
+ iac.readGrants = {};
444
+ }
445
+
446
+ // Initialize users array if it doesn't exist
447
+ if (!Array.isArray(iac.readGrants.users)) {
448
+ iac.readGrants.users = [];
449
+ }
450
+ if (publish) {
451
+ // Add public_user if not already present
452
+ if (!iac.readGrants.users.includes('public_user')) {
453
+ iac.readGrants.users.push('public_user');
454
+ }
455
+ } else {
456
+ // Remove public_user
457
+ iac.readGrants.users = iac.readGrants.users.filter(userId => userId !== 'public_user');
458
+ }
459
+
460
+ // Update the object with the cloned and modified IAC
461
+ this.set('iac', iac);
462
+
463
+ // Save if requested
464
+ if (save) {
465
+ return await this.save();
466
+ }
467
+ return this;
468
+ }
469
+
470
+ /**
471
+ * Unpublishes this object to remove public access.
472
+ * Removes 'public_user' from the IAC read permissions.
473
+ * This is an alias for publishObject({ publish: false }) for better code clarity.
474
+ *
475
+ * @param {Object} options - Unpublishing options
476
+ * @param {boolean} [options.save=true] - If true, automatically saves the object after updating permissions
477
+ * @returns {Promise<RbtObject>} - Returns this object (saved if options.save is true)
478
+ *
479
+ * @example
480
+ * // Unpublish an object (remove public access)
481
+ * await myObject.unpublishObject();
482
+ *
483
+ * @example
484
+ * // Unpublish without auto-saving
485
+ * await myObject.unpublishObject({ save: false });
486
+ * // ... make other changes ...
487
+ * await myObject.save();
488
+ */
489
+ async unpublishObject(options = {}) {
490
+ return await this.publishObject({
491
+ publish: false,
492
+ save: options.save !== undefined ? options.save : true
493
+ });
494
+ }
495
+
496
+ /**
497
+ * Revokes access from specific users and/or user groups.
498
+ *
499
+ * @param {Object} options - Access revocation options
500
+ * @param {string[]} [options.userIds=[]] - Array of user IDs to remove from read or write access
501
+ * @param {string[]} [options.groupIds=[]] - Array of group IDs to remove from read or write access
502
+ * @param {boolean} [options.write=false] - If true, removes write access; if false, removes read access
503
+ * @param {boolean} [options.save=true] - If true, automatically saves the object after updating permissions
504
+ * @returns {Promise<RbtObject>} - Returns this object (saved if options.save is true)
505
+ *
506
+ * @example
507
+ * // Revoke read access from specific users
508
+ * await myObject.revokeAccess({
509
+ * userIds: ['user_123', 'user_456']
510
+ * });
511
+ *
512
+ * @example
513
+ * // Revoke write access from specific groups
514
+ * await myObject.revokeAccess({
515
+ * groupIds: ['grpEditors'],
516
+ * write: true
517
+ * });
518
+ *
519
+ * @example
520
+ * // Revoke access from users and groups
521
+ * await myObject.revokeAccess({
522
+ * userIds: ['user_123'],
523
+ * groupIds: ['grpViewers']
524
+ * });
525
+ *
526
+ * @example
527
+ * // Revoke without auto-saving
528
+ * await myObject.revokeAccess({
529
+ * userIds: ['user_123'],
530
+ * save: false
531
+ * });
532
+ */
533
+ async revokeAccess(options = {}) {
534
+ const {
535
+ userIds = [],
536
+ groupIds = [],
537
+ write = false,
538
+ save = true
539
+ } = options;
540
+
541
+ // Validate inputs
542
+ if (!Array.isArray(userIds)) {
543
+ throw new Error('userIds must be an array');
544
+ }
545
+ if (!Array.isArray(groupIds)) {
546
+ throw new Error('groupIds must be an array');
547
+ }
548
+
549
+ // Get current IAC settings
550
+ const iac = this.get('iac') || {};
551
+
552
+ // Determine which grant type to update (read or write)
553
+ const grantType = write ? 'writeGrants' : 'readGrants';
554
+
555
+ // Initialize grants if they don't exist
556
+ if (!iac[grantType]) {
557
+ iac[grantType] = {};
558
+ }
559
+
560
+ // Remove specified users
561
+ if (userIds.length > 0 && Array.isArray(iac[grantType].users)) {
562
+ iac[grantType].users = iac[grantType].users.filter(userId => !userIds.includes(userId));
563
+ }
564
+
565
+ // Remove specified groups
566
+ if (groupIds.length > 0 && Array.isArray(iac[grantType].userGroups)) {
567
+ iac[grantType].userGroups = iac[grantType].userGroups.filter(groupId => !groupIds.includes(groupId));
568
+ }
569
+
570
+ // Update the object
571
+ this.set('iac', iac);
572
+
573
+ // Save if requested
574
+ if (save) {
575
+ return await this.save();
576
+ }
577
+ return this;
578
+ }
579
+
580
+ /**
581
+ * Checks if this object is currently published (publicly accessible).
582
+ *
583
+ * @returns {boolean} - True if 'public_user' is in the read grants, false otherwise
584
+ *
585
+ * @example
586
+ * if (myObject.isPublished()) {
587
+ * console.log('Object is public');
588
+ * }
589
+ */
590
+ isPublished() {
591
+ const iac = this.get('iac');
592
+ if (!iac || !iac.readGrants || !Array.isArray(iac.readGrants.users)) {
593
+ return false;
594
+ }
595
+ return iac.readGrants.users.includes('public_user');
596
+ }
597
+
598
+ /**
599
+ * Gets the current sharing permissions for this object.
600
+ *
601
+ * @returns {Object} - Object containing read and write grants
602
+ * @returns {Object} returns.readGrants - Read access grants
603
+ * @returns {string[]} returns.readGrants.users - Array of user IDs with read access
604
+ * @returns {string[]} returns.readGrants.userGroups - Array of group IDs with read access
605
+ * @returns {string[]} returns.readGrants.organizations - Array of organization IDs with read access
606
+ * @returns {Object} returns.writeGrants - Write access grants
607
+ * @returns {string[]} returns.writeGrants.users - Array of user IDs with write access
608
+ * @returns {string[]} returns.writeGrants.userGroups - Array of group IDs with write access
609
+ * @returns {string[]} returns.writeGrants.organizations - Array of organization IDs with write access
610
+ *
611
+ * @example
612
+ * const permissions = myObject.getSharing();
613
+ * console.log('Read users:', permissions.readGrants.users);
614
+ * console.log('Read groups:', permissions.readGrants.userGroups);
615
+ */
616
+ getSharing() {
617
+ const iac = this.get('iac') || {};
618
+ return {
619
+ readGrants: {
620
+ users: iac.readGrants?.users || [],
621
+ userGroups: iac.readGrants?.userGroups || [],
622
+ organizations: iac.readGrants?.organizations || [],
623
+ userSegments: iac.readGrants?.userSegments || []
624
+ },
625
+ writeGrants: {
626
+ users: iac.writeGrants?.users || [],
627
+ userGroups: iac.writeGrants?.userGroups || [],
628
+ organizations: iac.writeGrants?.organizations || [],
629
+ userSegments: iac.writeGrants?.userSegments || []
630
+ }
631
+ };
632
+ }
295
633
  async delete() {
296
634
  if (!this._internalData.type) {
297
635
  throw new Error('Cannot delete object without type');