From bf260819bb303d125379c92e712f762fe17f043c Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 18 May 2018 15:26:46 -0400 Subject: [PATCH] sync organizations --- src/abstractions/user.service.ts | 7 +++++ src/models/data/organizationData.ts | 20 +++++++++++++ src/models/domain/organization.ts | 24 ++++++++++++++++ src/services/sync.service.ts | 8 ++++++ src/services/user.service.ts | 44 +++++++++++++++++++++++++++-- 5 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 src/models/data/organizationData.ts create mode 100644 src/models/domain/organization.ts diff --git a/src/abstractions/user.service.ts b/src/abstractions/user.service.ts index 5621c3be50..1a16c4a1fa 100644 --- a/src/abstractions/user.service.ts +++ b/src/abstractions/user.service.ts @@ -1,3 +1,6 @@ +import { OrganizationData } from '../models/data/organizationData'; +import { Organization } from '../models/domain/organization'; + export abstract class UserService { userId: string; email: string; @@ -10,4 +13,8 @@ export abstract class UserService { getSecurityStamp: () => Promise; clear: () => Promise; isAuthenticated: () => Promise; + getOrganization: (id: string) => Promise; + getAllOrganizations: () => Promise; + replaceOrganizations: (organizations: { [id: string]: OrganizationData; }) => Promise; + clearOrganizations: (userId: string) => Promise; } diff --git a/src/models/data/organizationData.ts b/src/models/data/organizationData.ts new file mode 100644 index 0000000000..1af5223391 --- /dev/null +++ b/src/models/data/organizationData.ts @@ -0,0 +1,20 @@ +import { ProfileOrganizationResponse } from '../response/profileOrganizationResponse'; + +import { OrganizationUserStatusType } from '../../enums/organizationUserStatusType'; +import { OrganizationUserType } from '../../enums/organizationUserType'; + +export class OrganizationData { + id: string; + name: string; + status: OrganizationUserStatusType; + type: OrganizationUserType; + enabled: boolean; + + constructor(response: ProfileOrganizationResponse) { + this.id = response.id; + this.name = response.name; + this.status = response.status; + this.type = response.type; + this.enabled = response.enabled; + } +} diff --git a/src/models/domain/organization.ts b/src/models/domain/organization.ts new file mode 100644 index 0000000000..69f93b34c0 --- /dev/null +++ b/src/models/domain/organization.ts @@ -0,0 +1,24 @@ +import { OrganizationData } from '../data/organizationData'; + +import { OrganizationUserStatusType } from '../../enums/organizationUserStatusType'; +import { OrganizationUserType } from '../../enums/organizationUserType'; + +export class Organization { + id: string; + name: string; + status: OrganizationUserStatusType; + type: OrganizationUserType; + enabled: boolean; + + constructor(obj?: OrganizationData) { + if (obj == null) { + return; + } + + this.id = obj.id; + this.name = obj.name; + this.status = obj.status; + this.type = obj.type; + this.enabled = obj.enabled; + } +} diff --git a/src/services/sync.service.ts b/src/services/sync.service.ts index c053a34f63..2e025772c5 100644 --- a/src/services/sync.service.ts +++ b/src/services/sync.service.ts @@ -12,6 +12,7 @@ import { UserService } from '../abstractions/user.service'; 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 { CipherResponse } from '../models/response/cipherResponse'; import { CollectionResponse } from '../models/response/collectionResponse'; @@ -144,6 +145,13 @@ export class SyncService implements SyncServiceAbstraction { await this.cryptoService.setEncPrivateKey(response.privateKey); await this.cryptoService.setOrgKeys(response.organizations); await this.userService.setSecurityStamp(response.securityStamp); + await this.userService.setSecurityStamp(response.securityStamp); + + const organizations: { [id: string]: OrganizationData; } = {}; + response.organizations.forEach((o) => { + organizations[o.id] = new OrganizationData(o); + }); + return await this.userService.replaceOrganizations(organizations); } private async syncFolders(userId: string, response: FolderResponse[]) { diff --git a/src/services/user.service.ts b/src/services/user.service.ts index 1949cadb48..3b149643d0 100644 --- a/src/services/user.service.ts +++ b/src/services/user.service.ts @@ -1,14 +1,18 @@ import { StorageService } from '../abstractions/storage.service'; import { TokenService } from '../abstractions/token.service'; -import { UserService as UserServiceAbsrtaction } from '../abstractions/user.service'; +import { UserService as UserServiceAbstraction } from '../abstractions/user.service'; + +import { OrganizationData } from '../models/data/organizationData'; +import { Organization } from '../models/domain/organization'; const Keys = { userId: 'userId', userEmail: 'userEmail', stamp: 'securityStamp', + organizationsPrefix: 'organizations_', }; -export class UserService implements UserServiceAbsrtaction { +export class UserService implements UserServiceAbstraction { userId: string; email: string; stamp: string; @@ -59,10 +63,13 @@ export class UserService implements UserServiceAbsrtaction { } async clear(): Promise { + const userId = await this.getUserId(); + await Promise.all([ this.storageService.remove(Keys.userId), this.storageService.remove(Keys.userEmail), this.storageService.remove(Keys.stamp), + this.clearOrganizations(userId), ]); this.userId = this.email = this.stamp = null; @@ -77,4 +84,37 @@ export class UserService implements UserServiceAbsrtaction { const userId = await this.getUserId(); return userId != null; } + + async getOrganization(id: string): Promise { + const userId = await this.getUserId(); + const organizations = await this.storageService.get<{ [id: string]: OrganizationData; }>( + Keys.organizationsPrefix + userId); + if (organizations == null || !organizations.hasOwnProperty(id)) { + return null; + } + + return new Organization(organizations[id]); + } + + async getAllOrganizations(): Promise { + const userId = await this.getUserId(); + const organizations = await this.storageService.get<{ [id: string]: OrganizationData; }>( + Keys.organizationsPrefix + userId); + const response: Organization[] = []; + for (const id in organizations) { + if (organizations.hasOwnProperty(id)) { + response.push(new Organization(organizations[id])); + } + } + return response; + } + + async replaceOrganizations(organizations: { [id: string]: OrganizationData; }): Promise { + const userId = await this.getUserId(); + await this.storageService.save(Keys.organizationsPrefix + userId, organizations); + } + + async clearOrganizations(userId: string): Promise { + await this.storageService.remove(Keys.organizationsPrefix + userId); + } }