Internal
Public Access
Implement comprehensive email verification and admin approval system for user registration. Users must verify their email before logging in, and admins must approve new users before they can access the system. Major features: - Email verification with UUID tokens (24hr expiry, one-time use) - Admin approval workflow via Django admin interface - Custom authentication backend enforcing verification and approval - Password change functionality for authenticated users - Enhanced profile page with proper styling and theme support - Collect first name, last name, and timezone during registration - Profile link added to header navigation Email notifications: - Verification email sent after registration - Admin notification when users need approval - Approval notification sent to users Security features: - Cryptographically secure UUID tokens - Token expiration and one-time use enforcement - Email enumeration protection - Custom JWT token validation for API access - Existing users auto-approved via data migration Templates added: - Email templates (verification, approval, admin notification) - Web templates (password change, verification pages) - Enhanced profile page with dark/light mode support 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
111 lines
3.4 KiB
Python
111 lines
3.4 KiB
Python
"""
|
|
User models for KeepItGoing.
|
|
"""
|
|
|
|
import uuid
|
|
from django.contrib.auth.models import AbstractUser
|
|
from django.db import models
|
|
|
|
|
|
class User(AbstractUser):
|
|
"""
|
|
Custom user model for KeepItGoing.
|
|
|
|
Uses email as the primary identifier instead of username.
|
|
"""
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
email = models.EmailField(unique=True)
|
|
|
|
# Profile fields
|
|
timezone = models.CharField(max_length=50, default='UTC')
|
|
avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)
|
|
|
|
# Preferences
|
|
default_reminder_minutes = models.IntegerField(default=30)
|
|
email_notifications = models.BooleanField(default=True)
|
|
push_notifications = models.BooleanField(default=True)
|
|
|
|
# Email verification
|
|
email_verified = models.BooleanField(default=False)
|
|
email_verified_at = models.DateTimeField(null=True, blank=True)
|
|
|
|
# Admin approval
|
|
is_approved = models.BooleanField(default=False)
|
|
approved_by = models.ForeignKey(
|
|
'self',
|
|
on_delete=models.SET_NULL,
|
|
null=True,
|
|
blank=True,
|
|
related_name='approved_users',
|
|
limit_choices_to={'is_staff': True}
|
|
)
|
|
approved_at = models.DateTimeField(null=True, blank=True)
|
|
|
|
# Timestamps
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
# Use email as the username field
|
|
USERNAME_FIELD = 'email'
|
|
REQUIRED_FIELDS = ['username']
|
|
|
|
class Meta:
|
|
db_table = 'users'
|
|
verbose_name = 'user'
|
|
verbose_name_plural = 'users'
|
|
|
|
def __str__(self):
|
|
return self.email
|
|
|
|
|
|
class DeviceToken(models.Model):
|
|
"""
|
|
Stores push notification tokens for user devices.
|
|
"""
|
|
PLATFORM_CHOICES = [
|
|
('android', 'Android'),
|
|
('web', 'Web'),
|
|
('desktop', 'Desktop'),
|
|
]
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='device_tokens')
|
|
platform = models.CharField(max_length=20, choices=PLATFORM_CHOICES)
|
|
token = models.TextField()
|
|
device_name = models.CharField(max_length=255, blank=True)
|
|
is_active = models.BooleanField(default=True)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
class Meta:
|
|
db_table = 'device_tokens'
|
|
unique_together = ['user', 'token']
|
|
|
|
def __str__(self):
|
|
return f"{self.user.email} - {self.platform} - {self.device_name}"
|
|
|
|
|
|
class EmailVerificationToken(models.Model):
|
|
"""
|
|
Token for email verification.
|
|
Uses UUID tokens with expiration for security.
|
|
"""
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='verification_tokens')
|
|
token = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
expires_at = models.DateTimeField()
|
|
is_used = models.BooleanField(default=False)
|
|
used_at = models.DateTimeField(null=True, blank=True)
|
|
|
|
class Meta:
|
|
db_table = 'email_verification_tokens'
|
|
ordering = ['-created_at']
|
|
|
|
def is_expired(self):
|
|
from django.utils import timezone
|
|
return timezone.now() > self.expires_at
|
|
|
|
def __str__(self):
|
|
return f"Verification token for {self.user.email}"
|