DocSpace-client/packages/common/store/AuthStore.js

303 lines
7.5 KiB
JavaScript
Raw Normal View History

2021-03-01 15:15:11 +00:00
import { makeAutoObservable } from "mobx";
2021-02-02 14:26:58 +00:00
import api from "../api";
import { setWithCredentialsStatus } from "../api/client";
2021-02-02 09:55:14 +00:00
import SettingsStore from "./SettingsStore";
import UserStore from "./UserStore";
2021-05-05 11:59:08 +00:00
import TfaStore from "./TfaStore";
import InfoPanelStore from "./InfoPanelStore";
import { logout as logoutDesktop, desktopConstants } from "../desktop";
import { isAdmin } from "../utils";
import { LANGUAGE, TenantStatus } from "../constants";
2021-02-02 09:55:14 +00:00
class AuthStore {
userStore = null;
2021-02-02 09:55:14 +00:00
settingsStore = null;
2021-05-05 11:59:08 +00:00
tfaStore = null;
infoPanelStore = null;
2021-02-02 09:55:14 +00:00
2021-02-03 12:42:47 +00:00
isLoading = false;
Merge branch 'develop' into feature/mobx # Conflicts: # products/ASC.Files/Client/src/App.js # products/ASC.Files/Client/src/components/Article/Body/ThirdPartyList.js # products/ASC.Files/Client/src/components/pages/Home/Section/Body/index.js # products/ASC.Files/Client/src/components/pages/Home/Section/Header/index.js # products/ASC.Files/Client/src/components/pages/Home/index.js # products/ASC.Files/Client/src/store/files/selectors.js # products/ASC.People/Client/src/App.js # products/ASC.People/Client/src/components/pages/GroupAction/Section/Body/index.js # products/ASC.People/Client/src/components/pages/Home/Section/Body/index.js # products/ASC.People/Client/src/components/pages/Home/Section/Header/index.js # products/ASC.People/Client/src/components/pages/Home/index.js # products/ASC.People/Client/src/components/pages/Profile/Section/Body/index.js # products/ASC.People/Client/src/components/pages/Profile/index.js # products/ASC.People/Client/src/components/pages/ProfileAction/index.js # products/ASC.People/Client/src/store/group/actions.js # products/ASC.People/Client/src/store/people/actions.js # products/ASC.People/Client/src/store/people/selectors.js # products/ASC.People/Client/yarn.lock # web/ASC.Web.Client/src/App.js # web/ASC.Web.Client/src/components/pages/About/index.js # web/ASC.Web.Client/src/components/pages/Home/index.js # web/ASC.Web.Client/src/store/settings/actions.js # web/ASC.Web.Common/src/components/Layout/index.js # web/ASC.Web.Common/src/components/NavMenu/index.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header-nav.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header-unauth.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/nav-item.js # web/ASC.Web.Common/src/components/PageLayout/index.js # web/ASC.Web.Common/src/store/auth/actions.js # web/ASC.Web.Common/src/store/auth/reducer.js # web/ASC.Web.Common/src/store/auth/selectors.js # web/ASC.Web.Components/src/components/loader/types/rombs.js
2021-02-20 14:31:58 +00:00
version = null;
providers = [];
isInit = false;
2021-02-02 09:55:14 +00:00
constructor() {
this.userStore = new UserStore();
this.settingsStore = new SettingsStore();
2021-05-05 11:59:08 +00:00
this.tfaStore = new TfaStore();
this.infoPanelStore = new InfoPanelStore();
2021-02-03 12:42:47 +00:00
2021-03-01 15:15:11 +00:00
makeAutoObservable(this);
2021-02-02 09:55:14 +00:00
}
init = async () => {
if (this.isInit) return;
this.isInit = true;
2022-03-24 17:56:51 +00:00
try {
await this.userStore.init();
} catch (e) {
console.error(e);
}
2021-02-03 12:42:47 +00:00
const requests = [];
requests.push(this.settingsStore.init());
if (this.isAuthenticated) {
2022-07-13 18:00:42 +00:00
!this.settingsStore.passwordSettings &&
requests.push(this.settingsStore.getPortalPasswordSettings());
2021-02-03 12:42:47 +00:00
}
return Promise.all(requests);
};
setLanguage() {
2022-03-24 17:56:51 +00:00
if (this.userStore.user?.cultureName) {
localStorage.getItem(LANGUAGE) !== this.userStore.user.cultureName &&
localStorage.setItem(LANGUAGE, this.userStore.user.cultureName);
} else {
localStorage.setItem(LANGUAGE, this.settingsStore.culture || "en-US");
}
}
2021-02-03 12:42:47 +00:00
get isLoaded() {
let success = false;
if (this.isAuthenticated) {
2022-03-24 17:56:51 +00:00
success =
(this.userStore.isLoaded && this.settingsStore.isLoaded) ||
this.settingsStore.tenantStatus === TenantStatus.PortalRestore;
success && this.setLanguage();
2021-02-03 12:42:47 +00:00
} else {
success = this.settingsStore.isLoaded;
}
return success;
}
get language() {
return (
(this.userStore.user && this.userStore.user.cultureName) ||
this.settingsStore.culture ||
"en"
2021-02-03 12:42:47 +00:00
);
}
get isAdmin() {
const { user } = this.userStore;
2021-02-03 12:42:47 +00:00
const { currentProductId } = this.settingsStore;
if (!user || !user.id) return false;
2021-02-03 12:42:47 +00:00
return isAdmin(user, currentProductId);
2021-02-03 12:42:47 +00:00
}
login = async (user, hash, session = true) => {
2021-02-02 14:26:58 +00:00
try {
const response = await api.user.login(user, hash, session);
2021-02-02 14:26:58 +00:00
2021-05-14 12:59:15 +00:00
if (!response || (!response.token && !response.tfa))
2021-05-30 19:48:04 +00:00
throw response.error.message;
2021-05-14 12:59:15 +00:00
if (response.tfa && response.confirmUrl) {
const url = response.confirmUrl.replace(window.location.origin, "");
return Promise.resolve({ url, user, hash });
2021-05-14 12:59:15 +00:00
}
2021-02-02 14:26:58 +00:00
setWithCredentialsStatus(true);
this.reset();
this.init();
2021-02-02 14:26:58 +00:00
return Promise.resolve({ url: this.settingsStore.defaultPage });
2021-02-02 14:26:58 +00:00
} catch (e) {
2021-03-03 11:09:00 +00:00
return Promise.reject(e);
2021-02-02 14:26:58 +00:00
}
};
2021-02-03 12:42:47 +00:00
loginWithCode = async (userName, passwordHash, code) => {
await this.tfaStore.loginWithCode(userName, passwordHash, code);
setWithCredentialsStatus(true);
this.reset();
this.init();
return Promise.resolve(this.settingsStore.defaultPage);
};
2021-03-24 13:49:42 +00:00
thirdPartyLogin = async (SerializedProfile) => {
try {
const response = await api.user.thirdPartyLogin(SerializedProfile);
if (!response || !response.token) throw new Error("Empty API response");
2021-03-24 13:49:42 +00:00
setWithCredentialsStatus(true);
this.reset();
this.init();
2021-03-24 13:49:42 +00:00
return Promise.resolve(this.settingsStore.defaultPage);
2021-03-24 13:49:42 +00:00
} catch (e) {
return Promise.reject(e);
}
};
reset = (skipUser = false) => {
this.isInit = false;
this.skipModules = false;
if (!skipUser) {
this.userStore = new UserStore();
}
2021-02-25 08:35:31 +00:00
this.settingsStore = new SettingsStore();
};
logout = async () => {
await api.user.logout();
2021-02-03 12:42:47 +00:00
//console.log("Logout response ", response);
2021-02-03 12:42:47 +00:00
setWithCredentialsStatus(false);
const { isDesktopClient: isDesktop, personal } = this.settingsStore;
2021-02-03 12:42:47 +00:00
isDesktop && logoutDesktop();
this.reset(true);
this.userStore.setUser(null);
this.init();
// if (redirectToLogin) {
// if (redirectPath) {
// return window.location.replace(redirectPath);
// }
// if (personal) {
// return window.location.replace("/");
// } else {
// this.reset(true);
// this.userStore.setUser(null);
// this.init();
// return history.push(combineUrl(proxyURL, "/login"));
// }
// } else {
// this.reset();
// this.init();
// }
2021-02-03 12:42:47 +00:00
};
get isAuthenticated() {
2022-03-24 17:56:51 +00:00
return (
this.userStore.isAuthenticated ||
this.settingsStore.tenantStatus === TenantStatus.PortalRestore
);
}
2021-02-03 12:42:47 +00:00
getEncryptionAccess = (fileId) => {
return api.files
.getEncryptionAccess(fileId)
.then((keys) => {
return Promise.resolve(keys);
})
.catch((err) => console.error(err));
};
replaceFileStream = (fileId, file, encrypted, forcesave) => {
return api.files.updateFileStream(file, fileId, encrypted, forcesave);
};
setEncryptionAccess = (file) => {
return this.getEncryptionAccess(file.id).then((keys) => {
return new Promise((resolve, reject) => {
try {
window.AscDesktopEditor.cloudCryptoCommand(
"share",
{
cryptoEngineId: desktopConstants.cryptoEngineId,
file: [file.viewUrl],
keys: keys,
},
(obj) => {
let resFile = null;
if (obj.isCrypto) {
let bytes = obj.bytes;
let filename = "temp_name";
resFile = new File([bytes], filename);
}
resolve(resFile);
}
);
} catch (e) {
reject(e);
}
});
});
};
setDocumentTitle = (subTitle = null) => {
let title;
const currentModule = this.settingsStore.product;
const organizationName = this.settingsStore.organizationName;
if (subTitle) {
if (this.isAuthenticated && currentModule) {
title = subTitle + " - " + currentModule.title;
} else {
title = subTitle + " - " + organizationName;
}
} else if (currentModule && organizationName) {
title = currentModule.title + " - " + organizationName;
} else {
title = organizationName;
}
document.title = title;
};
Merge branch 'develop' into feature/mobx # Conflicts: # products/ASC.Files/Client/src/App.js # products/ASC.Files/Client/src/components/Article/Body/ThirdPartyList.js # products/ASC.Files/Client/src/components/pages/Home/Section/Body/index.js # products/ASC.Files/Client/src/components/pages/Home/Section/Header/index.js # products/ASC.Files/Client/src/components/pages/Home/index.js # products/ASC.Files/Client/src/store/files/selectors.js # products/ASC.People/Client/src/App.js # products/ASC.People/Client/src/components/pages/GroupAction/Section/Body/index.js # products/ASC.People/Client/src/components/pages/Home/Section/Body/index.js # products/ASC.People/Client/src/components/pages/Home/Section/Header/index.js # products/ASC.People/Client/src/components/pages/Home/index.js # products/ASC.People/Client/src/components/pages/Profile/Section/Body/index.js # products/ASC.People/Client/src/components/pages/Profile/index.js # products/ASC.People/Client/src/components/pages/ProfileAction/index.js # products/ASC.People/Client/src/store/group/actions.js # products/ASC.People/Client/src/store/people/actions.js # products/ASC.People/Client/src/store/people/selectors.js # products/ASC.People/Client/yarn.lock # web/ASC.Web.Client/src/App.js # web/ASC.Web.Client/src/components/pages/About/index.js # web/ASC.Web.Client/src/components/pages/Home/index.js # web/ASC.Web.Client/src/store/settings/actions.js # web/ASC.Web.Common/src/components/Layout/index.js # web/ASC.Web.Common/src/components/NavMenu/index.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header-nav.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header-unauth.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/nav-item.js # web/ASC.Web.Common/src/components/PageLayout/index.js # web/ASC.Web.Common/src/store/auth/actions.js # web/ASC.Web.Common/src/store/auth/reducer.js # web/ASC.Web.Common/src/store/auth/selectors.js # web/ASC.Web.Components/src/components/loader/types/rombs.js
2021-02-20 14:31:58 +00:00
setProductVersion = (version) => {
this.version = version;
};
setProviders = (providers) => {
this.providers = providers;
};
2022-04-21 12:33:48 +00:00
2022-08-29 08:27:13 +00:00
getOforms = (filter) => {
const culture =
this.userStore.user.cultureName || this.settingsStore.culture;
2022-08-29 08:27:13 +00:00
const formName = "&fields[0]=name_form";
const updatedAt = "&fields[1]=updatedAt";
const size = "&fields[2]=file_size";
const filePages = "&fields[3]=file_pages";
const cardPrewiew = "&populate[card_prewiew][fields][4]=url";
const templateImage = "&populate[template_image][fields][5]=formats";
const fields = `${formName}${updatedAt}${size}${filePages}${cardPrewiew}${templateImage}`;
const params = `?${filter.toUrlParams()}${fields}`;
const promise = new Promise(async (resolve, reject) => {
let oforms = await api.settings.getOforms(
2022-08-29 08:27:13 +00:00
`${this.settingsStore.urlOforms}${params}&locale=${culture}`
);
2022-06-16 14:03:42 +00:00
if (!oforms?.data?.data.length) {
oforms = await api.settings.getOforms(
2022-08-29 08:27:13 +00:00
`${this.settingsStore.urlOforms}${params}&locale=en`
);
}
resolve(oforms);
});
return promise;
2022-04-21 12:33:48 +00:00
};
2021-02-02 09:55:14 +00:00
}
export default new AuthStore();