Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bug uids should be filled from upstream ldap (#154) #175

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ celery:
celery -A config.celery_app worker -l info --beat


.PHONY: lack
.PHONY: black
black:
black . -l 100 --exclude '/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.?v?env|_build|buck-out|build|dist|src)/' $(arg)

Expand All @@ -36,3 +36,11 @@ test-snap:
isort:
isort --force-sort-within-sections --profile=black .


.PHONY: flake8
flake8:
flake8

.PHONY: format
format: isort black flake8

4 changes: 4 additions & 0 deletions hpcaccess/auth_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ def _ldap_auth_handler(user, ldap_user, **kwargs):
"""Signal for LDAP login handling"""

phone = ldap_user.attrs.get("telephoneNumber")
uid = ldap_user.attrs.get("uidNumber")

if phone:
user.phone = phone[0]

if uid:
user.uid = uid[0]

if hasattr(user, "ldap_username"):
# Make domain in username uppercase
if user.username.find("@") != -1 and user.username.split("@")[1].islower():
Expand Down
18 changes: 18 additions & 0 deletions hpcaccess/users/migrations/0004_user_uid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.10 on 2024-02-14 15:07

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("users", "0003_user_phone"),
]

operations = [
migrations.AddField(
model_name="user",
name="uid",
field=models.IntegerField(blank=True, null=True, verbose_name="UID of User"),
),
]
3 changes: 2 additions & 1 deletion hpcaccess/users/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib.auth.models import AbstractUser
from django.db.models import BooleanField, CharField
from django.db.models import BooleanField, CharField, IntegerField
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

Expand All @@ -21,6 +21,7 @@ class User(AbstractUser):
help_text=_("Designates whether the user is an HPC admin."),
)
phone = CharField(_("Phone number of User"), blank=True, max_length=32)
uid = IntegerField(_("UID of User"), blank=True, null=True)

def get_absolute_url(self):
"""Get url for user's detail view.
Expand Down
1 change: 1 addition & 0 deletions hpcaccess/users/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class UserFactory(DjangoModelFactory):
email = Faker("email")
name = Faker("name")
phone = Faker("phone_number")
uid = Faker("random_int", min=1000, max=9999)

@post_generation
def password(self, create: bool, extracted: Sequence[Any], **kwargs):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.10 on 2024-02-14 15:07

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("usersec", "0016_alter_hpcproject_group_alter_hpcprojectversion_group"),
]

operations = [
migrations.RemoveField(
model_name="hpcuser",
name="uid",
),
migrations.RemoveField(
model_name="hpcuserversion",
name="uid",
),
]
5 changes: 1 addition & 4 deletions usersec/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,6 @@ class Meta:
help_text="Additional information about the user",
)

#: POSIX id of the user on the cluster.
uid = models.IntegerField(null=True, help_text="Id of the user on the cluster")

#: POSIX username on the cluster.
username = models.CharField(max_length=32, help_text="Username of the user on the cluster")

Expand All @@ -353,7 +350,7 @@ def __repr__(self):
self.id,
self.user.username if self.user else None,
self.username,
self.uid,
self.user.uid if self.user else None,
self.primary_group.name,
self.status,
self.creator.username if self.creator else None,
Expand Down
5 changes: 4 additions & 1 deletion usersec/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class HpcUserAbstractSerializer(HpcObjectAbstractSerializer):
resources_used = serializers.JSONField()
status = serializers.CharField(read_only=True)
description = serializers.CharField(read_only=True)
uid = serializers.IntegerField()
uid = serializers.SerializerMethodField()
username = serializers.CharField(read_only=True)
expiration = serializers.DateTimeField(read_only=True)
full_name = serializers.SerializerMethodField()
Expand All @@ -48,6 +48,9 @@ def get_first_name(self, obj) -> Optional[str]:
def get_phone_number(self, obj) -> Optional[str]:
return obj.user.phone

def get_uid(self, obj) -> Optional[int]:
return obj.user.uid

class Meta:
fields = HpcObjectAbstractSerializer.Meta.fields + [
"full_name",
Expand Down
1 change: 0 additions & 1 deletion usersec/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ class Meta:
resources_used = {"null": "null"}
creator = factory.SubFactory(UserFactory) # User
description = "this is a user"
uid = 2000
username = factory.Sequence(lambda n: f"user{n}_" + settings.INSTITUTE_USERNAME_SUFFIX)
expiration = datetime(2050, 1, 1, tzinfo=utc)

Expand Down
1 change: 1 addition & 0 deletions usersec/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def testSerializeExisting(self):
result["primary_group"] = "primary_group_uuid_placeholder"
result["phone_number"] = "phone_number_placeholder"
result["full_name"] = "name_placeholder"
result["uid"] = 2000
self.assertMatchSnapshot(result)


Expand Down
Loading