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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-03-09 19:46:34 +03:00
|
|
|
import api from './api';
|
|
|
|
|
|
|
|
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) {
|
2018-05-24 16:45:57 +03:00
|
|
|
return groups.sort((a, b) => a.usercount-a.disabled < b.usercount - b.disabled);
|
2018-04-09 12:03:21 +03:00
|
|
|
} else {
|
|
|
|
return groups.sort((a, b) => a.name.localeCompare(b.name));
|
|
|
|
}
|
2018-04-11 11:58:46 +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,
|
|
|
|
canRemove: true
|
2018-05-23 14:01:40 +03:00
|
|
|
}
|
2018-06-22 18:05:33 +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,
|
|
|
|
userCount: 0
|
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
|
|
|
|
let users = state.users.concat(Object.keys(usersObj).map(userid => usersObj[userid]));
|
|
|
|
state.usersOffset += state.usersLimit;
|
|
|
|
state.users = users;
|
|
|
|
},
|
|
|
|
setPasswordPolicyMinLength(state, length) {
|
|
|
|
state.minPasswordLength = length!=='' ? length : 0;
|
|
|
|
},
|
|
|
|
initGroups(state, {groups, orderBy, userCount}) {
|
2018-05-23 14:01:40 +03:00
|
|
|
state.groups = groups.map(group => Object.assign({}, defaults.group, group));
|
2018-04-09 12:03:21 +03:00
|
|
|
state.orderBy = orderBy;
|
|
|
|
state.userCount = userCount;
|
|
|
|
state.groups = orderGroups(state.groups, state.orderBy);
|
2018-05-23 14:01:40 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2018-05-22 09:58:35 +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') {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-23 14:01:40 +03:00
|
|
|
// extend group to default values
|
|
|
|
let group = Object.assign({}, defaults.group, {
|
2018-04-15 17:00:44 +03:00
|
|
|
id: gid,
|
2018-05-22 09:58:35 +03:00
|
|
|
name: displayName,
|
2018-04-09 12:03:21 +03:00
|
|
|
});
|
2018-05-23 14:01:40 +03:00
|
|
|
state.groups.push(group);
|
2018-04-09 12:03:21 +03:00
|
|
|
state.groups = orderGroups(state.groups, state.orderBy);
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Can\'t create group', e);
|
|
|
|
}
|
|
|
|
},
|
2018-04-15 17:00:44 +03:00
|
|
|
removeGroup(state, gid) {
|
|
|
|
let groupIndex = state.groups.findIndex(groupSearch => groupSearch.id == gid);
|
|
|
|
if (groupIndex >= 0) {
|
|
|
|
state.groups.splice(groupIndex, 1);
|
|
|
|
}
|
|
|
|
},
|
2018-04-09 12:03:21 +03:00
|
|
|
addUserGroup(state, { userid, gid }) {
|
|
|
|
let group = state.groups.find(groupSearch => groupSearch.id == gid);
|
2018-05-24 16:45:57 +03:00
|
|
|
let user = state.users.find(user => user.id == userid);
|
|
|
|
// increase count if user is enabled
|
|
|
|
if (group && user.enabled) {
|
|
|
|
group.usercount++;
|
2018-04-09 12:03:21 +03:00
|
|
|
}
|
2018-05-24 16:45:57 +03:00
|
|
|
let groups = user.groups;
|
2018-04-09 12:03:21 +03:00
|
|
|
groups.push(gid);
|
|
|
|
state.groups = orderGroups(state.groups, state.orderBy);
|
|
|
|
},
|
|
|
|
removeUserGroup(state, { userid, gid }) {
|
|
|
|
let group = state.groups.find(groupSearch => groupSearch.id == gid);
|
2018-05-24 16:45:57 +03:00
|
|
|
let user = state.users.find(user => user.id == userid);
|
|
|
|
// lower count if user is enabled
|
|
|
|
if (group && user.enabled) {
|
|
|
|
group.usercount--;
|
2018-04-09 12:03:21 +03:00
|
|
|
}
|
2018-05-24 16:45:57 +03:00
|
|
|
let groups = user.groups;
|
2018-04-09 12:03:21 +03:00
|
|
|
groups.splice(groups.indexOf(gid),1);
|
|
|
|
state.groups = orderGroups(state.groups, state.orderBy);
|
|
|
|
},
|
|
|
|
addUserSubAdmin(state, { userid, gid }) {
|
|
|
|
let groups = state.users.find(user => user.id == userid).subadmin;
|
|
|
|
groups.push(gid);
|
|
|
|
},
|
|
|
|
removeUserSubAdmin(state, { userid, gid }) {
|
|
|
|
let groups = state.users.find(user => user.id == userid).subadmin;
|
|
|
|
groups.splice(groups.indexOf(gid),1);
|
|
|
|
},
|
|
|
|
deleteUser(state, userid) {
|
|
|
|
let userIndex = state.users.findIndex(user => user.id == userid);
|
|
|
|
state.users.splice(userIndex, 1);
|
|
|
|
},
|
|
|
|
addUserData(state, response) {
|
|
|
|
state.users.push(response.data.ocs.data);
|
|
|
|
},
|
|
|
|
enableDisableUser(state, { userid, enabled }) {
|
2018-05-23 14:01:40 +03:00
|
|
|
let user = state.users.find(user => user.id == userid);
|
|
|
|
user.enabled = enabled;
|
2018-04-09 12:03:21 +03:00
|
|
|
// increment or not
|
2018-04-14 11:34:40 +03:00
|
|
|
state.groups.find(group => group.id == 'disabled').usercount += enabled ? -1 : 1;
|
2018-04-09 12:03:21 +03:00
|
|
|
state.userCount += enabled ? 1 : -1;
|
2018-05-23 14:01:40 +03:00
|
|
|
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') {
|
|
|
|
let humanValue = OC.Util.computerFileSize(value);
|
2018-05-26 11:51:24 +03:00
|
|
|
state.users.find(user => user.id == userid)[key][key] = humanValue!==null ? humanValue : value;
|
2018-04-09 12:03:21 +03:00
|
|
|
} else {
|
|
|
|
state.users.find(user => user.id == userid)[key] = value;
|
|
|
|
}
|
|
|
|
},
|
2018-04-06 21:56:02 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Reset users list
|
|
|
|
*/
|
|
|
|
resetUsers(state) {
|
|
|
|
state.users = [];
|
|
|
|
state.usersOffset = 0;
|
|
|
|
}
|
2018-03-09 19:46:34 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const getters = {
|
2018-04-09 12:03:21 +03:00
|
|
|
getUsers(state) {
|
|
|
|
return state.users;
|
|
|
|
},
|
|
|
|
getGroups(state) {
|
|
|
|
return state.groups;
|
|
|
|
},
|
2018-05-23 10:43:33 +03:00
|
|
|
getSubadminGroups(state) {
|
|
|
|
// Can't be subadmin of admin or disabled
|
|
|
|
return state.groups.filter(group => group.id !== 'admin' && group.id !== 'disabled');
|
|
|
|
},
|
2018-04-09 12:03:21 +03:00
|
|
|
getPasswordPolicyMinLength(state) {
|
|
|
|
return state.minPasswordLength;
|
|
|
|
},
|
|
|
|
getUsersOffset(state) {
|
|
|
|
return state.usersOffset;
|
|
|
|
},
|
|
|
|
getUsersLimit(state) {
|
|
|
|
return state.usersLimit;
|
|
|
|
},
|
|
|
|
getUserCount(state) {
|
|
|
|
return state.userCount;
|
|
|
|
}
|
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
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
* @param {Object} options
|
|
|
|
* @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 }) {
|
|
|
|
search = typeof search === 'string' ? search : '';
|
|
|
|
group = typeof group === 'string' ? group : '';
|
|
|
|
if (group !== '') {
|
|
|
|
return api.get(OC.linkToOCS(`cloud/groups/${group}/users/details?offset=${offset}&limit=${limit}&search=${search}`, 2))
|
|
|
|
.then((response) => {
|
|
|
|
if (Object.keys(response.data.ocs.data.users).length > 0) {
|
|
|
|
context.commit('appendUsers', response.data.ocs.data.users);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
.catch((error) => context.commit('API_FAILURE', error));
|
|
|
|
}
|
2018-04-06 21:56:02 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
return api.get(OC.linkToOCS(`cloud/users/details?offset=${offset}&limit=${limit}&search=${search}`, 2))
|
|
|
|
.then((response) => {
|
|
|
|
if (Object.keys(response.data.ocs.data.users).length > 0) {
|
|
|
|
context.commit('appendUsers', response.data.ocs.data.users);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
.catch((error) => context.commit('API_FAILURE', error));
|
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-06-05 18:37:58 +03:00
|
|
|
getGroups(context, { offset, limit, search }) {
|
|
|
|
search = typeof search === 'string' ? search : '';
|
2018-07-04 13:05:30 +03:00
|
|
|
let limitParam = limit === -1 ? '' : `&limit=${limit}`;
|
|
|
|
return api.get(OC.linkToOCS(`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) {
|
|
|
|
context.commit('addGroup', {gid: group, displayName: group});
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
.catch((error) => context.commit('API_FAILURE', error));
|
|
|
|
},
|
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Get all users with full details
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
* @param {Object} options
|
|
|
|
* @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 }) {
|
|
|
|
search = typeof search === 'string' ? search : '';
|
|
|
|
return api.get(OC.linkToOCS(`cloud/users/details?offset=${offset}&limit=${limit}&search=${search}`, 2))
|
|
|
|
.then((response) => {
|
|
|
|
if (Object.keys(response.data.ocs.data.users).length > 0) {
|
|
|
|
context.commit('appendUsers', response.data.ocs.data.users);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
.catch((error) => context.commit('API_FAILURE', error));
|
|
|
|
},
|
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
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
* @param {Object} options
|
|
|
|
* @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 }) {
|
|
|
|
return api.get(OC.linkToOCS(`cloud/users/${groupid}/details?offset=${offset}&limit=${limit}`, 2))
|
|
|
|
.then((response) => context.commit('getUsersFromList', response.data.ocs.data.users))
|
|
|
|
.catch((error) => context.commit('API_FAILURE', error));
|
|
|
|
},
|
|
|
|
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
getPasswordPolicyMinLength(context) {
|
2019-05-13 22:29:40 +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
|
|
|
}
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
* @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) => {
|
|
|
|
return api.post(OC.linkToOCS(`cloud/groups`, 2), {groupid: gid})
|
2018-09-12 18:20:39 +03:00
|
|
|
.then((response) => {
|
|
|
|
context.commit('addGroup', {gid: gid, displayName: gid})
|
|
|
|
return {gid: gid, displayName: gid}
|
|
|
|
})
|
2018-04-09 12:03:21 +03:00
|
|
|
.catch((error) => {throw error;});
|
2018-05-24 16:45:57 +03:00
|
|
|
}).catch((error) => {
|
|
|
|
context.commit('API_FAILURE', { gid, error });
|
|
|
|
// let's throw one more time to prevent the view
|
|
|
|
// from adding the user to a group that doesn't exists
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
* @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) => {
|
2018-04-15 17:00:44 +03:00
|
|
|
return api.delete(OC.linkToOCS(`cloud/groups/${gid}`, 2))
|
2018-04-09 12:03:21 +03:00
|
|
|
.then((response) => context.commit('removeGroup', gid))
|
|
|
|
.catch((error) => {throw error;});
|
2018-04-15 17:00:44 +03:00
|
|
|
}).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
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
* @param {Object} options
|
|
|
|
* @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) => {
|
|
|
|
return api.post(OC.linkToOCS(`cloud/users/${userid}/groups`, 2), { groupid: gid })
|
|
|
|
.then((response) => context.commit('addUserGroup', { userid, gid }))
|
|
|
|
.catch((error) => {throw error;});
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }));
|
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Remove user from group
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
* @param {Object} options
|
|
|
|
* @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) => {
|
|
|
|
return api.delete(OC.linkToOCS(`cloud/users/${userid}/groups`, 2), { groupid: gid })
|
|
|
|
.then((response) => context.commit('removeUserGroup', { userid, gid }))
|
|
|
|
.catch((error) => {throw error;});
|
2018-05-24 16:45:57 +03:00
|
|
|
}).catch((error) => {
|
|
|
|
context.commit('API_FAILURE', { userid, error });
|
|
|
|
// let's throw one more time to prevent
|
|
|
|
// the view from removing the user row on failure
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
* @param {Object} options
|
|
|
|
* @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) => {
|
|
|
|
return api.post(OC.linkToOCS(`cloud/users/${userid}/subadmins`, 2), { groupid: gid })
|
|
|
|
.then((response) => context.commit('addUserSubAdmin', { userid, gid }))
|
|
|
|
.catch((error) => {throw error;});
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }));
|
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Remove user from group admin
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
* @param {Object} options
|
|
|
|
* @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) => {
|
|
|
|
return api.delete(OC.linkToOCS(`cloud/users/${userid}/subadmins`, 2), { groupid: gid })
|
|
|
|
.then((response) => context.commit('removeUserSubAdmin', { userid, gid }))
|
|
|
|
.catch((error) => {throw error;});
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }));
|
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Delete a user
|
|
|
|
*
|
|
|
|
* @param {Object} 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) => {
|
|
|
|
return api.delete(OC.linkToOCS(`cloud/users/${userid}`, 2))
|
|
|
|
.then((response) => context.commit('deleteUser', userid))
|
|
|
|
.catch((error) => {throw error;});
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }));
|
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Add a user
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
* @param {Object} options
|
|
|
|
* @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
|
|
|
*/
|
2018-04-19 01:20:14 +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) => {
|
2018-04-19 01:20:14 +03:00
|
|
|
return api.post(OC.linkToOCS(`cloud/users`, 2), { userid, password, displayName, email, groups, subadmin, quota, language })
|
2019-06-14 18:28:50 +03:00
|
|
|
.then((response) => dispatch('addUserData', userid || response.data.ocs.data.UserID))
|
2018-04-09 12:03:21 +03:00
|
|
|
.catch((error) => {throw error;});
|
2018-11-22 13:29:20 +03:00
|
|
|
}).catch((error) => {
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param {Object} 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) => {
|
|
|
|
return api.get(OC.linkToOCS(`cloud/users/${userid}`, 2))
|
|
|
|
.then((response) => context.commit('addUserData', response))
|
|
|
|
.catch((error) => {throw error;});
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }));
|
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/** Enable or disable user
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
* @param {Object} options
|
|
|
|
* @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 }) {
|
|
|
|
let userStatus = enabled ? 'enable' : 'disable';
|
|
|
|
return api.requireAdmin().then((response) => {
|
|
|
|
return api.put(OC.linkToOCS(`cloud/users/${userid}/${userStatus}`, 2))
|
|
|
|
.then((response) => context.commit('enableDisableUser', { userid, enabled }))
|
|
|
|
.catch((error) => {throw error;});
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }));
|
|
|
|
},
|
2018-03-09 19:46:34 +03:00
|
|
|
|
2018-04-09 12:03:21 +03:00
|
|
|
/**
|
|
|
|
* Edit user data
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
* @param {Object} options
|
|
|
|
* @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 }) {
|
|
|
|
let allowedEmpty = ['email', 'displayname'];
|
|
|
|
if (['email', 'language', 'quota', 'displayname', 'password'].indexOf(key) !== -1) {
|
|
|
|
// We allow empty email or displayname
|
|
|
|
if (typeof value === 'string' &&
|
|
|
|
(
|
|
|
|
(allowedEmpty.indexOf(key) === -1 && value.length > 0) ||
|
|
|
|
allowedEmpty.indexOf(key) !== -1
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return api.requireAdmin().then((response) => {
|
|
|
|
return api.put(OC.linkToOCS(`cloud/users/${userid}`, 2), { key: key, value: value })
|
|
|
|
.then((response) => context.commit('setUserData', { userid, key, value }))
|
|
|
|
.catch((error) => {throw error;});
|
|
|
|
}).catch((error) => context.commit('API_FAILURE', { userid, error }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.reject(new Error('Invalid request data'));
|
2018-06-22 18:05:33 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send welcome mail
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
* @param {string} userid User id
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
sendWelcomeMail(context, userid) {
|
|
|
|
return api.requireAdmin().then((response) => {
|
|
|
|
return api.post(OC.linkToOCS(`cloud/users/${userid}/welcome`, 2))
|
|
|
|
.then(response => true)
|
|
|
|
.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-05-22 09:58:35 +03:00
|
|
|
export default { state, mutations, getters, actions };
|