@@ -167,6 +167,25 @@ const (
167
167
BastionSchemeTelnet BastionScheme = "telnet"
168
168
)
169
169
170
+ // Generic Helper
171
+ func GenericNameOrID (db * gorm.DB , identifiers []string ) * gorm.DB {
172
+ var ids []string ;
173
+ var names []string ;
174
+ for _ , s := range identifiers {
175
+ if _ , err := strconv .Atoi (s ); err == nil {
176
+ ids = append (ids , s )
177
+ } else {
178
+ names = append (names , s )
179
+ }
180
+ }
181
+ if len (ids ) > 0 && len (names ) > 0 {
182
+ return db .Where ("id IN (?)" , ids ).Or ("name IN (?)" , names )
183
+ } else if len (ids ) > 0 {
184
+ return db .Where ("id IN (?)" , ids )
185
+ }
186
+ return db .Where ("name IN (?)" , names )
187
+ }
188
+
170
189
// Host helpers
171
190
172
191
func (host * Host ) DialAddr () string {
@@ -268,7 +287,7 @@ func HostsPreload(db *gorm.DB) *gorm.DB {
268
287
return db .Preload ("Groups" ).Preload ("SSHKey" )
269
288
}
270
289
func HostsByIdentifiers (db * gorm.DB , identifiers []string ) * gorm.DB {
271
- return db . Where ( "id IN (?)" , identifiers ). Or ( "name IN (?)" , identifiers )
290
+ return GenericNameOrID ( db , identifiers )
272
291
}
273
292
func HostByName (db * gorm.DB , name string ) (* Host , error ) {
274
293
var host Host
@@ -308,7 +327,7 @@ func SSHKeysPreload(db *gorm.DB) *gorm.DB {
308
327
return db .Preload ("Hosts" )
309
328
}
310
329
func SSHKeysByIdentifiers (db * gorm.DB , identifiers []string ) * gorm.DB {
311
- return db . Where ( "id IN (?)" , identifiers ). Or ( "name IN (?)" , identifiers )
330
+ return GenericNameOrID ( db , identifiers )
312
331
}
313
332
314
333
// HostGroup helpers
@@ -317,7 +336,7 @@ func HostGroupsPreload(db *gorm.DB) *gorm.DB {
317
336
return db .Preload ("ACLs" ).Preload ("Hosts" )
318
337
}
319
338
func HostGroupsByIdentifiers (db * gorm.DB , identifiers []string ) * gorm.DB {
320
- return db . Where ( "id IN (?)" , identifiers ). Or ( "name IN (?)" , identifiers )
339
+ return GenericNameOrID ( db , identifiers )
321
340
}
322
341
323
342
// UserGroup helpers
@@ -326,7 +345,7 @@ func UserGroupsPreload(db *gorm.DB) *gorm.DB {
326
345
return db .Preload ("ACLs" ).Preload ("Users" )
327
346
}
328
347
func UserGroupsByIdentifiers (db * gorm.DB , identifiers []string ) * gorm.DB {
329
- return db . Where ( "id IN (?)" , identifiers ). Or ( "name IN (?)" , identifiers )
348
+ return GenericNameOrID ( db , identifiers )
330
349
}
331
350
332
351
// User helpers
@@ -335,7 +354,21 @@ func UsersPreload(db *gorm.DB) *gorm.DB {
335
354
return db .Preload ("Groups" ).Preload ("Keys" ).Preload ("Roles" )
336
355
}
337
356
func UsersByIdentifiers (db * gorm.DB , identifiers []string ) * gorm.DB {
338
- return db .Where ("id IN (?)" , identifiers ).Or ("email IN (?)" , identifiers ).Or ("name IN (?)" , identifiers )
357
+ var ids []string ;
358
+ var names []string ;
359
+ for _ , s := range identifiers {
360
+ if _ , err := strconv .Atoi (s ); err == nil {
361
+ ids = append (ids , s )
362
+ } else {
363
+ names = append (names , s )
364
+ }
365
+ }
366
+ if len (ids ) > 0 && len (names ) > 0 {
367
+ db .Where ("id IN (?)" , identifiers ).Or ("email IN (?)" , identifiers ).Or ("name IN (?)" , identifiers )
368
+ } else if len (ids ) > 0 {
369
+ return db .Where ("id IN (?)" , ids )
370
+ }
371
+ return db .Where ("email IN (?)" , identifiers ).Or ("name IN (?)" , identifiers )
339
372
}
340
373
func (u * User ) HasRole (name string ) bool {
341
374
for _ , role := range u .Roles {
@@ -381,7 +414,7 @@ func UserKeysByUserID(db *gorm.DB, identifiers []string) *gorm.DB {
381
414
// return db.Preload("Users")
382
415
//}
383
416
func UserRolesByIdentifiers (db * gorm.DB , identifiers []string ) * gorm.DB {
384
- return db . Where ( "id IN (?)" , identifiers ). Or ( "name IN (?)" , identifiers )
417
+ return GenericNameOrID ( db , identifiers )
385
418
}
386
419
387
420
// Session helpers
0 commit comments