summaryrefslogtreecommitdiffstats
path: root/settings/src/store/users.js
blob: cf418f81ca3f827fa16b12ebc682e8703e8a80cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import api from './api';

const orderGroups = function(groups, orderBy) {
	/* const SORT_USERCOUNT = 1;
	 * const SORT_GROUPNAME = 2;
	 * https://github.com/nextcloud/server/blob/208e38e84e1a07a49699aa90dc5b7272d24489f0/lib/private/Group/MetaData.php#L34
	 */
	if (orderBy === 1) {
		return groups.sort((a, b) => a.usercount < b.usercount);
	} else {
		return groups.sort((a, b) => a.name.localeCompare(b.name));
	}
}

const state = {
	users: [],
	groups: [],
	orderBy: 1,
	minPasswordLength: 0,
	usersOffset: 0,
	usersLimit: 25,
	userCount: 0
};

const mutations = {
	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}) {
		state.groups = groups;
		state.orderBy = orderBy;
		state.userCount = userCount;
		state.groups = orderGroups(state.groups, state.orderBy);
	},
	addGroup(state, groupid) {
		try {
			state.groups.push({
				id: groupid,
				name: groupid,
				usercount: 0 // user will be added after the creation
			});
			state.groups = orderGroups(state.groups, state.orderBy);
		} catch (e) {
			console.log('Can\'t create group', e);
		}
	},
	addUserGroup(state, { userid, gid }) {
		// this should not be needed as it would means the user contains a group
		// the server database doesn't have.
		let group = state.groups.find(groupSearch => groupSearch.id == gid);
		if (group) {
			group.usercount++; // increase count
		}
		let groups = state.users.find(user => user.id == userid).groups;
		groups.push(gid);
		state.groups = orderGroups(state.groups, state.orderBy);
	},
	removeUserGroup(state, { userid, gid }) {
		// this should not be needed as it would means the user contains a group
		// the server database doesn't have.
		let group = state.groups.find(groupSearch => groupSearch.id == gid);
		if (group) {
			group.usercount--; // lower count
		}
		let groups = state.users.find(user => user.id == userid).groups;
		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 }) {
		state.users.find(user => user.id == userid).enabled = enabled;
		// increment or not
		state.groups.find(group => group.id == '_disabled').usercount += enabled ? -1 : 1;
		state.userCount += enabled ? 1 : -1;
		console.log(enabled);
	},
	setUserData(state, { userid, key, value }) {
		if (key === 'quota') {
			let humanValue = OC.Util.computerFileSize(value);
			state.users.find(user => user.id == userid)[key][key] = humanValue?humanValue:value;
		} else {
			state.users.find(user => user.id == userid)[key] = value;
		}
	},

	/**
	 * Reset users list
	 */
	resetUsers(state) {
		state.users = [];
		state.usersOffset = 0;
	}
};

const getters = {
	getUsers(state) {
		return state.users;
	},
	getGroups(state) {
		return state.groups;
	},
	getPasswordPolicyMinLength(state) {
		return state.minPasswordLength;
	},
	getUsersOffset(state) {
		return state.usersOffset;
	},
	getUsersLimit(state) {
		return state.usersLimit;
	},
	getUserCount(state) {
		return state.userCount;
	}
};

const actions = {

	/**
	 * 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
	 * @returns {Promise}
	 */
	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));
		}

		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));
	},

	/**
	 * 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
	 * @returns {Promise}
	 */
	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));
	},

	/**
	 * 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
	 * @returns {Promise}
	 */
	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));
	},
	

	getPasswordPolicyMinLength(context) {
		return api.get(OC.linkToOCS('apps/provisioning_api/api/v1/config/apps/password_policy/minLength', 2))
			.then((response) => context.commit('setPasswordPolicyMinLength', response.data.ocs.data.data))
			.catch((error) => context.commit('API_FAILURE', error));
	},

	/**
	 * Add group
	 * 
	 * @param {Object} context
	 * @param {string} gid Group id
	 * @returns {Promise}
	 */
	addGroup(context, gid) {
		return api.requireAdmin().then((response) => {
			return api.post(OC.linkToOCS(`cloud/groups`, 2), {groupid: gid})
				.then((response) => context.commit('addGroup', gid))
				.catch((error) => {throw error;});
		}).catch((error) => context.commit('API_FAILURE', { userid, error }));
	},

	/**
	 * Remove group
	 * 
	 * @param {Object} context
	 * @param {string} gid Group id
	 * @returns {Promise}
	 */
	removeGroup(context, gid) {
		return api.requireAdmin().then((response) => {
			return api.post(OC.linkToOCS(`cloud/groups`, 2), {groupid: gid})
				.then((response) => context.commit('removeGroup', gid))
				.catch((error) => {throw error;});
		}).catch((error) => context.commit('API_FAILURE', { userid, error }));
	},

	/**
	 * Add user to group
	 * 
	 * @param {Object} context
	 * @param {Object} options
	 * @param {string} options.userid User id
	 * @param {string} options.gid Group id
	 * @returns {Promise}
	 */
	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 }));
	},

	/**
	 * Remove user from group
	 * 
	 * @param {Object} context
	 * @param {Object} options
	 * @param {string} options.userid User id
	 * @param {string} options.gid Group id
	 * @returns {Promise}
	 */
	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;});
		}).catch((error) => context.commit('API_FAILURE', { userid, error }));
	},

	/**
	 * Add user to group admin
	 * 
	 * @param {Object} context
	 * @param {Object} options
	 * @param {string} options.userid User id
	 * @param {string} options.gid Group id
	 * @returns {Promise}
	 */
	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 }));
	},

	/**
	 * Remove user from group admin
	 * 
	 * @param {Object} context
	 * @param {Object} options
	 * @param {string} options.userid User id
	 * @param {string} options.gid Group id
	 * @returns {Promise}
	 */
	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 }));
	},

	/**
	 * Delete a user
	 * 
	 * @param {Object} context
	 * @param {string} userid User id 
	 * @returns {Promise}
	 */
	deleteUser(context, { userid }) {
		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 }));
	},

	/**
	 * Add a user
	 * 
	 * @param {Object} context
	 * @param {Object} options
	 * @param {string} options.userid User id
	 * @param {string} options.password User password 
	 * @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
	 * @returns {Promise}
	 */
	addUser({context, dispatch}, { userid, password, email, groups, subadmin, quota, language }) {
		return api.requireAdmin().then((response) => {
			return api.post(OC.linkToOCS(`cloud/users`, 2), { userid, password, email, groups, subadmin, quota, language })
				.then((response) => dispatch('addUserData', userid))
				.catch((error) => {throw error;});
		}).catch((error) => context.commit('API_FAILURE', { userid, error }));
	},

	/**
	 * Get user data and commit addition
	 * 
	 * @param {Object} context
	 * @param {string} userid User id 
	 * @returns {Promise}
	 */
	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 }));
	},

	/** Enable or disable user 
	 * 
	 * @param {Object} context
	 * @param {Object} options
	 * @param {string} options.userid User id
	 * @param {boolean} options.enabled User enablement status
	 * @returns {Promise}
	 */
	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 }));
	},

	/**
	 * 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
	 * @returns {Promise}
	 */
	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'));
	}
};

export default { state, mutations, getters, actions };