2018-05-14 19:37:52 +02:00
|
|
|
import * as program from 'commander';
|
|
|
|
|
2018-10-14 04:27:33 +02:00
|
|
|
import { CipherView } from 'jslib/models/view/cipherView';
|
|
|
|
|
2018-05-14 19:37:52 +02:00
|
|
|
import { CipherService } from 'jslib/abstractions/cipher.service';
|
2018-05-18 21:26:59 +02:00
|
|
|
import { CollectionService } from 'jslib/abstractions/collection.service';
|
|
|
|
import { FolderService } from 'jslib/abstractions/folder.service';
|
2018-08-13 20:38:04 +02:00
|
|
|
import { SearchService } from 'jslib/abstractions/search.service';
|
2018-05-18 21:26:59 +02:00
|
|
|
import { UserService } from 'jslib/abstractions/user.service';
|
2018-05-14 19:37:52 +02:00
|
|
|
|
2018-05-14 20:54:19 +02:00
|
|
|
import { Response } from '../models/response';
|
|
|
|
import { CipherResponse } from '../models/response/cipherResponse';
|
|
|
|
import { CollectionResponse } from '../models/response/collectionResponse';
|
|
|
|
import { FolderResponse } from '../models/response/folderResponse';
|
|
|
|
import { ListResponse } from '../models/response/listResponse';
|
2018-05-18 21:26:59 +02:00
|
|
|
import { OrganizationResponse } from '../models/response/organizationResponse';
|
2018-05-14 20:54:19 +02:00
|
|
|
|
2018-05-16 17:17:40 +02:00
|
|
|
import { CliUtils } from '../utils';
|
|
|
|
|
2018-05-14 19:37:52 +02:00
|
|
|
export class ListCommand {
|
|
|
|
constructor(private cipherService: CipherService, private folderService: FolderService,
|
2018-08-13 20:38:04 +02:00
|
|
|
private collectionService: CollectionService, private userService: UserService,
|
|
|
|
private searchService: SearchService) { }
|
2018-05-14 19:37:52 +02:00
|
|
|
|
2018-05-14 20:54:19 +02:00
|
|
|
async run(object: string, cmd: program.Command): Promise<Response> {
|
2018-05-14 22:25:14 +02:00
|
|
|
switch (object.toLowerCase()) {
|
2018-05-14 19:37:52 +02:00
|
|
|
case 'items':
|
2018-05-16 16:56:46 +02:00
|
|
|
return await this.listCiphers(cmd);
|
2018-05-14 19:37:52 +02:00
|
|
|
case 'folders':
|
2018-05-16 16:56:46 +02:00
|
|
|
return await this.listFolders(cmd);
|
2018-05-14 19:37:52 +02:00
|
|
|
case 'collections':
|
2018-05-16 16:56:46 +02:00
|
|
|
return await this.listCollections(cmd);
|
2018-05-18 21:26:59 +02:00
|
|
|
case 'organizations':
|
|
|
|
return await this.listOrganizations(cmd);
|
2018-05-14 19:37:52 +02:00
|
|
|
default:
|
2018-05-14 20:54:19 +02:00
|
|
|
return Response.badRequest('Unknown object.');
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 16:56:46 +02:00
|
|
|
private async listCiphers(cmd: program.Command) {
|
2018-10-14 04:27:33 +02:00
|
|
|
let ciphers: CipherView[];
|
|
|
|
if (cmd.url != null && cmd.url.trim() !== '') {
|
|
|
|
ciphers = await this.cipherService.getAllDecryptedForUrl(cmd.url);
|
|
|
|
} else {
|
|
|
|
ciphers = await this.cipherService.getAllDecrypted();
|
|
|
|
}
|
2018-05-16 16:56:46 +02:00
|
|
|
|
|
|
|
if (cmd.folderid != null || cmd.collectionid != null || cmd.organizationid != null) {
|
|
|
|
ciphers = ciphers.filter((c) => {
|
|
|
|
if (cmd.folderid != null) {
|
2018-05-21 15:14:58 +02:00
|
|
|
if (cmd.folderid === 'notnull' && c.folderId != null) {
|
2018-05-16 16:56:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const folderId = cmd.folderid === 'null' ? null : cmd.folderid;
|
|
|
|
if (folderId === c.folderId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.organizationid != null) {
|
2018-05-21 15:14:58 +02:00
|
|
|
if (cmd.organizationid === 'notnull' && c.organizationId != null) {
|
2018-05-16 16:56:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const organizationId = cmd.organizationid === 'null' ? null : cmd.organizationid;
|
|
|
|
if (organizationId === c.organizationId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.collectionid != null) {
|
2018-05-21 15:14:58 +02:00
|
|
|
if (cmd.collectionid === 'notnull' && c.collectionIds != null && c.collectionIds.length > 0) {
|
2018-05-16 16:56:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const collectionId = cmd.collectionid === 'null' ? null : cmd.collectionid;
|
2018-10-14 06:09:53 +02:00
|
|
|
if (collectionId == null && (c.collectionIds == null || c.collectionIds.length === 0)) {
|
2018-05-16 16:56:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (collectionId != null && c.collectionIds != null && c.collectionIds.indexOf(collectionId) > -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.search != null && cmd.search.trim() !== '') {
|
2018-08-13 20:38:04 +02:00
|
|
|
ciphers = this.searchService.searchCiphersBasic(ciphers, cmd.search);
|
2018-05-16 16:56:46 +02:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:54:19 +02:00
|
|
|
const res = new ListResponse(ciphers.map((o) => new CipherResponse(o)));
|
|
|
|
return Response.success(res);
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 16:56:46 +02:00
|
|
|
private async listFolders(cmd: program.Command) {
|
|
|
|
let folders = await this.folderService.getAllDecrypted();
|
|
|
|
|
|
|
|
if (cmd.search != null && cmd.search.trim() !== '') {
|
2018-05-16 17:17:40 +02:00
|
|
|
folders = CliUtils.searchFolders(folders, cmd.search);
|
2018-05-16 16:56:46 +02:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:54:19 +02:00
|
|
|
const res = new ListResponse(folders.map((o) => new FolderResponse(o)));
|
|
|
|
return Response.success(res);
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 16:56:46 +02:00
|
|
|
private async listCollections(cmd: program.Command) {
|
|
|
|
let collections = await this.collectionService.getAllDecrypted();
|
|
|
|
|
|
|
|
if (cmd.organizationid != null) {
|
|
|
|
collections = collections.filter((c) => {
|
|
|
|
if (cmd.organizationid === c.organizationId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.search != null && cmd.search.trim() !== '') {
|
2018-05-16 17:17:40 +02:00
|
|
|
collections = CliUtils.searchCollections(collections, cmd.search);
|
2018-05-16 16:56:46 +02:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:54:19 +02:00
|
|
|
const res = new ListResponse(collections.map((o) => new CollectionResponse(o)));
|
|
|
|
return Response.success(res);
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
2018-05-18 21:26:59 +02:00
|
|
|
|
|
|
|
private async listOrganizations(cmd: program.Command) {
|
|
|
|
let organizations = await this.userService.getAllOrganizations();
|
|
|
|
|
|
|
|
if (cmd.search != null && cmd.search.trim() !== '') {
|
|
|
|
organizations = CliUtils.searchOrganizations(organizations, cmd.search);
|
|
|
|
}
|
|
|
|
|
|
|
|
const res = new ListResponse(organizations.map((o) => new OrganizationResponse(o)));
|
|
|
|
return Response.success(res);
|
|
|
|
}
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|