from django.contrib.auth.models import AbstractUser from django.db import models class CustomUser(AbstractUser): """Custom User model to store CAS attributes from PolyLAN.""" # Store CAS user ID (the numeric ID from CAS) cas_user_id = models.CharField(max_length=50, blank=True, null=True, unique=True) # Store CAS groups as JSON cas_groups = models.JSONField(default=list, blank=True) # Additional fields that might come from CAS cas_attributes = models.JSONField(default=dict, blank=True) def __str__(self): return f"{self.username} ({self.cas_user_id})" def has_cas_group(self, group_name): """Check if user has a specific CAS group.""" return group_name in self.cas_groups def get_cas_groups_display(self): """Get a comma-separated list of CAS groups for display.""" return ", ".join(self.cas_groups) if self.cas_groups else "No groups" def update_cas_data(self, cas_user_id, attributes): """Update user with CAS data.""" self.cas_user_id = cas_user_id # Update basic fields from CAS attributes if "firstname" in attributes: self.first_name = attributes["firstname"] if "lastname" in attributes: self.last_name = attributes["lastname"] if "email" in attributes: self.email = attributes["email"] # Store groups if "groups" in attributes: # CAS groups come as a list or single value groups = attributes["groups"] if isinstance(groups, str): self.cas_groups = [groups] elif isinstance(groups, list): self.cas_groups = groups else: self.cas_groups = [] if "RESPONSABLE_ANIMATION" in self.cas_groups: self.is_staff = True self.is_superuser = True # Store all other attributes self.cas_attributes = attributes self.save()