|
| 1 | +/** |
| 2 | + * @copyright 2021, Christopher Ng <chrng8@gmail.com> |
| 3 | + * |
| 4 | + * @author Christopher Ng <chrng8@gmail.com> |
| 5 | + * |
| 6 | + * @license GNU AGPL version 3 or any later version |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Affero General Public License as |
| 10 | + * published by the Free Software Foundation, either version 3 of the |
| 11 | + * License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU Affero General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Affero General Public License |
| 19 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +import axios from '@nextcloud/axios' |
| 24 | +import { getCurrentUser } from '@nextcloud/auth' |
| 25 | +import { generateOcsUrl } from '@nextcloud/router' |
| 26 | +import confirmPassword from '@nextcloud/password-confirmation' |
| 27 | + |
| 28 | +import { ACCOUNT_PROPERTY_ENUM, SCOPE_SUFFIX } from '../constants/AccountPropertyConstants' |
| 29 | + |
| 30 | +/** |
| 31 | + * Save the primary email of the user |
| 32 | + * |
| 33 | + * @param {string} email the primary email |
| 34 | + * @returns {Object} |
| 35 | + */ |
| 36 | +export const savePrimaryEmail = async(email) => { |
| 37 | + const userId = getCurrentUser().uid |
| 38 | + // TODO upgrade @nextcloud/router to v2.0 so we can remove the .slice() trailing slash hacks (same below) |
| 39 | + const url = generateOcsUrl(`cloud/users/${userId}`, 2).slice(0, -1) |
| 40 | + |
| 41 | + await confirmPassword() |
| 42 | + |
| 43 | + const res = await axios.put(url, { |
| 44 | + key: ACCOUNT_PROPERTY_ENUM.EMAIL, |
| 45 | + value: email, |
| 46 | + }) |
| 47 | + |
| 48 | + return res.data |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Save an additional email of the user |
| 53 | + * |
| 54 | + * *Will be appended to the user's additional emails* |
| 55 | + * |
| 56 | + * @param {string} email the additional email |
| 57 | + * @returns {Object} |
| 58 | + */ |
| 59 | +export const saveAdditionalEmail = async(email) => { |
| 60 | + const userId = getCurrentUser().uid |
| 61 | + const url = generateOcsUrl(`cloud/users/${userId}`, 2).slice(0, -1) |
| 62 | + |
| 63 | + await confirmPassword() |
| 64 | + |
| 65 | + const res = await axios.put(url, { |
| 66 | + key: ACCOUNT_PROPERTY_ENUM.EMAIL_COLLECTION, |
| 67 | + value: email, |
| 68 | + }) |
| 69 | + |
| 70 | + return res.data |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Remove an additional email of the user |
| 75 | + * |
| 76 | + * @param {string} email the additional email |
| 77 | + * @returns {Object} |
| 78 | + */ |
| 79 | +export const removeAdditionalEmail = async(email) => { |
| 80 | + const userId = getCurrentUser().uid |
| 81 | + const url = generateOcsUrl(`cloud/users/${userId}/${ACCOUNT_PROPERTY_ENUM.EMAIL_COLLECTION}`, 2).slice(0, -1) |
| 82 | + |
| 83 | + await confirmPassword() |
| 84 | + |
| 85 | + const res = await axios.put(url, { |
| 86 | + key: email, |
| 87 | + value: '', |
| 88 | + }) |
| 89 | + |
| 90 | + return res.data |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Update an additional email of the user |
| 95 | + * |
| 96 | + * @param {string} prevEmail the additional email to be updated |
| 97 | + * @param {string} newEmail the new additional email |
| 98 | + * @returns {Object} |
| 99 | + */ |
| 100 | +export const updateAdditionalEmail = async(prevEmail, newEmail) => { |
| 101 | + const userId = getCurrentUser().uid |
| 102 | + const url = generateOcsUrl(`cloud/users/${userId}/${ACCOUNT_PROPERTY_ENUM.EMAIL_COLLECTION}`, 2).slice(0, -1) |
| 103 | + |
| 104 | + await confirmPassword() |
| 105 | + |
| 106 | + const res = await axios.put(url, { |
| 107 | + key: prevEmail, |
| 108 | + value: newEmail, |
| 109 | + }) |
| 110 | + |
| 111 | + return res.data |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Save the federation scope for the primary email of the user |
| 116 | + * |
| 117 | + * @param {string} scope the federation scope |
| 118 | + * @returns {Object} |
| 119 | + */ |
| 120 | +export const savePrimaryEmailScope = async(scope) => { |
| 121 | + const userId = getCurrentUser().uid |
| 122 | + const url = generateOcsUrl(`cloud/users/${userId}`, 2).slice(0, -1) |
| 123 | + |
| 124 | + await confirmPassword() |
| 125 | + |
| 126 | + const res = await axios.put(url, { |
| 127 | + key: `${ACCOUNT_PROPERTY_ENUM.EMAIL}${SCOPE_SUFFIX}`, |
| 128 | + value: scope, |
| 129 | + }) |
| 130 | + |
| 131 | + return res.data |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Save the federation scope for the additional email of the user |
| 136 | + * |
| 137 | + * @param {string} email the additional email |
| 138 | + * @param {string} scope the federation scope |
| 139 | + * @returns {Object} |
| 140 | + */ |
| 141 | +export const saveAdditionalEmailScope = async(email, scope) => { |
| 142 | + const userId = getCurrentUser().uid |
| 143 | + const url = generateOcsUrl(`cloud/users/${userId}/${ACCOUNT_PROPERTY_ENUM.EMAIL_COLLECTION}${SCOPE_SUFFIX}`, 2).slice(0, -1) |
| 144 | + |
| 145 | + await confirmPassword() |
| 146 | + |
| 147 | + const res = await axios.put(url, { |
| 148 | + key: email, |
| 149 | + value: scope, |
| 150 | + }) |
| 151 | + |
| 152 | + return res.data |
| 153 | +} |
0 commit comments