mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 00:33:44 +00:00
list and get organizations
This commit is contained in:
@@ -10,6 +10,9 @@ import { CryptoService } from 'jslib/abstractions/crypto.service';
|
||||
import { FolderService } from 'jslib/abstractions/folder.service';
|
||||
import { TokenService } from 'jslib/abstractions/token.service';
|
||||
import { TotpService } from 'jslib/abstractions/totp.service';
|
||||
import { UserService } from 'jslib/abstractions/user.service';
|
||||
|
||||
import { Organization } from 'jslib/models/domain/organization';
|
||||
|
||||
import { CipherView } from 'jslib/models/view/cipherView';
|
||||
import { CollectionView } from 'jslib/models/view/collectionView';
|
||||
@@ -20,6 +23,7 @@ import { CipherResponse } from '../models/response/cipherResponse';
|
||||
import { CollectionResponse } from '../models/response/collectionResponse';
|
||||
import { FolderResponse } from '../models/response/folderResponse';
|
||||
import { MessageResponse } from '../models/response/messageResponse';
|
||||
import { OrganizationResponse } from '../models/response/organizationResponse';
|
||||
import { StringResponse } from '../models/response/stringResponse';
|
||||
import { TemplateResponse } from '../models/response/templateResponse';
|
||||
|
||||
@@ -39,7 +43,7 @@ export class GetCommand {
|
||||
constructor(private cipherService: CipherService, private folderService: FolderService,
|
||||
private collectionService: CollectionService, private totpService: TotpService,
|
||||
private auditService: AuditService, private cryptoService: CryptoService,
|
||||
private tokenService: TokenService) { }
|
||||
private tokenService: TokenService, private userService: UserService) { }
|
||||
|
||||
async run(object: string, id: string, cmd: program.Command): Promise<Response> {
|
||||
if (id != null) {
|
||||
@@ -65,6 +69,8 @@ export class GetCommand {
|
||||
return await this.getFolder(id);
|
||||
case 'collection':
|
||||
return await this.getCollection(id);
|
||||
case 'organization':
|
||||
return await this.getOrganization(id);
|
||||
case 'template':
|
||||
return await this.getTemplate(id);
|
||||
default:
|
||||
@@ -301,6 +307,28 @@ export class GetCommand {
|
||||
return Response.success(res);
|
||||
}
|
||||
|
||||
private async getOrganization(id: string) {
|
||||
let org: Organization = null;
|
||||
if (this.isGuid(id)) {
|
||||
org = await this.userService.getOrganization(id);
|
||||
} else if (id.trim() !== '') {
|
||||
let orgs = await this.userService.getAllOrganizations();
|
||||
orgs = CliUtils.searchOrganizations(orgs, id);
|
||||
if (orgs.length > 1) {
|
||||
return Response.multipleResults(orgs.map((c) => c.id));
|
||||
}
|
||||
if (orgs.length > 0) {
|
||||
org = orgs[0];
|
||||
}
|
||||
}
|
||||
|
||||
if (org == null) {
|
||||
return Response.notFound();
|
||||
}
|
||||
const res = new OrganizationResponse(org);
|
||||
return Response.success(res);
|
||||
}
|
||||
|
||||
private isGuid(id: string) {
|
||||
return RegExp(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/, 'i').test(id);
|
||||
}
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
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';
|
||||
import { CollectionService } from 'jslib/abstractions/collection.service';
|
||||
import { FolderService } from 'jslib/abstractions/folder.service';
|
||||
import { UserService } from 'jslib/abstractions/user.service';
|
||||
|
||||
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';
|
||||
import { OrganizationResponse } from '../models/response/organizationResponse';
|
||||
|
||||
import { CliUtils } from '../utils';
|
||||
|
||||
export class ListCommand {
|
||||
constructor(private cipherService: CipherService, private folderService: FolderService,
|
||||
private collectionService: CollectionService) { }
|
||||
private collectionService: CollectionService, private userService: UserService) { }
|
||||
|
||||
async run(object: string, cmd: program.Command): Promise<Response> {
|
||||
switch (object.toLowerCase()) {
|
||||
@@ -24,6 +26,8 @@ export class ListCommand {
|
||||
return await this.listFolders(cmd);
|
||||
case 'collections':
|
||||
return await this.listCollections(cmd);
|
||||
case 'organizations':
|
||||
return await this.listOrganizations(cmd);
|
||||
default:
|
||||
return Response.badRequest('Unknown object.');
|
||||
}
|
||||
@@ -108,4 +112,15 @@ export class ListCommand {
|
||||
const res = new ListResponse(collections.map((o) => new CollectionResponse(o)));
|
||||
return Response.success(res);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user