@@ -17,8 +17,6 @@ function initialize() {
17
17
instances = { } ;
18
18
} else {
19
19
instances = JSON . parse ( fs . readFileSync ( instancesFile , 'utf8' ) ) ;
20
- //require() does caching?
21
- //instances = require(instancesFile);
22
20
}
23
21
}
24
22
@@ -33,6 +31,19 @@ function persist() {
33
31
module . exports = function ( ) {
34
32
initialize ( ) ;
35
33
34
+ /**
35
+ * Checks if the user in the given instance is the same as the user who logged in
36
+ **/
37
+ function isInstanceUser ( instance , userid , username ) {
38
+ // TODO pixtron - deprecated check for username can be removed in version 2.0.0 (see #230)
39
+ if ( instance . userid ) {
40
+ return instance . userid === userid ;
41
+ } else {
42
+ logger . warning ( 'Identifying instance with username' , { category : 'instance' , instance} ) ;
43
+ return instance . username === username ;
44
+ }
45
+ }
46
+
36
47
function archiveDataDir ( clientConfig ) {
37
48
return new Promise ( function ( resolve , reject ) {
38
49
var instance = instances . lastActive ;
@@ -132,9 +143,12 @@ module.exports = function() {
132
143
}
133
144
}
134
145
135
- function loadInstance ( name , clientConfig ) {
146
+ function loadInstance ( name , userid , clientConfig ) {
147
+ // TODO pixtron - deprecated userid can be removed again in version 2.0.0 (see #230)
148
+ // Currently it is only here to migrate the instances
136
149
var switchInstance = function ( ) {
137
150
instances . active = name ;
151
+ instances . instances [ name ] . userid = userid ;
138
152
instances . instances [ name ] . balloonDir = undefined ;
139
153
instances . instances [ name ] . balloonDirIno = undefined ;
140
154
persist ( ) ;
@@ -163,14 +177,13 @@ module.exports = function() {
163
177
} ) ;
164
178
}
165
179
166
- function setNewInstance ( username , server , context , clientConfig ) {
180
+ function setNewInstance ( username , userid , server , context , clientConfig ) {
167
181
if ( ! instances . instances ) {
168
182
instances . instances = { } ;
169
183
}
170
184
171
185
var name = getNewInstanceName ( ) ;
172
-
173
- instances . instances [ name ] = { username, server, context } ;
186
+ instances . instances [ name ] = { username, userid, server, context } ;
174
187
instances . active = name ;
175
188
persist ( ) ;
176
189
clientConfig . initialize ( ) ;
@@ -193,13 +206,14 @@ module.exports = function() {
193
206
194
207
return instances . instances [ name ] ;
195
208
} ,
196
- getInstance : function ( username , url , context ) {
209
+ // TODO pixtron - deprecated username can be removed in version 2.0.0 (see #230)
210
+ getInstance : function ( username , url , context , userid ) {
197
211
if ( instances . instances ) {
198
212
for ( instance in instances . instances ) {
199
213
if (
200
214
instances . instances [ instance ] . server === url
201
215
&&
202
- instances . instances [ instance ] . username === username
216
+ isInstanceUser ( instances . instances [ instance ] , userid , username )
203
217
&&
204
218
instances . instances [ instance ] . context === context
205
219
) {
@@ -210,34 +224,34 @@ module.exports = function() {
210
224
211
225
return null ;
212
226
} ,
213
- link : function ( username , url , context , clientConfig ) {
227
+ link : function ( username , userid , url , context , clientConfig ) {
214
228
return new Promise ( ( resolve , reject ) => {
215
229
var activeInstance = this . getActiveInstance ( ) ;
216
230
217
231
if ( activeInstance ) {
218
232
var activeInstanceCfg = instances [ activeInstance ] ;
219
- if ( activeInstanceCfg && activeInstanceCfg . username === username ) {
233
+ if ( activeInstanceCfg && isInstanceUser ( activeInstanceCfg , userid , username ) ) {
220
234
return resolve ( false ) ;
221
235
} else {
222
- // if username changed, we need to unlink the active instance
236
+ // if user changed, we need to unlink the active instance
223
237
this . unlink ( clientConfig ) ;
224
238
}
225
239
}
226
240
227
- var instanceName = this . getInstance ( username , url , context ) ;
241
+ var instanceName = this . getInstance ( username , url , context , userid ) ;
228
242
229
243
if ( instanceName === this . getLastActiveInstance ( ) ) {
230
- loadInstance ( instanceName , clientConfig ) . then ( ( ) => {
244
+ loadInstance ( instanceName , userid , clientConfig ) . then ( ( ) => {
231
245
resolve ( false ) ;
232
246
} ) . catch ( reject ) ;
233
247
} else {
234
248
archiveDataDir ( clientConfig ) . then ( ( ) => {
235
249
if ( instanceName === null ) {
236
- setNewInstance ( username , url , context , clientConfig ) . then ( ( ) => {
250
+ setNewInstance ( username , userid , url , context , clientConfig ) . then ( ( ) => {
237
251
resolve ( true ) ;
238
252
} ) . catch ( reject ) ;
239
253
} else {
240
- loadInstance ( instanceName , clientConfig ) . then ( ( ) => {
254
+ loadInstance ( instanceName , userid , clientConfig ) . then ( ( ) => {
241
255
resolve ( false ) ;
242
256
} ) . catch ( reject ) ;
243
257
}
0 commit comments