-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathuser.py
More file actions
2027 lines (1675 loc) · 77.9 KB
/
user.py
File metadata and controls
2027 lines (1675 loc) · 77.9 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
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
"""
Models for User Information (students, staff, etc)
Migration Notes
If you make changes to this model, be sure to create an appropriate migration
file and check it in at the same time as your model changes. To do that,
1. Go to the edx-platform dir
2. ./manage.py lms schemamigration student --auto description_of_your_change
3. Add the migration file created in edx-platform/common/djangoapps/student/migrations/
"""
import hashlib
import json
import logging
import uuid
from datetime import datetime, timedelta
from functools import total_ordering
from importlib import import_module
from urllib.parse import urlencode
from zoneinfo import ZoneInfo
import crum
from config_models.models import ConfigurationModel
from django.apps import apps
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.contrib.sites.models import Site
from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist
from django.core.validators import FileExtensionValidator, RegexValidator
from django.db import IntegrityError, models
from django.db.models import Q
from django.db.models.signals import post_delete, post_save, pre_save
from django.db.utils import ProgrammingError
from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _
from django.utils.translation import gettext_noop
from django_countries.fields import CountryField
from edx_django_utils import monitoring
from edx_django_utils.cache import RequestCache
from eventtracking import tracker
from model_utils.models import TimeStampedModel
from opaque_keys.edx.django.models import CourseKeyField, LearningContextKeyField
from pytz import UTC, timezone
import openedx.core.djangoapps.django_comment_common.comment_client as cc
from common.djangoapps.util.model_utils import emit_field_changed_events, get_changed_fields_dict
from lms.djangoapps.courseware.toggles import streak_celebration_is_active
from openedx.core.djangoapps.signals.signals import USER_ACCOUNT_ACTIVATED
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
from openedx.core.djangoapps.xmodule_django.models import NoneToEmptyManager
from openedx.core.djangolib.model_mixins import DeletableByUserValue
from openedx.core.lib import user_util
from openedx.core.toggles import ENTRANCE_EXAMS
from .course_enrollment import (
ALLOWEDTOENROLL_TO_ENROLLED,
CourseEnrollment,
CourseEnrollmentAllowed,
CourseOverview,
ManualEnrollmentAudit,
segment,
)
User = get_user_model()
log = logging.getLogger(__name__)
AUDIT_LOG = logging.getLogger("audit")
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore # pylint: disable=invalid-name
IS_MARKETABLE = 'is_marketable'
USER_LOGGED_IN_EVENT_NAME = 'edx.user.login'
USER_LOGGED_OUT_EVENT_NAME = 'edx.user.logout'
USER_STREAK_UPDATED_EVENT_NAME = "edx.user.celebration.streak_updated"
class AnonymousUserId(models.Model): # noqa: DJ008
"""
This table contains user, course_Id and anonymous_user_id
Purpose of this table is to provide user by anonymous_user_id.
We generate anonymous_user_id using md5 algorithm,
and use result in hex form, so its length is equal to 32 bytes.
.. no_pii: We store anonymous_user_ids here, but do not consider them PII under OEP-30.
"""
objects = NoneToEmptyManager()
user = models.ForeignKey(User, db_index=True, on_delete=models.CASCADE) # noqa: DJ012
anonymous_user_id = models.CharField(unique=True, max_length=32)
course_id = LearningContextKeyField(db_index=True, blank=True)
def anonymous_id_for_user(user, course_id):
"""
Inputs:
user: User model
course_id: string or None
Return a unique id for a (user, course_id) pair, suitable for inserting
into e.g. personalized survey links.
If user is an `AnonymousUser`, returns `None`
else If this user/course_id pair already has an anonymous id in AnonymousUserId object, return that
else: create new anonymous_id, save it in AnonymousUserId, and return anonymous id
"""
# This part is for ability to get xblock instance in xblock_noauth handlers, where user is unauthenticated.
assert user
if user.is_anonymous:
return None
# ARCHBOM-1674: Get a sense of what fraction of anonymous_user_id calls are
# cached, stored in the DB, or retrieved from the DB. This will help inform
# us on decisions about whether we can
# pregenerate IDs, use random instead of deterministic IDs, etc.
monitoring.increment('temp_anon_uid_v2.requested')
cached_id = getattr(user, '_anonymous_id', {}).get(course_id)
if cached_id is not None:
monitoring.increment('temp_anon_uid_v2.returned_from_cache')
return cached_id
# Check if an anonymous id already exists for this user and
# course_id combination. Prefer the one with the highest record ID
# (see below.)
anonymous_user_ids = AnonymousUserId.objects.filter(user=user).filter(course_id=course_id).order_by('-id')
if anonymous_user_ids:
# If there are multiple anonymous_user_ids per user, course_id pair
# select the row which was created most recently.
# There might be more than one if the Django SECRET_KEY had
# previously been rotated at a time before this function was
# changed to always save the generated IDs to the DB. In that
# case, just pick the one with the highest record ID, which is
# probably the most recently created one.
anonymous_user_id = anonymous_user_ids[0].anonymous_user_id
monitoring.increment('temp_anon_uid_v2.fetched_existing')
else:
# Uses SECRET_KEY as a cryptographic pepper. This
# deterministic ID generation means that concurrent identical
# calls to this function return the same value -- no need for
# locking. (There may be a low level of integrity errors on
# creation as a result of concurrent duplicate row inserts.)
#
# Consequences for this function of SECRET_KEY exposure: Data
# researchers and other third parties receiving these
# anonymous user IDs would be able to identify users across
# courses, and predict the anonymous user IDs of all users
# (but not necessarily identify their accounts.)
#
# Rotation process of SECRET_KEY with respect to this
# function: Rotate at will, since the hashes are stored and
# will not change.
# include the secret key as a salt, and to make the ids unique across different LMS installs.
hasher = hashlib.shake_128()
hasher.update(settings.SECRET_KEY.encode('utf8'))
hasher.update(str(user.id).encode('utf8'))
if course_id:
hasher.update(str(course_id).encode('utf-8'))
anonymous_user_id = hasher.hexdigest(16)
try:
AnonymousUserId.objects.create(
user=user,
course_id=course_id,
anonymous_user_id=anonymous_user_id,
)
monitoring.increment('temp_anon_uid_v2.stored')
except IntegrityError:
# Another thread has already created this entry, so
# continue
monitoring.increment('temp_anon_uid_v2.store_db_error')
# cache the anonymous_id in the user object
if not hasattr(user, '_anonymous_id'):
user._anonymous_id = {} # pylint: disable=protected-access
user._anonymous_id[course_id] = anonymous_user_id # pylint: disable=protected-access
return anonymous_user_id
def user_by_anonymous_id(uid):
"""
Return user by anonymous_user_id using AnonymousUserId lookup table.
Do not raise `django.ObjectDoesNotExist` exception,
if there is no user for anonymous_student_id,
because this function will be used inside xmodule w/o django access.
"""
if uid is None:
return None
request_cache = RequestCache('user_by_anonymous_id')
cache_response = request_cache.get_cached_response(uid)
if cache_response.is_found:
return cache_response.value
try:
user = User.objects.get(anonymoususerid__anonymous_user_id=uid)
request_cache.set(uid, user)
return user
except ObjectDoesNotExist:
request_cache.set(uid, None)
return None
def is_username_retired(username):
"""
Checks to see if the given username has been previously retired
"""
locally_hashed_usernames = user_util.get_all_retired_usernames(
username,
settings.RETIRED_USER_SALTS,
settings.RETIRED_USERNAME_FMT
)
# TODO: Revert to this after username capitalization issues detailed in
# PLAT-2276, PLAT-2277, PLAT-2278 are sorted out:
# return User.objects.filter(username__in=list(locally_hashed_usernames)).exists()
# Avoid circular import issues
from openedx.core.djangoapps.user_api.models import UserRetirementStatus
# Sandbox clean builds attempt to create users during migrations, before the database
# is stable so UserRetirementStatus may not exist yet. This workaround can also go
# when we are done with the username updates.
try:
return User.objects.filter(username__in=list(locally_hashed_usernames)).exists() or \
UserRetirementStatus.objects.filter(original_username=username).exists()
except ProgrammingError as exc:
# Check the error message to make sure it's what we expect
if "user_api_userretirementstatus" in str(exc):
return User.objects.filter(username__in=list(locally_hashed_usernames)).exists()
raise
def username_exists_or_retired(username):
"""
Check a username for existence -or- retirement against the User model.
"""
return User.objects.filter(username=username).exists() or is_username_retired(username)
def is_email_retired(email):
"""
Checks to see if the given email has been previously retired
"""
locally_hashed_emails = user_util.get_all_retired_emails(
email,
settings.RETIRED_USER_SALTS,
settings.RETIRED_EMAIL_FMT
)
return User.objects.filter(email__in=list(locally_hashed_emails)).exists()
def email_exists_or_retired(email):
"""
Check an email against the User model for existence.
"""
return (
User.objects.filter(email=email).exists() or
is_email_retired(email) or
AccountRecovery.objects.filter(secondary_email=email).exists()
)
def get_retired_username_by_username(username):
"""
If a UserRetirementStatus object with an original_username matching the given username exists,
returns that UserRetirementStatus.retired_username value. Otherwise, returns a "retired username"
hashed using the newest configured salt.
"""
UserRetirementStatus = apps.get_model('user_api', 'UserRetirementStatus')
try:
status = UserRetirementStatus.objects.filter(original_username=username).order_by('-modified').first()
if status:
return status.retired_username
except UserRetirementStatus.DoesNotExist:
pass
return user_util.get_retired_username(username, settings.RETIRED_USER_SALTS, settings.RETIRED_USERNAME_FMT)
def get_retired_email_by_email(email):
"""
If a UserRetirementStatus object with an original_email matching the given email exists,
returns that UserRetirementStatus.retired_email value. Otherwise, returns a "retired email"
hashed using the newest configured salt.
"""
UserRetirementStatus = apps.get_model('user_api', 'UserRetirementStatus')
try:
status = UserRetirementStatus.objects.filter(original_email=email).order_by('-modified').first()
if status:
return status.retired_email
except UserRetirementStatus.DoesNotExist:
pass
return user_util.get_retired_email(email, settings.RETIRED_USER_SALTS, settings.RETIRED_EMAIL_FMT)
def get_all_retired_usernames_by_username(username):
"""
Returns a generator of "retired usernames", one hashed with each
configured salt. Used for finding out if the given username has
ever been used and retired.
"""
return user_util.get_all_retired_usernames(username, settings.RETIRED_USER_SALTS, settings.RETIRED_USERNAME_FMT)
def _get_all_retired_emails_by_email(email):
"""
Returns a generator of "retired emails", one hashed with each
configured salt. Used for finding out if the given email has
ever been used and retired.
"""
return user_util.get_all_retired_emails(email, settings.RETIRED_USER_SALTS, settings.RETIRED_EMAIL_FMT)
def get_potentially_retired_user_by_username(username):
"""
Attempt to return a User object based on the username, or if it
does not exist, then any hashed username salted with the historical
salts.
"""
locally_hashed_usernames = list(get_all_retired_usernames_by_username(username))
locally_hashed_usernames.append(username)
potential_users = User.objects.filter(username__in=locally_hashed_usernames)
# Have to disambiguate between several Users here as we could have retirees with
# the same username, but for case.
# If there's only 1 we're done, this should be the common case
if len(potential_users) == 1:
return potential_users[0]
# No user found, throw the usual error
if not potential_users:
raise User.DoesNotExist()
# For a brief period, users were able to retire accounts and make another account with
# the same differently-cased username, like "testuser" and "TestUser".
# If there are two users found, return the one that's the *actual* case-matching username,
# whether retired or not.
if len(potential_users) == 2:
# Figure out which user has been retired.
if potential_users[0].username.startswith(settings.RETIRED_USERNAME_PREFIX):
retired = potential_users[0]
active = potential_users[1]
else:
retired = potential_users[1]
active = potential_users[0]
# If the active (non-retired) user's username doesn't *exactly* match (including case),
# then the retired account must be the one that exactly matches.
return active if active.username == username else retired
# We should have, at most, a retired username and an active one with a username
# differing only by case. If there are more we need to disambiguate them by hand.
raise Exception(f'Expected 1 or 2 Users, received {str(potential_users)}')
def get_potentially_retired_user_by_username_and_hash(username, hashed_username):
"""
To assist in the retirement process this method will:
- Confirm that any locally hashed username matches the passed in one
(in case of salt mismatches with the upstream script).
- Attempt to return a User object based on the username, or if it
does not exist, the any hashed username salted with the historical
salts.
"""
locally_hashed_usernames = list(get_all_retired_usernames_by_username(username))
if hashed_username not in locally_hashed_usernames:
raise Exception('Mismatched hashed_username, bad salt?')
locally_hashed_usernames.append(username)
return User.objects.get(username__in=locally_hashed_usernames)
class UserStanding(models.Model): # noqa: DJ008
"""
This table contains a student's account's status.
Currently, we're only disabling accounts; in the future we can imagine
taking away more specific privileges, like forums access, or adding
more specific karma levels or probationary stages.
.. no_pii:
"""
ACCOUNT_DISABLED = "disabled"
ACCOUNT_ENABLED = "enabled"
USER_STANDING_CHOICES = (
(ACCOUNT_DISABLED, "Account Disabled"),
(ACCOUNT_ENABLED, "Account Enabled"),
)
user = models.OneToOneField(User, db_index=True, related_name='standing', on_delete=models.CASCADE)
account_status = models.CharField(
blank=True, max_length=31, choices=USER_STANDING_CHOICES
)
changed_by = models.ForeignKey(User, blank=True, on_delete=models.CASCADE)
standing_last_changed_at = models.DateTimeField(auto_now=True)
class UserProfile(models.Model): # noqa: DJ008
"""This is where we store all the user demographic fields. We have a
separate table for this rather than extending the built-in Django auth_user.
Notes:
* Some fields are legacy ones from the first run of 6.002, from which
we imported many users.
* Fields like name and address are intentionally open ended, to account
for international variations. An unfortunate side-effect is that we
cannot efficiently sort on last names for instance.
Replication:
* Only the Portal servers should ever modify this information.
* All fields are replicated into relevant Course databases
Some of the fields are legacy ones that were captured during the initial
MITx fall prototype.
.. pii: Contains many PII fields. Retired in AccountRetirementView.
.. pii_types: name, location, birth_date, gender, biography, phone_number
.. pii_retirement: local_api
"""
# cache key format e.g user.<user_id>.profile.country = 'SG'
PROFILE_COUNTRY_CACHE_KEY = "user.{user_id}.profile.country"
class Meta:
db_table = "auth_userprofile"
permissions = (("can_deactivate_users", "Can deactivate, but NOT delete users"),)
# CRITICAL TODO/SECURITY
# Sanitize all fields.
# This is not visible to other users, but could introduce holes later
user = models.OneToOneField(User, unique=True, db_index=True, related_name='profile', on_delete=models.CASCADE) # noqa: DJ012 # pylint: disable=line-too-long
name = models.CharField(blank=True, max_length=255, db_index=True)
# How meta field works: meta will only store those fields which are available in extended_profile configuration,
# so in order to store a field in meta, it must be available in extended_profile configuration.
meta = models.TextField(blank=True) # JSON dictionary for future expansion
courseware = models.CharField(blank=True, max_length=255, default='course.xml')
# Language is deprecated and no longer used. Old rows exist that have
# user-entered free form text values (ex. "English"), some of which have
# non-ASCII values. You probably want UserPreference version of this, which
# stores the user's preferred language code. See openedx/core/djangoapps/lang_pref
# for more information.
language = models.CharField(blank=True, max_length=255, db_index=True)
# Location is no longer used, but is held here for backwards compatibility
# for users imported from our first class.
location = models.CharField(blank=True, max_length=255, db_index=True)
# Optional demographic data we started capturing from Fall 2012
this_year = datetime.now(ZoneInfo("UTC")).year
VALID_YEARS = list(range(this_year, this_year - 120, -1))
year_of_birth = models.IntegerField(blank=True, null=True, db_index=True)
GENDER_CHOICES = (
('m', gettext_noop('Male')),
('f', gettext_noop('Female')),
# Translators: 'Other' refers to the student's gender
('o', gettext_noop('Other/Prefer Not to Say'))
)
gender = models.CharField( # noqa: DJ001
blank=True, null=True, max_length=6, db_index=True, choices=GENDER_CHOICES
)
# [03/21/2013] removed these, but leaving comment since there'll still be
# p_se and p_oth in the existing data in db.
# ('p_se', 'Doctorate in science or engineering'),
# ('p_oth', 'Doctorate in another field'),
LEVEL_OF_EDUCATION_CHOICES = (
('p', gettext_noop('Doctorate')),
('m', gettext_noop("Master's or professional degree")),
('b', gettext_noop("Bachelor's degree")),
('a', gettext_noop("Associate degree")),
('hs', gettext_noop("Secondary/high school")),
('jhs', gettext_noop("Junior secondary/junior high/middle school")),
('el', gettext_noop("Elementary/primary school")),
# Translators: 'None' refers to the student's level of education
('none', gettext_noop("No formal education")),
# Translators: 'Other' refers to the student's level of education
('other', gettext_noop("Other education"))
)
level_of_education = models.CharField( # noqa: DJ001
blank=True, null=True, max_length=6, db_index=True,
choices=LEVEL_OF_EDUCATION_CHOICES
)
mailing_address = models.TextField(blank=True, null=True) # noqa: DJ001
city = models.TextField(blank=True, null=True) # noqa: DJ001
country = CountryField(blank=True, null=True)
COUNTRY_WITH_STATES = 'US'
STATE_CHOICES = (
('AL', 'Alabama'),
('AK', 'Alaska'),
('AZ', 'Arizona'),
('AR', 'Arkansas'),
('AA', 'Armed Forces Americas'),
('AE', 'Armed Forces Europe'),
('AP', 'Armed Forces Pacific'),
('CA', 'California'),
('CO', 'Colorado'),
('CT', 'Connecticut'),
('DE', 'Delaware'),
('DC', 'District Of Columbia'),
('FL', 'Florida'),
('GA', 'Georgia'),
('HI', 'Hawaii'),
('ID', 'Idaho'),
('IL', 'Illinois'),
('IN', 'Indiana'),
('IA', 'Iowa'),
('KS', 'Kansas'),
('KY', 'Kentucky'),
('LA', 'Louisiana'),
('ME', 'Maine'),
('MD', 'Maryland'),
('MA', 'Massachusetts'),
('MI', 'Michigan'),
('MN', 'Minnesota'),
('MS', 'Mississippi'),
('MO', 'Missouri'),
('MT', 'Montana'),
('NE', 'Nebraska'),
('NV', 'Nevada'),
('NH', 'New Hampshire'),
('NJ', 'New Jersey'),
('NM', 'New Mexico'),
('NY', 'New York'),
('NC', 'North Carolina'),
('ND', 'North Dakota'),
('OH', 'Ohio'),
('OK', 'Oklahoma'),
('OR', 'Oregon'),
('PA', 'Pennsylvania'),
('RI', 'Rhode Island'),
('SC', 'South Carolina'),
('SD', 'South Dakota'),
('TN', 'Tennessee'),
('TX', 'Texas'),
('UT', 'Utah'),
('VT', 'Vermont'),
('VA', 'Virginia'),
('WA', 'Washington'),
('WV', 'West Virginia'),
('WI', 'Wisconsin'),
('WY', 'Wyoming'),
)
state = models.CharField(blank=True, null=True, max_length=2, choices=STATE_CHOICES) # noqa: DJ001
goals = models.TextField(blank=True, null=True) # noqa: DJ001
bio = models.CharField(blank=True, null=True, max_length=3000, db_index=False) # noqa: DJ001
profile_image_uploaded_at = models.DateTimeField(null=True, blank=True)
phone_regex = RegexValidator(
regex=r'^\+?1?\d*$',
message="Phone number must start with '+' (optional) followed by digits (0-9) only.",
)
phone_number = models.CharField(validators=[phone_regex], blank=True, null=True, max_length=50) # noqa: DJ001
@property
def has_profile_image(self):
"""
Convenience method that returns a boolean indicating whether or not
this user has uploaded a profile image.
"""
return self.profile_image_uploaded_at is not None
@property
def age(self):
""" Convenience method that returns the age given a year_of_birth. """
year_of_birth = self.year_of_birth
year = datetime.now(ZoneInfo("UTC")).year
if year_of_birth is not None:
return self._calculate_age(year, year_of_birth)
@property
def level_of_education_display(self):
""" Convenience method that returns the human readable level of education. """
if self.level_of_education:
return self.__enumerable_to_display(self.LEVEL_OF_EDUCATION_CHOICES, self.level_of_education)
@property
def gender_display(self):
""" Convenience method that returns the human readable gender. """
if self.gender:
return self.__enumerable_to_display(self.GENDER_CHOICES, self.gender)
def get_meta(self): # pylint: disable=missing-function-docstring
js_str = self.meta
if not js_str:
js_str = {}
else:
js_str = json.loads(self.meta)
return js_str
def set_meta(self, meta_json):
self.meta = json.dumps(meta_json)
def set_login_session(self, session_id=None):
"""
Sets the current session id for the logged-in user.
If session_id doesn't match the existing session,
deletes the old session object.
"""
meta = self.get_meta()
old_login = meta.get('session_id', None)
if old_login:
SessionStore(session_key=old_login).delete()
meta['session_id'] = session_id
self.set_meta(meta)
self.save()
def requires_parental_consent(self, year=None, age_limit=None, default_requires_consent=True):
"""Returns true if this user requires parental consent.
Args:
year (int): The year for which consent needs to be tested (defaults to now).
age_limit (int): The age limit at which parental consent is no longer required.
This defaults to the value of the setting 'PARENTAL_CONTROL_AGE_LIMIT'.
default_requires_consent (bool): True if users require parental consent if they
have no specified year of birth (default is True).
Returns:
True if the user requires parental consent.
"""
if age_limit is None:
age_limit = getattr(settings, 'PARENTAL_CONSENT_AGE_LIMIT', None)
if age_limit is None:
return False
# Return True if either:
# a) The user has a year of birth specified and that year is fewer years in the past than the limit.
# b) The user has no year of birth specified and the default is to require consent.
#
# Note: we have to be conservative using the user's year of birth as their birth date could be
# December 31st. This means that if the number of years since their birth year is exactly equal
# to the age limit then we have to assume that they might still not be old enough.
year_of_birth = self.year_of_birth
if year_of_birth is None:
return default_requires_consent
if year is None:
age = self.age
else:
age = self._calculate_age(year, year_of_birth)
return age < age_limit
def __enumerable_to_display(self, enumerables, enum_value):
""" Get the human readable value from an enumerable list of key-value pairs. """
return dict(enumerables)[enum_value]
def _calculate_age(self, year, year_of_birth):
"""Calculate the youngest age for a user with a given year of birth.
:param year: year
:param year_of_birth: year of birth
:return: youngest age a user could be for the given year
"""
# There are legal implications regarding how we can contact users and what information we can make public
# based on their age, so we must take the most conservative estimate.
return year - year_of_birth - 1
@classmethod
def country_cache_key_name(cls, user_id):
"""Return cache key name to be used to cache current country.
Args:
user_id(int): Id of user.
Returns:
Unicode cache key
"""
return cls.PROFILE_COUNTRY_CACHE_KEY.format(user_id=user_id)
@receiver(models.signals.post_save, sender=UserProfile)
def invalidate_user_profile_country_cache(sender, instance, **kwargs): # pylint: disable=unused-argument
"""Invalidate the cache of country in UserProfile model. """
changed_fields = getattr(instance, '_changed_fields', {})
if 'country' in changed_fields:
cache_key = UserProfile.country_cache_key_name(instance.user_id)
cache.delete(cache_key)
log.info("Country changed in UserProfile for %s, cache deleted", instance.user_id)
@receiver(pre_save, sender=UserProfile)
def user_profile_pre_save_callback(sender, **kwargs):
"""
Ensure consistency of a user profile before saving it.
"""
user_profile = kwargs['instance']
# Cache "old" field values on the model instance so that they can be
# retrieved in the post_save callback when we emit an event with new and
# old field values.
user_profile._changed_fields = get_changed_fields_dict(user_profile, sender) # lint-amnesty, pylint: disable=protected-access
@receiver(post_save, sender=UserProfile)
def user_profile_post_save_callback(sender, **kwargs):
"""
Emit analytics events after saving the UserProfile.
"""
user_profile = kwargs['instance']
emit_field_changed_events(
user_profile,
user_profile.user,
sender._meta.db_table,
excluded_fields=['meta']
)
@receiver(pre_save, sender=User)
def user_pre_save_callback(sender, **kwargs):
"""
Capture old fields on the user instance before save and cache them as a
private field on the current model for use in the post_save callback.
"""
user = kwargs['instance']
user._changed_fields = get_changed_fields_dict(user, sender) # lint-amnesty, pylint: disable=protected-access
@receiver(post_save, sender=User)
def user_post_save_callback(sender, **kwargs):
"""
When a user is modified and either its `is_active` state or email address
is changed, and the user is, in fact, active, then check to see if there
are any courses that it needs to be automatically enrolled in and enroll them if needed.
Additionally, emit analytics events after saving the User.
"""
user = kwargs['instance']
changed_fields = user._changed_fields # lint-amnesty, pylint: disable=protected-access
if 'is_active' in changed_fields or 'email' in changed_fields:
if user.is_active:
ceas = CourseEnrollmentAllowed.for_user(user).filter(auto_enroll=True)
for cea in ceas:
# skip enrolling already enrolled users
if CourseEnrollment.is_enrolled(user, cea.course_id):
# Link the CEA to the user if the CEA isn't already linked to the user
# (e.g. the user was invited to a course but hadn't activated the account yet)
# This is to prevent students from changing e-mails and
# enrolling many accounts through the same e-mail.
if not cea.user:
cea.user = user
cea.save()
continue
# Skip auto enrollment of user if enrollment is not open for the course
# We are checking this here instead of passing check_access=True to CourseEnrollment.enroll()
# as we want to skip course full check.
if CourseEnrollment.is_enrollment_closed(user, CourseOverview.get_from_id(cea.course_id)):
log.info(f'Skipping auto enrollment of user as enrollment for course {cea.course_id} has ended')
continue
enrollment = CourseEnrollment.enroll(user, cea.course_id)
manual_enrollment_audit = ManualEnrollmentAudit.get_manual_enrollment_by_email(user.email)
if manual_enrollment_audit is not None:
# get the enrolled by user and reason from the ManualEnrollmentAudit table.
# then create a new ManualEnrollmentAudit table entry for the same email
# different transition state.
ManualEnrollmentAudit.create_manual_enrollment_audit(
manual_enrollment_audit.enrolled_by,
user.email,
ALLOWEDTOENROLL_TO_ENROLLED,
manual_enrollment_audit.reason,
enrollment
)
# Ensure the user has a profile when run via management command
_called_by_management_command = getattr(user, '_called_by_management_command', None)
if _called_by_management_command:
try:
profile = user.profile
except UserProfile.DoesNotExist:
profile = UserProfile.objects.create(user=user)
log.info('Created new profile for user: %s', user)
# If user is created using management command, ensure that the user's
# marketable attribute is set (default: False) and an account is created
# on segment. By created an account on segment, it is ensured that data
# will be sent to relevant places like Braze.
if settings.MARKETING_EMAILS_OPT_IN:
UserAttribute.set_user_attribute(user, IS_MARKETABLE, 'false')
traits = {
'email': user.email,
'username': user.username,
'name': profile.name,
'age': profile.age or -1,
'yearOfBirth': profile.year_of_birth or datetime.now(ZoneInfo("UTC")).year,
'education': profile.level_of_education_display,
'address': profile.mailing_address,
'gender': profile.gender_display,
'country': str(profile.country),
'is_marketable': False
}
# .. pii: Many pieces of PII are sent to Segment here. Retired directly through Segment API call in Tubular.
# .. pii_types: email_address, username
# .. pii_retirement: third_party
segment.identify(user.id, traits)
# Because `emit_field_changed_events` removes the record of the fields that
# were changed, wait to do that until after we've checked them as part of
# the condition on whether we want to check for automatic enrollments.
emit_field_changed_events(
user,
user,
sender._meta.db_table,
excluded_fields=['last_login', 'first_name', 'last_name'],
hidden_fields=['password']
)
class UserSignupSource(models.Model): # noqa: DJ008
"""
This table contains information about users registering
via Micro-Sites
.. no_pii:
"""
user = models.ForeignKey(User, db_index=True, on_delete=models.CASCADE)
site = models.CharField(max_length=255, db_index=True)
def unique_id_for_user(user):
"""
Return a unique id for a user, suitable for inserting into
e.g. personalized survey links.
"""
# Setting course_id to '' makes it not affect the generated hash,
# and thus produce the old per-student anonymous id
return anonymous_id_for_user(user, None)
# TODO: Should be renamed to generic UserGroup, and possibly
# Given an optional field for type of group
class UserTestGroup(models.Model): # noqa: DJ008
"""
.. no_pii:
"""
users = models.ManyToManyField(User, db_index=True)
name = models.CharField(blank=False, max_length=32, db_index=True)
description = models.TextField(blank=True)
class Registration(models.Model): # noqa: DJ008
"""
Allows us to wait for e-mail before user is registered. A
registration profile is created when the user creates an
account, but that account is inactive. Once the user clicks
on the activation key, it becomes active.
.. no_pii:
"""
class Meta:
db_table = "auth_registration"
user = models.OneToOneField(User, on_delete=models.CASCADE) # noqa: DJ012
activation_key = models.CharField(('activation key'), max_length=32, unique=True, db_index=True)
activation_timestamp = models.DateTimeField(default=None, null=True, blank=True)
def register(self, user):
# MINOR TODO: Switch to crypto-secure key
self.activation_key = uuid.uuid4().hex
self.user = user
self.save()
def activate(self): # lint-amnesty, pylint: disable=missing-function-docstring
self.user.is_active = True
self.user.save(update_fields=['is_active'])
self.activation_timestamp = datetime.utcnow()
self.save()
USER_ACCOUNT_ACTIVATED.send_robust(self.__class__, user=self.user)
log.info('User %s (%s) account is successfully activated.', self.user.username, self.user.email)
class PendingNameChange(DeletableByUserValue, models.Model): # noqa: DJ008
"""
This model keeps track of pending requested changes to a user's name.
.. pii: Contains new_name, retired in LMSAccountRetirementView
.. pii_types: name
.. pii_retirement: local_api
"""
user = models.OneToOneField(User, unique=True, db_index=True, on_delete=models.CASCADE)
new_name = models.CharField(blank=True, max_length=255)
rationale = models.CharField(blank=True, max_length=1024)
class PendingEmailChange(DeletableByUserValue, models.Model): # noqa: DJ008
"""
This model keeps track of pending requested changes to a user's email address.
.. pii: Contains new_email, redacted then deleted in AccountRetirementView
.. pii_types: email_address
.. pii_retirement: local_api
"""
user = models.OneToOneField(User, unique=True, db_index=True, on_delete=models.CASCADE)
new_email = models.CharField(blank=True, max_length=255, db_index=True)
activation_key = models.CharField(('activation key'), max_length=32, unique=True, db_index=True)
@classmethod
def delete_by_user_value(cls, value, field):
"""
Deletes instances of this model where ``field`` equals ``value``.
Automatically redacts new_email before deletion to ensure PII is cleared.
Uses bulk ORM update for efficiency.
Returns True if any instances were deleted.
Returns False otherwise.
"""
filter_kwargs = {field: value}
records_matching_user_value = cls.objects.filter(**filter_kwargs)
if not records_matching_user_value.exists():
return False
# Redact new_email before deletion using bulk update
records_matching_user_value.update(new_email='[email protected]')
records_matching_user_value.delete()
return True
def request_change(self, email):
"""Request a change to a user's email.
Implicitly saves the pending email change record.
Arguments:
email (unicode): The proposed new email for the user.
Returns:
unicode: The activation code to confirm the change.
"""
self.new_email = email
self.activation_key = uuid.uuid4().hex
self.save()
return self.activation_key
class PendingSecondaryEmailChange(DeletableByUserValue, models.Model): # noqa: DJ008
"""
This model keeps track of pending requested changes to a user's secondary email address.
.. pii: Contains new_secondary_email, not currently retired
.. pii_types: email_address
.. pii_retirement: retained
"""
user = models.OneToOneField(User, unique=True, db_index=True, on_delete=models.CASCADE)
new_secondary_email = models.CharField(blank=True, max_length=255, db_index=True)
activation_key = models.CharField(('activation key'), max_length=32, unique=True, db_index=True)
class LoginFailures(models.Model):
"""
This model will keep track of failed login attempts.
.. no_pii:
"""
user = models.ForeignKey(User, on_delete=models.CASCADE)
failure_count = models.IntegerField(default=0)
lockout_until = models.DateTimeField(null=True)
@classmethod
def _get_record_for_user(cls, user):
"""
Gets a user's record, and fixes any duplicates that may have arisen due to get_or_create
race conditions. See https://code.djangoproject.com/ticket/13906 for details.
Use this method in place of `LoginFailures.objects.get(user=user)`
"""
records = LoginFailures.objects.filter(user=user).order_by('-lockout_until')
for extra_record in records[1:]:
extra_record.delete()
return records.get()
@classmethod
def is_feature_enabled(cls):
"""
Returns whether the feature flag around this functionality has been set
"""
return settings.FEATURES['ENABLE_MAX_FAILED_LOGIN_ATTEMPTS']
@classmethod
def is_user_locked_out(cls, user):
"""