From e2b336d017e135a7200b18745ef61c7fbb06e8e5 Mon Sep 17 00:00:00 2001 From: Andrey Platov Date: Tue, 24 Aug 2021 20:42:32 +0200 Subject: [PATCH] store user account Signed-off-by: Andrey Platov --- models/contact/src/plugin.ts | 5 ++--- plugins/contact/src/index.ts | 3 ++- .../login-resources/src/components/LoginForm.svelte | 1 + plugins/login-resources/src/utils.ts | 7 ++++--- plugins/login/src/index.ts | 1 + plugins/workbench-resources/package.json | 3 ++- .../src/components/WorkbenchApp.svelte | 12 ++++++++++-- plugins/workbench-resources/src/utils.ts | 13 +++++++++++-- 8 files changed, 33 insertions(+), 12 deletions(-) diff --git a/models/contact/src/plugin.ts b/models/contact/src/plugin.ts index 9d098e6604..92783f8c1c 100644 --- a/models/contact/src/plugin.ts +++ b/models/contact/src/plugin.ts @@ -14,8 +14,8 @@ // import { mergeIds } from '@anticrm/platform' -import type { Ref, Class, Space } from '@anticrm/core' -import contact, { contactId, Employee } from '@anticrm/contact' +import type { Ref, Space } from '@anticrm/core' +import contact, { contactId } from '@anticrm/contact' import type { AnyComponent } from '@anticrm/ui' import {} from '@anticrm/core' @@ -24,7 +24,6 @@ export const ids = mergeIds(contactId, contact, { PersonPresenter: '' as AnyComponent }, class: { - Employee: '' as Ref> }, space: { Employee: '' as Ref diff --git a/plugins/contact/src/index.ts b/plugins/contact/src/index.ts index 55156e90b4..e293f8e3d5 100644 --- a/plugins/contact/src/index.ts +++ b/plugins/contact/src/index.ts @@ -57,6 +57,7 @@ export default plugin(contactId, { class: { Contact: '' as Ref>, Person: '' as Ref>, - Organization: '' as Ref> + Organization: '' as Ref>, + Employee: '' as Ref> } }) diff --git a/plugins/login-resources/src/components/LoginForm.svelte b/plugins/login-resources/src/components/LoginForm.svelte index 263677ea08..29270159ca 100644 --- a/plugins/login-resources/src/components/LoginForm.svelte +++ b/plugins/login-resources/src/components/LoginForm.svelte @@ -56,6 +56,7 @@ console.log('token', result.token) setMetadataLocalStorage(login.metadata.LoginToken, result.token) setMetadataLocalStorage(login.metadata.LoginEndpoint, result.endpoint) + setMetadataLocalStorage(login.metadata.LoginEmail, result.email) navigate({ path: [workbench.component.WorkbenchApp] }) } } diff --git a/plugins/login-resources/src/utils.ts b/plugins/login-resources/src/utils.ts index 67840200d6..f0f3de04f6 100644 --- a/plugins/login-resources/src/utils.ts +++ b/plugins/login-resources/src/utils.ts @@ -21,13 +21,14 @@ import login from '@anticrm/login' export interface LoginInfo { token: string endpoint: string + email: string } /** * Perform a login operation to required workspace with user credentials. */ export async function doLogin ( - username: string, + email: string, password: string, workspace: string ): Promise<[Status, LoginInfo | undefined]> { @@ -41,13 +42,13 @@ export async function doLogin ( if (token !== undefined) { const endpoint = getMetadata(login.metadata.OverrideEndpoint) if (endpoint !== undefined) { - return [OK, { token, endpoint }] + return [OK, { token, endpoint, email }] } } const request: Request<[string, string, string]> = { method: 'login', - params: [username, password, workspace] + params: [email, password, workspace] } try { diff --git a/plugins/login/src/index.ts b/plugins/login/src/index.ts index d35797e6c9..96f07de48f 100644 --- a/plugins/login/src/index.ts +++ b/plugins/login/src/index.ts @@ -40,6 +40,7 @@ export default plugin(loginId, { UploadUrl: '' as Asset, LoginToken: '' as Metadata, LoginEndpoint: '' as Metadata, + LoginEmail: '' as Metadata, OverrideLoginToken: '' as Metadata, // debug purposes OverrideEndpoint: '' as Metadata }, diff --git a/plugins/workbench-resources/package.json b/plugins/workbench-resources/package.json index 36a4dc7980..c459bde762 100644 --- a/plugins/workbench-resources/package.json +++ b/plugins/workbench-resources/package.json @@ -27,6 +27,7 @@ "@anticrm/ui": "~0.6.0", "@anticrm/view": "~0.6.0", "@anticrm/presentation": "~0.6.1", - "@anticrm/login": "~0.6.1" + "@anticrm/login": "~0.6.1", + "@anticrm/contact": "~0.6.0" } } diff --git a/plugins/workbench-resources/src/components/WorkbenchApp.svelte b/plugins/workbench-resources/src/components/WorkbenchApp.svelte index a4ca7925af..e7b55cb280 100644 --- a/plugins/workbench-resources/src/components/WorkbenchApp.svelte +++ b/plugins/workbench-resources/src/components/WorkbenchApp.svelte @@ -21,20 +21,28 @@ import { navigate, Loading, fetchMetadataLocalStorage } from '@anticrm/ui' import client from '@anticrm/client' import login from '@anticrm/login' +import contact from '@anticrm/contact' import Workbench from './Workbench.svelte' +import { setCurrentAccount } from '../utils' async function connect(): Promise { const token = fetchMetadataLocalStorage(login.metadata.LoginToken) const endpoint = fetchMetadataLocalStorage(login.metadata.LoginEndpoint) + const email = fetchMetadataLocalStorage(login.metadata.LoginEmail) - if (token === null || endpoint === null) { + if (token === null || endpoint === null || email === null) { navigate({ path: [login.component.LoginApp] }) return } const getClient = await getResource(client.function.GetClient) - return getClient(token, endpoint) + const instance = await getClient(token, endpoint) + const me = (await instance.findAll(contact.class.Employee, { email }))[0] + if (me !== undefined) { + setCurrentAccount(me._id) + } + return instance } diff --git a/plugins/workbench-resources/src/utils.ts b/plugins/workbench-resources/src/utils.ts index aa268d50fa..1d852bef09 100644 --- a/plugins/workbench-resources/src/utils.ts +++ b/plugins/workbench-resources/src/utils.ts @@ -14,11 +14,20 @@ // limitations under the License. // -import type { Ref, Obj, Class, WithLookup } from '@anticrm/core' -import type { Asset, IntlString } from '@anticrm/platform' +import type { Ref, Obj, Class } from '@anticrm/core' +import type { Asset } from '@anticrm/platform' import type { Client } from '@anticrm/core' +import type { Employee } from '@anticrm/contact' export function classIcon(client: Client, _class: Ref>): Asset | undefined { return client.getHierarchy().getClass(_class).icon } +let currentAccount: Ref + +export function getCurrentAccount(): Ref { return currentAccount } + +export function setCurrentAccount(account: Ref): void { + currentAccount = account + console.log('current account', currentAccount) +}