DocSpace-buildtools/packages/asc-web-common/store/AuthStore.js

278 lines
6.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-03 12:42:47 +00:00
import history from "../history";
2021-02-02 09:55:14 +00:00
import ModuleStore from "./ModuleStore";
import SettingsStore from "./SettingsStore";
import UserStore from "./UserStore";
import { logout as logoutDesktop, desktopConstants } from "../desktop";
import { combineUrl, isAdmin } from "../utils";
2021-02-20 17:17:27 +00:00
import isEmpty from "lodash/isEmpty";
import { AppServerConfig } from "../constants";
const { proxyURL } = AppServerConfig;
2021-02-02 09:55:14 +00:00
class AuthStore {
userStore = null;
moduleStore = null;
settingsStore = null;
2021-02-03 12:42:47 +00:00
isLoading = false;
isAuthenticated = 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;
2021-02-03 12:42:47 +00:00
providers = [];
2021-02-02 09:55:14 +00:00
constructor() {
this.userStore = new UserStore();
this.moduleStore = new ModuleStore();
this.settingsStore = new SettingsStore();
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 () => {
2021-02-03 12:42:47 +00:00
await this.getIsAuthenticated();
const requests = [];
requests.push(this.settingsStore.init());
if (this.isAuthenticated) {
requests.push(this.userStore.init());
requests.push(this.moduleStore.init());
}
return Promise.all(requests);
};
get isLoaded() {
let success = false;
if (this.isAuthenticated) {
success =
this.userStore.isLoaded &&
this.moduleStore.isLoaded &&
this.settingsStore.isLoaded;
} else {
success = this.settingsStore.isLoaded;
}
return success;
}
get language() {
return (
(this.userStore.user && this.userStore.user.cultureName) ||
this.settingsStore.culture ||
"en-US"
);
}
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
}
get product() {
2021-03-01 15:15:11 +00:00
return (
this.moduleStore.modules.find(
(item) => item.id === this.settingsStore.currentProductId
) || ""
);
2021-02-03 12:42:47 +00:00
}
2021-02-02 14:26:58 +00:00
2021-02-20 17:17:27 +00:00
get availableModules() {
const { user } = this.userStore;
const { modules } = this.moduleStore;
2021-02-20 17:17:27 +00:00
if (isEmpty(modules) || isEmpty(this.userStore.user)) {
return [];
}
const isUserAdmin = user.isAdmin;
const customProducts = this.getCustomModules(isUserAdmin);
const readyProducts = [];
const inProgressProducts = [];
modules.forEach((p) => {
if (p.appName === "people" || p.appName === "files") {
readyProducts.push(p);
} else {
inProgressProducts.push(p);
}
});
2021-02-20 17:17:27 +00:00
return [
{
separator: true,
id: "nav-products-separator",
},
...readyProducts,
...customProducts,
2021-02-20 17:17:27 +00:00
{
separator: true,
dashed: true,
id: "nav-dummy-products-separator",
},
...inProgressProducts,
2021-02-20 17:17:27 +00:00
];
}
getCustomModules = (isAdmin) => {
if (!isAdmin) {
return [];
}
const settingsModuleWrapper = this.moduleStore.toModuleWrapper({
id: "settings",
title: "Settings",
link: "/settings",
iconUrl: "/static/images/settings.react.svg",
});
settingsModuleWrapper.onClick = this.onClick;
settingsModuleWrapper.onBadgeClick = this.onBadgeClick;
2021-02-20 17:17:27 +00:00
return [settingsModuleWrapper];
};
2021-02-03 12:42:47 +00:00
getIsAuthenticated = async () => {
const isAuthenticated = await api.user.checkIsAuthenticated();
this.setIsAuthenticated(isAuthenticated);
2021-02-02 09:55:14 +00:00
};
2021-02-02 14:26:58 +00:00
login = async (user, hash) => {
try {
const response = await api.user.login(user, hash);
2021-03-03 11:46:37 +00:00
if (!response || !response.token) throw "Empty API response";
2021-02-02 14:26:58 +00:00
setWithCredentialsStatus(true);
await this.init();
2021-03-03 11:09:00 +00:00
return Promise.resolve(true);
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
2021-03-24 13:49:42 +00:00
thirdPartyLogin = async (SerializedProfile) => {
try {
const response = await api.user.thirdPartyLogin(SerializedProfile);
if (!response || !response.token) throw "Empty API response";
setWithCredentialsStatus(true);
await this.init();
return Promise.resolve(true);
} catch (e) {
return Promise.reject(e);
}
};
2021-02-25 08:35:31 +00:00
reset = () => {
this.userStore = new UserStore();
this.moduleStore = new ModuleStore();
this.settingsStore = new SettingsStore();
};
2021-02-03 12:42:47 +00:00
logout = async (withoutRedirect) => {
const response = await api.user.logout();
//console.log("Logout response ", response);
2021-02-03 12:42:47 +00:00
setWithCredentialsStatus(false);
const { isDesktopClient: isDesktop } = this.settingsStore;
isDesktop && logoutDesktop();
2021-02-25 08:35:31 +00:00
this.reset();
2021-02-03 12:42:47 +00:00
this.init();
if (!withoutRedirect) {
history.push(combineUrl(proxyURL, "/login"));
2021-02-03 12:42:47 +00:00
}
};
setIsAuthenticated = (isAuthenticated) => {
this.isAuthenticated = isAuthenticated;
};
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) => {
let promise = new Promise((resolve, reject) => {
try {
window.AscDesktopEditor.cloudCryptoCommand(
"share",
{
cryptoEngineId: desktopConstants.guid,
file: [file.viewUrl],
keys: keys,
},
(obj) => {
let file = null;
if (obj.isCrypto) {
let bytes = obj.bytes;
let filename = "temp_name";
file = new File([bytes], filename);
}
resolve(file);
}
);
} catch (e) {
reject(e);
}
});
return promise;
});
};
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;
};
2021-02-02 09:55:14 +00:00
}
export default new AuthStore();