forked from garsh0p/garpr
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdao.py
741 lines (579 loc) · 28.5 KB
/
dao.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
from datetime import timedelta
import base64
import hashlib
import os
import pymongo
import re
from itertools import groupby
from config.config import Config
import model as M
config = Config()
ITERATION_COUNT = 100000
DATABASE_NAME = config.get_db_name()
special_chars = re.compile("[^\w\s]*")
# make sure all the exceptions here are properly caught, or the server code
# knows about them.
class InvalidRegionsException(Exception):
# safe, only used from script
pass
class DuplicateAliasException(Exception):
# safe, only used in dead code
pass
class DuplicateUsernameException(Exception):
# safe, only used from script
pass
class DuplicateRegionException(Exception):
pass
class InvalidNameException(Exception):
# safe only used in dead code
pass
def gen_password(password):
# more bytes of randomness? i think 16 bytes is sufficient for a salt
salt = base64.b64encode(os.urandom(16))
hashed_password = base64.b64encode(hashlib.pbkdf2_hmac(
'sha256', password, salt, ITERATION_COUNT))
return salt, hashed_password
def verify_password(password, salt, hashed_password):
the_hash = base64.b64encode(hashlib.pbkdf2_hmac(
'sha256', password, salt, ITERATION_COUNT))
return (the_hash and the_hash == hashed_password)
# TODO create RegionSpecificDao object rn we pass in norcal for a buncha
# things we dont need to
class Dao(object):
# here lies some serious abuse of magic methods, here be dragons
# use __new__ so that we can return None
def __new__(cls, region_id, mongo_client, database_name=DATABASE_NAME):
all_region_ids = [r.id for r in Dao.get_all_regions(
mongo_client, database_name=database_name)]
if region_id and region_id not in all_region_ids:
return None
# this is how we call __init__
return super(Dao, cls).__new__(cls, region_id, mongo_client, database_name)
def __init__(self, region_id, mongo_client, database_name=DATABASE_NAME):
self.players_col = mongo_client[database_name][M.Player.collection_name]
self.tournaments_col = mongo_client[
database_name][M.Tournament.collection_name]
self.rankings_col = mongo_client[
database_name][M.Ranking.collection_name]
self.users_col = mongo_client[database_name][M.User.collection_name]
self.pending_tournaments_col = mongo_client[
database_name][M.PendingTournament.collection_name]
self.merges_col = mongo_client[database_name][M.Merge.collection_name]
self.sessions_col = mongo_client[database_name][M.Session.collection_name]
self.raw_files_col = mongo_client[database_name][M.RawFile.collection_name]
self.regions_col = mongo_client[database_name][M.Region.collection_name]
self.mongo_client = mongo_client
self.region_id = region_id
@classmethod
def insert_region(cls, region, mongo_client, database_name=DATABASE_NAME):
return mongo_client[database_name][M.Region.collection_name].insert(region.dump(context='db'))
# sorted by display name
@classmethod
def get_all_regions(cls, mongo_client, database_name=DATABASE_NAME):
regions = [M.Region.load(r, context='db') for r in mongo_client[
database_name][M.Region.collection_name].find()]
return sorted(regions, key=lambda r: r.display_name)
def get_player_by_id(self, id):
'''id must be an ObjectId'''
return M.Player.load(self.players_col.find_one({'_id': id}), context='db')
def get_player_by_alias(self, alias):
'''Converts alias to lowercase'''
return M.Player.load(self.players_col.find_one({
'aliases': {'$in': [alias.lower()]},
'regions': {'$in': [self.region_id]},
'merged': False
}), context='db')
def get_players_by_alias_from_all_regions(self, alias):
'''Converts alias to lowercase'''
return [M.Player.load(p, context='db') for p in self.players_col.find({
'aliases': {'$in': [alias.lower()]},
'merged': False
})]
def get_player_id_map_from_player_aliases(self, aliases):
'''Given a list of player aliases, returns a list of player aliases/id pairs for the current
region. If no player can be found, the player id field will be set to None.'''
player_alias_to_player_id_map = []
for alias in aliases:
id = None
player = self.get_player_by_alias(alias)
if player is not None:
id = player.id
player_alias_to_player_id_map.append(M.AliasMapping(
player_alias=alias,
player_id=id
))
return player_alias_to_player_id_map
def get_all_players(self, all_regions=False, include_merged=False):
'''Sorts by name in lexographical order.'''
mongo_request = {}
if not all_regions:
mongo_request['regions'] = {'$in': [self.region_id]}
if not include_merged:
mongo_request['merged'] = False
return [M.Player.load(p, context='db')
for p in self.players_col.find(mongo_request).sort([('name', 1)])]
def insert_player(self, player):
return self.players_col.insert(player.dump(context='db'))
def delete_player(self, player):
return self.players_col.remove({'_id': player.id})
def update_player(self, player):
return self.players_col.update({'_id': player.id}, player.dump(context='db'))
def update_region(self, region):
return self.regions_col.update({'_id': region.id}, region.dump(context='db'))
# TODO bulk update
def update_players(self, players):
pass
# unused, if you use this, make sure to surround it in a try block!
def add_alias_to_player(self, player, alias):
lowercase_alias = alias.lower()
if lowercase_alias in player.aliases:
raise DuplicateAliasException(
'%s is already an alias for %s!' % (alias, player.name))
player.aliases.append(lowercase_alias)
return self.update_player(player)
# unused, if you use this, make sure to surround it in a try block!
def update_player_name(self, player, name):
# ensure this name is already an alias
if not name.lower() in player.aliases:
raise InvalidNameException(
'Player %s does not have %s as an alias already, cannot change name.'
% (player, name))
player.name = name
return self.update_player(player)
def get_all_player_tournaments_by_id(self, id):
result = self.players_col.find({"_id": id})
if result.count() == 0:
return None
tournaments = \
[ M.Tournament.load(t, context='db') for t in self.tournaments_col.find({'players': {'$in': [id] }}) ]
return tournaments
def sort_player_tournaments_by_region(self, id):
result = self.players_col.find({"_id": id})
if result.count() == 0:
return None
tournaments = self.get_all_player_tournaments_by_id(id)
region_count = {}
for tournament in tournaments:
if not tournament.regions[0]: pass
region = tournament.regions[0]
if region_count.get(region, None) is None:
region_count[region] = 0
region_count[region] = region_count[region] + 1
counts = []
for region, count in region_count.iteritems():
r = {
'name': region,
'count': count
}
counts.append(r)
counts = sorted(counts, key=lambda x: x['count'], reverse=True)
return counts
def insert_pending_tournament(self, pending_tournament):
return self.pending_tournaments_col.insert(pending_tournament.dump(context='db'))
def update_pending_tournament(self, tournament):
return self.pending_tournaments_col.update({'_id': tournament.id}, tournament.dump(context='db'))
def delete_pending_tournament(self, pending_tournament):
return self.pending_tournaments_col.remove({'_id': pending_tournament.id})
def get_all_pending_tournament_jsons(self, regions=None):
query_dict = {'regions': {'$in': regions}} if regions else {}
return self.pending_tournaments_col.find(query_dict).sort([('date', 1)])
def get_all_pending_tournaments(self, regions=None):
'''players is a list of Players'''
query_dict = {}
query_list = []
if regions:
query_list.append({'regions': {'$in': regions}})
if query_list:
query_dict['$and'] = query_list
pending_tournaments = [t for t in self.pending_tournaments_col.find(
query_dict).sort([('date', 1)])]
return [M.PendingTournament.load(t, context='db') for t in pending_tournaments]
def get_pending_tournament_by_id(self, id):
'''id must be an ObjectId'''
return M.PendingTournament.load(self.pending_tournaments_col.find_one({'_id': id}),
context='db')
def insert_tournament(self, tournament):
return self.tournaments_col.insert(tournament.dump(context='db'))
# all uses of this MUST use a try/except block!
def update_tournament(self, tournament):
return self.tournaments_col.update({'_id': tournament.id}, tournament.dump(context='db'))
def delete_tournament(self, tournament):
return self.tournaments_col.remove({'_id': tournament.id})
def get_all_tournament_ids(self, players=None, regions=None):
'''players is a list of Players'''
query_dict = {}
query_list = []
if players:
for player in players:
query_list.append({'players': {'$in': [player.id]}})
if regions:
query_list.append({'regions': {'$in': regions}})
if query_list:
query_dict['$and'] = query_list
return [t['_id'] for t in self.tournaments_col.find(query_dict, {'_id': 1}).sort([('date', 1)])]
def get_all_tournaments(self, players=None, regions=None):
'''players is a list of Players'''
query_dict = {}
query_list = []
if players:
for player in players:
query_list.append({'players': {'$in': [player.id]}})
if regions:
query_list.append({'regions': {'$in': regions}})
if query_list:
query_dict['$and'] = query_list
tournaments = [t for t in self.tournaments_col.find(
query_dict).sort([('date', 1)])]
return [M.Tournament.load(t, context='db') for t in tournaments]
def get_tournament_by_id(self, id):
'''id must be an ObjectId'''
return M.Tournament.load(self.tournaments_col.find_one({'_id': id}), context='db')
def get_match_by_tournament_id_and_match_id(self, tournament_id, match_id):
tourney_m = M.Tournament.load(self.tournaments_col.find_one({'_id': tournament_id}, {'matches', 1}), context='db')
for match in tourney_m.matches:
try:
if match_id == match.match_id:
return match
except Exception as e:
print('Could not attain match. ' + str(e))
def set_tournament_exclusion_by_tournament_id(self, tournament_id, excluded):
if self.tournaments_col.find_one({'_id': tournament_id}):
self.tournaments_col.update({'_id': tournament_id},
{'$set':
{
'excluded': excluded
}
})
def set_match_exclusion_by_tournament_id_and_match_id(self, tournament_id, match_id, excluded):
# TODO ENHANCE THIS ALGORITHM TO ONLY UPDATE MATCH
match_updated = False
new_matches = []
tourney_m = \
M.Tournament.load(self.tournaments_col.find_one({'_id': tournament_id}), context='db')
for match in tourney_m.matches:
try:
if match_id == match.match_id:
match.excluded = excluded
match_updated = True
new_matches.append(match)
except Exception as e:
print('Could not attain match. ' + str(e))
if match_updated is True:
tourney_m.matches = new_matches
self.tournaments_col.update({'_id': tournament_id}, tourney_m.dump(context='db'))
def add_match_by_tournament_id(self, tournament_id, winner_id, loser_id):
match_updates = False
tourney_m = \
M.Tournament.load(self.tournaments_col.find_one({'_id':tournament_id}), context='db')
new_match_id = len(tourney_m.matches)
new_match = M.Match(match_id=new_match_id, winner=winner_id, loser=loser_id, excluded=False)
tourney_m.matches.append(new_match)
if winner_id not in tourney_m.players:
tourney_m.players.append(winner_id)
if loser_id not in tourney_m.players:
tourney_m.players.append(loser_id)
self.tournaments_col.update({'_id': tournament_id}, tourney_m.dump(context='db'))
def swap_winner_loser_by_tournament_id_and_match_id(self, tournament_id, match_id):
new_matches = []
tourney_m = \
M.Tournament.load(self.tournaments_col.find_one({'_id': tournament_id}), context='db')
for match in tourney_m.matches:
try:
if match_id == match.match_id:
winner_holder = match.winner
match.winner = match.loser
match.loser = winner_holder
match_updated = True
new_matches.append(match)
except Exception as e:
raise Exception('Could not attain match. ' + str(e))
if match_updated is True:
tourney_m.matches = new_matches
self.tournaments_col.update({'_id': tournament_id}, tourney_m.dump(context='db'))
# gets potential merge targets from all regions
# basically, get players who have an alias similar to the given alias
def get_players_with_similar_alias(self, alias):
alias_lower = alias.lower()
# here be regex dragons
re_test_1 = '([1-9]+\s+[1-9]+\s+)(.+)' # to match '1 1 slox'
re_test_2 = '(.[1-9]+.[1-9]+\s+)(.+)' # to match 'p1s1 slox'
alias_set_1 = re.split(re_test_1, alias_lower)
alias_set_2 = re.split(re_test_2, alias_lower)
similar_aliases = [
alias_lower,
alias_lower.replace(" ", ""), # remove spaces
# remove special characters
re.sub(special_chars, '', alias_lower),
# remove everything before the last special character; hopefully
# removes crew/sponsor tags
re.split(special_chars, alias_lower)[-1].strip()
]
# regex nonsense to deal with pool prefixes
# prevent index OOB errors when dealing with tags that don't split well
if len(alias_set_1) == 4:
similar_aliases.append(alias_set_1[2].strip())
if len(alias_set_2) == 4:
similar_aliases.append(alias_set_2[2].strip())
# add suffixes of the string
alias_words = alias_lower.split()
similar_aliases.extend([' '.join(alias_words[i:])
for i in xrange(len(alias_words))])
# uniqify
similar_aliases = list(set(similar_aliases))
ret = self.players_col.find({'aliases': {'$in': similar_aliases},
'merged': False})
return [M.Player.load(p, context='db') for p in ret]
# inserts and merges players!
# TODO: add support for pending merges
def insert_merge(self, the_merge):
self.merge_players(the_merge)
return self.merges_col.insert(the_merge.dump(context='db'))
def get_merge(self, merge_id):
info = self.merges_col.find_one({'_id': merge_id})
return M.Merge.load(info, context='db')
def get_all_merges(self):
return [M.Merge.load(m, context='db') for m in self.merges_col.find().sort([('time', 1)])]
def undo_merge(self, the_merge):
self.unmerge_players(the_merge)
self.merges_col.remove({'_id': the_merge.id})
def merge_players(self, merge):
if merge is None:
raise TypeError("merge cannot be none")
source = self.get_player_by_id(merge.source_player_obj_id)
target = self.get_player_by_id(merge.target_player_obj_id)
if source is None or target is None:
raise TypeError("source or target can't be none!")
# check if already merged
if source.merged:
raise ValueError("source is already merged")
if target.merged:
raise ValueError("target is already merged")
print 'source:', source
print 'target:', target
if (source.id in target.merge_children) or (target.id in source.merge_children):
raise ValueError("source and target already merged")
# check if these two players have ever played each other
# (can't merge players who've played each other)
# TODO: reduce db calls for this
for tournament_id in self.get_all_tournament_ids():
tournament = self.get_tournament_by_id(tournament_id)
if source.id in tournament.players and target.id in tournament.players:
raise ValueError("source and target have played each other")
# update target and source players
target.aliases = list(set(source.aliases + target.aliases))
target.regions = list(set(source.regions + target.regions))
target.merge_children = target.merge_children + source.merge_children
source.merge_parent = target.id
source.merged = True
print 'source:', source
print 'target:', target
self.update_player(source)
self.update_player(target)
# replace source with target in all tournaments that contain source
# TODO: reduce db calls for this (index tournaments by players)
for tournament_id in self.get_all_tournament_ids():
tournament = self.get_tournament_by_id(tournament_id)
if source.id in tournament.players:
try:
tournament.replace_player(
player_to_remove=source, player_to_add=target)
self.update_tournament(tournament)
except Exception as e:
print "error replacing source with target in tournament", tournament
print e
def unmerge_players(self, merge):
source = self.get_player_by_id(merge.source_player_obj_id)
target = self.get_player_by_id(merge.target_player_obj_id)
if source is None or target is None:
raise TypeError("source or target can't be none!")
if source.merge_parent != target.id:
raise ValueError("source not merged into target")
if target.merged:
raise ValueError("target has been merged; undo that merge first")
# TODO: unmerge aliases and regions
# (probably best way to do this is to store which aliases and regions were merged in the merge Object)
source.merge_parent = None
source.merged = False
target.merge_children = [
child for child in target.merge_children if child not in source.merge_children]
self.update_player(source)
self.update_player(target)
# unmerge source from target
# TODO: reduce db calls for this (index tournaments by players)
for tournament_id in self.get_all_tournament_ids():
tournament = self.get_tournament_by_id(tournament_id)
if target.id in tournament.players:
print "unmerging tournament", tournament
# check if original id now belongs to source
if any([child in tournament.orig_ids for child in source.merge_children]):
# replace target with source in tournament
tournament.replace_player(
player_to_remove=target, player_to_add=source)
self.update_tournament(tournament)
def insert_ranking(self, ranking):
return self.rankings_col.insert(ranking.dump(context='db'))
def get_latest_ranking(self):
return M.Ranking.load(
self.rankings_col.find({'region': self.region_id}).sort(
'time', pymongo.DESCENDING)[0],
context='db')
def insert_raw_file(self, raw_file):
return self.raw_files_col.insert(raw_file.dump(context='db'))
# TODO add more tests
def is_inactive(self, player, now, day_limit, num_tourneys):
qualifying_tournaments = [x for x in self.get_all_tournaments(
players=[player], regions=[self.region_id]) if x.date >= (now - timedelta(days=day_limit))]
if len(qualifying_tournaments) >= num_tourneys:
return False
return True
# session management
# region addition
def create_region(self, display_name):
the_region = M.Region(id=display_name.lower(), display_name=display_name)
return self.insert_region(the_region, self.mongo_client, database_name=DATABASE_NAME)
def remove_region(self, region):
if self.regions_col.find_one({'display_name': region.display_name}):
self.regions_col.remove(region.dump(context='db'))
def update_region_ranking_criteria(self, region_id,
ranking_num_tourneys_attended,
ranking_activity_day_limit,
tournament_qualified_day_limit):
if self.regions_col.find_one({'_id': region_id}):
self.regions_col.update({'_id': region_id},
{'$set':
{
'ranking_num_tourneys_attended': ranking_num_tourneys_attended,
'ranking_activity_day_limit': ranking_activity_day_limit,
'tournament_qualified_day_limit': tournament_qualified_day_limit
}
})
def update_region_activeTF(self, region_id, new_activeTF):
if self.regions_col.find_one({'_id': region_id}):
self.regions_col.update({'_id': region_id},
{'$set':
{
'activeTF': new_activeTF
}
})
def get_region(self, region_id):
return M.Region.load(self.regions_col.find_one({'_id': region_id}), context='db')
def get_region_ranking_criteria(self, region_id):
result = self.regions_col.find_one({'_id': region_id})
if result:
region = M.Region.load(result, context='db')
return region.dump(context='web')
# throws an exception, which is okay because this is called from just create_user
def insert_user(self, user):
# validate that no user with same username exists currently
if self.users_col.find_one({'username': user.username}):
raise DuplicateUsernameException(
"already a user with that username in the db, exiting")
return self.users_col.insert(user.dump(context='db'))
def create_user(self, username, password, regions, perm='REGION'):
valid_regions = [
region.id for region in Dao.get_all_regions(self.mongo_client)]
for region in regions:
if region not in valid_regions:
print 'Invalid region name:', region
regions = [region for region in regions if region in valid_regions]
if len(regions) == 0 and perm == 'REGION':
raise InvalidRegionsException("No valid region for new user")
salt, hashed_password = gen_password(password)
the_user = M.User(id="userid--" + username,
admin_regions=regions,
username=username,
salt=salt,
hashed_password=hashed_password,
admin_level=perm)
return self.insert_user(the_user)
def change_passwd(self, username, password):
salt, hashed_password = gen_password(password)
# modifies the users password, or returns None if it couldnt find the
# user
return self.users_col.find_and_modify(
query={'username': username},
update={"$set": {'hashed_password': hashed_password, 'salt': salt}})
def get_all_users(self):
return [M.User.load(u, context='db') for u in self.users_col.find()]
def get_user_by_id_or_none(self, id):
result = self.users_col.find({"_id": id})
if result.count() == 0:
return None
assert result.count() == 1, "WE HAVE MULTIPLE USERS WITH THE SAME UID"
return M.User.load(result[0], context='db')
def get_user_by_username_or_none(self, username):
result = self.users_col.find({"username": username})
if result.count() == 0:
return None
assert result.count() == 1, "WE HAVE MULTIPLE USERS WITH THE SAME USERNAME"
return M.User.load(result[0], context='db')
def get_user_by_session_id_or_none(self, session_id):
# mongo magic here, go through and get a user by session_id if they
# exist, otherwise return none
result = self.sessions_col.find({"session_id": session_id})
if result.count() == 0:
return None
assert result.count() == 1, "WE HAVE MULTIPLE MAPPINGS FOR THE SAME SESSION_ID"
user_id = result[0]["user_id"]
return self.get_user_by_id_or_none(user_id)
def get_user_by_region(self, regions):
pass
def get_is_superadmin(self, user_id):
user = None
if self.users_col.find_one({'_id': user_id}):
user = self.get_user_by_id_or_none(user_id)
return user.admin_level == 'SUPER'
else:
return False
'''
def set_user_admin_level(self, user_id, admin_level):
if type(admin_level) not in M.ADMIN_LEVEL_CHOICES:
raise Exception('Submitted admin level is not of correct type')
else:
user = self.get_user_by_id_or_none(user_id)
user.admin_level = admin_level
self.users_col.update({'_id': user.id}, user.dump(context='db'))
'''
#### FOR INTERNAL USE ONLY ####
#XXX: this method must NEVER be publicly routeable, or you have session-hijacking
def get_session_id_by_user_or_none(self, User):
results = self.sessions_col.find()
for session_mapping in results:
if session_mapping.user_id == User.user_id:
return session_mapping.session_id
return None
# END OF YELLING #
def check_creds_and_get_session_id_or_none(self, username, password):
result = self.users_col.find({"username": username})
if result.count() == 0:
return None
assert result.count() == 1, "WE HAVE DUPLICATE USERNAMES IN THE DB"
user = M.User.load(result[0], context='db')
assert user, "mongo has stopped being consistent, abort ship"
# timing oracle on this... good luck
if verify_password(password, user.salt, user.hashed_password):
session_id = base64.b64encode(os.urandom(128))
self.update_session_id_for_user(user.id, session_id)
return session_id
else:
return None
def check_creds(self, username, password):
result = self.users_col.find({"username": username})
if result.count() == 0:
return None
assert result.count() == 1, "WE HAVE DUPLICATE USERNAMES IN THE DB"
user = M.User.load(result[0], context='db')
assert user, "mongo has stopped being consistent, abort ship"
return verify_password(password, user.salt, user.hashed_password)
def update_session_id_for_user(self, user_id, session_id):
# lets force people to have only one session at a time
self.sessions_col.remove({"user_id": user_id})
session_mapping = M.Session(session_id=session_id,
user_id=user_id)
self.sessions_col.insert(session_mapping.dump(context='db'))
def logout_user_or_none(self, session_id):
user = self.get_user_by_session_id_or_none(session_id)
if user:
self.sessions_col.remove({"user_id": user.id})
return True
return None