2018-05-14 19:37:52 +02:00
|
|
|
import * as program from 'commander';
|
|
|
|
|
|
|
|
import { CipherService } from 'jslib/abstractions/cipher.service';
|
|
|
|
import { CollectionService } from 'jslib/services/collection.service';
|
|
|
|
import { FolderService } from 'jslib/services/folder.service';
|
|
|
|
|
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-14 19:37:52 +02:00
|
|
|
export class ListCommand {
|
|
|
|
constructor(private cipherService: CipherService, private folderService: FolderService,
|
|
|
|
private collectionService: CollectionService) { }
|
|
|
|
|
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-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) {
|
|
|
|
let ciphers = await this.cipherService.getAllDecrypted();
|
|
|
|
|
|
|
|
if (cmd.folderid != null || cmd.collectionid != null || cmd.organizationid != null) {
|
|
|
|
ciphers = ciphers.filter((c) => {
|
|
|
|
if (cmd.folderid != null) {
|
|
|
|
if (cmd.folderid === '!null' && c.folderId != null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const folderId = cmd.folderid === 'null' ? null : cmd.folderid;
|
|
|
|
if (folderId === c.folderId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.organizationid != null) {
|
|
|
|
if (cmd.organizationid === '!null' && c.organizationId != null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const organizationId = cmd.organizationid === 'null' ? null : cmd.organizationid;
|
|
|
|
if (organizationId === c.organizationId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.collectionid != null) {
|
|
|
|
if (cmd.collectionid === '!null' && c.collectionIds != null && c.collectionIds.length > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const collectionId = cmd.collectionid === 'null' ? null : cmd.collectionid;
|
|
|
|
if (collectionId == null && c.collectionIds == null || c.collectionIds.length === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (collectionId != null && c.collectionIds != null && c.collectionIds.indexOf(collectionId) > -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.search != null && cmd.search.trim() !== '') {
|
|
|
|
const search = cmd.search.toLowerCase();
|
|
|
|
ciphers = ciphers.filter((c) => {
|
|
|
|
if (c.name != null && c.name.toLowerCase().indexOf(search) > -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (c.subTitle != null && c.subTitle.toLowerCase().indexOf(search) > -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (c.login && c.login.uri != null && c.login.uri.toLowerCase().indexOf(search) > -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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() !== '') {
|
|
|
|
const search = cmd.search.toLowerCase();
|
|
|
|
folders = folders.filter((f) => {
|
|
|
|
if (f.name != null && f.name.toLowerCase().indexOf(search) > -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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() !== '') {
|
|
|
|
const search = cmd.search.toLowerCase();
|
|
|
|
collections = collections.filter((c) => {
|
|
|
|
if (c.name != null && c.name.toLowerCase().indexOf(search) > -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|