Skip to content

Commit

Permalink
Set name from FEIDE
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikhorluck committed Aug 3, 2024
1 parent 0755e56 commit 283d1c1
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 20 deletions.
1 change: 1 addition & 0 deletions apps/authentication/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class OnlineUser(AbstractUser):
)
bio = models.TextField(_("bio"), max_length=2048, blank=True)
# NTNU credentials
# Only set if the user signs in with FEIDE, or by admins
ntnu_username = models.CharField(
_("NTNU-brukernavn"), max_length=50, blank=True, null=True, unique=True
)
Expand Down
5 changes: 0 additions & 5 deletions apps/dataporten/study/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,3 @@ def find_user_study_and_update(user, groups):
membership.save()

return True, study_name, study_year


def set_ntnu_username(user, username):
user.ntnu_username = username
user.save()
8 changes: 0 additions & 8 deletions apps/dataporten/study/tests/task_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from apps.dataporten.study.tasks import (
fetch_groups_information,
find_user_study_and_update,
set_ntnu_username,
)

from .course_test_data import (
Expand Down Expand Up @@ -74,10 +73,3 @@ def test_find_user_study_and_update_5th_grader(self):
self.assertTrue(application.processed)

self.assertEqual(len(mail.outbox), 0)


class UserUpdatingTestCase(TestCase):
def test_set_ntnu_username(self):
user = G(OnlineUser)
set_ntnu_username(user, "testname")
self.assertEqual(user.ntnu_username, "testname")
19 changes: 16 additions & 3 deletions apps/dataporten/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from urllib.parse import urlparse

import requests
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import login_required
Expand All @@ -9,10 +10,10 @@
from oic import rndstr
from oic.oauth2 import AuthorizationResponse, ResponseError

from apps.authentication.models import OnlineUser
from apps.dataporten.study.tasks import (
fetch_groups_information,
find_user_study_and_update,
set_ntnu_username,
)

from .client import client_setup
Expand Down Expand Up @@ -146,12 +147,24 @@ def study_callback(request): # noqa: C901
pass
# @ToDo: Register email address. Maybe store it, but ask user to confirm? -> resend auth email

extended_userinfo = requests.get(
"https://api.dataporten.no/userinfo/v1/userinfo",
headers={"Authorization": f"Bearer {access_token}"},
).json()

given_name = extended_userinfo["givenName"][0]
family_name = extended_userinfo["sn"][0]

user: OnlineUser = request.user
user.ntnu_username = ntnu_username_dataporten
user.first_name = given_name
user.last_name = family_name
user.save()

# Getting information about study of the user
groups = fetch_groups_information(access_token)

try:
if not request.user.ntnu_username:
set_ntnu_username(request.user, ntnu_username_dataporten)
studies_info = find_user_study_and_update(request.user, groups)

if not studies_info:
Expand Down
10 changes: 9 additions & 1 deletion onlineweb4/settings/dataporten.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
"CLIENT_SECRET": config("OW4_DP_STUDY_CLIENT_SECRET", default=""),
"REDIRECT_URI": config("OW4_DP_STUDY_REDIRECT_URI", default=""),
"PROVIDER_URL": "https://auth.dataporten.no/oauth/token",
"SCOPES": ["openid", "userid-feide", "profile", "groups", "email"],
# userid-name gives access to extended userinfo https://docs.feide.no/reference/apis/extended_userinfo.html#example
"SCOPES": [
"openid",
"userid-feide",
"userid-name",
"profile",
"groups",
"email",
],
}
}
9 changes: 8 additions & 1 deletion onlineweb4/settings/zappa.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@
"CLIENT_SECRET": env["DP_STUDY_CLIENT_SECRET"],
"REDIRECT_URI": config("OW4_DP_STUDY_REDIRECT_URI", default=""),
"PROVIDER_URL": "https://auth.dataporten.no/oauth/token",
"SCOPES": ["openid", "userid-feide", "profile", "groups", "email"],
"SCOPES": [
"openid",
"userid-feide",
"userid-name",
"profile",
"groups",
"email",
],
}
}

Expand Down
4 changes: 2 additions & 2 deletions templates/profiles/membership.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ <h3>Medlemskap</h3>
</div>
</div>
<div class="manual-membership-form">
{% if user.ntnu_username %}
{% if user.ntnu_username %} <!-- they have or have had a membership at somepoint, ntnu_username is *only* set after an interaction with FEIDE -->
{% if user.has_expiring_membership %}
<hr />
<div class="row">
Expand Down Expand Up @@ -99,7 +99,7 @@ <h3>Medlemskap</h3>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-4">
<p>For å aktivere ditt medlemskap kreves det at du har knyttet din studentkonto til din profil.</p>
<p>Gå til Epost-innstillinger og registrer din @stud.ntnu.no epost.</p>
<p>Dette kan gjøres ved å søke om automatisk medlemskap, uavhengig av om du får innvilget det automatisk.</p>
</div>
</div>
{% endif %}
Expand Down

0 comments on commit 283d1c1

Please sign in to comment.