roboto-js 1.7.4 → 1.8.1

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,351 @@ 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
453
+ const iac = this.get('iac') || {};
454
+
455
+ // Initialize readGrants if it doesn't exist
456
+ if (!iac.readGrants) {
457
+ iac.readGrants = {};
458
+ }
459
+
460
+ // Initialize users array if it doesn't exist
461
+ if (!Array.isArray(iac.readGrants.users)) {
462
+ iac.readGrants.users = [];
463
+ }
464
+
465
+ if (publish) {
466
+ // Add public_user if not already present
467
+ if (!iac.readGrants.users.includes('public_user')) {
468
+ iac.readGrants.users.push('public_user');
469
+ }
470
+ } else {
471
+ // Remove public_user
472
+ iac.readGrants.users = iac.readGrants.users.filter(userId => userId !== 'public_user');
473
+ }
474
+
475
+ // Update the object
476
+ this.set('iac', iac);
477
+
478
+ // Save if requested
479
+ if (save) {
480
+ return await this.save();
481
+ }
482
+
483
+ return this;
484
+ }
485
+
486
+ /**
487
+ * Unpublishes this object to remove public access.
488
+ * Removes 'public_user' from the IAC read permissions.
489
+ * This is an alias for publishObject({ publish: false }) for better code clarity.
490
+ *
491
+ * @param {Object} options - Unpublishing options
492
+ * @param {boolean} [options.save=true] - If true, automatically saves the object after updating permissions
493
+ * @returns {Promise<RbtObject>} - Returns this object (saved if options.save is true)
494
+ *
495
+ * @example
496
+ * // Unpublish an object (remove public access)
497
+ * await myObject.unpublishObject();
498
+ *
499
+ * @example
500
+ * // Unpublish without auto-saving
501
+ * await myObject.unpublishObject({ save: false });
502
+ * // ... make other changes ...
503
+ * await myObject.save();
504
+ */
505
+ async unpublishObject(options = {}) {
506
+ return await this.publishObject({
507
+ publish: false,
508
+ save: options.save !== undefined ? options.save : true
509
+ });
510
+ }
511
+
512
+ /**
513
+ * Revokes access from specific users and/or user groups.
514
+ *
515
+ * @param {Object} options - Access revocation options
516
+ * @param {string[]} [options.userIds=[]] - Array of user IDs to remove from read or write access
517
+ * @param {string[]} [options.groupIds=[]] - Array of group IDs to remove from read or write access
518
+ * @param {boolean} [options.write=false] - If true, removes write access; if false, removes read access
519
+ * @param {boolean} [options.save=true] - If true, automatically saves the object after updating permissions
520
+ * @returns {Promise<RbtObject>} - Returns this object (saved if options.save is true)
521
+ *
522
+ * @example
523
+ * // Revoke read access from specific users
524
+ * await myObject.revokeAccess({
525
+ * userIds: ['user_123', 'user_456']
526
+ * });
527
+ *
528
+ * @example
529
+ * // Revoke write access from specific groups
530
+ * await myObject.revokeAccess({
531
+ * groupIds: ['grpEditors'],
532
+ * write: true
533
+ * });
534
+ *
535
+ * @example
536
+ * // Revoke access from users and groups
537
+ * await myObject.revokeAccess({
538
+ * userIds: ['user_123'],
539
+ * groupIds: ['grpViewers']
540
+ * });
541
+ *
542
+ * @example
543
+ * // Revoke without auto-saving
544
+ * await myObject.revokeAccess({
545
+ * userIds: ['user_123'],
546
+ * save: false
547
+ * });
548
+ */
549
+ async revokeAccess(options = {}) {
550
+ const {
551
+ userIds = [],
552
+ groupIds = [],
553
+ write = false,
554
+ save = true
555
+ } = options;
556
+
557
+ // Validate inputs
558
+ if (!Array.isArray(userIds)) {
559
+ throw new Error('userIds must be an array');
560
+ }
561
+ if (!Array.isArray(groupIds)) {
562
+ throw new Error('groupIds must be an array');
563
+ }
564
+
565
+ // Get current IAC settings
566
+ const iac = this.get('iac') || {};
567
+
568
+ // Determine which grant type to update (read or write)
569
+ const grantType = write ? 'writeGrants' : 'readGrants';
570
+
571
+ // Initialize grants if they don't exist
572
+ if (!iac[grantType]) {
573
+ iac[grantType] = {};
574
+ }
575
+
576
+ // Remove specified users
577
+ if (userIds.length > 0 && Array.isArray(iac[grantType].users)) {
578
+ iac[grantType].users = iac[grantType].users.filter(
579
+ userId => !userIds.includes(userId)
580
+ );
581
+ }
582
+
583
+ // Remove specified groups
584
+ if (groupIds.length > 0 && Array.isArray(iac[grantType].userGroups)) {
585
+ iac[grantType].userGroups = iac[grantType].userGroups.filter(
586
+ groupId => !groupIds.includes(groupId)
587
+ );
588
+ }
589
+
590
+ // Update the object
591
+ this.set('iac', iac);
592
+
593
+ // Save if requested
594
+ if (save) {
595
+ return await this.save();
596
+ }
597
+
598
+ return this;
599
+ }
600
+
601
+ /**
602
+ * Checks if this object is currently published (publicly accessible).
603
+ *
604
+ * @returns {boolean} - True if 'public_user' is in the read grants, false otherwise
605
+ *
606
+ * @example
607
+ * if (myObject.isPublished()) {
608
+ * console.log('Object is public');
609
+ * }
610
+ */
611
+ isPublished() {
612
+ const iac = this.get('iac');
613
+ if (!iac || !iac.readGrants || !Array.isArray(iac.readGrants.users)) {
614
+ return false;
615
+ }
616
+ return iac.readGrants.users.includes('public_user');
617
+ }
618
+
619
+ /**
620
+ * Gets the current sharing permissions for this object.
621
+ *
622
+ * @returns {Object} - Object containing read and write grants
623
+ * @returns {Object} returns.readGrants - Read access grants
624
+ * @returns {string[]} returns.readGrants.users - Array of user IDs with read access
625
+ * @returns {string[]} returns.readGrants.userGroups - Array of group IDs with read access
626
+ * @returns {string[]} returns.readGrants.organizations - Array of organization IDs with read access
627
+ * @returns {Object} returns.writeGrants - Write access grants
628
+ * @returns {string[]} returns.writeGrants.users - Array of user IDs with write access
629
+ * @returns {string[]} returns.writeGrants.userGroups - Array of group IDs with write access
630
+ * @returns {string[]} returns.writeGrants.organizations - Array of organization IDs with write access
631
+ *
632
+ * @example
633
+ * const permissions = myObject.getSharing();
634
+ * console.log('Read users:', permissions.readGrants.users);
635
+ * console.log('Read groups:', permissions.readGrants.userGroups);
636
+ */
637
+ getSharing() {
638
+ const iac = this.get('iac') || {};
639
+ return {
640
+ readGrants: {
641
+ users: iac.readGrants?.users || [],
642
+ userGroups: iac.readGrants?.userGroups || [],
643
+ organizations: iac.readGrants?.organizations || [],
644
+ userSegments: iac.readGrants?.userSegments || []
645
+ },
646
+ writeGrants: {
647
+ users: iac.writeGrants?.users || [],
648
+ userGroups: iac.writeGrants?.userGroups || [],
649
+ organizations: iac.writeGrants?.organizations || [],
650
+ userSegments: iac.writeGrants?.userSegments || []
651
+ }
652
+ };
653
+ }
654
+
310
655
  async delete() {
311
656
  if (!this._internalData.type) {
312
657
  throw new Error('Cannot delete object without type');