Skip to content

Commit

Permalink
rhsso: collect name and email (#1281)
Browse files Browse the repository at this point in the history
  • Loading branch information
goneri authored Aug 29, 2024
1 parent 57e421e commit a1274bb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 4.2.15 on 2024-08-29 17:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("users", "0012_userplan_accept_marketing"),
]

operations = [
migrations.AddField(
model_name="user",
name="email_verified",
field=models.BooleanField(default=False, null=True),
),
migrations.AddField(
model_name="user",
name="family_name",
field=models.CharField(default=None, null=True),
),
migrations.AddField(
model_name="user",
name="given_name",
field=models.CharField(default=None, null=True),
),
migrations.AddField(
model_name="user",
name="name",
field=models.CharField(default=None, null=True),
),
migrations.AlterField(
model_name="user",
name="email",
field=models.CharField(default=None, null=True),
),
]
5 changes: 5 additions & 0 deletions ansible_ai_connect/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class User(ExportModelOperationsMixin("user"), AbstractUser):
rh_user_is_org_admin = models.BooleanField(default=False)
rh_employee = models.BooleanField(default=False)
external_username = models.CharField(default="", null=False)
name = models.CharField(default=None, null=True)
given_name = models.CharField(default=None, null=True)
family_name = models.CharField(default=None, null=True)
email = models.CharField(default=None, null=True)
email_verified = models.BooleanField(default=False, null=True)

def ams(self) -> dict[str, str]:
if not self.organization:
Expand Down
5 changes: 5 additions & 0 deletions ansible_ai_connect/users/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ def redhat_organization(backend, user, response, *args, **kwargs):
realm_access = payload.get("realm_access", {})
roles = realm_access.get("roles", [])
user.external_username = payload.get("preferred_username")
user.name = payload.get("name")
user.given_name = payload.get("given_name")
user.family_name = payload.get("family_name")
user.email = payload.get("email")
user.email_verified = payload.get("email_verified")
user.rh_user_is_org_admin = "admin:org:all" in roles
user.rh_employee = "redhat:employees" in roles

Expand Down
25 changes: 25 additions & 0 deletions ansible_ai_connect/users/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,28 @@ def test_rh_employee_field(self):
response=response,
)
self.assertEqual(answer["rh_employee"], True)

def test_rhoss_user_and_email(self):
response = {
"access_token": build_access_token(
self.rsa_private_key,
{
"family_name": "Drake",
"email": "francis.drake@example.foo",
"given_name": "Francis",
"name": "Francis Drake",
"organization": {"id": "345"},
"preferred_username": "fdrake01",
},
)
}

redhat_organization(
backend=DummyRHBackend(public_key=self.jwk_public_key),
user=self.rh_user,
response=response,
)
self.assertEqual(self.rh_user.family_name, "Drake")
self.assertEqual(self.rh_user.email, "francis.drake@example.foo")
self.assertEqual(self.rh_user.given_name, "Francis")
self.assertEqual(self.rh_user.name, "Francis Drake")

0 comments on commit a1274bb

Please sign in to comment.