mirror of
https://github.com/bitwarden/browser.git
synced 2024-09-19 02:51:14 +02:00
Allow import to organization (#325)
* Allow import to organization vaults * Use organization-aware Importer
This commit is contained in:
parent
5a377f8ef9
commit
17ab740914
@ -1,5 +1,6 @@
|
|||||||
import * as program from 'commander';
|
import * as program from 'commander';
|
||||||
import { ImportService } from 'jslib-common/abstractions/import.service';
|
import { ImportService } from 'jslib-common/abstractions/import.service';
|
||||||
|
import { UserService } from 'jslib-common/abstractions/user.service';
|
||||||
|
|
||||||
import { Response } from 'jslib-node/cli/models/response';
|
import { Response } from 'jslib-node/cli/models/response';
|
||||||
import { MessageResponse } from 'jslib-node/cli/models/response/messageResponse';
|
import { MessageResponse } from 'jslib-node/cli/models/response/messageResponse';
|
||||||
@ -7,17 +8,30 @@ import { MessageResponse } from 'jslib-node/cli/models/response/messageResponse'
|
|||||||
import { CliUtils } from '../utils';
|
import { CliUtils } from '../utils';
|
||||||
|
|
||||||
export class ImportCommand {
|
export class ImportCommand {
|
||||||
constructor(private importService: ImportService) { }
|
constructor(private importService: ImportService, private userService: UserService) { }
|
||||||
|
|
||||||
async run(format: string, filepath: string, options: program.OptionValues): Promise<Response> {
|
async run(format: string, filepath: string, options: program.OptionValues): Promise<Response> {
|
||||||
if (options.formats || false) {
|
const organizationId = options.organizationid;
|
||||||
return this.list();
|
if (organizationId != null) {
|
||||||
} else {
|
const organization = await this.userService.getOrganization(organizationId);
|
||||||
return this.import(format, filepath);
|
|
||||||
|
if (organization == null) {
|
||||||
|
return Response.badRequest(`You do not belong to an organization with the ID of ${organizationId}. Check the organization ID and sync your vault.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!organization.canAccessImportExport) {
|
||||||
|
return Response.badRequest('You are not authorized to import into the provided organization.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async import(format: string, filepath: string) {
|
if (options.formats || false) {
|
||||||
|
return await this.list();
|
||||||
|
} else {
|
||||||
|
return await this.import(format, filepath, organizationId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async import(format: string, filepath: string, organizationId: string) {
|
||||||
if (format == null || format === '') {
|
if (format == null || format === '') {
|
||||||
return Response.badRequest('`format` was not provided.');
|
return Response.badRequest('`format` was not provided.');
|
||||||
}
|
}
|
||||||
@ -25,7 +39,7 @@ export class ImportCommand {
|
|||||||
return Response.badRequest('`filepath` was not provided.');
|
return Response.badRequest('`filepath` was not provided.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const importer = await this.importService.getImporter(format, null);
|
const importer = await this.importService.getImporter(format, organizationId);
|
||||||
if (importer === null) {
|
if (importer === null) {
|
||||||
return Response.badRequest('Proper importer type required.');
|
return Response.badRequest('Proper importer type required.');
|
||||||
}
|
}
|
||||||
@ -36,7 +50,7 @@ export class ImportCommand {
|
|||||||
return Response.badRequest('Import file was empty.');
|
return Response.badRequest('Import file was empty.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const err = await this.importService.import(importer, contents, null);
|
const err = await this.importService.import(importer, contents, organizationId);
|
||||||
if (err != null) {
|
if (err != null) {
|
||||||
return Response.badRequest(err.message);
|
return Response.badRequest(err.message);
|
||||||
}
|
}
|
||||||
|
@ -320,16 +320,18 @@ export class VaultProgram extends Program {
|
|||||||
.command('import [format] [input]')
|
.command('import [format] [input]')
|
||||||
.description('Import vault data from a file.')
|
.description('Import vault data from a file.')
|
||||||
.option('--formats', 'List formats')
|
.option('--formats', 'List formats')
|
||||||
|
.option('--organizationid <organizationid>', 'ID of the organization to import to.')
|
||||||
.on('--help', () => {
|
.on('--help', () => {
|
||||||
writeLn('\n Examples:');
|
writeLn('\n Examples:');
|
||||||
writeLn('');
|
writeLn('');
|
||||||
writeLn(' bw import --formats');
|
writeLn(' bw import --formats');
|
||||||
writeLn(' bw import bitwardencsv ./from/source.csv');
|
writeLn(' bw import bitwardencsv ./from/source.csv');
|
||||||
writeLn(' bw import keepass2xml keepass_backup.xml');
|
writeLn(' bw import keepass2xml keepass_backup.xml');
|
||||||
|
writeLn(' bw import --organizationid cf14adc3-aca5-4573-890a-f6fa231436d9 keepass2xml keepass_backup.xml');
|
||||||
})
|
})
|
||||||
.action(async (format, filepath, options) => {
|
.action(async (format, filepath, options) => {
|
||||||
await this.exitIfLocked();
|
await this.exitIfLocked();
|
||||||
const command = new ImportCommand(this.main.importService);
|
const command = new ImportCommand(this.main.importService, this.main.userService);
|
||||||
const response = await command.run(format, filepath, options);
|
const response = await command.run(format, filepath, options);
|
||||||
this.processResponse(response);
|
this.processResponse(response);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user