Users.js
2.74 KB
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
'use strict';
const configurationApi = require('./http/endpoints/configuration')
, ApiDefinition = require('./http/ApiDefinition.js')
, util = require('../util')
;
module.exports = class Users extends ApiDefinition {
constructor(hueApi) {
super(hueApi);
}
getAll() {
return getAllUsersAsArray(this);
}
getUser(username) {
return getAllUsers(this)
.then(users => {
let result = null;
if (users[username]) {
result = Object.assign({username: username}, users[username]);
}
return result;
});
}
/**
* @deprecated Use getUserByName(username) instead
* @param username {string}
*/
get(username) {
util.deprecatedFunction('5.x', 'users.get(username)', 'Use users.getUser(username) instead.');
return this.getUser(username);
}
getUserByName(appName, deviceName) {
let nameToMatch;
if (arguments.length === 0) {
return Promise.resolve(null);
} else if (arguments.length === 1) {
nameToMatch = appName;
} else {
nameToMatch = `${appName}#${deviceName}`;
}
return getAllUsersAsArray(this)
.then(users => {
return users.filter(user => user.name === nameToMatch);
});
}
/**
* @deprecated use getUserByName(appName, deviceName) instead.
* @param appName {string}
* @param deviceName {string}
* @returns {Promise<Object[]>}
*/
getByName(appName, deviceName) {
util.deprecatedFunction('5.x', 'users.getByName(appName, deviceName)', 'Use users.getUserByName(appName, deviceName) instead.');
return this.getUserByName(appName, deviceName);
}
/**
* @param appName {string}
* @param deviceName {string}
* @returns {Promise<any>>}
*/
createUser(appName, deviceName) {
return this.hueApi.getCachedState()
.then(state => {
//TODO may need to combine the modelid and API version, but am assuming that all newer bridges are kept up to
// date, as do not know the specific version number of the introduction of the generateclientkey parameter.
const oldBridge = state.modelid === 'BSB001';
return this.execute(configurationApi.createUser, {appName: appName, deviceName: deviceName, generateKey: !oldBridge});
});
}
deleteUser(username) {
return this.execute(configurationApi.deleteUser, {element: username});
}
};
function getAllUsers(api) {
return api.execute(configurationApi.getConfiguration)
.then(config => {
return config.whitelist || null;
});
}
function getAllUsersAsArray(api) {
return getAllUsers(api)
.then(users => {
const results = [];
Object.keys(users).forEach(username => {
results.push(Object.assign({username: username}, users[username]));
});
return results;
});
}