Django

Code

Changeset 5771

Show
Ignore:
Timestamp:
07/28/07 13:30:40 (1 year ago)
Author:
simon
Message:

After discussing with Malcolm, added set_unusable_password() and has_usable_password() methods to the User object, plus tests and updated documentation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/auth/models.py

    r5609 r5771  
    77import datetime 
    88import urllib 
     9 
     10UNUSABLE_PASSWORD = '!' # This will never be a valid hash 
    911 
    1012try: 
     
    8486 
    8587class UserManager(models.Manager): 
    86     def create_user(self, username, email, password): 
     88    def create_user(self, username, email, password=None): 
    8789        "Creates and saves a User with the given username, e-mail and password." 
    8890        now = datetime.datetime.now() 
    8991        user = self.model(None, username, '', '', email.strip().lower(), 'placeholder', False, True, False, now, now) 
    90         user.set_password(password) 
     92        if password: 
     93            user.set_password(password) 
     94        else: 
     95            user.set_unusable_password() 
    9196        user.save() 
    9297        return user 
     
    179184            return is_correct 
    180185        return check_password(raw_password, self.password) 
     186 
     187    def set_unusable_password(self): 
     188        # Sets a value that will never be a valid hash 
     189        self.password = UNUSABLE_PASSWORD 
     190 
     191    def has_usable_password(self): 
     192        return self.password != UNUSABLE_PASSWORD 
    181193 
    182194    def get_group_permissions(self): 
     
    269281 
    270282class Message(models.Model): 
    271     """The message system is a lightweight way to queue messages for given users. A message is associated with a User instance (so it is only applicable for registered users). There's no concept of expiration or timestamps. Messages are created by the Django admin after successful actions. For example, "The poll Foo was created successfully." is a message. 
     283    """ 
     284    The message system is a lightweight way to queue messages for given users. A message is associated with a User instance (so it is only applicable for registered users). There's no concept of expiration or timestamps. Messages are created by the Django admin after successful actions. For example, "The poll Foo was created successfully." is a message. 
    272285    """ 
    273286    user = models.ForeignKey(User) 
  • django/trunk/docs/authentication.txt

    r5571 r5771  
    115115      password hashing in making the comparison.) 
    116116 
     117    * ``set_unusable_password()`` -- Marks the user as having no password set.  
     118      This isn't the same as having a blank string for a password. 
     119      ``check_password()`` for this user will never return ``True``. Doesn't  
     120      save the ``User`` object. 
     121       
     122      You may need this if authentication for your application takes place   
     123      against an existing external source such as an LDAP directory. 
     124 
     125    * ``has_usable_password()`` -- Returns ``False`` if  
     126      ``set_unusable_password()`` has been called for this user. 
     127 
    117128    * ``get_group_permissions()`` -- Returns a list of permission strings that 
    118129      the user has, through his/her groups. 
     
    153164The ``User`` model has a custom manager that has the following helper functions: 
    154165 
    155     * ``create_user(username, email, password)`` -- Creates, saves and returns 
    156       a ``User``. The ``username``, ``email`` and ``password`` are set as 
    157       given, and the ``User`` gets ``is_active=True``. 
     166    * ``create_user(username, email, password=None)`` -- Creates, saves and  
     167      returns a ``User``. The ``username``, ``email`` and ``password`` are set 
     168      as given, and the ``User`` gets ``is_active=True``. 
     169       
     170      If no password is provided, ``set_unusable_password()`` will be called. 
    158171 
    159172      See _`Creating users` for example usage.