mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-22 11:45:59 +01:00
sync policies., set up policy service
This commit is contained in:
parent
337a7ba59f
commit
3d2e2cb174
14
src/abstractions/policy.service.ts
Normal file
14
src/abstractions/policy.service.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { PolicyData } from '../models/data/policyData';
|
||||
|
||||
import { Policy } from '../models/domain/policy';
|
||||
|
||||
import { PolicyType } from '../enums/policyType';
|
||||
|
||||
export abstract class PolicyService {
|
||||
policyCache: Policy[];
|
||||
|
||||
clearCache: () => void;
|
||||
getAll: (type?: PolicyType) => Promise<Policy[]>;
|
||||
replace: (policies: { [id: string]: PolicyData; }) => Promise<any>;
|
||||
clear: (userId: string) => Promise<any>;
|
||||
}
|
19
src/models/data/policyData.ts
Normal file
19
src/models/data/policyData.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { PolicyResponse } from '../response/policyResponse';
|
||||
|
||||
import { PolicyType } from '../../enums/policyType';
|
||||
|
||||
export class PolicyData {
|
||||
id: string;
|
||||
organizationId: string;
|
||||
type: PolicyType;
|
||||
data: any;
|
||||
enabled: boolean;
|
||||
|
||||
constructor(response: PolicyResponse) {
|
||||
this.id = response.id;
|
||||
this.organizationId = response.organizationId;
|
||||
this.type = response.type;
|
||||
this.data = response.data;
|
||||
this.enabled = response.enabled;
|
||||
}
|
||||
}
|
26
src/models/domain/policy.ts
Normal file
26
src/models/domain/policy.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { PolicyData } from '../data/policyData';
|
||||
|
||||
import Domain from './domainBase';
|
||||
|
||||
import { PolicyType } from '../../enums/policyType';
|
||||
|
||||
export class Policy extends Domain {
|
||||
id: string;
|
||||
organizationId: string;
|
||||
type: PolicyType;
|
||||
data: any;
|
||||
enabled: boolean;
|
||||
|
||||
constructor(obj?: PolicyData) {
|
||||
super();
|
||||
if (obj == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.id = obj.id;
|
||||
this.organizationId = obj.organizationId;
|
||||
this.type = obj.type;
|
||||
this.data = obj.data;
|
||||
this.enabled = obj.enabled;
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ import { CipherResponse } from './cipherResponse';
|
||||
import { CollectionDetailsResponse } from './collectionResponse';
|
||||
import { DomainsResponse } from './domainsResponse';
|
||||
import { FolderResponse } from './folderResponse';
|
||||
import { PolicyResponse } from './policyResponse';
|
||||
import { ProfileResponse } from './profileResponse';
|
||||
|
||||
export class SyncResponse extends BaseResponse {
|
||||
@ -11,6 +12,7 @@ export class SyncResponse extends BaseResponse {
|
||||
collections: CollectionDetailsResponse[] = [];
|
||||
ciphers: CipherResponse[] = [];
|
||||
domains?: DomainsResponse;
|
||||
policies?: PolicyResponse[] = [];
|
||||
|
||||
constructor(response: any) {
|
||||
super(response);
|
||||
@ -39,5 +41,10 @@ export class SyncResponse extends BaseResponse {
|
||||
if (domains != null) {
|
||||
this.domains = new DomainsResponse(domains);
|
||||
}
|
||||
|
||||
const policies = this.getResponseProperty('Policies');
|
||||
if (policies != null) {
|
||||
this.policies = policies.map((p: any) => new PolicyResponse(p));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
55
src/services/policy.service.ts
Normal file
55
src/services/policy.service.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { PolicyService as PolicyServiceAbstraction } from '../abstractions/policy.service';
|
||||
import { StorageService } from '../abstractions/storage.service';
|
||||
import { UserService } from '../abstractions/user.service';
|
||||
|
||||
import { PolicyData } from '../models/data/policyData';
|
||||
|
||||
import { Policy } from '../models/domain/policy';
|
||||
|
||||
import { PolicyType } from '../enums/policyType';
|
||||
|
||||
const Keys = {
|
||||
policiesPrefix: 'policies_',
|
||||
};
|
||||
|
||||
export class PolicyService implements PolicyServiceAbstraction {
|
||||
policyCache: Policy[];
|
||||
|
||||
constructor(private userService: UserService, private storageService: StorageService) {
|
||||
}
|
||||
|
||||
clearCache(): void {
|
||||
this.policyCache = null;
|
||||
}
|
||||
|
||||
async getAll(type?: PolicyType): Promise<Policy[]> {
|
||||
if (this.policyCache == null) {
|
||||
const userId = await this.userService.getUserId();
|
||||
const policies = await this.storageService.get<{ [id: string]: PolicyData; }>(
|
||||
Keys.policiesPrefix + userId);
|
||||
const response: Policy[] = [];
|
||||
for (const id in policies) {
|
||||
if (policies.hasOwnProperty(id)) {
|
||||
response.push(new Policy(policies[id]));
|
||||
}
|
||||
}
|
||||
this.policyCache = response;
|
||||
}
|
||||
if (type != null) {
|
||||
return this.policyCache.filter((p) => p.type === type);
|
||||
} else {
|
||||
return this.policyCache;
|
||||
}
|
||||
}
|
||||
|
||||
async replace(policies: { [id: string]: PolicyData; }): Promise<any> {
|
||||
const userId = await this.userService.getUserId();
|
||||
await this.storageService.save(Keys.policiesPrefix + userId, policies);
|
||||
this.policyCache = null;
|
||||
}
|
||||
|
||||
async clear(userId: string): Promise<any> {
|
||||
await this.storageService.remove(Keys.policiesPrefix + userId);
|
||||
this.policyCache = null;
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import { CollectionService } from '../abstractions/collection.service';
|
||||
import { CryptoService } from '../abstractions/crypto.service';
|
||||
import { FolderService } from '../abstractions/folder.service';
|
||||
import { MessagingService } from '../abstractions/messaging.service';
|
||||
import { PolicyService } from '../abstractions/policy.service';
|
||||
import { SettingsService } from '../abstractions/settings.service';
|
||||
import { StorageService } from '../abstractions/storage.service';
|
||||
import { SyncService as SyncServiceAbstraction } from '../abstractions/sync.service';
|
||||
@ -13,6 +14,7 @@ import { CipherData } from '../models/data/cipherData';
|
||||
import { CollectionData } from '../models/data/collectionData';
|
||||
import { FolderData } from '../models/data/folderData';
|
||||
import { OrganizationData } from '../models/data/organizationData';
|
||||
import { PolicyData } from '../models/data/policyData';
|
||||
|
||||
import { CipherResponse } from '../models/response/cipherResponse';
|
||||
import { CollectionDetailsResponse } from '../models/response/collectionResponse';
|
||||
@ -22,6 +24,7 @@ import {
|
||||
SyncCipherNotification,
|
||||
SyncFolderNotification,
|
||||
} from '../models/response/notificationResponse';
|
||||
import { PolicyResponse } from '../models/response/policyResponse';
|
||||
import { ProfileResponse } from '../models/response/profileResponse';
|
||||
|
||||
const Keys = {
|
||||
@ -35,7 +38,8 @@ export class SyncService implements SyncServiceAbstraction {
|
||||
private settingsService: SettingsService, private folderService: FolderService,
|
||||
private cipherService: CipherService, private cryptoService: CryptoService,
|
||||
private collectionService: CollectionService, private storageService: StorageService,
|
||||
private messagingService: MessagingService, private logoutCallback: (expired: boolean) => Promise<void>) {
|
||||
private messagingService: MessagingService, private policyService: PolicyService,
|
||||
private logoutCallback: (expired: boolean) => Promise<void>) {
|
||||
}
|
||||
|
||||
async getLastSync(): Promise<Date> {
|
||||
@ -91,6 +95,7 @@ export class SyncService implements SyncServiceAbstraction {
|
||||
await this.syncCollections(response.collections);
|
||||
await this.syncCiphers(userId, response.ciphers);
|
||||
await this.syncSettings(userId, response.domains);
|
||||
await this.syncPolicies(response.policies);
|
||||
|
||||
await this.setLastSync(now);
|
||||
return this.syncCompleted(true);
|
||||
@ -298,4 +303,12 @@ export class SyncService implements SyncServiceAbstraction {
|
||||
|
||||
return this.settingsService.setEquivalentDomains(eqDomains);
|
||||
}
|
||||
|
||||
private async syncPolicies(response: PolicyResponse[]) {
|
||||
const policies: { [id: string]: PolicyData; } = {};
|
||||
response.forEach((p) => {
|
||||
policies[p.id] = new PolicyData(p);
|
||||
});
|
||||
return await this.policyService.replace(policies);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user