1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-07 09:31:31 +01:00

org collection edit command

This commit is contained in:
Kyle Spearrin 2019-10-01 11:16:24 -04:00
parent abcd43b4ba
commit 04df76e83f
3 changed files with 67 additions and 5 deletions

View File

@ -25,6 +25,8 @@ import { OrganizationCollectionRequest } from '../models/request/organizationCol
import { CliUtils } from '../utils';
import { Utils } from 'jslib/misc/utils';
export class CreateCommand {
constructor(private cipherService: CipherService, private folderService: FolderService,
private userService: UserService, private cryptoService: CryptoService,
@ -57,7 +59,7 @@ export class CreateCommand {
case 'folder':
return await this.createFolder(req);
case 'org-collection':
return await this.createOrganizationCollection(req);
return await this.createOrganizationCollection(req, cmd);
default:
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 {
const orgKey = await this.cryptoService.getOrgKey(req.organizationId);
if (orgKey == null) {

View File

@ -1,20 +1,32 @@
import * as program from 'commander';
import { ApiService } from 'jslib/abstractions/api.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 { Collection } from 'jslib/models/export/collection';
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 { CipherResponse } from '../models/response/cipherResponse';
import { FolderResponse } from '../models/response/folderResponse';
import { OrganizationCollectionResponse } from '../models/response/organizationCollectionResponse';
import { OrganizationCollectionRequest } from '../models/request/organizationCollectionRequest';
import { CliUtils } from '../utils';
import { Utils } from 'jslib/misc/utils';
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> {
if (requestJson == null || requestJson === '') {
@ -44,6 +56,8 @@ export class EditCommand {
return await this.editCipherCollections(id, req);
case 'folder':
return await this.editFolder(id, req);
case 'org-collection':
return await this.editOrganizationCollection(id, req, cmd);
default:
return Response.badRequest('Unknown object.');
}
@ -109,4 +123,37 @@ export class EditCommand {
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);
}
}
}

View File

@ -313,6 +313,7 @@ export class Program extends BaseProgram {
.command('create <object> [encodedJson]')
.option('--file <file>', 'Path to file for attachment.')
.option('--itemid <itemid>', 'Attachment\'s item id.')
.option('--organizationid <organizationid>', 'Organization id for an organization object.')
.description('Create an object in the vault.')
.on('--help', () => {
writeLn('\n Objects:');
@ -344,6 +345,7 @@ export class Program extends BaseProgram {
program
.command('edit <object> <id> [encodedJson]')
.option('--organizationid <organizationid>', 'Organization id for an organization object.')
.description('Edit an object from the vault.')
.on('--help', () => {
writeLn('\n Objects:');
@ -351,6 +353,7 @@ export class Program extends BaseProgram {
writeLn(' item');
writeLn(' item-collections');
writeLn(' folder');
writeLn(' org-collection');
writeLn('');
writeLn(' Id:');
writeLn('');
@ -371,7 +374,8 @@ export class Program extends BaseProgram {
})
.action(async (object, id, encodedJson, cmd) => {
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);
this.processResponse(response);
});