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

268 lines
6.4 KiB
JavaScript
Raw Normal View History

2021-02-03 12:42:47 +00:00
import { action, computed, makeObservable, observable } 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 { isAdmin } from "../utils";
2021-02-20 17:17:27 +00:00
import isEmpty from "lodash/isEmpty";
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
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
makeObservable(this, {
isLoading: observable,
isAuthenticated: observable,
isAdmin: computed,
isLoaded: computed,
language: computed,
2021-02-16 09:01:51 +00:00
product: computed,
2021-02-20 17:17:27 +00:00
availableModules: computed,
2021-02-03 12:42:47 +00:00
userStore: observable,
moduleStore: observable,
settingsStore: observable,
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: observable,
2021-02-03 12:42:47 +00:00
init: action,
login: action,
logout: action,
setIsAuthenticated: action,
replaceFileStream: action,
getEncryptionAccess: action,
setEncryptionAccess: action,
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: action,
reset: action,
2021-02-03 12:42:47 +00:00
});
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() {
return this.moduleStore.modules.find(
(item) => item.id === this.settingsStore.currentProductId
2021-03-01 09:03:12 +00:00
) || "";
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, toModuleWrapper } = this.moduleStore;
if (isEmpty(modules) || isEmpty(this.userStore.user)) {
return [];
}
const isUserAdmin = user.isAdmin;
const customModules = this.getCustomModules(isUserAdmin);
const products = modules.map((m) => toModuleWrapper(m, false));
const primaryProducts = products.filter((m) => m.isPrimary === true);
const dummyProducts = products.filter((m) => m.isPrimary === false);
return [
{
separator: true,
id: "nav-products-separator",
},
...primaryProducts,
...customModules,
{
separator: true,
dashed: true,
id: "nav-dummy-products-separator",
},
...dummyProducts,
];
}
getCustomModules = (isAdmin) => {
if (!isAdmin) {
return [];
}
const settingsModuleWrapper = this.moduleStore.toModuleWrapper(
{
id: "settings",
title: "Settings",
link: "/settings",
},
false,
"SettingsIcon",
"static/images/settings.react.svg"
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);
console.log("Login response", response);
setWithCredentialsStatus(true);
await this.init();
return true;
} catch (e) {
return false;
}
};
2021-02-03 12:42:47 +00:00
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);
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("/login");
}
};
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;
};
2021-02-02 09:55:14 +00:00
}
export default new AuthStore();