2019-09-25 19:19:42 +03:00
|
|
|
/**
|
2018-06-12 11:23:00 +03:00
|
|
|
* @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
|
|
|
* @author John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
import api from './api'
|
2020-12-16 12:41:41 +03:00
|
|
|
import axios from '@nextcloud/axios'
|
2020-05-08 11:01:54 +03:00
|
|
|
import { generateOcsUrl } from '@nextcloud/router'
|
2018-03-09 19:46:34 +03:00
|
|
|
|
|
|
|
const orderGroups = function(groups, orderBy) {
|
2018-04-09 12:03:21 +03:00
|
|
|
/* const SORT_USERCOUNT = 1;
|
|
|
|
* const SORT_GROUPNAME = 2;
|
|
|
|
* https://github.com/nextcloud/server/blob/208e38e84e1a07a49699aa90dc5b7272d24489f0/lib/private/Group/MetaData.php#L34
|
|
|
|
*/
|
|
|
|
if (orderBy === 1) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return groups.sort((a, b) => a.usercount - a.disabled < b.usercount - b.disabled)
|
2018-04-09 12:03:21 +03:00
|
|
|
} else {
|
2019-09-25 19:19:42 +03:00
|
|
|
return groups.sort((a, b) => a.name.localeCompare(b.name))
|
2018-04-09 12:03:21 +03:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
}
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-05-23 14:01:40 +03:00
|
|
|
const defaults = {
|
|
|
|
group: {
|
|
|
|
id: '',
|
|
|
|
name: '',
|
|
|
|
usercount: 0,
|
2018-06-29 09:37:04 +03:00
|
|
|
disabled: 0,
|
|
|
|
canAdd: true,
|
2019-11-13 15:05:10 +03:00
|
|
|
canRemove: true,
|
|
|
|
},
|
2019-09-25 19:19:42 +03:00
|
|
|
}
|
2018-05-23 14:01:40 +03:00
|
|
|
|
2018-03-09 19:46:34 +03:00
|
|
|
const state = {
|
2018-04-09 12:03:21 +03:00
|
|
|
users: [],
|
|
|
|
groups: [],
|
|
|
|
orderBy: 1,
|
|
|
|
minPasswordLength: 0,
|
|
|
|
usersOffset: 0,
|
|
|
|
usersLimit: 25,
|
2019-11-13 15:05:10 +03:00
|
|
|
userCount: 0,
|
2019-09-25 19:19:42 +03:00
|
|
|
}
|
2018-03-09 19:46:34 +03:00
|
|
|
|
|
|
|
const mutations = {
|
2018-04-09 12:03:21 +03:00
|
|
|
appendUsers(state, usersObj) {
|
|
|
|
// convert obj to array
|
2019-11-13 15:05:10 +03:00
|
|
|
const users = state.users.concat(Object.keys(usersObj).map(userid => usersObj[userid]))
|
2019-09-25 19:19:42 +03:00
|
|
|
state.usersOffset += state.usersLimit
|
|
|
|
state.users = users
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
setPasswordPolicyMinLength(state, length) {
|
2019-09-25 19:19:42 +03:00
|
|
|
state.minPasswordLength = length !== '' ? length : 0
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2019-09-25 19:19:42 +03:00
|
|
|
initGroups(state, { groups, orderBy, userCount }) {
|
|
|
|
state.groups = groups.map(group => Object.assign({}, defaults.group, group))
|
|
|
|
state.orderBy = orderBy
|
|
|
|
state.userCount = userCount
|
|
|
|
state.groups = orderGroups(state.groups, state.orderBy)
|
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2019-09-25 19:19:42 +03:00
|
|
|
addGroup(state, { gid, displayName }) {
|
2018-04-09 12:03:21 +03:00
|
|
|
try {
|
2018-07-04 13:05:30 +03:00
|
|
|
if (typeof state.groups.find((group) => group.id === gid) !== 'undefined') {
|
2019-09-25 19:19:42 +03:00
|
|
|
return
|
2018-07-04 13:05:30 +03:00
|
|
|
}
|
2018-05-23 14:01:40 +03:00
|
|
|
// extend group to default values
|
2019-11-13 15:05:10 +03:00
|
|
|
const group = Object.assign({}, defaults.group, {
|
2018-04-15 17:00:44 +03:00
|
|
|
id: gid,
|
2019-11-13 15:05:10 +03:00
|
|
|
name: displayName,
|
2019-09-25 19:19:42 +03:00
|
|
|
})
|
|
|
|
state.groups.push(group)
|
|
|
|
state.groups = orderGroups(state.groups, state.orderBy)
|
2018-04-09 12:03:21 +03:00
|
|
|
} catch (e) {
|
2019-09-25 19:19:42 +03:00
|
|
|
console.error('Can\'t create group', e)
|
2018-04-09 12:03:21 +03:00
|
|
|
}
|
|
|
|
},
|
2018-04-15 17:00:44 +03:00
|
|
|
removeGroup(state, gid) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const groupIndex = state.groups.findIndex(groupSearch => groupSearch.id === gid)
|
2018-04-15 17:00:44 +03:00
|
|
|
if (groupIndex >= 0) {
|
2019-09-25 19:19:42 +03:00
|
|
|
state.groups.splice(groupIndex, 1)
|
2018-04-15 17:00:44 +03:00
|
|
|
}
|
|
|
|
},
|
2018-04-09 12:03:21 +03:00
|
|
|
addUserGroup(state, { userid, gid }) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const group = state.groups.find(groupSearch => groupSearch.id === gid)
|
|
|
|
const user = state.users.find(user => user.id === userid)
|
2018-05-24 16:45:57 +03:00
|
|
|
// increase count if user is enabled
|
2019-10-15 19:19:23 +03:00
|
|
|
if (group && user.enabled && state.userCount > 0) {
|
2019-09-25 19:19:42 +03:00
|
|
|
group.usercount++
|
2018-04-09 12:03:21 +03:00
|
|
|
}
|
2019-11-13 15:05:10 +03:00
|
|
|
const groups = user.groups
|
2019-09-25 19:19:42 +03:00
|
|
|
groups.push(gid)
|
|
|
|
state.groups = orderGroups(state.groups, state.orderBy)
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
removeUserGroup(state, { userid, gid }) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const group = state.groups.find(groupSearch => groupSearch.id === gid)
|
|
|
|
const user = state.users.find(user => user.id === userid)
|
2018-05-24 16:45:57 +03:00
|
|
|
// lower count if user is enabled
|
2019-10-15 19:19:23 +03:00
|
|
|
if (group && user.enabled && state.userCount > 0) {
|
2019-09-25 19:19:42 +03:00
|
|
|
group.usercount--
|
2018-04-09 12:03:21 +03:00
|
|
|
}
|
2019-11-13 15:05:10 +03:00
|
|
|
const groups = user.groups
|
2019-09-25 19:19:42 +03:00
|
|
|
groups.splice(groups.indexOf(gid), 1)
|
|
|
|
state.groups = orderGroups(state.groups, state.orderBy)
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
addUserSubAdmin(state, { userid, gid }) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const groups = state.users.find(user => user.id === userid).subadmin
|
2019-09-25 19:19:42 +03:00
|
|
|
groups.push(gid)
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
removeUserSubAdmin(state, { userid, gid }) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const groups = state.users.find(user => user.id === userid).subadmin
|
2019-09-25 19:19:42 +03:00
|
|
|
groups.splice(groups.indexOf(gid), 1)
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
deleteUser(state, userid) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const userIndex = state.users.findIndex(user => user.id === userid)
|
2019-09-25 19:19:42 +03:00
|
|
|
state.users.splice(userIndex, 1)
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
addUserData(state, response) {
|
2019-09-25 19:19:42 +03:00
|
|
|
state.users.push(response.data.ocs.data)
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
enableDisableUser(state, { userid, enabled }) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const user = state.users.find(user => user.id === userid)
|
2019-09-25 19:19:42 +03:00
|
|
|
user.enabled = enabled
|
2018-04-09 12:03:21 +03:00
|
|
|
// increment or not
|
2019-10-15 19:19:23 +03:00
|
|
|
if (state.userCount > 0) {
|
|
|
|
state.groups.find(group => group.id === 'disabled').usercount += enabled ? -1 : 1
|
|
|
|
state.userCount += enabled ? 1 : -1
|
|
|
|
user.groups.forEach(group => {
|
|
|
|
// Increment disabled count
|
|
|
|
state.groups.find(groupSearch => groupSearch.id === group).disabled += enabled ? -1 : 1
|
|
|
|
})
|
|
|
|
}
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
setUserData(state, { userid, key, value }) {
|
|
|
|
if (key === 'quota') {
|
2019-11-13 15:05:10 +03:00
|
|
|
const humanValue = OC.Util.computerFileSize(value)
|
2019-09-25 19:19:42 +03:00
|
|
|
state.users.find(user => user.id === userid)[key][key] = humanValue !== null ? humanValue : value
|
2018-04-09 12:03:21 +03:00
|
|
|
} else {
|
2019-09-25 19:19:42 +03:00
|
|
|
state.users.find(user => user.id === userid)[key] = value
|
2018-04-09 12:03:21 +03:00
|
|
|
}
|
|
|
|
},
|
2018-04-06 21:56:02 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Reset users list
|
2019-09-25 19:19:42 +03:00
|
|
|
* @param {Object} state the store state
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
|
|
|
resetUsers(state) {
|
2019-09-25 19:19:42 +03:00
|
|
|
state.users = []
|
|
|
|
state.usersOffset = 0
|
2019-11-13 15:05:10 +03:00
|
|
|
},
|
2019-09-25 19:19:42 +03:00
|
|
|
}
|
2018-03-09 19:46:34 +03:00
|
|
|
|
|
|
|
const getters = {
|
2018-04-09 12:03:21 +03:00
|
|
|
getUsers(state) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return state.users
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
getGroups(state) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return state.groups
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-05-23 10:43:33 +03:00
|
|
|
getSubadminGroups(state) {
|
|
|
|
// Can't be subadmin of admin or disabled
|
2019-09-25 19:19:42 +03:00
|
|
|
return state.groups.filter(group => group.id !== 'admin' && group.id !== 'disabled')
|
2018-05-23 10:43:33 +03:00
|
|
|
},
|
2018-04-09 12:03:21 +03:00
|
|
|
getPasswordPolicyMinLength(state) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return state.minPasswordLength
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
getUsersOffset(state) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return state.usersOffset
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
getUsersLimit(state) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return state.usersLimit
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
getUserCount(state) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return state.userCount
|
2019-11-13 15:05:10 +03:00
|
|
|
},
|
2019-09-25 19:19:42 +03:00
|
|
|
}
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2020-12-16 12:41:41 +03:00
|
|
|
const CancelToken = axios.CancelToken
|
|
|
|
let searchRequestCancelSource = null
|
|
|
|
|
2018-03-09 19:46:34 +03:00
|
|
|
const actions = {
|
2018-04-06 21:56:02 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Get all users with full details
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
|
|
|
* @param {Object} options destructuring object
|
2018-04-09 12:03:21 +03:00
|
|
|
* @param {int} options.offset List offset to request
|
|
|
|
* @param {int} options.limit List number to return from offset
|
|
|
|
* @param {string} options.search Search amongst users
|
|
|
|
* @param {string} options.group Get users from group
|
2018-04-15 17:00:44 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
|
|
|
getUsers(context, { offset, limit, search, group }) {
|
2020-12-16 12:41:41 +03:00
|
|
|
if (searchRequestCancelSource) {
|
|
|
|
searchRequestCancelSource.cancel('Operation canceled by another search request.')
|
|
|
|
}
|
|
|
|
searchRequestCancelSource = CancelToken.source()
|
2019-09-25 19:19:42 +03:00
|
|
|
search = typeof search === 'string' ? search : ''
|
|
|
|
group = typeof group === 'string' ? group : ''
|
2018-04-09 12:03:21 +03:00
|
|
|
if (group !== '') {
|
2020-12-16 12:41:41 +03:00
|
|
|
return api.get(generateOcsUrl(`cloud/groups/${encodeURIComponent(encodeURIComponent(group))}/users/details?offset=${offset}&limit=${limit}&search=${search}`, 2), {
|
|
|
|
cancelToken: searchRequestCancelSource.token,
|
|
|
|
})
|
2019-09-25 19:19:42 +03:00
|
|
|
.then((response) => {
|
|
|
|
if (Object.keys(response.data.ocs.data.users).length > 0) {
|
|
|
|
context.commit('appendUsers', response.data.ocs.data.users)
|
2020-12-16 12:42:17 +03:00
|
|
|
return Object.keys(response.data.ocs.data.users).length === limit
|
2019-09-25 19:19:42 +03:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2020-12-16 12:41:41 +03:00
|
|
|
.catch((error) => {
|
|
|
|
if (!axios.isCancel(error)) {
|
|
|
|
context.commit('API_FAILURE', error)
|
|
|
|
}
|
|
|
|
})
|
2018-04-09 12:03:21 +03:00
|
|
|
}
|
2018-04-06 21:56:02 +03:00
|
|
|
|
2020-12-16 12:41:41 +03:00
|
|
|
return api.get(generateOcsUrl(`cloud/users/details?offset=${offset}&limit=${limit}&search=${search}`, 2), {
|
|
|
|
cancelToken: searchRequestCancelSource.token,
|
|
|
|
})
|
2018-04-09 12:03:21 +03:00
|
|
|
.then((response) => {
|
|
|
|
if (Object.keys(response.data.ocs.data.users).length > 0) {
|
2019-09-25 19:19:42 +03:00
|
|
|
context.commit('appendUsers', response.data.ocs.data.users)
|
2020-12-16 12:42:17 +03:00
|
|
|
return Object.keys(response.data.ocs.data.users).length === limit
|
2018-04-09 12:03:21 +03:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
return false
|
2018-04-09 12:03:21 +03:00
|
|
|
})
|
2020-12-16 12:41:41 +03:00
|
|
|
.catch((error) => {
|
|
|
|
if (!axios.isCancel(error)) {
|
|
|
|
context.commit('API_FAILURE', error)
|
|
|
|
}
|
|
|
|
})
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-06-05 18:37:58 +03:00
|
|
|
getGroups(context, { offset, limit, search }) {
|
2019-09-25 19:19:42 +03:00
|
|
|
search = typeof search === 'string' ? search : ''
|
2019-11-13 15:05:10 +03:00
|
|
|
const limitParam = limit === -1 ? '' : `&limit=${limit}`
|
2020-05-08 11:01:54 +03:00
|
|
|
return api.get(generateOcsUrl(`cloud/groups?offset=${offset}&search=${search}${limitParam}`, 2))
|
2018-05-22 09:58:35 +03:00
|
|
|
.then((response) => {
|
|
|
|
if (Object.keys(response.data.ocs.data.groups).length > 0) {
|
|
|
|
response.data.ocs.data.groups.forEach(function(group) {
|
2019-09-25 19:19:42 +03:00
|
|
|
context.commit('addGroup', { gid: group, displayName: group })
|
|
|
|
})
|
|
|
|
return true
|
2018-05-22 09:58:35 +03:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
return false
|
2018-05-22 09:58:35 +03:00
|
|
|
})
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => context.commit('API_FAILURE', error))
|
2018-05-22 09:58:35 +03:00
|
|
|
},
|
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Get all users with full details
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
|
|
|
* @param {Object} options destructuring object
|
2018-04-09 12:03:21 +03:00
|
|
|
* @param {int} options.offset List offset to request
|
|
|
|
* @param {int} options.limit List number to return from offset
|
2018-04-15 17:00:44 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
|
|
|
getUsersFromList(context, { offset, limit, search }) {
|
2019-09-25 19:19:42 +03:00
|
|
|
search = typeof search === 'string' ? search : ''
|
2020-05-08 11:01:54 +03:00
|
|
|
return api.get(generateOcsUrl(`cloud/users/details?offset=${offset}&limit=${limit}&search=${search}`, 2))
|
2018-04-09 12:03:21 +03:00
|
|
|
.then((response) => {
|
|
|
|
if (Object.keys(response.data.ocs.data.users).length > 0) {
|
2019-09-25 19:19:42 +03:00
|
|
|
context.commit('appendUsers', response.data.ocs.data.users)
|
|
|
|
return true
|
2018-04-09 12:03:21 +03:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
return false
|
2018-04-09 12:03:21 +03:00
|
|
|
})
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => context.commit('API_FAILURE', error))
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Get all users with full details from a groupid
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
|
|
|
* @param {Object} options destructuring object
|
2018-04-09 12:03:21 +03:00
|
|
|
* @param {int} options.offset List offset to request
|
|
|
|
* @param {int} options.limit List number to return from offset
|
2018-04-15 17:00:44 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
|
|
|
getUsersFromGroup(context, { groupid, offset, limit }) {
|
2020-05-08 11:01:54 +03:00
|
|
|
return api.get(generateOcsUrl(`cloud/users/${encodeURIComponent(encodeURIComponent(groupid))}/details?offset=${offset}&limit=${limit}`, 2))
|
2018-04-09 12:03:21 +03:00
|
|
|
.then((response) => context.commit('getUsersFromList', response.data.ocs.data.users))
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => context.commit('API_FAILURE', error))
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
getPasswordPolicyMinLength(context) {
|
2019-09-25 19:19:42 +03:00
|
|
|
if (OC.getCapabilities().password_policy && OC.getCapabilities().password_policy.minLength) {
|
|
|
|
context.commit('setPasswordPolicyMinLength', OC.getCapabilities().password_policy.minLength)
|
|
|
|
return OC.getCapabilities().password_policy.minLength
|
2018-05-15 19:22:19 +03:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
return false
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Add group
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
2018-04-09 12:03:21 +03:00
|
|
|
* @param {string} gid Group id
|
2018-04-15 17:00:44 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
|
|
|
addGroup(context, gid) {
|
|
|
|
return api.requireAdmin().then((response) => {
|
2020-07-31 10:40:53 +03:00
|
|
|
return api.post(generateOcsUrl('cloud/groups', 2), { groupid: gid })
|
2018-09-12 18:20:39 +03:00
|
|
|
.then((response) => {
|
2020-07-31 10:40:53 +03:00
|
|
|
context.commit('addGroup', { gid, displayName: gid })
|
|
|
|
return { gid, displayName: gid }
|
2018-09-12 18:20:39 +03:00
|
|
|
})
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => { throw error })
|
2018-05-24 16:45:57 +03:00
|
|
|
}).catch((error) => {
|
2019-09-25 19:19:42 +03:00
|
|
|
context.commit('API_FAILURE', { gid, error })
|
2018-05-24 16:45:57 +03:00
|
|
|
// let's throw one more time to prevent the view
|
|
|
|
// from adding the user to a group that doesn't exists
|
2019-09-25 19:19:42 +03:00
|
|
|
throw error
|
|
|
|
})
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Remove group
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
2018-04-09 12:03:21 +03:00
|
|
|
* @param {string} gid Group id
|
2018-04-15 17:00:44 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
|
|
|
removeGroup(context, gid) {
|
|
|
|
return api.requireAdmin().then((response) => {
|
2020-05-08 11:01:54 +03:00
|
|
|
return api.delete(generateOcsUrl(`cloud/groups/${encodeURIComponent(encodeURIComponent(gid))}`, 2))
|
2018-04-09 12:03:21 +03:00
|
|
|
.then((response) => context.commit('removeGroup', gid))
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => { throw error })
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { gid, error }))
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Add user to group
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
|
|
|
* @param {Object} options destructuring object
|
2018-04-09 12:03:21 +03:00
|
|
|
* @param {string} options.userid User id
|
|
|
|
* @param {string} options.gid Group id
|
2018-04-15 17:00:44 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
|
|
|
addUserGroup(context, { userid, gid }) {
|
|
|
|
return api.requireAdmin().then((response) => {
|
2020-05-08 11:01:54 +03:00
|
|
|
return api.post(generateOcsUrl(`cloud/users/${userid}/groups`, 2), { groupid: gid })
|
2018-04-09 12:03:21 +03:00
|
|
|
.then((response) => context.commit('addUserGroup', { userid, gid }))
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => { throw error })
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }))
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Remove user from group
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
|
|
|
* @param {Object} options destructuring object
|
2018-04-09 12:03:21 +03:00
|
|
|
* @param {string} options.userid User id
|
|
|
|
* @param {string} options.gid Group id
|
2018-04-15 17:00:44 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
|
|
|
removeUserGroup(context, { userid, gid }) {
|
|
|
|
return api.requireAdmin().then((response) => {
|
2020-05-08 11:01:54 +03:00
|
|
|
return api.delete(generateOcsUrl(`cloud/users/${userid}/groups`, 2), { groupid: gid })
|
2018-04-09 12:03:21 +03:00
|
|
|
.then((response) => context.commit('removeUserGroup', { userid, gid }))
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => { throw error })
|
2018-05-24 16:45:57 +03:00
|
|
|
}).catch((error) => {
|
2019-09-25 19:19:42 +03:00
|
|
|
context.commit('API_FAILURE', { userid, error })
|
2018-05-24 16:45:57 +03:00
|
|
|
// let's throw one more time to prevent
|
|
|
|
// the view from removing the user row on failure
|
2019-09-25 19:19:42 +03:00
|
|
|
throw error
|
|
|
|
})
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Add user to group admin
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
|
|
|
* @param {Object} options destructuring object
|
2018-04-09 12:03:21 +03:00
|
|
|
* @param {string} options.userid User id
|
|
|
|
* @param {string} options.gid Group id
|
2018-04-15 17:00:44 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
|
|
|
addUserSubAdmin(context, { userid, gid }) {
|
|
|
|
return api.requireAdmin().then((response) => {
|
2020-05-08 11:01:54 +03:00
|
|
|
return api.post(generateOcsUrl(`cloud/users/${userid}/subadmins`, 2), { groupid: gid })
|
2018-04-09 12:03:21 +03:00
|
|
|
.then((response) => context.commit('addUserSubAdmin', { userid, gid }))
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => { throw error })
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }))
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Remove user from group admin
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
|
|
|
* @param {Object} options destructuring object
|
2018-04-09 12:03:21 +03:00
|
|
|
* @param {string} options.userid User id
|
|
|
|
* @param {string} options.gid Group id
|
2018-04-15 17:00:44 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
|
|
|
removeUserSubAdmin(context, { userid, gid }) {
|
|
|
|
return api.requireAdmin().then((response) => {
|
2020-05-08 11:01:54 +03:00
|
|
|
return api.delete(generateOcsUrl(`cloud/users/${userid}/subadmins`, 2), { groupid: gid })
|
2018-04-09 12:03:21 +03:00
|
|
|
.then((response) => context.commit('removeUserSubAdmin', { userid, gid }))
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => { throw error })
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }))
|
2019-07-03 11:10:56 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark all user devices for remote wipe
|
|
|
|
*
|
2019-09-25 19:19:42 +03:00
|
|
|
* @param {Object} context store context
|
2019-07-03 11:10:56 +03:00
|
|
|
* @param {string} userid User id
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
wipeUserDevices(context, userid) {
|
|
|
|
return api.requireAdmin().then((response) => {
|
2020-05-08 11:01:54 +03:00
|
|
|
return api.post(generateOcsUrl(`cloud/users/${userid}/wipe`, 2))
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => { throw error })
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }))
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Delete a user
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
|
|
|
* @param {string} userid User id
|
2018-04-15 17:00:44 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
2018-06-22 18:05:33 +03:00
|
|
|
deleteUser(context, userid) {
|
2018-04-09 12:03:21 +03:00
|
|
|
return api.requireAdmin().then((response) => {
|
2020-05-08 11:01:54 +03:00
|
|
|
return api.delete(generateOcsUrl(`cloud/users/${userid}`, 2))
|
2018-04-09 12:03:21 +03:00
|
|
|
.then((response) => context.commit('deleteUser', userid))
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => { throw error })
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }))
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Add a user
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
|
|
|
* @param {Object} options destructuring object
|
2018-04-09 12:03:21 +03:00
|
|
|
* @param {string} options.userid User id
|
2018-04-19 01:20:14 +03:00
|
|
|
* @param {string} options.password User password
|
|
|
|
* @param {string} options.displayName User display name
|
2018-04-09 12:03:21 +03:00
|
|
|
* @param {string} options.email User email
|
|
|
|
* @param {string} options.groups User groups
|
|
|
|
* @param {string} options.subadmin User subadmin groups
|
|
|
|
* @param {string} options.quota User email
|
2018-04-15 17:00:44 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
2019-09-25 19:19:42 +03:00
|
|
|
addUser({ commit, dispatch }, { userid, password, displayName, email, groups, subadmin, quota, language }) {
|
2018-04-09 12:03:21 +03:00
|
|
|
return api.requireAdmin().then((response) => {
|
2020-07-31 10:40:53 +03:00
|
|
|
return api.post(generateOcsUrl('cloud/users', 2), { userid, password, displayName, email, groups, subadmin, quota, language })
|
2019-06-21 17:38:25 +03:00
|
|
|
.then((response) => dispatch('addUserData', userid || response.data.ocs.data.id))
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => { throw error })
|
2018-11-22 13:29:20 +03:00
|
|
|
}).catch((error) => {
|
2019-09-25 19:19:42 +03:00
|
|
|
commit('API_FAILURE', { userid, error })
|
|
|
|
throw error
|
|
|
|
})
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Get user data and commit addition
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
|
|
|
* @param {string} userid User id
|
2018-04-15 17:00:44 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
|
|
|
addUserData(context, userid) {
|
|
|
|
return api.requireAdmin().then((response) => {
|
2020-05-08 11:01:54 +03:00
|
|
|
return api.get(generateOcsUrl(`cloud/users/${userid}`, 2))
|
2018-04-09 12:03:21 +03:00
|
|
|
.then((response) => context.commit('addUserData', response))
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => { throw error })
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }))
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
/** Enable or disable user
|
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
|
|
|
* @param {Object} options destructuring object
|
2018-04-09 12:03:21 +03:00
|
|
|
* @param {string} options.userid User id
|
|
|
|
* @param {boolean} options.enabled User enablement status
|
2018-04-15 17:00:44 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
|
|
|
enableDisableUser(context, { userid, enabled = true }) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const userStatus = enabled ? 'enable' : 'disable'
|
2018-04-09 12:03:21 +03:00
|
|
|
return api.requireAdmin().then((response) => {
|
2020-05-08 11:01:54 +03:00
|
|
|
return api.put(generateOcsUrl(`cloud/users/${userid}/${userStatus}`, 2))
|
2018-04-09 12:03:21 +03:00
|
|
|
.then((response) => context.commit('enableDisableUser', { userid, enabled }))
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => { throw error })
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }))
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Edit user data
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
|
|
|
* @param {Object} options destructuring object
|
2018-04-09 12:03:21 +03:00
|
|
|
* @param {string} options.userid User id
|
|
|
|
* @param {string} options.key User field to edit
|
|
|
|
* @param {string} options.value Value of the change
|
2018-04-15 17:00:44 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
|
|
|
setUserData(context, { userid, key, value }) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const allowedEmpty = ['email', 'displayname']
|
2018-04-09 12:03:21 +03:00
|
|
|
if (['email', 'language', 'quota', 'displayname', 'password'].indexOf(key) !== -1) {
|
|
|
|
// We allow empty email or displayname
|
2019-09-25 19:19:42 +03:00
|
|
|
if (typeof value === 'string'
|
|
|
|
&& (
|
|
|
|
(allowedEmpty.indexOf(key) === -1 && value.length > 0)
|
|
|
|
|| allowedEmpty.indexOf(key) !== -1
|
2018-04-09 12:03:21 +03:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
return api.requireAdmin().then((response) => {
|
2020-07-31 10:40:53 +03:00
|
|
|
return api.put(generateOcsUrl(`cloud/users/${userid}`, 2), { key, value })
|
2018-04-09 12:03:21 +03:00
|
|
|
.then((response) => context.commit('setUserData', { userid, key, value }))
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => { throw error })
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }))
|
2018-04-09 12:03:21 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
return Promise.reject(new Error('Invalid request data'))
|
2018-06-22 18:05:33 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send welcome mail
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
|
|
|
* @param {Object} context store context
|
|
|
|
* @param {string} userid User id
|
2018-06-22 18:05:33 +03:00
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
sendWelcomeMail(context, userid) {
|
|
|
|
return api.requireAdmin().then((response) => {
|
2020-05-08 11:01:54 +03:00
|
|
|
return api.post(generateOcsUrl(`cloud/users/${userid}/welcome`, 2))
|
2018-06-22 18:05:33 +03:00
|
|
|
.then(response => true)
|
2019-09-25 19:19:42 +03:00
|
|
|
.catch((error) => { throw error })
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }))
|
2019-11-13 15:05:10 +03:00
|
|
|
},
|
2019-09-25 19:19:42 +03:00
|
|
|
}
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
export default { state, mutations, getters, actions }
|