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