2025-01-03 15:06:41 +01:00
|
|
|
|
2025-03-03 11:22:56 +01:00
|
|
|
import { ROLES, API_ORIGIN } from '../constants.js';
|
2025-01-03 15:06:41 +01:00
|
|
|
import { fetcher } from 'pankow';
|
|
|
|
|
|
2025-04-10 11:35:59 +02:00
|
|
|
const changeHandlers = {};
|
|
|
|
|
const KEYS = {
|
|
|
|
|
AVATAR: 'avatar', // only returns a URI with cachebusting
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function notifyChange(key, value) {
|
|
|
|
|
const listener = changeHandlers[key] || [];
|
|
|
|
|
listener.forEach(h => h(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// key is one of KEYS
|
|
|
|
|
function onChange(key, handler) {
|
|
|
|
|
if (typeof handler !== 'function') return;
|
|
|
|
|
|
|
|
|
|
if (!changeHandlers[key]) changeHandlers[key] = [];
|
|
|
|
|
|
|
|
|
|
changeHandlers[key].push(handler);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-31 21:02:48 +01:00
|
|
|
function create() {
|
|
|
|
|
const accessToken = localStorage.token;
|
|
|
|
|
|
2025-04-10 11:35:59 +02:00
|
|
|
// we use that currently only to generate the avatarUrl for the change handler
|
|
|
|
|
let profileCached = null;
|
|
|
|
|
|
2025-01-03 15:06:41 +01:00
|
|
|
return {
|
|
|
|
|
name: 'ProfileModel',
|
2025-01-14 14:52:10 +01:00
|
|
|
async logout() {
|
2025-05-12 22:23:36 +02:00
|
|
|
// attempt to destroy oidc session in the spirit of true SSO
|
|
|
|
|
// eslint-disable-next-line no-empty, no-unused-vars
|
|
|
|
|
try { await fetcher.del(`${API_ORIGIN}/api/v1/oidc/sessions`, null, { access_token: accessToken }); } catch (e) {}
|
2025-01-14 14:52:10 +01:00
|
|
|
|
|
|
|
|
localStorage.removeItem('token');
|
|
|
|
|
|
|
|
|
|
window.location.href = '/';
|
|
|
|
|
},
|
2025-01-03 15:06:41 +01:00
|
|
|
async get() {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.get(`${API_ORIGIN}/api/v1/profile`, { access_token: accessToken });
|
2025-01-03 15:06:41 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) {
|
|
|
|
|
console.error('Failed to get profile.', error || result.status);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-04 15:10:38 +01:00
|
|
|
if (error || result.status !== 200) return [error || result];
|
|
|
|
|
|
2025-03-16 11:12:49 +01:00
|
|
|
result.body.isAtLeastUserManager = [ ROLES.OWNER, ROLES.ADMIN, ROLES.MAIL_MANAGER, ROLES.USER_MANAGER ].indexOf(result.body.role) !== -1;
|
|
|
|
|
result.body.isAtLeastMailManager = [ ROLES.OWNER, ROLES.ADMIN, ROLES.MAIL_MANAGER ].indexOf(result.body.role) !== -1;
|
2025-01-03 15:06:41 +01:00
|
|
|
result.body.isAtLeastAdmin = [ ROLES.OWNER, ROLES.ADMIN ].indexOf(result.body.role) !== -1;
|
2025-02-04 15:10:38 +01:00
|
|
|
result.body.isAtLeastOwner = [ ROLES.OWNER ].indexOf(result.body.role) !== -1;
|
2025-01-03 15:06:41 +01:00
|
|
|
|
2025-03-25 15:05:08 +01:00
|
|
|
result.body.backgroundImageUrl = result.body.hasBackgroundImage ? `${API_ORIGIN}/api/v1/profile/background_image?access_token=${accessToken}&bustcache=${Date.now()}` : '';
|
2025-06-08 12:42:13 +02:00
|
|
|
result.body.avatarUrl = `${API_ORIGIN}/api/v1/profile/avatar/${result.body.id}`;
|
2025-03-25 15:05:08 +01:00
|
|
|
|
2025-04-10 11:35:59 +02:00
|
|
|
profileCached = result.body;
|
|
|
|
|
|
2025-02-04 15:10:38 +01:00
|
|
|
return [null, result.body];
|
2025-01-03 15:06:41 +01:00
|
|
|
},
|
2025-01-14 14:52:10 +01:00
|
|
|
async setPassword(password, newPassword) {
|
2025-06-11 10:34:58 +02:00
|
|
|
let result;
|
2025-01-14 14:52:10 +01:00
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/profile/password`, { password, newPassword }, { access_token: accessToken });
|
2025-01-14 14:52:10 +01:00
|
|
|
} catch (e) {
|
2025-06-11 10:34:58 +02:00
|
|
|
return [e];
|
2025-01-14 14:52:10 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-11 10:34:58 +02:00
|
|
|
if (result.status !== 204) return [result];
|
|
|
|
|
return [null];
|
2025-01-14 14:52:10 +01:00
|
|
|
},
|
2025-01-14 10:27:27 +01:00
|
|
|
async setDisplayName(displayName) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/profile/display_name`, { displayName }, { access_token: accessToken });
|
2025-01-14 10:27:27 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error) return error;
|
|
|
|
|
if (result.status !== 204) return result;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
},
|
2025-01-14 11:09:13 +01:00
|
|
|
async setEmail(email, password) {
|
2025-06-11 09:53:42 +02:00
|
|
|
let result;
|
2025-01-14 11:09:13 +01:00
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/profile/email`, { email, password }, { access_token: accessToken });
|
2025-01-14 11:09:13 +01:00
|
|
|
} catch (e) {
|
2025-06-11 09:53:42 +02:00
|
|
|
return [e];
|
2025-01-14 11:09:13 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-11 09:53:42 +02:00
|
|
|
if (result.status !== 204) return [result];
|
|
|
|
|
return [null];
|
2025-01-14 11:09:13 +01:00
|
|
|
},
|
2025-01-14 11:54:19 +01:00
|
|
|
async setFallbackEmail(fallbackEmail, password) {
|
2025-06-11 10:22:09 +02:00
|
|
|
let result;
|
2025-01-14 11:54:19 +01:00
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/profile/fallback_email`, { fallbackEmail, password }, { access_token: accessToken });
|
2025-01-14 11:54:19 +01:00
|
|
|
} catch (e) {
|
2025-06-11 10:22:09 +02:00
|
|
|
return [e];
|
2025-01-14 11:54:19 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-11 10:22:09 +02:00
|
|
|
if (result.status !== 204) return [result];
|
|
|
|
|
return [null];
|
2025-01-14 11:54:19 +01:00
|
|
|
},
|
2025-01-14 10:27:27 +01:00
|
|
|
async setLanguage(language) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/profile/language`, { language }, { access_token: accessToken });
|
2025-01-14 10:27:27 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error) return error;
|
|
|
|
|
if (result.status !== 204) return result;
|
|
|
|
|
|
2025-01-14 11:54:19 +01:00
|
|
|
return null;
|
|
|
|
|
},
|
2025-01-14 14:52:10 +01:00
|
|
|
async setAvatar(file) {
|
|
|
|
|
const fd = new FormData();
|
|
|
|
|
fd.append('avatar', file);
|
|
|
|
|
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/profile/avatar`, fd, { access_token: accessToken });
|
2025-01-14 14:52:10 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error) return error;
|
|
|
|
|
if (result.status !== 202) return result;
|
|
|
|
|
|
2025-04-10 11:35:59 +02:00
|
|
|
notifyChange(KEYS.AVATAR, `${API_ORIGIN}/api/v1/profile/avatar/${profileCached.id}?ts=${Date.now()}`);
|
|
|
|
|
|
2025-01-14 14:52:10 +01:00
|
|
|
return null;
|
|
|
|
|
},
|
2025-03-25 15:05:08 +01:00
|
|
|
async setBackgroundImage(file) {
|
|
|
|
|
const fd = new FormData();
|
|
|
|
|
if (file) fd.append('backgroundImage', file);
|
|
|
|
|
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
|
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/profile/background_image`, fd, { access_token: accessToken });
|
|
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error) return error;
|
|
|
|
|
if (result.status !== 202) return result;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
},
|
2025-01-14 11:54:19 +01:00
|
|
|
async sendPasswordReset(identifier) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/auth/password_reset_request`, { identifier }, { access_token: accessToken });
|
2025-01-14 11:54:19 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error) return error;
|
|
|
|
|
if (result.status !== 202) return result;
|
|
|
|
|
|
2025-01-14 10:27:27 +01:00
|
|
|
return null;
|
|
|
|
|
},
|
2025-01-16 17:27:12 +01:00
|
|
|
async setTwoFASecret() {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/profile/twofactorauthentication_secret`, {}, { access_token: accessToken });
|
2025-01-16 17:27:12 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) return [error || result];
|
|
|
|
|
return [null, result.body];
|
|
|
|
|
},
|
|
|
|
|
async enableTwoFA(totpToken) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/profile/twofactorauthentication_enable`, { totpToken }, { access_token: accessToken });
|
2025-01-16 17:27:12 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 204) return [error || result];
|
|
|
|
|
return [null];
|
|
|
|
|
},
|
|
|
|
|
async disableTwoFA(password) {
|
2025-06-11 10:47:47 +02:00
|
|
|
let result;
|
2025-01-16 17:27:12 +01:00
|
|
|
try {
|
2025-03-03 11:22:56 +01:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/profile/twofactorauthentication_disable`, { password }, { access_token: accessToken });
|
2025-01-16 17:27:12 +01:00
|
|
|
} catch (e) {
|
2025-06-11 10:47:47 +02:00
|
|
|
return [e];
|
2025-01-16 17:27:12 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-11 10:47:47 +02:00
|
|
|
if (result.status !== 204) return [result];
|
2025-01-16 17:27:12 +01:00
|
|
|
return [null];
|
|
|
|
|
},
|
2025-05-07 14:19:20 +02:00
|
|
|
async setNotificationConfig(notificationConfig) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
|
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/profile/notification_config`, { notificationConfig }, { access_token: accessToken });
|
|
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 204) return [error || result];
|
|
|
|
|
return [null, result.body];
|
|
|
|
|
},
|
2025-03-28 21:48:52 +01:00
|
|
|
async setupAccount(data) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
|
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/auth/setup_account`, data);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 201) return [error || result];
|
|
|
|
|
return [null, result.body];
|
|
|
|
|
},
|
2025-01-03 15:06:41 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
2025-04-10 11:35:59 +02:00
|
|
|
KEYS,
|
|
|
|
|
onChange,
|
2025-01-03 15:06:41 +01:00
|
|
|
create,
|
|
|
|
|
};
|