This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
account.py
1161 lines (949 loc) · 39.3 KB
/
account.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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# The contents of this file are subject to the Common Public Attribution
# License Version 1.0. (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
# License Version 1.1, but Sections 14 and 15 have been added to cover use of
# software over a computer network and provide for limited attribution for the
# Original Developer. In addition, Exhibit A has been modified to be consistent
# with Exhibit B.
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
#
# The Original Code is reddit.
#
# The Original Developer is the Initial Developer. The Initial Developer of
# the Original Code is reddit Inc.
#
# All portions of the code written by reddit are Copyright (c) 2006-2015 reddit
# Inc. All Rights Reserved.
###############################################################################
import bcrypt
from collections import Counter, OrderedDict
from datetime import datetime, timedelta
import hashlib
import hmac
import time
from pylons import request
from pylons import tmpl_context as c
from pylons import app_globals as g
from pycassa.system_manager import ASCII_TYPE, DATE_TYPE, UTF8_TYPE
from r2.config import feature
from r2.lib import amqp, filters, hooks
from r2.lib.db.thing import Thing, Relation, NotFound
from r2.lib.db.operators import lower
from r2.lib.db.userrel import UserRel
from r2.lib.db import tdb_cassandra
from r2.lib.memoize import memoize
from r2.lib.utils import (
randstr,
UrlParser,
constant_time_compare,
canonicalize_email,
tup,
)
from r2.models.bans import TempTimeout
from r2.models.last_modified import LastModified
from r2.models.modaction import ModAction
from r2.models.trylater import TryLater
trylater_hooks = hooks.HookRegistrar()
COOKIE_TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S'
class AccountExists(Exception):
pass
class Account(Thing):
_cache = g.thingcache
_data_int_props = Thing._data_int_props + ('link_karma', 'comment_karma',
'report_made', 'report_correct',
'report_ignored', 'spammer',
'reported', 'gold_creddits',
'inbox_count',
'num_payment_methods',
'num_failed_payments',
'num_gildings',
'admin_takedown_strikes',
)
_int_prop_suffix = '_karma'
_essentials = ('name', )
_defaults = dict(pref_numsites = 25,
pref_newwindow = False,
pref_clickgadget = 5,
pref_store_visits = False,
pref_public_votes = False,
pref_hide_from_robots = False,
pref_research = False,
pref_hide_ups = False,
pref_hide_downs = False,
pref_min_link_score = -4,
pref_min_comment_score = -4,
pref_num_comments = g.num_comments,
pref_highlight_controversial=False,
pref_default_comment_sort = 'confidence',
pref_lang = g.lang,
pref_content_langs = (g.lang,),
pref_over_18 = False,
pref_compress = False,
pref_domain_details = False,
pref_organic = True,
pref_no_profanity = True,
pref_label_nsfw = True,
pref_show_stylesheets = True,
pref_enable_default_themes=False,
pref_default_theme_sr=None,
pref_show_flair = True,
pref_show_link_flair = True,
pref_mark_messages_read = True,
pref_threaded_messages = True,
pref_collapse_read_messages = False,
pref_email_messages = False,
pref_private_feeds = True,
pref_force_https = False,
pref_hide_ads = False,
pref_show_trending=True,
pref_highlight_new_comments = True,
pref_monitor_mentions=True,
pref_collapse_left_bar=False,
pref_public_server_seconds=False,
pref_ignore_suggested_sort=False,
pref_beta=False,
pref_legacy_search=False,
mobile_compress = False,
mobile_thumbnail = True,
reported = 0,
report_made = 0,
report_correct = 0,
report_ignored = 0,
spammer = 0,
sort_options = {},
has_subscribed = False,
pref_media = 'subreddit',
pref_media_preview = 'subreddit',
wiki_override = None,
email = "",
email_verified = False,
nsfw_media_acknowledged = False,
ignorereports = False,
pref_show_promote = None,
gold = False,
gold_charter = False,
gold_creddits = 0,
num_gildings=0,
cake_expiration=None,
otp_secret=None,
state=0,
modmsgtime=None,
inbox_count=0,
banned_profile_visible=False,
pref_use_global_defaults=False,
pref_hide_locationbar=False,
pref_creddit_autorenew=False,
update_sent_messages=True,
num_payment_methods=0,
num_failed_payments=0,
pref_show_snoovatar=False,
gild_reveal_username=False,
selfserve_cpm_override_pennies=None,
pref_show_gold_expiration=False,
admin_takedown_strikes=0,
pref_threaded_modmail=False,
in_timeout=False,
has_used_mobile_app=False,
disable_karma=False,
)
_preference_attrs = tuple(k for k in _defaults.keys()
if k.startswith("pref_"))
@classmethod
def _cache_prefix(cls):
return "account:"
def preferences(self):
return {pref: getattr(self, pref) for pref in self._preference_attrs}
def __eq__(self, other):
if type(self) != type(other):
return False
return self._id == other._id
def __ne__(self, other):
return not self.__eq__(other)
def has_interacted_with(self, sr):
try:
r = SubredditParticipationByAccount.fast_query(self, [sr])
except tdb_cassandra.NotFound:
return False
return (self, sr) in r
def karma(self, kind, sr = None):
suffix = '_' + kind + '_karma'
#if no sr, return the sum
if sr is None:
total = 0
for k, v in self._t.iteritems():
if k.endswith(suffix):
total += v
# link karma includes both "link" and "self" values
if kind == "link":
total += self.karma("self")
return total
# if positive karma overall, default to MIN_UP_KARMA instead of 0
if self.karma(kind) > 0:
default_karma = g.MIN_UP_KARMA
else:
default_karma = 0
if kind == "link":
# link karma includes both "link" and "self", so it's a bit trickier
link_karma = getattr(self, sr.name + suffix, None)
self_karma = getattr(self, "%s_self_karma" % sr.name, None)
# return default value only if they have *neither* link nor self
if all(karma is None for karma in (link_karma, self_karma)):
return default_karma
return sum(karma for karma in (link_karma, self_karma) if karma)
else:
return getattr(self, sr.name + suffix, default_karma)
def incr_karma(self, kind, sr, amt):
# accounts can (manually) have their ability to gain/lose karma
# disabled, to prevent special accounts like AutoModerator from
# having a massive number of subreddit-karma attributes
if self.disable_karma:
return
if sr.name.startswith('_'):
g.log.info("Ignoring karma increase for subreddit %r" % (sr.name,))
return
prop = '%s_%s_karma' % (sr.name, kind)
if hasattr(self, prop):
return self._incr(prop, amt)
else:
default_val = self.karma(kind, sr)
setattr(self, prop, default_val + amt)
self._commit()
@property
def link_karma(self):
return self.karma('link')
@property
def comment_karma(self):
return self.karma('comment')
def all_karmas(self, include_old=True):
"""Get all of the user's subreddit-specific karma totals.
Returns an OrderedDict keyed on subreddit name and containing
(link_karma, comment_karma) tuples, ordered by the combined total
descending.
"""
link_suffix = '_link_karma'
self_suffix = '_self_karma'
comment_suffix = '_comment_karma'
comment_karmas = Counter()
link_karmas = Counter()
combined_karmas = Counter()
for key, value in self._t.iteritems():
if key.endswith(link_suffix):
sr_name = key[:-len(link_suffix)]
link_karmas[sr_name] += value
elif key.endswith(self_suffix):
# self karma gets added to link karma too
sr_name = key[:-len(self_suffix)]
link_karmas[sr_name] += value
elif key.endswith(comment_suffix):
sr_name = key[:-len(comment_suffix)]
comment_karmas[sr_name] = value
else:
continue
combined_karmas[sr_name] += value
all_karmas = OrderedDict()
for sr_name, total in combined_karmas.most_common():
all_karmas[sr_name] = (link_karmas[sr_name],
comment_karmas[sr_name])
if include_old:
old_link_karma = self._t.get('link_karma', 0)
old_comment_karma = self._t.get('comment_karma', 0)
if old_link_karma or old_comment_karma:
all_karmas['ancient history'] = (old_link_karma,
old_comment_karma)
return all_karmas
def update_last_visit(self, current_time):
from admintools import apply_updates
timer = g.stats.get_timer("account.update_last_visit")
timer.start()
apply_updates(self, timer)
prev_visit = LastModified.get(self._fullname, "Visit")
timer.intermediate("get_last_modified")
if prev_visit and current_time - prev_visit < timedelta(days=1):
timer.intermediate("set_last_modified.noop")
timer.stop()
return
LastModified.touch(self._fullname, "Visit")
timer.intermediate("set_last_modified.done")
timer.stop()
def make_cookie(self, timestr=None):
timestr = timestr or time.strftime(COOKIE_TIMESTAMP_FORMAT)
id_time = str(self._id) + ',' + timestr
to_hash = ','.join((id_time, self.password, g.secrets["SECRET"]))
return id_time + ',' + hashlib.sha1(to_hash).hexdigest()
def make_admin_cookie(self, first_login=None, last_request=None):
first_login = first_login or datetime.utcnow().strftime(COOKIE_TIMESTAMP_FORMAT)
last_request = last_request or datetime.utcnow().strftime(COOKIE_TIMESTAMP_FORMAT)
hashable = ','.join((first_login, last_request, request.ip, request.user_agent, self.password))
mac = hmac.new(g.secrets["SECRET"], hashable, hashlib.sha1).hexdigest()
return ','.join((first_login, last_request, mac))
def make_otp_cookie(self, timestamp=None):
timestamp = timestamp or datetime.utcnow().strftime(COOKIE_TIMESTAMP_FORMAT)
secrets = [request.user_agent, self.otp_secret, self.password]
signature = hmac.new(g.secrets["SECRET"], ','.join([timestamp] + secrets), hashlib.sha1).hexdigest()
return ",".join((timestamp, signature))
def needs_captcha(self):
if g.disable_captcha:
return False
hook = hooks.get_hook("account.is_captcha_exempt")
captcha_exempt = hook.call_until_return(account=self)
if captcha_exempt:
return False
if self.link_karma >= g.live_config["captcha_exempt_link_karma"]:
return False
if self.comment_karma >= g.live_config["captcha_exempt_comment_karma"]:
return False
return True
@property
def can_create_subreddit(self):
hook = hooks.get_hook("account.can_create_subreddit")
can_create = hook.call_until_return(account=self)
if can_create is not None:
return can_create
min_age = timedelta(days=g.live_config["create_sr_account_age_days"])
if self._age < min_age:
return False
if (self.link_karma < g.live_config["create_sr_link_karma"] and
self.comment_karma < g.live_config["create_sr_comment_karma"]):
return False
return True
@classmethod
@memoize('account._by_name')
def _by_name_cache(cls, name, allow_deleted=False):
#relower name here, just in case
deleted = (True, False) if allow_deleted else False
q = cls._query(
lower(cls.c.name) == name.lower(),
cls.c._spam == (True, False),
cls.c._deleted == deleted,
data=True,
)
q._limit = 1
l = list(q)
if l:
return l[0]._id
@classmethod
def _by_name(cls, name, allow_deleted = False, _update = False):
#lower name here so there is only one cache
uid = cls._by_name_cache(name.lower(), allow_deleted, _update = _update)
if uid:
return cls._byID(uid, data=True)
else:
raise NotFound, 'Account %s' % name
@classmethod
def _names_to_ids(cls, names, ignore_missing=False, allow_deleted=False,
_update=False):
for name in names:
uid = cls._by_name_cache(name.lower(), allow_deleted, _update=_update)
if not uid:
if ignore_missing:
continue
raise NotFound('Account %s' % name)
yield uid
# Admins only, since it's not memoized
@classmethod
def _by_name_multiple(cls, name):
q = cls._query(lower(Account.c.name) == name.lower(),
Account.c._spam == (True, False),
Account.c._deleted == (True, False))
return list(q)
@property
def friends(self):
return self.friend_ids()
@property
def enemies(self):
return self.enemy_ids()
@property
def is_moderator_somewhere(self):
# modmsgtime can be:
# - a date: the user is a mod somewhere and has unread modmail
# - False: the user is a mod somewhere and has no unread modmail
# - None: (the default) the user is not a mod anywhere
return self.modmsgtime is not None
def is_mutable(self, subreddit):
# Don't allow muting of other mods in the subreddit
if subreddit.is_moderator(self):
return False
# Don't allow muting of u/reddit or u/AutoModerator
return not (self == self.system_user() or
self == self.automoderator_user())
# Used on the goldmember version of /prefs/friends
@memoize('account.friend_rels')
def friend_rels_cache(self):
q = Friend._query(
Friend.c._thing1_id == self._id,
Friend.c._name == 'friend',
thing_data=True,
)
return list(f._id for f in q)
def friend_rels(self, _update = False):
rel_ids = self.friend_rels_cache(_update=_update)
try:
rels = Friend._byID_rel(rel_ids, return_dict=False,
eager_load = True, data = True,
thing_data = True)
rels = list(rels)
except NotFound:
if _update:
raise
else:
return self.friend_rels(_update=True)
if not _update:
sorted_1 = sorted([r._thing2_id for r in rels])
sorted_2 = sorted(list(self.friends))
if sorted_1 != sorted_2:
self.friend_ids(_update=True)
return self.friend_rels(_update=True)
return dict((r._thing2_id, r) for r in rels)
def add_friend_note(self, friend, note):
rels = self.friend_rels()
rel = rels[friend._id]
rel.note = note
rel._commit()
def _get_friend_ids_by(self, data_value_name, limit):
friend_ids = self.friend_ids()
if len(friend_ids) <= limit:
return friend_ids
with g.stats.get_timer("friends_query.%s" % data_value_name):
result = self.sort_ids_by_data_value(
friend_ids, data_value_name, limit=limit, desc=True)
return result.fetchall()
@memoize("get_recently_submitted_friend_ids", time=10*60)
def get_recently_submitted_friend_ids(self, limit=100):
return self._get_friend_ids_by("last_submit_time", limit)
@memoize("get_recently_commented_friend_ids", time=10*60)
def get_recently_commented_friend_ids(self, limit=100):
return self._get_friend_ids_by("last_comment_time", limit)
def delete(self, delete_message=None):
self.delete_message = delete_message
self.delete_time = datetime.now(g.tz)
self._deleted = True
self._commit()
#update caches
Account._by_name(self.name, allow_deleted = True, _update = True)
#we need to catch an exception here since it will have been
#recently deleted
try:
Account._by_name(self.name, _update = True)
except NotFound:
pass
# Mark this account for immediate cleanup tasks
amqp.add_item('account_deleted', self._fullname)
# schedule further cleanup after a possible recovery period
TryLater.schedule("account_deletion", self._id36,
delay=timedelta(days=90))
# 'State' bitfield properties
@property
def _banned(self):
return self.state & 1
@_banned.setter
def _banned(self, value):
if value and not self._banned:
self.state |= 1
# Invalidate all cookies by changing the password
# First back up the password so we can reverse this
self.backup_password = self.password
# New PW doesn't matter, they can't log in with it anyway.
# Even if their PW /was/ 'banned' for some reason, this
# will change the salt and thus invalidate the cookies
change_password(self, 'banned')
# deauthorize all access tokens
from r2.models.token import OAuth2AccessToken
from r2.models.token import OAuth2RefreshToken
OAuth2AccessToken.revoke_all_by_user(self)
OAuth2RefreshToken.revoke_all_by_user(self)
elif not value and self._banned:
self.state &= ~1
# Undo the password thing so they can log in
self.password = self.backup_password
# They're on their own for OAuth tokens, though.
self._commit()
@property
def subreddits(self):
from subreddit import Subreddit
return Subreddit.user_subreddits(self)
def special_distinguish(self):
if self._t.get("special_distinguish_name"):
return dict((k, self._t.get("special_distinguish_"+k, None))
for k in ("name", "kind", "symbol", "cssclass", "label", "link"))
else:
return None
def set_email(self, email):
old_email = self.email
self.email = email
self._commit()
AccountsByCanonicalEmail.update_email(self, old_email, email)
def canonical_email(self):
return canonicalize_email(self.email)
@classmethod
def system_user(cls):
try:
return cls._by_name(g.system_user)
except (NotFound, AttributeError):
return None
@classmethod
def automoderator_user(cls):
try:
return cls._by_name(g.automoderator_account)
except (NotFound, AttributeError):
return None
def use_subreddit_style(self, sr):
"""Return whether to show subreddit stylesheet depending on
individual selection if available, else use pref_show_stylesheets"""
# if FakeSubreddit, there is no stylesheet
if not hasattr(sr, '_id'):
return False
if not feature.is_enabled('stylesheets_everywhere'):
return self.pref_show_stylesheets
# if stylesheet isn't individually enabled/disabled, use global pref
return bool(getattr(self, "sr_style_%s_enabled" % sr._id,
self.pref_show_stylesheets))
def set_subreddit_style(self, sr, use_style):
if hasattr(sr, '_id'):
setattr(self, "sr_style_%s_enabled" % sr._id, use_style)
self._commit()
def flair_enabled_in_sr(self, sr_id):
return getattr(self, 'flair_%s_enabled' % sr_id, True)
def flair_text(self, sr_id, obey_disabled=False):
if obey_disabled and not self.flair_enabled_in_sr(sr_id):
return None
return getattr(self, 'flair_%s_text' % sr_id, None)
def flair_css_class(self, sr_id, obey_disabled=False):
if obey_disabled and not self.flair_enabled_in_sr(sr_id):
return None
return getattr(self, 'flair_%s_css_class' % sr_id, None)
def can_flair_in_sr(self, user, sr):
"""Return whether a user can set this one's flair in a subreddit."""
can_assign_own = self._id == user._id and sr.flair_self_assign_enabled
return can_assign_own or sr.is_moderator_with_perms(user, "flair")
def set_flair(self, subreddit, text=None, css_class=None, set_by=None,
log_details="edit"):
log_details = "flair_%s" % log_details
if not text and not css_class:
# set to None instead of potentially empty strings
text = css_class = None
subreddit.remove_flair(self)
log_details = "flair_delete"
elif not subreddit.is_flair(self):
subreddit.add_flair(self)
setattr(self, 'flair_%s_text' % subreddit._id, text)
setattr(self, 'flair_%s_css_class' % subreddit._id, css_class)
self._commit()
if set_by and set_by != self:
ModAction.create(subreddit, set_by, action='editflair',
target=self, details=log_details)
def get_trophy_id(self, uid):
'''Return the ID of the Trophy associated with the given "uid"
`uid` - The unique identifier for the Trophy to look up
'''
return getattr(self, 'received_trophy_%s' % uid, None)
def set_trophy_id(self, uid, trophy_id):
'''Recored that a user has received a Trophy with "uid"
`uid` - The trophy "type" that the user should only have one of
`trophy_id` - The ID of the corresponding Trophy object
'''
return setattr(self, 'received_trophy_%s' % uid, trophy_id)
@property
def employee(self):
"""Return if the user is an employee.
Being an employee grants them various special privileges.
"""
return (hasattr(self, 'name') and
(self.name in g.admins or
self.name in g.sponsors or
self.name in g.employees))
@property
def has_gold_subscription(self):
return bool(getattr(self, 'gold_subscr_id', None))
@property
def has_paypal_subscription(self):
return (self.has_gold_subscription and
not self.gold_subscr_id.startswith('cus_'))
@property
def has_stripe_subscription(self):
return (self.has_gold_subscription and
self.gold_subscr_id.startswith('cus_'))
@property
def gold_will_autorenew(self):
return (self.has_gold_subscription or
(self.pref_creddit_autorenew and self.gold_creddits > 0))
@property
def timeout_expiration(self):
"""Find the expiration date of the user's temp-timeout as a datetime
object.
Returns None if no temp-timeout found.
"""
if not self.in_timeout:
return None
return TempTimeout.search(self.name).get(self.name)
@property
def days_remaining_in_timeout(self):
if not self.in_timeout:
return 0
expires = self.timeout_expiration
if not expires:
return 0
# TryLater runs periodically, so if the suspension expires
# within that time, then the remaining number of days is 0
# which is the same as a permanent suspension. Return 1 day
# remaining if it already expired but the TryLater queue hasn't
# cleared it yet.
days_left = (expires - datetime.now(g.tz)).days + 1
return max(days_left, 1)
def incr_admin_takedown_strikes(self, amt=1):
return self._incr('admin_takedown_strikes', amt)
def get_style_override(self):
"""Return the subreddit selected for reddit theme.
If the user has a theme selected and enabled and also has
the feature flag enabled, return the subreddit name.
Otherwise, return None.
"""
# Experiment to change the default style to determine if
# engagement metrics change
if (feature.is_enabled("default_design") and
feature.variant("default_design") == "nautclassic"):
return "nautclassic"
if (feature.is_enabled("default_design") and
feature.variant("default_design") == "serene"):
return "serene"
# Reddit themes is not enabled for this user
if not feature.is_enabled('stylesheets_everywhere'):
return None
# Make sure they have the theme enabled
if not self.pref_enable_default_themes:
return None
return self.pref_default_theme_sr
def has_been_atoed(self):
"""Return true if this account has ever been required to reset their password
"""
return 'force_password_reset' in self._t
class FakeAccount(Account):
_nodb = True
pref_no_profanity = True
def __eq__(self, other):
return self is other
def valid_admin_cookie(cookie):
if g.read_only_mode:
return (False, None)
# parse the cookie
try:
first_login, last_request, hash = cookie.split(',')
except ValueError:
return (False, None)
# make sure it's a recent cookie
try:
first_login_time = datetime.strptime(first_login, COOKIE_TIMESTAMP_FORMAT)
last_request_time = datetime.strptime(last_request, COOKIE_TIMESTAMP_FORMAT)
except ValueError:
return (False, None)
cookie_age = datetime.utcnow() - first_login_time
if cookie_age.total_seconds() > g.ADMIN_COOKIE_TTL:
return (False, None)
idle_time = datetime.utcnow() - last_request_time
if idle_time.total_seconds() > g.ADMIN_COOKIE_MAX_IDLE:
return (False, None)
# validate
expected_cookie = c.user.make_admin_cookie(first_login, last_request)
return (constant_time_compare(cookie, expected_cookie),
first_login)
def valid_otp_cookie(cookie):
if g.read_only_mode:
return False
# parse the cookie
try:
remembered_at, signature = cookie.split(",")
except ValueError:
return False
# make sure it hasn't expired
try:
remembered_at_time = datetime.strptime(remembered_at, COOKIE_TIMESTAMP_FORMAT)
except ValueError:
return False
age = datetime.utcnow() - remembered_at_time
if age.total_seconds() > g.OTP_COOKIE_TTL:
return False
# validate
expected_cookie = c.user.make_otp_cookie(remembered_at)
return constant_time_compare(cookie, expected_cookie)
def valid_feed(name, feedhash, path):
if name and feedhash and path:
from r2.lib.template_helpers import add_sr
path = add_sr(path)
try:
user = Account._by_name(name)
if (user.pref_private_feeds and
constant_time_compare(feedhash, make_feedhash(user, path))):
return user
except NotFound:
pass
def make_feedhash(user, path):
return hashlib.sha1("".join([user.name, user.password,
g.secrets["FEEDSECRET"]])
).hexdigest()
def make_feedurl(user, path, ext = "rss"):
u = UrlParser(path)
u.update_query(user = user.name,
feed = make_feedhash(user, path))
u.set_extension(ext)
return u.unparse()
def valid_password(a, password, compare_password=None):
# bail out early if the account or password's invalid
if not hasattr(a, 'name') or not hasattr(a, 'password') or not password:
return False
convert_password = False
if compare_password is None:
convert_password = True
compare_password = a.password
# standardize on utf-8 encoding
password = filters._force_utf8(password)
if compare_password.startswith('$2a$'):
# it's bcrypt.
try:
expected_hash = bcrypt.hashpw(password, compare_password)
except ValueError:
# password is invalid because it contains null characters
return False
if not constant_time_compare(compare_password, expected_hash):
return False
# if it's using the current work factor, we're done, but if it's not
# we'll have to rehash.
# the format is $2a$workfactor$salt+hash
work_factor = int(compare_password.split("$")[2])
if work_factor == g.bcrypt_work_factor:
return a
else:
# alright, so it's not bcrypt. how old is it?
# if the length of the stored hash is 43 bytes, the sha-1 hash has a salt
# otherwise it's sha-1 with no salt.
salt = ''
if len(compare_password) == 43:
salt = compare_password[:3]
expected_hash = passhash(a.name, password, salt)
if not constant_time_compare(compare_password, expected_hash):
return False
# since we got this far, it's a valid password but in an old format
# let's upgrade it
if convert_password:
a.password = bcrypt_password(password)
a._commit()
return a
def bcrypt_password(password):
salt = bcrypt.gensalt(log_rounds=g.bcrypt_work_factor)
return bcrypt.hashpw(password, salt)
def passhash(username, password, salt = ''):
if salt is True:
salt = randstr(3)
tohash = '%s%s %s' % (salt, username, password)
return salt + hashlib.sha1(tohash).hexdigest()
def change_password(user, newpassword):
user.password = bcrypt_password(newpassword)
user._commit()
LastModified.touch(user._fullname, 'Password')
return True
def register(name, password, registration_ip):
# get a lock for registering an Account with this name to prevent
# simultaneous operations from creating multiple Accounts with the same name
with g.make_lock("account_register", "register_%s" % name.lower()):
try:
account = Account._by_name(name)
raise AccountExists
except NotFound:
account = Account(
name=name,
password=bcrypt_password(password),
# new accounts keep the profanity filter settings until opting out
pref_no_profanity=True,
registration_ip=registration_ip,
)
account._commit()
# update Account._by_name to pick up this new name->Account
Account._by_name(name, _update=True)
Account._by_name(name, allow_deleted=True, _update=True)
return account
class Friend(Relation(Account, Account)):
_cache = g.thingcache
@classmethod
def _cache_prefix(cls):
return "friend:"
Account.__bases__ += (UserRel('friend', Friend, disable_reverse_ids_fn=True),
UserRel('enemy', Friend, disable_reverse_ids_fn=False))
class DeletedUser(FakeAccount):
@property
def name(self):
return '[deleted]'
@property
def _deleted(self):
return True
def _fullname(self):
raise NotImplementedError
def _id(self):
raise NotImplementedError
def __setattr__(self, attr, val):
if attr == '_deleted':
pass
else:
object.__setattr__(self, attr, val)
class BlockedSubredditsByAccount(tdb_cassandra.DenormalizedRelation):
_use_db = True
_last_modified_name = 'block_subreddit'
_read_consistency_level = tdb_cassandra.CL.QUORUM
_write_consistency_level = tdb_cassandra.CL.QUORUM
_connection_pool = 'main'
_views = []
@classmethod
def value_for(cls, thing1, thing2):
return ''
@classmethod
def block(cls, user, sr):
cls.create(user, sr)
@classmethod
def unblock(cls, user, sr):
cls.destroy(user, sr)
@classmethod
def is_blocked(cls, user, sr):
try:
r = cls.fast_query(user, [sr])
except tdb_cassandra.NotFound:
return False
return (user, sr) in r
@trylater_hooks.on("trylater.account_deletion")
def deleted_account_cleanup(data):
from r2.models import Subreddit
from r2.models.admin_notes import AdminNotesBySystem
from r2.models.flair import Flair
from r2.models.token import OAuth2Client