2022-12-14 01:04:10 -08:00
|
|
|
import {
|
|
|
|
changePassword,
|
|
|
|
deleteUser,
|
2023-01-05 07:10:08 -08:00
|
|
|
getInviteLink,
|
2023-05-30 03:52:02 -07:00
|
|
|
getPasswordResetLink,
|
2022-12-14 01:04:10 -08:00
|
|
|
getUsers,
|
|
|
|
inviteUsers,
|
|
|
|
login,
|
|
|
|
loginCurrentUser,
|
|
|
|
logout,
|
2023-02-17 01:59:09 -08:00
|
|
|
preOwnerSetup,
|
2022-12-14 01:04:10 -08:00
|
|
|
reinvite,
|
|
|
|
sendForgotPasswordEmail,
|
|
|
|
setupOwner,
|
|
|
|
signup,
|
|
|
|
skipOwnerSetup,
|
|
|
|
submitPersonalizationSurvey,
|
|
|
|
updateCurrentUser,
|
|
|
|
updateCurrentUserPassword,
|
2023-04-11 09:43:47 -07:00
|
|
|
updateCurrentUserSettings,
|
2023-05-30 03:52:02 -07:00
|
|
|
updateOtherUserSettings,
|
2022-12-14 01:04:10 -08:00
|
|
|
validatePasswordToken,
|
|
|
|
validateSignupToken,
|
|
|
|
} from '@/api/users';
|
2023-06-05 10:47:06 -07:00
|
|
|
import { PERSONALIZATION_MODAL_KEY, STORES } from '@/constants';
|
2023-02-08 01:42:22 -08:00
|
|
|
import type {
|
2022-12-23 07:32:06 -08:00
|
|
|
ICredentialsResponse,
|
2022-12-14 01:04:10 -08:00
|
|
|
IInviteResponse,
|
|
|
|
IPersonalizationLatestVersion,
|
2023-02-08 01:42:22 -08:00
|
|
|
IRole,
|
2022-12-14 01:04:10 -08:00
|
|
|
IUser,
|
|
|
|
IUserResponse,
|
|
|
|
IUsersState,
|
|
|
|
} from '@/Interface';
|
2022-12-23 07:32:06 -08:00
|
|
|
import { getCredentialPermissions } from '@/permissions';
|
2022-12-14 01:04:10 -08:00
|
|
|
import { getPersonalizedNodeTypes, isAuthorized, PERMISSIONS, ROLE } from '@/utils';
|
|
|
|
import { defineStore } from 'pinia';
|
2023-05-05 01:41:54 -07:00
|
|
|
import { useRootStore } from './n8nRoot.store';
|
|
|
|
import { usePostHog } from './posthog.store';
|
|
|
|
import { useSettingsStore } from './settings.store';
|
|
|
|
import { useUIStore } from './ui.store';
|
2022-11-04 06:04:31 -07:00
|
|
|
|
2022-12-14 01:04:10 -08:00
|
|
|
const isDefaultUser = (user: IUserResponse | null) =>
|
|
|
|
Boolean(user && user.isPending && user.globalRole && user.globalRole.name === ROLE.Owner);
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2022-11-04 06:04:31 -07:00
|
|
|
const isPendingUser = (user: IUserResponse | null) => Boolean(user && user.isPending);
|
|
|
|
|
2023-02-08 01:42:22 -08:00
|
|
|
const isInstanceOwner = (user: IUserResponse | null) =>
|
|
|
|
Boolean(user?.globalRole?.name === ROLE.Owner);
|
|
|
|
|
2022-11-04 06:04:31 -07:00
|
|
|
export const useUsersStore = defineStore(STORES.USERS, {
|
|
|
|
state: (): IUsersState => ({
|
|
|
|
currentUserId: null,
|
|
|
|
users: {},
|
|
|
|
}),
|
|
|
|
getters: {
|
|
|
|
allUsers(): IUser[] {
|
|
|
|
return Object.values(this.users);
|
|
|
|
},
|
2023-04-11 09:43:47 -07:00
|
|
|
userActivated(): boolean {
|
|
|
|
return Boolean(this.currentUser?.settings?.userActivated);
|
|
|
|
},
|
2022-11-04 06:04:31 -07:00
|
|
|
currentUser(): IUser | null {
|
|
|
|
return this.currentUserId ? this.users[this.currentUserId] : null;
|
|
|
|
},
|
2022-12-12 01:53:02 -08:00
|
|
|
isDefaultUser(): boolean {
|
|
|
|
return isDefaultUser(this.currentUser);
|
|
|
|
},
|
2023-02-08 01:42:22 -08:00
|
|
|
isInstanceOwner(): boolean {
|
|
|
|
return isInstanceOwner(this.currentUser);
|
|
|
|
},
|
2022-11-15 04:25:04 -08:00
|
|
|
getUserById(state) {
|
|
|
|
return (userId: string): IUser | null => state.users[userId];
|
2022-11-04 06:04:31 -07:00
|
|
|
},
|
2023-01-24 17:18:39 -08:00
|
|
|
globalRoleName(): IRole {
|
|
|
|
return this.currentUser?.globalRole?.name ?? 'default';
|
2022-11-04 06:04:31 -07:00
|
|
|
},
|
|
|
|
canUserDeleteTags(): boolean {
|
|
|
|
return isAuthorized(PERMISSIONS.TAGS.CAN_DELETE_TAGS, this.currentUser);
|
|
|
|
},
|
2022-12-20 01:52:01 -08:00
|
|
|
canUserActivateLicense(): boolean {
|
|
|
|
return isAuthorized(PERMISSIONS.USAGE.CAN_ACTIVATE_LICENSE, this.currentUser);
|
|
|
|
},
|
2022-11-04 06:04:31 -07:00
|
|
|
canUserAccessSidebarUserInfo() {
|
|
|
|
if (this.currentUser) {
|
|
|
|
const currentUser: IUser = this.currentUser;
|
|
|
|
return isAuthorized(PERMISSIONS.PRIMARY_MENU.CAN_ACCESS_USER_INFO, currentUser);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
showUMSetupWarning() {
|
|
|
|
if (this.currentUser) {
|
|
|
|
const currentUser: IUser = this.currentUser;
|
|
|
|
return isAuthorized(PERMISSIONS.USER_SETTINGS.VIEW_UM_SETUP_WARNING, currentUser);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
personalizedNodeTypes(): string[] {
|
|
|
|
const user = this.currentUser as IUser | null;
|
|
|
|
if (!user) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const answers = user.personalizationAnswers;
|
|
|
|
if (!answers) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return getPersonalizedNodeTypes(answers);
|
|
|
|
},
|
2022-12-23 07:32:06 -08:00
|
|
|
isResourceAccessible() {
|
|
|
|
return (resource: ICredentialsResponse): boolean => {
|
|
|
|
const permissions = getCredentialPermissions(this.currentUser, resource);
|
|
|
|
|
|
|
|
return permissions.use;
|
|
|
|
};
|
|
|
|
},
|
2022-11-04 06:04:31 -07:00
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
addUsers(users: IUserResponse[]) {
|
|
|
|
users.forEach((userResponse: IUserResponse) => {
|
|
|
|
const prevUser = this.users[userResponse.id] || {};
|
|
|
|
const updatedUser = {
|
|
|
|
...prevUser,
|
|
|
|
...userResponse,
|
|
|
|
};
|
|
|
|
const user: IUser = {
|
|
|
|
...updatedUser,
|
2022-12-14 01:04:10 -08:00
|
|
|
fullName: userResponse.firstName
|
|
|
|
? `${updatedUser.firstName} ${updatedUser.lastName || ''}`
|
|
|
|
: undefined,
|
2022-11-04 06:04:31 -07:00
|
|
|
isDefaultUser: isDefaultUser(updatedUser),
|
|
|
|
isPendingUser: isPendingUser(updatedUser),
|
2023-01-24 17:18:39 -08:00
|
|
|
isOwner: updatedUser.globalRole?.name === ROLE.Owner,
|
2022-11-04 06:04:31 -07:00
|
|
|
};
|
2023-06-15 05:30:05 -07:00
|
|
|
|
|
|
|
this.users = {
|
|
|
|
...this.users,
|
|
|
|
[user.id]: user,
|
|
|
|
};
|
2022-11-04 06:04:31 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
deleteUserById(userId: string): void {
|
2023-06-15 05:30:05 -07:00
|
|
|
const { [userId]: _, ...users } = this.users;
|
|
|
|
this.users = users;
|
2022-11-04 06:04:31 -07:00
|
|
|
},
|
|
|
|
setPersonalizationAnswers(answers: IPersonalizationLatestVersion): void {
|
|
|
|
if (!this.currentUser) {
|
|
|
|
return;
|
|
|
|
}
|
2023-06-15 05:30:05 -07:00
|
|
|
|
|
|
|
this.users = {
|
|
|
|
...this.users,
|
|
|
|
[this.currentUser.id]: {
|
|
|
|
...this.currentUser,
|
|
|
|
personalizationAnswers: answers,
|
|
|
|
},
|
|
|
|
};
|
2022-11-04 06:04:31 -07:00
|
|
|
},
|
2022-11-16 05:21:49 -08:00
|
|
|
async loginWithCookie(): Promise<void> {
|
2022-11-04 06:04:31 -07:00
|
|
|
const rootStore = useRootStore();
|
|
|
|
const user = await loginCurrentUser(rootStore.getRestApiContext);
|
2023-02-21 00:35:35 -08:00
|
|
|
if (!user) {
|
|
|
|
return;
|
2022-11-04 06:04:31 -07:00
|
|
|
}
|
2023-02-21 00:35:35 -08:00
|
|
|
|
|
|
|
this.addUsers([user]);
|
|
|
|
this.currentUserId = user.id;
|
|
|
|
|
2023-02-28 02:44:37 -08:00
|
|
|
usePostHog().init(user.featureFlags);
|
2022-11-04 06:04:31 -07:00
|
|
|
},
|
2022-12-14 01:04:10 -08:00
|
|
|
async loginWithCreds(params: { email: string; password: string }): Promise<void> {
|
2022-11-04 06:04:31 -07:00
|
|
|
const rootStore = useRootStore();
|
|
|
|
const user = await login(rootStore.getRestApiContext, params);
|
2023-02-21 00:35:35 -08:00
|
|
|
if (!user) {
|
|
|
|
return;
|
2022-11-04 06:04:31 -07:00
|
|
|
}
|
2023-02-21 00:35:35 -08:00
|
|
|
|
|
|
|
this.addUsers([user]);
|
|
|
|
this.currentUserId = user.id;
|
|
|
|
|
2023-02-28 02:44:37 -08:00
|
|
|
usePostHog().init(user.featureFlags);
|
2022-11-04 06:04:31 -07:00
|
|
|
},
|
|
|
|
async logout(): Promise<void> {
|
|
|
|
const rootStore = useRootStore();
|
|
|
|
await logout(rootStore.getRestApiContext);
|
|
|
|
this.currentUserId = null;
|
2023-02-28 02:44:37 -08:00
|
|
|
usePostHog().reset();
|
2022-11-04 06:04:31 -07:00
|
|
|
},
|
2023-02-17 01:59:09 -08:00
|
|
|
async preOwnerSetup() {
|
|
|
|
return preOwnerSetup(useRootStore().getRestApiContext);
|
|
|
|
},
|
2022-12-14 01:04:10 -08:00
|
|
|
async createOwner(params: {
|
|
|
|
firstName: string;
|
|
|
|
lastName: string;
|
|
|
|
email: string;
|
|
|
|
password: string;
|
|
|
|
}): Promise<void> {
|
2022-11-04 06:04:31 -07:00
|
|
|
const rootStore = useRootStore();
|
|
|
|
const user = await setupOwner(rootStore.getRestApiContext, params);
|
|
|
|
const settingsStore = useSettingsStore();
|
|
|
|
if (user) {
|
|
|
|
this.addUsers([user]);
|
|
|
|
this.currentUserId = user.id;
|
|
|
|
settingsStore.stopShowingSetupPage();
|
|
|
|
}
|
|
|
|
},
|
2022-12-14 01:04:10 -08:00
|
|
|
async validateSignupToken(params: {
|
|
|
|
inviteeId: string;
|
|
|
|
inviterId: string;
|
|
|
|
}): Promise<{ inviter: { firstName: string; lastName: string } }> {
|
2022-11-04 06:04:31 -07:00
|
|
|
const rootStore = useRootStore();
|
2023-05-10 08:10:03 -07:00
|
|
|
return validateSignupToken(rootStore.getRestApiContext, params);
|
2022-11-04 06:04:31 -07:00
|
|
|
},
|
2022-12-14 01:04:10 -08:00
|
|
|
async signup(params: {
|
|
|
|
inviteeId: string;
|
|
|
|
inviterId: string;
|
|
|
|
firstName: string;
|
|
|
|
lastName: string;
|
|
|
|
password: string;
|
|
|
|
}): Promise<void> {
|
2022-11-04 06:04:31 -07:00
|
|
|
const rootStore = useRootStore();
|
|
|
|
const user = await signup(rootStore.getRestApiContext, params);
|
|
|
|
if (user) {
|
|
|
|
this.addUsers([user]);
|
|
|
|
this.currentUserId = user.id;
|
|
|
|
}
|
2023-02-21 00:35:35 -08:00
|
|
|
|
2023-02-28 02:44:37 -08:00
|
|
|
usePostHog().init(user.featureFlags);
|
2022-11-04 06:04:31 -07:00
|
|
|
},
|
2022-12-14 01:04:10 -08:00
|
|
|
async sendForgotPasswordEmail(params: { email: string }): Promise<void> {
|
2022-11-04 06:04:31 -07:00
|
|
|
const rootStore = useRootStore();
|
|
|
|
await sendForgotPasswordEmail(rootStore.getRestApiContext, params);
|
|
|
|
},
|
2022-12-14 01:04:10 -08:00
|
|
|
async validatePasswordToken(params: { token: string; userId: string }): Promise<void> {
|
2022-11-04 06:04:31 -07:00
|
|
|
const rootStore = useRootStore();
|
|
|
|
await validatePasswordToken(rootStore.getRestApiContext, params);
|
|
|
|
},
|
2022-12-14 01:04:10 -08:00
|
|
|
async changePassword(params: {
|
|
|
|
token: string;
|
|
|
|
password: string;
|
|
|
|
userId: string;
|
|
|
|
}): Promise<void> {
|
2022-11-04 06:04:31 -07:00
|
|
|
const rootStore = useRootStore();
|
|
|
|
await changePassword(rootStore.getRestApiContext, params);
|
|
|
|
},
|
2022-12-14 01:04:10 -08:00
|
|
|
async updateUser(params: {
|
|
|
|
id: string;
|
|
|
|
firstName: string;
|
|
|
|
lastName: string;
|
|
|
|
email: string;
|
|
|
|
}): Promise<void> {
|
2022-11-04 06:04:31 -07:00
|
|
|
const rootStore = useRootStore();
|
|
|
|
const user = await updateCurrentUser(rootStore.getRestApiContext, params);
|
|
|
|
this.addUsers([user]);
|
|
|
|
},
|
2023-04-11 09:43:47 -07:00
|
|
|
async updateUserSettings(settings: IUserResponse['settings']): Promise<void> {
|
|
|
|
const rootStore = useRootStore();
|
|
|
|
const updatedSettings = await updateCurrentUserSettings(
|
|
|
|
rootStore.getRestApiContext,
|
|
|
|
settings,
|
|
|
|
);
|
|
|
|
if (this.currentUser) {
|
|
|
|
this.currentUser.settings = updatedSettings;
|
|
|
|
this.addUsers([this.currentUser]);
|
|
|
|
}
|
|
|
|
},
|
2023-05-30 03:52:02 -07:00
|
|
|
async updateOtherUserSettings(
|
|
|
|
userId: string,
|
|
|
|
settings: IUserResponse['settings'],
|
|
|
|
): Promise<void> {
|
|
|
|
const rootStore = useRootStore();
|
|
|
|
const updatedSettings = await updateOtherUserSettings(
|
|
|
|
rootStore.getRestApiContext,
|
|
|
|
userId,
|
|
|
|
settings,
|
|
|
|
);
|
|
|
|
this.users[userId].settings = updatedSettings;
|
|
|
|
this.addUsers([this.users[userId]]);
|
|
|
|
},
|
2022-12-14 01:04:10 -08:00
|
|
|
async updateCurrentUserPassword({
|
|
|
|
password,
|
|
|
|
currentPassword,
|
|
|
|
}: {
|
|
|
|
password: string;
|
|
|
|
currentPassword: string;
|
|
|
|
}): Promise<void> {
|
2022-11-04 06:04:31 -07:00
|
|
|
const rootStore = useRootStore();
|
2022-12-14 01:04:10 -08:00
|
|
|
await updateCurrentUserPassword(rootStore.getRestApiContext, {
|
|
|
|
newPassword: password,
|
|
|
|
currentPassword,
|
|
|
|
});
|
2022-11-04 06:04:31 -07:00
|
|
|
},
|
2022-12-14 01:04:10 -08:00
|
|
|
async deleteUser(params: { id: string; transferId?: string }): Promise<void> {
|
2022-11-04 06:04:31 -07:00
|
|
|
const rootStore = useRootStore();
|
|
|
|
await deleteUser(rootStore.getRestApiContext, params);
|
|
|
|
this.deleteUserById(params.id);
|
|
|
|
},
|
|
|
|
async fetchUsers(): Promise<void> {
|
|
|
|
const rootStore = useRootStore();
|
|
|
|
const users = await getUsers(rootStore.getRestApiContext);
|
|
|
|
this.addUsers(users);
|
|
|
|
},
|
2022-12-14 01:04:10 -08:00
|
|
|
async inviteUsers(params: Array<{ email: string }>): Promise<IInviteResponse[]> {
|
2022-11-04 06:04:31 -07:00
|
|
|
const rootStore = useRootStore();
|
|
|
|
const users = await inviteUsers(rootStore.getRestApiContext, params);
|
2022-12-14 01:04:10 -08:00
|
|
|
this.addUsers(users.map(({ user }) => ({ isPending: true, ...user })));
|
2022-11-04 06:04:31 -07:00
|
|
|
return users;
|
|
|
|
},
|
2022-12-14 01:04:10 -08:00
|
|
|
async reinviteUser(params: { id: string }): Promise<void> {
|
2022-11-04 06:04:31 -07:00
|
|
|
const rootStore = useRootStore();
|
|
|
|
await reinvite(rootStore.getRestApiContext, params);
|
|
|
|
},
|
2023-01-05 07:10:08 -08:00
|
|
|
async getUserInviteLink(params: { id: string }): Promise<{ link: string }> {
|
|
|
|
const rootStore = useRootStore();
|
2023-05-10 08:10:03 -07:00
|
|
|
return getInviteLink(rootStore.getRestApiContext, params);
|
2023-01-05 07:10:08 -08:00
|
|
|
},
|
2023-05-30 03:52:02 -07:00
|
|
|
async getUserPasswordResetLink(params: { id: string }): Promise<{ link: string }> {
|
|
|
|
const rootStore = useRootStore();
|
|
|
|
return getPasswordResetLink(rootStore.getRestApiContext, params);
|
|
|
|
},
|
2022-11-04 06:04:31 -07:00
|
|
|
async submitPersonalizationSurvey(results: IPersonalizationLatestVersion): Promise<void> {
|
|
|
|
const rootStore = useRootStore();
|
|
|
|
await submitPersonalizationSurvey(rootStore.getRestApiContext, results);
|
|
|
|
this.setPersonalizationAnswers(results);
|
|
|
|
},
|
|
|
|
async showPersonalizationSurvey(): Promise<void> {
|
|
|
|
const settingsStore = useSettingsStore();
|
|
|
|
const surveyEnabled = settingsStore.isPersonalizationSurveyEnabled;
|
|
|
|
const currentUser = this.currentUser;
|
|
|
|
if (surveyEnabled && currentUser && !currentUser.personalizationAnswers) {
|
|
|
|
const uiStore = useUIStore();
|
|
|
|
uiStore.openModal(PERSONALIZATION_MODAL_KEY);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async skipOwnerSetup(): Promise<void> {
|
|
|
|
try {
|
|
|
|
const rootStore = useRootStore();
|
|
|
|
const settingsStore = useSettingsStore();
|
|
|
|
settingsStore.stopShowingSetupPage();
|
|
|
|
await skipOwnerSetup(rootStore.getRestApiContext);
|
|
|
|
} catch (error) {}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|