Permalink
Browse files

Authenticate users using crypt(3)

While I'm here add a custom manager for user model.
  • Loading branch information...
1 parent a736541 commit f44c11fb3abcf09cf8f536888f89396ab4091189 @william-gr william-gr committed Nov 1, 2013
Showing with 15 additions and 3 deletions.
  1. +15 −3 gui/account/models.py
View
@@ -24,6 +24,8 @@
# POSSIBILITY OF SUCH DAMAGE.
#
#####################################################################
+import crypt
+import pwd
from django.db import models
from django.utils.translation import ugettext_lazy as _
@@ -67,6 +69,11 @@ def get_sentinel_group():
return bsdGroups.objects.get(bsdgrp_group='nobody')
+class UserManager(models.Manager):
+ def get_by_natural_key(self, username):
+ return self.get(**{self.model.USERNAME_FIELD: username})
+
+
class bsdUsers(Model):
USERNAME_FIELD = 'bsdusr_username'
@@ -132,6 +139,7 @@ class bsdUsers(Model):
is_active = True
is_staff = True
+ objects = UserManager()
class Meta:
verbose_name = _("User")
@@ -140,9 +148,6 @@ class Meta:
def __unicode__(self):
return self.bsdusr_username
- def is_authenticated(self):
- return True
-
def get_username(self):
"Return the identifying username for this User"
return getattr(self, self.USERNAME_FIELD)
@@ -167,6 +172,13 @@ def is_authenticated(self):
"""
return True
+ def check_password(self, raw_password):
+ cryptedpasswd = pwd.getpwnam(self.get_username())[1]
+ if cryptedpasswd:
+ if cryptedpasswd == 'x' or cryptedpasswd == '*':
+ return False
+ return crypt.crypt(raw_password, cryptedpasswd) == cryptedpasswd
+
def delete(self, using=None, reload=True):
if self.bsdusr_builtin == True:
raise ValueError(_("User %s is built-in and can not be "

0 comments on commit f44c11f

Please sign in to comment.