-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
44 lines (39 loc) · 1.39 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
var exceptions = require('./exceptions');
const config = require('./config');
module.exports = {
isArray: function(data) {
return data.constructor === Array;
},
isAuth: function(client) {
if (!client)
throw new exceptions.InvalidArgumentException('Client cannot be null !');
if (!this.hasProperties(client, ['username', 'password', 'room'], false))
throw new exceptions.InvalidArgumentException('Client\'s properties missing !');
return client.username && client.password && client.room && client.token && client.chatToken && client.displayedName && client.uid != -1 && client.rank != config.RIGHTS.UNKNOW;
},
AuthentificationRequired: function (client) {
client.emit('cerror', {'code': 401, 'message': 'Authentification required'});
},
hasProperties: function(toCheck, requiredProperties, isStrict = false) {
if (!this.isArray(requiredProperties)) {
throw new exceptions.InvalidSyntaxException('requiredProperties must be an array of string !');
}
for (var i in requiredProperties) {
if (!toCheck.hasOwnProperty(requiredProperties[i])) {
console.log('missing property: ' + requiredProperties[i]);
return false;
}
}
if (isStrict) {
var propertyCount = 0;
for (var property in toCheck) {
if (toCheck.hasOwnProperty(property)) {
propertyCount++;
}
}
if (requiredProperties.length != propertyCount)
return false;
}
return true;
}
}