Skip to content

Commit

Permalink
feat: obtain first_name and last_name from upstream ldap (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
stolpeo committed Feb 15, 2024
1 parent 888af29 commit 22d5540
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 13 deletions.
22 changes: 20 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ test-keepdb:
ENABLE_LDAP=0 ENABLE_LDAP_SECONDARY=0 $(manage) test -v2 --settings=config.settings.test --keepdb


.PHONY: _test-snap
_test-snap:
ENABLE_LDAP=0 ENABLE_LDAP_SECONDARY=0 $(manage) test -v2 --settings=config.settings.test --keepdb --snapshot-update usersec.tests.test_serializers adminsec.tests.test_views_api


.PHONY: test-snap
test-snap:
ENABLE_LDAP=0 ENABLE_LDAP_SECONDARY=0 $(manage) test -v2 --settings=config.settings.test --keepdb usersec.tests.test_serializers adminsec.tests.test_views_api
test-snap: _test-snap format


.PHONY: isort
Expand All @@ -41,6 +45,20 @@ isort:
flake8:
flake8


.PHONY: format
format: isort black flake8


.PHONY: migrations
migrations:
$(manage) makemigrations


.PHONY: _migrate
_migrate: migrations
$(manage) migrate


.PHONY: migrate
migrate: _migrate format
16 changes: 11 additions & 5 deletions hpcaccess/auth_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,31 @@ def _ldap_auth_handler(user, ldap_user, **kwargs):

phone = ldap_user.attrs.get("telephoneNumber")
uid = ldap_user.attrs.get("uidNumber")
first_name = ldap_user.attrs.get("givenName")
last_name = ldap_user.attrs.get("sn")

if phone:
user.phone = phone[0]

if uid:
user.uid = uid[0]

if first_name:
user.first_name = first_name[0]

if last_name:
user.last_name = last_name[0]

if hasattr(user, "ldap_username"):
# Make domain in username uppercase
if user.username.find("@") != -1 and user.username.split("@")[1].islower():
u_split = user.username.split("@")
user.username = u_split[0] + "@" + u_split[1].upper()
user.save()

# Save user name from first_name and last_name into name
if user.name in ["", None]:
if user.first_name != "":
user.name = user.first_name + (" " + user.last_name if user.last_name != "" else "")
user.save()
# Save user name from first_name and last_name into name
user.name = " ".join([user.first_name, user.last_name])
user.save()


@receiver(populate_user, sender=PrimaryLDAPBackend)
Expand Down
4 changes: 2 additions & 2 deletions hpcaccess/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class UserAdmin(auth_admin.UserAdmin):
form = UserAdminChangeForm
add_form = UserAdminCreationForm
fieldsets = (
(None, {"fields": ("username", "password")}),
(_("Personal info"), {"fields": ("name", "email")}),
(None, {"fields": ("username", "password", "uid")}),
(_("Personal info"), {"fields": ("name", "first_name", "last_name", "email", "phone")}),
(
_("Permissions"),
{
Expand Down
23 changes: 23 additions & 0 deletions hpcaccess/users/migrations/0005_user_first_name_user_last_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.10 on 2024-02-15 10:15

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("users", "0004_user_uid"),
]

operations = [
migrations.AddField(
model_name="user",
name="first_name",
field=models.CharField(blank=True, max_length=30, verbose_name="First Name of User"),
),
migrations.AddField(
model_name="user",
name="last_name",
field=models.CharField(blank=True, max_length=30, verbose_name="Last Name of User"),
),
]
4 changes: 2 additions & 2 deletions hpcaccess/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class User(AbstractUser):

#: First and last name do not cover name patterns around the globe
name = CharField(_("Name of User"), blank=True, max_length=255)
first_name = None # type: ignore
last_name = None # type: ignore
first_name = CharField(_("First Name of User"), blank=True, max_length=30)
last_name = CharField(_("Last Name of User"), blank=True, max_length=30)
is_hpcadmin = BooleanField(
_("HPC admin status"),
default=False,
Expand Down
4 changes: 2 additions & 2 deletions usersec/tests/snapshots/snap_test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
"date_created": "2019-01-01T00:00:00Z",
"description": "this is a user",
"expiration": "2050-01-01T00:00:00Z",
"first_name": None,
"first_name": "first_name_placeholder",
"full_name": "name_placeholder",
"home_directory": "/data/cephfs-1/home/users/user0_c",
"last_name": None,
"last_name": "last_name_placeholder",
"login_shell": "/usr/bin/bash",
"phone_number": "phone_number_placeholder",
"primary_group": "primary_group_uuid_placeholder",
Expand Down
2 changes: 2 additions & 0 deletions usersec/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def testSerializeExisting(self):
result["primary_group"] = "primary_group_uuid_placeholder"
result["phone_number"] = "phone_number_placeholder"
result["full_name"] = "name_placeholder"
result["first_name"] = "first_name_placeholder"
result["last_name"] = "last_name_placeholder"
result["uid"] = 2000
self.assertMatchSnapshot(result)

Expand Down

0 comments on commit 22d5540

Please sign in to comment.