mirror of
https://github.com/bitwarden/cli
synced 2025-12-12 14:23:32 +00:00
org collection edit command
This commit is contained in:
@@ -25,6 +25,8 @@ import { OrganizationCollectionRequest } from '../models/request/organizationCol
|
|||||||
|
|
||||||
import { CliUtils } from '../utils';
|
import { CliUtils } from '../utils';
|
||||||
|
|
||||||
|
import { Utils } from 'jslib/misc/utils';
|
||||||
|
|
||||||
export class CreateCommand {
|
export class CreateCommand {
|
||||||
constructor(private cipherService: CipherService, private folderService: FolderService,
|
constructor(private cipherService: CipherService, private folderService: FolderService,
|
||||||
private userService: UserService, private cryptoService: CryptoService,
|
private userService: UserService, private cryptoService: CryptoService,
|
||||||
@@ -57,7 +59,7 @@ export class CreateCommand {
|
|||||||
case 'folder':
|
case 'folder':
|
||||||
return await this.createFolder(req);
|
return await this.createFolder(req);
|
||||||
case 'org-collection':
|
case 'org-collection':
|
||||||
return await this.createOrganizationCollection(req);
|
return await this.createOrganizationCollection(req, cmd);
|
||||||
default:
|
default:
|
||||||
return Response.badRequest('Unknown object.');
|
return Response.badRequest('Unknown object.');
|
||||||
}
|
}
|
||||||
@@ -130,7 +132,16 @@ export class CreateCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async createOrganizationCollection(req: OrganizationCollectionRequest) {
|
private async createOrganizationCollection(req: OrganizationCollectionRequest, cmd: program.Command) {
|
||||||
|
if (cmd.organizationid == null || cmd.organizationid === '') {
|
||||||
|
return Response.badRequest('--organizationid <organizationid> required.');
|
||||||
|
}
|
||||||
|
if (!Utils.isGuid(cmd.organizationid)) {
|
||||||
|
return Response.error('`' + cmd.organizationid + '` is not a GUID.');
|
||||||
|
}
|
||||||
|
if (cmd.organizationid !== req.organizationId) {
|
||||||
|
return Response.error('--organizationid <organizationid> does not match request object.');
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const orgKey = await this.cryptoService.getOrgKey(req.organizationId);
|
const orgKey = await this.cryptoService.getOrgKey(req.organizationId);
|
||||||
if (orgKey == null) {
|
if (orgKey == null) {
|
||||||
|
|||||||
@@ -1,20 +1,32 @@
|
|||||||
import * as program from 'commander';
|
import * as program from 'commander';
|
||||||
|
|
||||||
|
import { ApiService } from 'jslib/abstractions/api.service';
|
||||||
import { CipherService } from 'jslib/abstractions/cipher.service';
|
import { CipherService } from 'jslib/abstractions/cipher.service';
|
||||||
import { FolderService } from 'jslib/services/folder.service';
|
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
||||||
|
import { FolderService } from 'jslib/abstractions/folder.service';
|
||||||
|
|
||||||
import { Cipher } from 'jslib/models/export/cipher';
|
import { Cipher } from 'jslib/models/export/cipher';
|
||||||
|
import { Collection } from 'jslib/models/export/collection';
|
||||||
import { Folder } from 'jslib/models/export/folder';
|
import { Folder } from 'jslib/models/export/folder';
|
||||||
|
|
||||||
|
import { CollectionRequest } from 'jslib/models/request/collectionRequest';
|
||||||
|
import { SelectionReadOnlyRequest } from 'jslib/models/request/selectionReadOnlyRequest';
|
||||||
|
|
||||||
import { Response } from 'jslib/cli/models/response';
|
import { Response } from 'jslib/cli/models/response';
|
||||||
|
|
||||||
import { CipherResponse } from '../models/response/cipherResponse';
|
import { CipherResponse } from '../models/response/cipherResponse';
|
||||||
import { FolderResponse } from '../models/response/folderResponse';
|
import { FolderResponse } from '../models/response/folderResponse';
|
||||||
|
import { OrganizationCollectionResponse } from '../models/response/organizationCollectionResponse';
|
||||||
|
|
||||||
|
import { OrganizationCollectionRequest } from '../models/request/organizationCollectionRequest';
|
||||||
|
|
||||||
import { CliUtils } from '../utils';
|
import { CliUtils } from '../utils';
|
||||||
|
|
||||||
|
import { Utils } from 'jslib/misc/utils';
|
||||||
|
|
||||||
export class EditCommand {
|
export class EditCommand {
|
||||||
constructor(private cipherService: CipherService, private folderService: FolderService) { }
|
constructor(private cipherService: CipherService, private folderService: FolderService,
|
||||||
|
private cryptoService: CryptoService, private apiService: ApiService) { }
|
||||||
|
|
||||||
async run(object: string, id: string, requestJson: string, cmd: program.Command): Promise<Response> {
|
async run(object: string, id: string, requestJson: string, cmd: program.Command): Promise<Response> {
|
||||||
if (requestJson == null || requestJson === '') {
|
if (requestJson == null || requestJson === '') {
|
||||||
@@ -44,6 +56,8 @@ export class EditCommand {
|
|||||||
return await this.editCipherCollections(id, req);
|
return await this.editCipherCollections(id, req);
|
||||||
case 'folder':
|
case 'folder':
|
||||||
return await this.editFolder(id, req);
|
return await this.editFolder(id, req);
|
||||||
|
case 'org-collection':
|
||||||
|
return await this.editOrganizationCollection(id, req, cmd);
|
||||||
default:
|
default:
|
||||||
return Response.badRequest('Unknown object.');
|
return Response.badRequest('Unknown object.');
|
||||||
}
|
}
|
||||||
@@ -109,4 +123,37 @@ export class EditCommand {
|
|||||||
return Response.error(e);
|
return Response.error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async editOrganizationCollection(id: string, req: OrganizationCollectionRequest, cmd: program.Command) {
|
||||||
|
if (cmd.organizationid == null || cmd.organizationid === '') {
|
||||||
|
return Response.badRequest('--organizationid <organizationid> required.');
|
||||||
|
}
|
||||||
|
if (!Utils.isGuid(id)) {
|
||||||
|
return Response.error('`' + id + '` is not a GUID.');
|
||||||
|
}
|
||||||
|
if (!Utils.isGuid(cmd.organizationid)) {
|
||||||
|
return Response.error('`' + cmd.organizationid + '` is not a GUID.');
|
||||||
|
}
|
||||||
|
if (cmd.organizationid !== req.organizationId) {
|
||||||
|
return Response.error('--organizationid <organizationid> does not match request object.');
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const orgKey = await this.cryptoService.getOrgKey(req.organizationId);
|
||||||
|
if (orgKey == null) {
|
||||||
|
throw new Error('No encryption key for this organization.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const groups = req.groups == null ? null :
|
||||||
|
req.groups.map((g) => new SelectionReadOnlyRequest(g.id, g.readOnly));
|
||||||
|
const request = new CollectionRequest();
|
||||||
|
request.name = (await this.cryptoService.encrypt(req.name, orgKey)).encryptedString;
|
||||||
|
request.externalId = req.externalId;
|
||||||
|
request.groups = groups;
|
||||||
|
await this.apiService.putCollection(req.organizationId, id, request);
|
||||||
|
const res = new OrganizationCollectionResponse(Collection.toView(req), groups);
|
||||||
|
return Response.success(res);
|
||||||
|
} catch (e) {
|
||||||
|
return Response.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -313,6 +313,7 @@ export class Program extends BaseProgram {
|
|||||||
.command('create <object> [encodedJson]')
|
.command('create <object> [encodedJson]')
|
||||||
.option('--file <file>', 'Path to file for attachment.')
|
.option('--file <file>', 'Path to file for attachment.')
|
||||||
.option('--itemid <itemid>', 'Attachment\'s item id.')
|
.option('--itemid <itemid>', 'Attachment\'s item id.')
|
||||||
|
.option('--organizationid <organizationid>', 'Organization id for an organization object.')
|
||||||
.description('Create an object in the vault.')
|
.description('Create an object in the vault.')
|
||||||
.on('--help', () => {
|
.on('--help', () => {
|
||||||
writeLn('\n Objects:');
|
writeLn('\n Objects:');
|
||||||
@@ -344,6 +345,7 @@ export class Program extends BaseProgram {
|
|||||||
|
|
||||||
program
|
program
|
||||||
.command('edit <object> <id> [encodedJson]')
|
.command('edit <object> <id> [encodedJson]')
|
||||||
|
.option('--organizationid <organizationid>', 'Organization id for an organization object.')
|
||||||
.description('Edit an object from the vault.')
|
.description('Edit an object from the vault.')
|
||||||
.on('--help', () => {
|
.on('--help', () => {
|
||||||
writeLn('\n Objects:');
|
writeLn('\n Objects:');
|
||||||
@@ -351,6 +353,7 @@ export class Program extends BaseProgram {
|
|||||||
writeLn(' item');
|
writeLn(' item');
|
||||||
writeLn(' item-collections');
|
writeLn(' item-collections');
|
||||||
writeLn(' folder');
|
writeLn(' folder');
|
||||||
|
writeLn(' org-collection');
|
||||||
writeLn('');
|
writeLn('');
|
||||||
writeLn(' Id:');
|
writeLn(' Id:');
|
||||||
writeLn('');
|
writeLn('');
|
||||||
@@ -371,7 +374,8 @@ export class Program extends BaseProgram {
|
|||||||
})
|
})
|
||||||
.action(async (object, id, encodedJson, cmd) => {
|
.action(async (object, id, encodedJson, cmd) => {
|
||||||
await this.exitIfLocked();
|
await this.exitIfLocked();
|
||||||
const command = new EditCommand(this.main.cipherService, this.main.folderService);
|
const command = new EditCommand(this.main.cipherService, this.main.folderService,
|
||||||
|
this.main.cryptoService, this.main.apiService);
|
||||||
const response = await command.run(object, id, encodedJson, cmd);
|
const response = await command.run(object, id, encodedJson, cmd);
|
||||||
this.processResponse(response);
|
this.processResponse(response);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user