Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
org user apis, sort function utils
Browse files Browse the repository at this point in the history
  • Loading branch information
kspearrin committed Jul 6, 2018
1 parent e25ad93 commit 7b23b90
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 37 deletions.
13 changes: 13 additions & 0 deletions src/abstractions/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { IdentityTokenResponse } from '../models/response/identityTokenResponse'
import { IdentityTwoFactorResponse } from '../models/response/identityTwoFactorResponse';
import { ListResponse } from '../models/response/listResponse';
import { OrganizationResponse } from '../models/response/organizationResponse';
import { OrganizationUserUserDetailsResponse } from '../models/response/organizationUserResponse';
import { ProfileResponse } from '../models/response/profileResponse';
import { SyncResponse } from '../models/response/syncResponse';
import { TwoFactorAuthenticatorResponse } from '../models/response/twoFactorAuthenticatorResponse';
Expand All @@ -70,6 +71,7 @@ export abstract class ApiService {
setUrls: (urls: EnvironmentUrls) => void;
postIdentityToken: (request: TokenRequest) => Promise<IdentityTokenResponse | IdentityTwoFactorResponse>;
refreshIdentityToken: () => Promise<any>;

getProfile: () => Promise<ProfileResponse>;
getUserBilling: () => Promise<BillingResponse>;
putProfile: (request: UpdateProfileRequest) => Promise<ProfileResponse>;
Expand All @@ -88,9 +90,11 @@ export abstract class ApiService {
postAccountPayment: (request: PaymentRequest) => Promise<any>;
postAccountLicense: (data: FormData) => Promise<any>;
postAccountKeys: (request: KeysRequest) => Promise<any>;

postFolder: (request: FolderRequest) => Promise<FolderResponse>;
putFolder: (id: string, request: FolderRequest) => Promise<FolderResponse>;
deleteFolder: (id: string) => Promise<any>;

getCipher: (id: string) => Promise<CipherResponse>;
getCipherAdmin: (id: string) => Promise<CipherResponse>;
getCiphersOrganization: (organizationId: string) => Promise<ListResponse<CipherResponse>>;
Expand All @@ -109,28 +113,36 @@ export abstract class ApiService {
postPurgeCiphers: (request: PasswordVerificationRequest) => Promise<any>;
postImportCiphers: (request: ImportCiphersRequest) => Promise<any>;
postImportOrganizationCiphers: (organizationId: string, request: ImportOrganizationCiphersRequest) => Promise<any>;

postCipherAttachment: (id: string, data: FormData) => Promise<CipherResponse>;
postCipherAttachmentAdmin: (id: string, data: FormData) => Promise<CipherResponse>;
deleteCipherAttachment: (id: string, attachmentId: string) => Promise<any>;
deleteCipherAttachmentAdmin: (id: string, attachmentId: string) => Promise<any>;
postShareCipherAttachment: (id: string, attachmentId: string, data: FormData,
organizationId: string) => Promise<any>;

getCollectionDetails: (organizationId: string, id: string) => Promise<CollectionGroupDetailsResponse>;
getCollections: (organizationId: string) => Promise<ListResponse<CollectionResponse>>;
getCollectionUsers: (organizationId: string, id: string) => Promise<ListResponse<CollectionUserResponse>>;
postCollection: (request: CollectionRequest) => Promise<CollectionResponse>;
putCollection: (id: string, request: CollectionRequest) => Promise<CollectionResponse>;
deleteCollection: (id: string) => Promise<any>;

getGroupDetails: (organizationId: string, id: string) => Promise<GroupDetailsResponse>;
getGroups: (organizationId: string) => Promise<ListResponse<GroupResponse>>;
getGroupUsers: (organizationId: string, id: string) => Promise<ListResponse<GroupUserResponse>>;
postGroup: (request: GroupRequest) => Promise<GroupResponse>;
putGroup: (id: string, request: GroupRequest) => Promise<GroupResponse>;
deleteGroup: (id: string) => Promise<any>;

getOrganizationUsers: (organizationId: string) => Promise<ListResponse<OrganizationUserUserDetailsResponse>>;

getSync: () => Promise<SyncResponse>;
postImportDirectory: (organizationId: string, request: ImportDirectoryRequest) => Promise<any>;

getSettingsDomains: () => Promise<DomainsResponse>;
putSettingsDomains: (request: UpdateDomainsRequest) => Promise<DomainsResponse>;

getTwoFactorProviders: () => Promise<ListResponse<TwoFactorProviderResponse>>;
getTwoFactorAuthenticator: (request: PasswordVerificationRequest) => Promise<TwoFactorAuthenticatorResponse>;
getTwoFactorEmail: (request: PasswordVerificationRequest) => Promise<TwoFactorEmailResponse>;
Expand All @@ -148,6 +160,7 @@ export abstract class ApiService {
postTwoFactorRecover: (request: TwoFactorRecoveryRequest) => Promise<any>;
postTwoFactorEmailSetup: (request: TwoFactorEmailRequest) => Promise<any>;
postTwoFactorEmail: (request: TwoFactorEmailRequest) => Promise<any>;

postOrganization: (request: OrganizationCreateRequest) => Promise<OrganizationResponse>;
postLeaveOrganization: (id: string) => Promise<any>;
postOrganizationLicense: (data: FormData) => Promise<OrganizationResponse>;
Expand Down
1 change: 0 additions & 1 deletion src/abstractions/collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ export abstract class CollectionService {
replace: (collections: { [id: string]: CollectionData; }) => Promise<any>;
clear: (userId: string) => Promise<any>;
delete: (id: string | string[]) => Promise<any>;
getLocaleSortingFunction: () => (a: CollectionView, b: CollectionView) => number;
}
19 changes: 19 additions & 0 deletions src/misc/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { I18nService } from '../abstractions/i18n.service';

// tslint:disable-next-line
const nodeURL = typeof window === 'undefined' ? require('url').URL : null;

Expand Down Expand Up @@ -149,6 +151,23 @@ export class Utils {
return url != null ? url.host : null;
}

static getSortFunction(i18nService: I18nService, prop: string) {
return (a: any, b: any) => {
if (a[prop] == null && b[prop] != null) {
return -1;
}
if (a[prop] != null && b[prop] == null) {
return 1;
}
if (a[prop] == null && b[prop] == null) {
return 0;
}

return i18nService.collator ? i18nService.collator.compare(a[prop], b[prop]) :
a[prop].localeCompare(b[prop]);
};
}

private static getUrl(uriString: string): URL {
if (uriString == null) {
return null;
Expand Down
41 changes: 41 additions & 0 deletions src/models/response/organizationUserResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { OrganizationUserStatusType } from '../../enums/organizationUserStatusType';
import { OrganizationUserType } from '../../enums/organizationUserType';
import { SelectionReadOnlyResponse } from './selectionReadOnlyResponse';

export class OrganizationUserResponse {
id: string;
userId: string;
type: OrganizationUserType;
status: OrganizationUserStatusType;
accessAll: boolean;

constructor(response: any) {
this.id = response.Id;
this.userId = response.UserId;
this.type = response.Type;
this.status = response.Status;
this.accessAll = response.AccessAll;
}
}

export class OrganizationUserUserDetailsResponse extends OrganizationUserResponse {
name: string;
email: string;

constructor(response: any) {
super(response);
this.name = response.Name;
this.email = response.Email;
}
}

export class OrganizationUserDetailsResponse extends OrganizationUserResponse {
collections: SelectionReadOnlyResponse;

constructor(response: any) {
super(response);
if (response.Collections != null) {
this.collections = response.Collections.map((c: any) => new SelectionReadOnlyResponse(c));
}
}
}
8 changes: 8 additions & 0 deletions src/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import { IdentityTokenResponse } from '../models/response/identityTokenResponse'
import { IdentityTwoFactorResponse } from '../models/response/identityTwoFactorResponse';
import { ListResponse } from '../models/response/listResponse';
import { OrganizationResponse } from '../models/response/organizationResponse';
import { OrganizationUserUserDetailsResponse } from '../models/response/organizationUserResponse';
import { ProfileResponse } from '../models/response/profileResponse';
import { SyncResponse } from '../models/response/syncResponse';
import { TwoFactorAuthenticatorResponse } from '../models/response/twoFactorAuthenticatorResponse';
Expand Down Expand Up @@ -425,6 +426,13 @@ export class ApiService implements ApiServiceAbstraction {
return this.send('DELETE', '/groups/' + id, null, true, false);
}

// Organization User APIs

async getOrganizationUsers(organizationId: string): Promise<ListResponse<OrganizationUserUserDetailsResponse>> {
const r = await this.send('GET', '/organizations/' + organizationId + '/users', null, true, true);
return new ListResponse(r, OrganizationUserUserDetailsResponse);
}

// Sync APIs

async getSync(): Promise<SyncResponse> {
Expand Down
21 changes: 3 additions & 18 deletions src/services/collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { I18nService } from '../abstractions/i18n.service';
import { StorageService } from '../abstractions/storage.service';
import { UserService } from '../abstractions/user.service';

import { Utils } from '../misc/utils';

const Keys = {
collectionsPrefix: 'collections_',
};
Expand Down Expand Up @@ -51,7 +53,7 @@ export class CollectionService implements CollectionServiceAbstraction {
promises.push(collection.decrypt().then((c) => decCollections.push(c)));
});
await Promise.all(promises);
return decCollections.sort(this.getLocaleSortingFunction());
return decCollections.sort(Utils.getSortFunction(this.i18nService, 'name'));
}

async get(id: string): Promise<Collection> {
Expand Down Expand Up @@ -145,21 +147,4 @@ export class CollectionService implements CollectionServiceAbstraction {
await this.storageService.save(Keys.collectionsPrefix + userId, collections);
this.decryptedCollectionCache = null;
}

getLocaleSortingFunction(): (a: CollectionView, b: CollectionView) => number {
return (a, b) => {
if (a.name == null && b.name != null) {
return -1;
}
if (a.name != null && b.name == null) {
return 1;
}
if (a.name == null && b.name == null) {
return 0;
}

return this.i18nService.collator ? this.i18nService.collator.compare(a.name, b.name) :
a.name.localeCompare(b.name);
};
}
}
21 changes: 3 additions & 18 deletions src/services/folder.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { StorageService } from '../abstractions/storage.service';
import { UserService } from '../abstractions/user.service';
import { CipherData } from '../models/data/cipherData';

import { Utils } from '../misc/utils';

const Keys = {
foldersPrefix: 'folders_',
ciphersPrefix: 'ciphers_',
Expand Down Expand Up @@ -82,7 +84,7 @@ export class FolderService implements FolderServiceAbstraction {
});

await Promise.all(promises);
decFolders.sort(this.getLocaleSortingFunction());
decFolders.sort(Utils.getSortFunction(this.i18nService, 'name'));

const noneFolder = new FolderView();
noneFolder.name = this.i18nService.t('noneFolder');
Expand Down Expand Up @@ -180,21 +182,4 @@ export class FolderService implements FolderServiceAbstraction {
await this.apiService.deleteFolder(id);
await this.delete(id);
}

private getLocaleSortingFunction(): (a: FolderView, b: FolderView) => number {
return (a, b) => {
if (a.name == null && b.name != null) {
return -1;
}
if (a.name != null && b.name == null) {
return 1;
}
if (a.name == null && b.name == null) {
return 0;
}

return this.i18nService.collator ? this.i18nService.collator.compare(a.name, b.name) :
a.name.localeCompare(b.name);
};
}
}

0 comments on commit 7b23b90

Please sign in to comment.