-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
119 lines (105 loc) · 3.46 KB
/
utils.js
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
const axios = require("axios");
const USER_DATA_KEYS = exports.USER_DATA_KEYS = Object.freeze([
"email",
"quota",
"displayname",
"phone",
"address",
"website",
"twitter",
]);
/**
* Checks an array of users and logs errors to the console if something is wrong.
* @param users {Array} array of user objects
* @return {boolean} true if every user is valid, false if not
*/
exports.checkUsers = function(users) {
let allValid = true;
// check if the given users are valid
for (const user of users) {
if (!user.userid || user.userid.length <= 3) {
allValid = false;
console.error(`Invalid userid: ${user.userid}`);
}
if (!user.password || user.password.length < 10) {
allValid = false;
console.error(`Missing or too short (< 10 characters) password for user ${user.userid}: ${user.password}`);
}
// check the defined groups
if (user.groups) {
if (!Array.isArray(user.groups)) {
allValid = false;
console.error(`Groups for ${user.userid} must be an array.`);
} else if (user.groups.length === 0) {
allValid = false;
console.error(`Groups for ${user.userid} cannot be empty.`);
} else if (user.groups.some(group => group === "")) {
allValid = false;
console.error(`Invalid empty group name in user ${user.userid}.`);
}
}
// basic validation of the user object
for (const key of Object.keys(user)) {
// skip already checked keys
if (key === "userid" || key === "password" || key === "groups") {
continue;
}
// check if no additional keys are present
if (USER_DATA_KEYS.indexOf(key) < 0) {
allValid = false;
console.error(`Invalid data key for user ${user.userid}: ${key}`);
}
// check if the quota string is valid
if (key === "quota" && /^([1-9]\d* ?(MB|GB|TB))?$/.test(user.quota) !== true) {
allValid = false;
console.error(`Invalid quota value for user ${user.userid}: ${user.quota}`);
}
}
}
return allValid;
};
/**
* Creates a new axios instance suitable for Nextcloud API requests.
* @return {Object} an axios instance
*/
exports.getNextcloudClient = function () {
return axios.create({
headers: {
"OCS-APIRequest": "true",
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
}
});
};
/**
* Returns the status code of the API response.
*
* @param responseObj {Object}
* @return {Number} integer status code
*/
exports.getResponseStatusCode = function (responseObj) {
if (
responseObj.hasOwnProperty("ocs") &&
responseObj.ocs.hasOwnProperty("meta") &&
responseObj.ocs.meta.hasOwnProperty("statuscode")
) {
return responseObj.ocs.meta.statuscode;
}
return -1;
};
/**
* Returns the message of the API response.
*
* @param responseObj {Object}
* @return {String} message string
*/
exports.getResponseMessage = function (responseObj) {
if (
responseObj.hasOwnProperty("ocs") &&
responseObj.ocs.hasOwnProperty("meta") &&
responseObj.ocs.meta.hasOwnProperty("message")
) {
return responseObj.ocs.meta.message;
}
return "";
};