admin-fe/src/store/modules/users.js

325 lines
12 KiB
JavaScript
Raw Normal View History

import { Message } from 'element-ui'
import i18n from '@/lang'
import {
activateUsers,
addRight,
createNewAccount,
deactivateUsers,
deleteRight,
deleteUsers,
disableMfa,
fetchUsers,
getPasswordResetToken,
searchUsers,
tagUser,
untagUser,
2020-01-28 13:06:49 +00:00
forcePasswordReset,
2020-08-05 22:53:23 +00:00
approveUserAccount,
confirmUserEmail,
resendConfirmationEmail,
updateUserCredentials
} from '@/api/users'
import { fetchSettings, updateSettings } from '@/api/settings'
2019-02-23 21:40:26 +00:00
2019-03-22 20:58:58 +00:00
const users = {
2019-02-23 21:40:26 +00:00
state: {
2019-02-24 22:31:05 +00:00
fetchedUsers: [],
2019-03-03 20:01:00 +00:00
loading: true,
searchQuery: '',
2020-08-28 21:45:15 +00:00
mrfPolicies: [],
2019-03-03 20:01:00 +00:00
totalUsersCount: 0,
2019-03-06 19:44:37 +00:00
currentPage: 1,
pageSize: 50,
2019-03-29 14:25:53 +00:00
filters: {
local: false,
external: false,
active: false,
2020-08-08 22:59:14 +00:00
need_approval: false,
2019-03-29 14:25:53 +00:00
deactivated: false
2019-09-23 19:00:28 +00:00
},
passwordResetToken: {
token: '',
link: ''
2019-03-29 14:25:53 +00:00
}
2019-02-23 21:40:26 +00:00
},
mutations: {
SET_USERS: (state, users) => {
2019-03-01 17:32:03 +00:00
state.fetchedUsers = users
2019-02-24 22:31:05 +00:00
},
SET_LOADING: (state, status) => {
state.loading = status
2019-02-26 19:50:25 +00:00
},
SWAP_USERS: (state, users) => {
const usersWithoutSwapped = users.reduce((acc, user) => {
return acc.filter(u => u.id !== user.id)
}, state.fetchedUsers)
if (state.fetchedUsers.length === 0) {
return
}
const updatedUsers = [...usersWithoutSwapped, ...users]
state.fetchedUsers = updatedUsers
.filter(user => user.nickname && user.id)
.sort((a, b) => a.nickname.localeCompare(b.nickname))
.concat(updatedUsers.filter(user => !user.nickname || !user.id))
},
2019-02-27 22:13:20 +00:00
SET_COUNT: (state, count) => {
state.totalUsersCount = count
},
2019-03-03 20:46:14 +00:00
SET_PAGE: (state, page) => {
state.currentPage = page
},
2019-02-27 22:13:20 +00:00
SET_PAGE_SIZE: (state, pageSize) => {
state.pageSize = pageSize
2019-03-03 20:01:00 +00:00
},
2019-09-23 19:00:28 +00:00
SET_PASSWORD_RESET_TOKEN: (state, { token, link }) => {
state.passwordResetToken.token = token
state.passwordResetToken.link = link
},
2019-03-03 20:01:00 +00:00
SET_SEARCH_QUERY: (state, query) => {
state.searchQuery = query
2019-03-06 19:44:37 +00:00
},
2020-08-28 21:45:15 +00:00
SET_TAG_POLICY: (state, mrfPolicies) => {
state.mrfPolicies = mrfPolicies
2020-08-26 22:07:20 +00:00
},
2019-03-29 14:25:53 +00:00
SET_USERS_FILTERS: (state, filters) => {
state.filters = filters
2019-02-23 21:40:26 +00:00
}
},
actions: {
async ActivateUsers({ dispatch, getters }, { users, _userId }) {
const updatedUsers = users.map(user => {
return { ...user, deactivated: false }
})
2020-01-27 18:07:23 +00:00
const nicknames = users.map(user => user.nickname)
const callApiFn = async() => await activateUsers(nicknames, getters.authHost, getters.token)
dispatch('ApplyChanges', { updatedUsers, callApiFn, userId: _userId })
2020-01-27 18:07:23 +00:00
},
async ApplyChanges({ commit, dispatch, state }, { updatedUsers, callApiFn, userId, statusId }) {
commit('SWAP_USERS', updatedUsers)
try {
2020-01-27 18:07:23 +00:00
await callApiFn()
} catch (_e) {
return
} finally {
dispatch('SearchUsers', { query: state.searchQuery, page: state.currentPage })
}
if (statusId) {
dispatch('FetchStatusAfterUserModeration', statusId)
} else if (userId) {
dispatch('FetchUserProfile', { userId, godmode: false })
}
dispatch('SuccessMessage')
},
async AddRight({ dispatch, getters }, { users, right, _userId, _statusId }) {
const updatedUsers = users.map(user => {
return user.local ? { ...user, roles: { ...user.roles, [right]: true }} : user
})
2020-01-27 18:07:23 +00:00
const nicknames = users.map(user => user.nickname)
const callApiFn = async() => await addRight(nicknames, right, getters.authHost, getters.token)
dispatch('ApplyChanges', { updatedUsers, callApiFn, userId: _userId, statusId: _statusId })
},
async AddTag({ dispatch, getters }, { users, tag, _userId, _statusId }) {
const updatedUsers = users.map(user => {
return { ...user, tags: [...user.tags, tag] }
})
const nicknames = users.map(user => user.nickname)
2020-01-27 18:07:23 +00:00
const callApiFn = async() => await tagUser(nicknames, [tag], getters.authHost, getters.token)
dispatch('ApplyChanges', { updatedUsers, callApiFn, userId: _userId, statusId: _statusId })
},
2020-08-08 22:59:14 +00:00
async ApproveUsersAccount({ dispatch, getters }, { users, _userId, _statusId }) {
const updatedUsers = users.map(user => {
return { ...user, approval_pending: false }
})
const nicknames = users.map(user => user.nickname)
const callApiFn = async() => await approveUserAccount(nicknames, getters.authHost, getters.token)
dispatch('ApplyChanges', { updatedUsers, callApiFn, userId: _userId, statusId: _statusId })
},
ClearUsersState({ commit }) {
commit('SET_SEARCH_QUERY', '')
2020-08-08 22:59:14 +00:00
commit('SET_USERS_FILTERS', { local: false, external: false, active: false, need_approval: false, deactivated: false })
},
async ClearFilters({ commit, dispatch, state }) {
commit('CLEAR_USERS_FILTERS')
dispatch('SearchUsers', { query: state.searchQuery, page: 1 })
},
2020-08-08 22:59:14 +00:00
async ConfirmUsersEmail({ dispatch, getters }, { users, _userId, _statusId }) {
const updatedUsers = users.map(user => {
return { ...user, confirmation_pending: false }
})
const nicknames = users.map(user => user.nickname)
const callApiFn = async() => await confirmUserEmail(nicknames, getters.authHost, getters.token)
dispatch('ApplyChanges', { updatedUsers, callApiFn, userId: _userId, statusId: _statusId })
},
2019-06-11 15:13:48 +00:00
async CreateNewAccount({ dispatch, getters, state }, { nickname, email, password }) {
try {
await createNewAccount(nickname, email, password, getters.authHost, getters.token)
} catch (_e) {
return
} finally {
dispatch('SearchUsers', { query: state.searchQuery, page: state.currentPage })
}
dispatch('SuccessMessage')
2019-06-11 15:13:48 +00:00
},
async DeactivateUsers({ dispatch, getters }, { users, _userId }) {
const updatedUsers = users.map(user => {
return { ...user, deactivated: true }
})
2020-01-27 18:07:23 +00:00
const nicknames = users.map(user => user.nickname)
const callApiFn = async() => await deactivateUsers(nicknames, getters.authHost, getters.token)
dispatch('ApplyChanges', { updatedUsers, callApiFn, userId: _userId })
},
async DisableMfa({ dispatch, getters }, nickname) {
try {
await disableMfa(nickname, getters.authHost, getters.token)
} catch (_e) {
return
}
dispatch('SuccessMessage')
},
async DeleteRight({ dispatch, getters }, { users, right, _userId, _statusId }) {
const updatedUsers = users.map(user => {
return user.local ? { ...user, roles: { ...user.roles, [right]: false }} : user
})
2020-01-27 18:07:23 +00:00
const nicknames = users.map(user => user.nickname)
const callApiFn = async() => await deleteRight(nicknames, right, getters.authHost, getters.token)
dispatch('ApplyChanges', { updatedUsers, callApiFn, userId: _userId, statusId: _statusId })
},
async DeleteUsers({ commit, dispatch, getters, state }, { users, _userId }) {
const usersNicknames = users.map(user => user.nickname)
try {
await deleteUsers(usersNicknames, getters.authHost, getters.token)
} catch (_e) {
return
}
const updatedUsers = users.map(user => {
return { ...user, deactivated: true }
})
commit('SWAP_USERS', updatedUsers)
if (_userId) {
dispatch('FetchUserProfile', { userId: _userId, godmode: false })
}
dispatch('SuccessMessage')
},
async EnableTagPolicy({ dispatch, getters, state }) {
const configs = [{
group: ':pleroma',
key: ':mrf',
2020-08-28 21:45:15 +00:00
value: [{ tuple: [':policies', [...state.mrfPolicies, 'Pleroma.Web.ActivityPub.MRF.TagPolicy']] }]
}]
await updateSettings(configs, getters.authHost, getters.token)
dispatch('FetchTagPolicySetting')
},
2020-08-26 22:07:20 +00:00
async FetchTagPolicySetting({ commit, getters }) {
const { data } = await fetchSettings(getters.authHost, getters.token)
2020-08-28 21:45:15 +00:00
const mrfPolicies = data.configs
2020-08-26 22:07:20 +00:00
.find(el => el.key === ':mrf').value
.find(el => el.tuple[0] === ':policies').tuple[1] || []
2020-08-26 22:07:20 +00:00
2020-08-28 21:45:15 +00:00
commit('SET_TAG_POLICY', Array.isArray(mrfPolicies) ? mrfPolicies : [mrfPolicies])
2020-08-26 22:07:20 +00:00
},
async FetchUsers({ commit, dispatch, getters, state }, { page }) {
2019-04-01 21:17:35 +00:00
commit('SET_LOADING', true)
2019-03-29 14:25:53 +00:00
const filters = Object.keys(state.filters).filter(filter => state.filters[filter]).join()
const response = await fetchUsers(filters, getters.authHost, getters.token, page)
await dispatch('GetNodeInfo')
2019-03-03 20:46:14 +00:00
loadUsers(commit, page, response.data)
2019-02-26 19:50:25 +00:00
},
async GetPasswordResetToken({ commit, getters }, nickname) {
2019-09-23 19:00:28 +00:00
const { data } = await getPasswordResetToken(nickname, getters.authHost, getters.token)
commit('SET_PASSWORD_RESET_TOKEN', data)
},
RemovePasswordToken({ commit }) {
commit('SET_PASSWORD_RESET_TOKEN', { link: '', token: '' })
},
async RemoveTag({ dispatch, getters }, { users, tag, _userId, _statusId }) {
const updatedUsers = users.map(user => {
return { ...user, tags: user.tags.filter(userTag => userTag !== tag) }
})
const nicknames = users.map(user => user.nickname)
2020-01-27 18:07:23 +00:00
const callApiFn = async() => await untagUser(nicknames, [tag], getters.authHost, getters.token)
dispatch('ApplyChanges', { updatedUsers, callApiFn, userId: _userId, statusId: _statusId })
},
2020-01-28 13:06:49 +00:00
async RequirePasswordReset({ dispatch, getters }, users) {
const nicknames = users.map(user => user.nickname)
try {
2020-01-28 13:06:49 +00:00
await forcePasswordReset(nicknames, getters.authHost, getters.token)
} catch (_e) {
return
}
dispatch('SuccessMessage')
2019-03-01 17:32:03 +00:00
},
2020-08-08 22:59:14 +00:00
async ResendConfirmationEmail({ dispatch, getters }, users) {
const usersNicknames = users.map(user => user.nickname)
try {
await resendConfirmationEmail(usersNicknames, getters.authHost, getters.token)
} catch (_e) {
return
}
dispatch('SuccessMessage')
},
2019-03-22 20:58:58 +00:00
async SearchUsers({ commit, dispatch, state, getters }, { query, page }) {
2019-03-03 20:01:00 +00:00
if (query.length === 0) {
commit('SET_SEARCH_QUERY', query)
2019-06-11 15:13:48 +00:00
dispatch('FetchUsers', { page })
2019-03-01 17:32:03 +00:00
} else {
2019-03-03 20:01:00 +00:00
commit('SET_LOADING', true)
commit('SET_SEARCH_QUERY', query)
2019-03-29 14:25:53 +00:00
const filters = Object.keys(state.filters).filter(filter => state.filters[filter]).join()
const response = await searchUsers(query, filters, getters.authHost, getters.token, page)
2019-03-03 20:01:00 +00:00
2019-03-03 20:46:14 +00:00
loadUsers(commit, page, response.data)
2019-03-01 17:32:03 +00:00
}
2019-03-06 19:44:37 +00:00
},
SuccessMessage() {
2019-12-20 13:51:54 +00:00
Message.success({
message: i18n.t('users.completed'),
duration: 5 * 1000
})
},
2019-03-29 14:25:53 +00:00
async ToggleUsersFilter({ commit, dispatch, state }, filters) {
const defaultFilters = {
local: false,
external: false,
active: false,
2020-08-08 22:59:14 +00:00
need_approval: false,
2019-03-29 14:25:53 +00:00
deactivated: false
}
const currentFilters = { ...defaultFilters, ...filters }
commit('SET_USERS_FILTERS', currentFilters)
dispatch('SearchUsers', { query: state.searchQuery, page: 1 })
},
async UpdateActorType({ dispatch, getters }, { user, type, _userId, _statusId }) {
const updatedUsers = [{ ...user, actor_type: type }]
const credentials = { actor_type: type }
const callApiFn = async() => await updateUserCredentials(user.nickname, credentials, getters.authHost, getters.token)
dispatch('ApplyChanges', { updatedUsers, callApiFn, userId: _userId, statusId: _statusId })
2019-02-23 21:40:26 +00:00
}
}
}
2019-03-03 20:46:14 +00:00
const loadUsers = (commit, page, { users, count, page_size }) => {
2019-03-03 20:01:00 +00:00
commit('SET_USERS', users)
commit('SET_COUNT', count)
2019-03-03 20:46:14 +00:00
commit('SET_PAGE', page)
2019-03-03 20:01:00 +00:00
commit('SET_PAGE_SIZE', page_size)
commit('SET_LOADING', false)
}
2019-03-22 20:58:58 +00:00
export default users