Ticket #9444: anonuser.models.patch

File anonuser.models.patch, 5.6 KB (added by Digitalxero, 7 years ago)

django.contrib.auth.model Patch to include the new Special Anon user

  • \django\contrib\auth\models.py

    old new  
    147147        return self.username
    148148
    149149    def get_absolute_url(self):
     150        if self.username == 'BuiltinDjangoAnonymousUser':
     151            return '/'
     152           
    150153        return "/users/%s/" % urllib.quote(smart_str(self.username))
    151154
    152155    def is_anonymous(self):
    153         "Always returns False. This is a way of comparing User objects to anonymous users."
     156        "Return True if it is the Anonymous users, False if not"
     157        if self.username == 'BuiltinDjangoAnonymousUser':
     158            return True
    154159        return False
    155160
    156161    def is_authenticated(self):
    157         """Always return True. This is a way to tell if the user has been authenticated in templates.
     162        """Return True for loged in users and False of the Anon Users. This is a way to tell if the user has been authenticated in templates.
    158163        """
     164        if self.username == 'BuiltinDjangoAnonymousUser':
     165            return False
    159166        return True
    160167
    161168    def get_full_name(self):
    162         "Returns the first_name plus the last_name, with a space in between."
     169        "Returns the first_name plus the last_name, with a space in between."       
    163170        full_name = u'%s %s' % (self.first_name, self.last_name)
    164171        return full_name.strip()
    165172
    166173    def set_password(self, raw_password):
     174        """
     175        Set the password for the user, just return if it is the Anon User
     176        """
     177        if self.username == 'BuiltinDjangoAnonymousUser':
     178            return
    167179        import random
    168180        algo = 'sha1'
    169181        salt = get_hexdigest(algo, str(random.random()), str(random.random()))[:5]
     
    174186        """
    175187        Returns a boolean of whether the raw_password was correct. Handles
    176188        encryption formats behind the scenes.
     189        Always return False for the Anon user since people cannot login as that user
    177190        """
     191        if self.username == 'BuiltinDjangoAnonymousUser':
     192            return False
    178193        # Backwards-compatibility check. Older passwords won't include the
    179194        # algorithm or salt.
    180195        if '$' not in self.password:
     
    197212        """
    198213        Returns a list of permission strings that this user has through
    199214        his/her groups. This method queries all available auth backends.
     215        Not special casing the Anon user for groups or permissions because
     216        the whole point of actualy having the Anon user is so you can setup
     217        permissions for it
    200218        """
    201219        permissions = set()
    202220        for backend in auth.get_backends():
     
    217235        queries all available auth backends, but returns immediately if any
    218236        backend returns True. Thus, a user who has permission from a single
    219237        auth backend is assumed to have permission in general.
     238        Not special casing the Anon user for groups or permissions because
     239        the whole point of actualy having the Anon user is so you can setup
     240        permissions for it
    220241        """
    221242        # Inactive users have no permissions.
    222243        if not self.is_active:
     
    234255        return False
    235256
    236257    def has_perms(self, perm_list):
    237         """Returns True if the user has each of the specified permissions."""
     258        """
     259        Returns True if the user has each of the specified permissions.
     260        Not special casing the Anon user for groups or permissions because
     261        the whole point of actualy having the Anon user is so you can setup
     262        permissions for it
     263        """
    238264        for perm in perm_list:
    239265            if not self.has_perm(perm):
    240266                return False
     
    244270        """
    245271        Returns True if the user has any permissions in the given app
    246272        label. Uses pretty much the same logic as has_perm, above.
     273        Not special casing the Anon user for groups or permissions because
     274        the whole point of actualy having the Anon user is so you can setup
     275        permissions for it
    247276        """
    248277        if not self.is_active:
    249278            return False
     
    265294        return messages
    266295
    267296    def email_user(self, subject, message, from_email=None):
    268         "Sends an e-mail to this User."
     297        """
     298        Sends an e-mail to this User.
     299        Dont try to sent to the Anon User
     300        """
     301        if self.username == 'BuiltinDjangoAnonymousUser':
     302            return
    269303        from django.core.mail import send_mail
    270304        send_mail(subject, message, from_email, [self.email])
    271305
     
    273307        """
    274308        Returns site-specific profile for this user. Raises
    275309        SiteProfileNotAvailable if this site does not allow profiles.
     310        Anon cannot have a profile either
    276311        """
     312        if self.username == 'BuiltinDjangoAnonymousUser':
     313            raise SiteProfileNotAvailable
     314           
    277315        if not hasattr(self, '_profile_cache'):
    278316            from django.conf import settings
    279317            if not getattr(settings, 'AUTH_PROFILE_MODULE', False):
     
    302340    def __unicode__(self):
    303341        return self.message
    304342
    305 class AnonymousUser(object):
     343class AnonymousUserClass(object):
    306344    id = None
    307345    username = ''
    308346    is_staff = False
     
    366404
    367405    def is_authenticated(self):
    368406        return False
     407       
     408def AnonymousUser():
     409    try:
     410        return User.objects.get(username__exact='BuiltinDjangoAnonymousUser')
     411    except:
     412        return AnonymousUserClass()
Back to Top