mirror of
https://github.com/bitwarden/jslib
synced 2025-12-06 00:03:29 +00:00
Get collections from server if admin
This commit is contained in:
@@ -15,6 +15,8 @@ import { DynamicTreeNode } from "./models/dynamic-tree-node.model";
|
||||
|
||||
@Injectable()
|
||||
export class VaultFilterService {
|
||||
getAllCollectionsFromServer = false;
|
||||
|
||||
constructor(
|
||||
protected stateService: StateService,
|
||||
protected organizationService: OrganizationService,
|
||||
@@ -58,13 +60,10 @@ export class VaultFilterService {
|
||||
}
|
||||
|
||||
async buildCollections(organizationId?: string): Promise<DynamicTreeNode<CollectionView>> {
|
||||
const storedCollections = await this.collectionService.getAllDecrypted();
|
||||
let collections: CollectionView[];
|
||||
if (organizationId != null) {
|
||||
collections = storedCollections.filter((c) => c.organizationId === organizationId);
|
||||
} else {
|
||||
collections = storedCollections;
|
||||
}
|
||||
const collections = this.getAllCollectionsFromServer
|
||||
? await this.collectionService.getOrgCollectionsFromServer(organizationId)
|
||||
: await this.getUserCollections(organizationId);
|
||||
|
||||
const nestedCollections = await this.collectionService.getAllNested(collections);
|
||||
return new DynamicTreeNode<CollectionView>({
|
||||
fullList: collections,
|
||||
@@ -72,6 +71,13 @@ export class VaultFilterService {
|
||||
});
|
||||
}
|
||||
|
||||
private async getUserCollections(organizationId?: string): Promise<CollectionView[]> {
|
||||
const storedCollections = await this.collectionService.getAllDecrypted();
|
||||
return organizationId != null
|
||||
? storedCollections.filter((c) => c.organizationId === organizationId)
|
||||
: storedCollections;
|
||||
}
|
||||
|
||||
async checkForSingleOrganizationPolicy(): Promise<boolean> {
|
||||
return await this.policyService.policyAppliesToUser(PolicyType.SingleOrg);
|
||||
}
|
||||
|
||||
@@ -213,7 +213,12 @@ export const SYSTEM_LANGUAGE = new InjectionToken<string>("SYSTEM_LANGUAGE");
|
||||
{
|
||||
provide: CollectionServiceAbstraction,
|
||||
useClass: CollectionService,
|
||||
deps: [CryptoServiceAbstraction, I18nServiceAbstraction, StateServiceAbstraction],
|
||||
deps: [
|
||||
CryptoServiceAbstraction,
|
||||
I18nServiceAbstraction,
|
||||
StateServiceAbstraction,
|
||||
ApiServiceAbstraction,
|
||||
],
|
||||
},
|
||||
{
|
||||
provide: EnvironmentServiceAbstraction,
|
||||
|
||||
@@ -10,6 +10,7 @@ export abstract class CollectionService {
|
||||
get: (id: string) => Promise<Collection>;
|
||||
getAll: () => Promise<Collection[]>;
|
||||
getAllDecrypted: () => Promise<CollectionView[]>;
|
||||
getOrgCollectionsFromServer: (organizationId: string) => Promise<CollectionView[]>;
|
||||
getAllNested: (collections?: CollectionView[]) => Promise<TreeNode<CollectionView>[]>;
|
||||
getNested: (id: string) => Promise<TreeNode<CollectionView>>;
|
||||
upsert: (collection: CollectionData | CollectionData[]) => Promise<any>;
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import { ApiService } from "jslib-common/abstractions/api.service";
|
||||
import { CollectionDetailsResponse } from "jslib-common/models/response/collectionResponse";
|
||||
|
||||
import { CollectionService as CollectionServiceAbstraction } from "../abstractions/collection.service";
|
||||
import { CryptoService } from "../abstractions/crypto.service";
|
||||
import { I18nService } from "../abstractions/i18n.service";
|
||||
@@ -15,7 +18,8 @@ export class CollectionService implements CollectionServiceAbstraction {
|
||||
constructor(
|
||||
private cryptoService: CryptoService,
|
||||
private i18nService: I18nService,
|
||||
private stateService: StateService
|
||||
private stateService: StateService,
|
||||
private apiService: ApiService
|
||||
) {}
|
||||
|
||||
async clearCache(userId?: string): Promise<void> {
|
||||
@@ -154,4 +158,18 @@ export class CollectionService implements CollectionServiceAbstraction {
|
||||
|
||||
await this.replace(collections);
|
||||
}
|
||||
|
||||
async getOrgCollectionsFromServer(organizationId: string) {
|
||||
let result: CollectionView[] = [];
|
||||
|
||||
const collectionResponse = await this.apiService.getCollections(organizationId);
|
||||
if (collectionResponse?.data != null && collectionResponse.data.length) {
|
||||
const collectionDomains = collectionResponse.data.map(
|
||||
(r) => new Collection(new CollectionData(r as CollectionDetailsResponse))
|
||||
);
|
||||
result = await this.decryptMany(collectionDomains);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user