Skip to content

Commit 58269b6

Browse files
committed
Point Django to NAV's Account model for users
This changes NAV's Account model to inherit from Django's AbstractBaseUser in order to move towards more compatibility with Django libraries for authentication and authorization.
1 parent 237e1b6 commit 58269b6

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

python/nav/django/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@
230230
)
231231

232232
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
233+
AUTH_USER_MODEL = "nav.models.Account"
233234

234235
REST_FRAMEWORK = {
235236
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),

python/nav/models/profiles.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import re
2727
import json
2828

29+
from django.contrib.auth.base_user import AbstractBaseUser
2930
from django.contrib.postgres.fields import HStoreField
3031
from django.db import models, transaction
3132
from django.forms.models import model_to_dict
@@ -83,9 +84,10 @@
8384
### Account models
8485

8586

86-
class Account(models.Model):
87+
class Account(AbstractBaseUser):
8788
"""NAV's basic account model"""
8889

90+
USERNAME_FIELD = 'login'
8991
DEFAULT_ACCOUNT = 0
9092
ADMIN_ACCOUNT = 1
9193

@@ -199,6 +201,14 @@ def is_admin(self):
199201
"""Has this user administrator rights?"""
200202
return self.has_perm(None, None)
201203

204+
@property
205+
def is_anonymous(self):
206+
return self.id == self.DEFAULT_ACCOUNT
207+
208+
@property
209+
def is_authenticated(self):
210+
return self.id != self.DEFAULT_ACCOUNT
211+
202212
@sensitive_variables('password')
203213
def set_password(self, password):
204214
"""Sets user password. Copied from nav.db.navprofiles"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- Add missing field required when switching to Django's AbstractBaseUser
2+
ALTER TABLE account
3+
ADD COLUMN last_login TIMESTAMP WITH TIME ZONE;

0 commit comments

Comments
 (0)