From 5b771da972693ac04eb544c3bd070272d13761e8 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 27 Feb 2019 15:09:05 +0100 Subject: [PATCH] switch from first_name and last_name to name --- .../templates/passbook-configmap.yaml | 7 ++-- passbook/admin/api/v1/users.py | 2 +- .../templates/administration/user/list.html | 6 +-- passbook/api/v1/openid.py | 4 +- passbook/core/forms/authentication.py | 6 +-- passbook/core/forms/users.py | 5 ++- .../migrations/0016_auto_20190227_1355.py | 38 +++++++++++++++++++ passbook/core/models.py | 5 ++- .../core/tests/test_views_authentication.py | 3 +- passbook/core/views/authentication.py | 3 +- passbook/core/views/user.py | 7 +++- passbook/ldap/ldap_connector.py | 6 +-- passbook/lib/default.yml | 7 ++-- passbook/oauth_client/source_types/discord.py | 2 +- .../oauth_client/source_types/facebook.py | 2 +- passbook/oauth_client/source_types/github.py | 2 +- passbook/oauth_client/source_types/google.py | 2 +- passbook/oauth_client/source_types/reddit.py | 2 +- .../oauth_client/source_types/supervisr.py | 2 +- passbook/oauth_client/source_types/twitter.py | 2 +- passbook/pretend/views/github.py | 2 +- passbook/saml_idp/base.py | 2 +- 22 files changed, 78 insertions(+), 39 deletions(-) create mode 100644 passbook/core/migrations/0016_auto_20190227_1355.py diff --git a/helm/passbook/templates/passbook-configmap.yaml b/helm/passbook/templates/passbook-configmap.yaml index 6f90fe90a..a9e0034c3 100644 --- a/helm/passbook/templates/passbook-configmap.yaml +++ b/helm/passbook/templates/passbook-configmap.yaml @@ -105,10 +105,9 @@ data: email: mail # or userPrincipalName user_attribute_map: active_directory: - sAMAccountName: username - mail: email - given_name: first_name - name: last_name + username: "%(sAMAccountName)s" + email: "%(mail)s" + name: "%(displayName)" # # Create new users in LDAP upon sign-up # create_users: true # # Reset LDAP password when user reset their password diff --git a/passbook/admin/api/v1/users.py b/passbook/admin/api/v1/users.py index d79d9b6d7..c1e8236c0 100644 --- a/passbook/admin/api/v1/users.py +++ b/passbook/admin/api/v1/users.py @@ -11,7 +11,7 @@ class UserSerializer(ModelSerializer): class Meta: model = User - fields = ['is_superuser', 'username', 'first_name', 'last_name', 'email', 'date_joined', + fields = ['is_superuser', 'username', 'name', 'email', 'date_joined', 'uuid'] diff --git a/passbook/admin/templates/administration/user/list.html b/passbook/admin/templates/administration/user/list.html index f198d56a0..fe641038c 100644 --- a/passbook/admin/templates/administration/user/list.html +++ b/passbook/admin/templates/administration/user/list.html @@ -11,8 +11,7 @@ {% trans 'Username' %} - {% trans 'First Name' %} - {% trans 'Last Name' %} + {% trans 'Name' %} {% trans 'Active' %} {% trans 'Last Login' %} @@ -22,8 +21,7 @@ {% for user in object_list %} {{ user.username }} - {{ user.first_name|default:'-' }} - {{ user.last_name|default:'-' }} + {{ user.name|default:'-' }} {{ user.is_active }} {{ user.last_login }} diff --git a/passbook/api/v1/openid.py b/passbook/api/v1/openid.py index 59a26d9a4..82f683960 100644 --- a/passbook/api/v1/openid.py +++ b/passbook/api/v1/openid.py @@ -14,8 +14,8 @@ class OpenIDUserInfoView(ScopedResourceMixin, View): payload = { 'sub': request.user.uuid.int, 'name': request.user.get_full_name(), - 'given_name': request.user.first_name, - 'family_name': request.user.last_name, + 'given_name': request.user.name, + 'family_name': '', 'preferred_username': request.user.username, 'email': request.user.email, } diff --git a/passbook/core/forms/authentication.py b/passbook/core/forms/authentication.py index 84b6335a4..525c89671 100644 --- a/passbook/core/forms/authentication.py +++ b/passbook/core/forms/authentication.py @@ -38,10 +38,8 @@ class SignUpForm(forms.Form): """SignUp Form""" title = _('Sign Up') - first_name = forms.CharField(label=_('First Name'), - widget=forms.TextInput(attrs={'placeholder': _('First Name')})) - last_name = forms.CharField(label=_('Last Name'), - widget=forms.TextInput(attrs={'placeholder': _('Last Name')})) + name = forms.CharField(label=_('Name'), + widget=forms.TextInput(attrs={'placeholder': _('Name')})) username = forms.CharField(label=_('Username'), widget=forms.TextInput(attrs={'placeholder': _('Username')})) email = forms.EmailField(label=_('E-Mail'), diff --git a/passbook/core/forms/users.py b/passbook/core/forms/users.py index ad437d149..fb3fa92e4 100644 --- a/passbook/core/forms/users.py +++ b/passbook/core/forms/users.py @@ -13,7 +13,10 @@ class UserDetailForm(forms.ModelForm): class Meta: model = User - fields = ['username', 'first_name', 'last_name', 'email'] + fields = ['username', 'name', 'email'] + widgets = { + 'name': forms.TextInput + } class PasswordChangeForm(forms.Form): """Form to update password""" diff --git a/passbook/core/migrations/0016_auto_20190227_1355.py b/passbook/core/migrations/0016_auto_20190227_1355.py new file mode 100644 index 000000000..8a6a801b9 --- /dev/null +++ b/passbook/core/migrations/0016_auto_20190227_1355.py @@ -0,0 +1,38 @@ +# Generated by Django 2.1.7 on 2019-02-27 13:55 + +from django.db import migrations, models + + +def migrate_names(apps, schema_editor): + """migrate first_name and last_name to name""" + User = apps.get_model("passbook_core", "User") + for user in User.objects.all(): + user.name = '%s %s' % (user.first_name, user.last_name) + user.save() + +class Migration(migrations.Migration): + + dependencies = [ + ('passbook_core', '0015_passwordpolicy_error_message'), + ] + + operations = [ + migrations.AddField( + model_name='user', + name='name', + field=models.TextField(default=''), + preserve_default=False, + ), + migrations.RunPython(migrate_names), + migrations.AlterField( + model_name='user', + name='name', + field=models.TextField(), + preserve_default=False, + ), + migrations.AlterField( + model_name='fieldmatcherpolicy', + name='user_field', + field=models.TextField(choices=[('username', 'Username'), ('name', 'Name'), ('email', 'E-Mail'), ('is_staff', 'Is staff'), ('is_active', 'Is active'), ('data_joined', 'Date joined')]), + ), + ] diff --git a/passbook/core/models.py b/passbook/core/models.py index a8bf4b5e9..d7f2a9c70 100644 --- a/passbook/core/models.py +++ b/passbook/core/models.py @@ -44,6 +44,8 @@ class User(AbstractUser): """Custom User model to allow easier adding o f user-based settings""" uuid = models.UUIDField(default=uuid4, editable=False) + name = models.TextField() + sources = models.ManyToManyField('Source', through='UserSourceConnection') applications = models.ManyToManyField('Application') groups = models.ManyToManyField('Group') @@ -253,8 +255,7 @@ class FieldMatcherPolicy(Policy): USER_FIELDS = ( ('username', _('Username'),), - ('first_name', _('First Name'),), - ('last_name', _('Last Name'),), + ('name', _('Name'),), ('email', _('E-Mail'),), ('is_staff', _('Is staff'),), ('is_active', _('Is active'),), diff --git a/passbook/core/tests/test_views_authentication.py b/passbook/core/tests/test_views_authentication.py index abd95170e..994ea0e44 100644 --- a/passbook/core/tests/test_views_authentication.py +++ b/passbook/core/tests/test_views_authentication.py @@ -15,8 +15,7 @@ class TestAuthenticationViews(TestCase): def setUp(self): super().setUp() self.sign_up_data = { - 'first_name': 'Test', - 'last_name': 'User', + 'name': 'Test', 'username': 'beryjuorg', 'email': 'unittest@passbook.beryju.org', 'password': 'B3ryju0rg!', diff --git a/passbook/core/views/authentication.py b/passbook/core/views/authentication.py index 3d24dc5d7..32373d701 100644 --- a/passbook/core/views/authentication.py +++ b/passbook/core/views/authentication.py @@ -204,8 +204,7 @@ class SignUpView(UserPassesTestMixin, FormView): new_user = User.objects.create( username=data.get('username'), email=data.get('email'), - first_name=data.get('first_name'), - last_name=data.get('last_name'), + name=data.get('name'), ) new_user.is_active = True try: diff --git a/passbook/core/views/user.py b/passbook/core/views/user.py index 71479c008..ec69c795a 100644 --- a/passbook/core/views/user.py +++ b/passbook/core/views/user.py @@ -1,8 +1,10 @@ """passbook core user views""" from django.contrib import messages from django.contrib.auth import logout, update_session_auth_hash +from django.contrib.messages.views import SuccessMessageMixin from django.forms.utils import ErrorList from django.shortcuts import redirect, reverse +from django.urls import reverse_lazy from django.utils.translation import gettext as _ from django.views.generic import DeleteView, FormView, UpdateView @@ -11,12 +13,15 @@ from passbook.core.forms.users import PasswordChangeForm, UserDetailForm from passbook.lib.config import CONFIG -class UserSettingsView(UpdateView): +class UserSettingsView(SuccessMessageMixin, UpdateView): """Update User settings""" template_name = 'user/settings.html' form_class = UserDetailForm + success_message = _('Successfully updated user.') + success_url = reverse_lazy('passbook_core:user-settings') + def get_object(self): return self.request.user diff --git a/passbook/ldap/ldap_connector.py b/passbook/ldap/ldap_connector.py index c76f1ba39..87d0f038e 100644 --- a/passbook/ldap/ldap_connector.py +++ b/passbook/ldap/ldap_connector.py @@ -129,7 +129,7 @@ class LDAPConnector: # Create the user data. field_map = { 'username': '%(' + USERNAME_FIELD + ')s', - 'first_name': '%(givenName)s %(sn)s', + 'name': '%(givenName)s %(sn)s', 'email': '%(mail)s', } user_fields = {} @@ -224,9 +224,9 @@ class LDAPConnector: 'cn': str(username), 'description': str('t=' + time()), 'sAMAccountName': str(username_trunk), - 'givenName': str(user.first_name), + 'givenName': str(user.name), 'displayName': str(user.username), - 'name': str(user.first_name), + 'name': str(user.name), 'mail': str(user.email), 'userPrincipalName': str(username + '@' + self._source.domain), 'objectClass': ['top', 'person', 'organizationalPerson', 'user'], diff --git a/passbook/lib/default.yml b/passbook/lib/default.yml index cf2cb833b..ce1a05797 100644 --- a/passbook/lib/default.yml +++ b/passbook/lib/default.yml @@ -77,10 +77,9 @@ ldap: email: mail # or userPrincipalName user_attribute_map: active_directory: - sAMAccountName: username - mail: email - given_name: first_name - name: last_name + username: "%(sAMAccountName)s" + email: "%(mail)s" + name: "%(displayName)" oauth_client: # List of python packages with sources types to load. types: diff --git a/passbook/oauth_client/source_types/discord.py b/passbook/oauth_client/source_types/discord.py index 4a0daad17..5ea8419c3 100644 --- a/passbook/oauth_client/source_types/discord.py +++ b/passbook/oauth_client/source_types/discord.py @@ -52,7 +52,7 @@ class DiscordOAuth2Callback(OAuthCallback): user_data = { 'username': info.get('username'), 'email': info.get('email', 'None'), - 'first_name': info.get('username'), + 'name': info.get('username'), 'password': None, } discord_user = user_get_or_create(**user_data) diff --git a/passbook/oauth_client/source_types/facebook.py b/passbook/oauth_client/source_types/facebook.py index 57a3dc277..00b387177 100644 --- a/passbook/oauth_client/source_types/facebook.py +++ b/passbook/oauth_client/source_types/facebook.py @@ -23,7 +23,7 @@ class FacebookOAuth2Callback(OAuthCallback): user_data = { 'username': info.get('name'), 'email': info.get('email', ''), - 'first_name': info.get('name'), + 'name': info.get('name'), 'password': None, } fb_user = user_get_or_create(**user_data) diff --git a/passbook/oauth_client/source_types/github.py b/passbook/oauth_client/source_types/github.py index fb75fe506..cfdcc3a7c 100644 --- a/passbook/oauth_client/source_types/github.py +++ b/passbook/oauth_client/source_types/github.py @@ -13,7 +13,7 @@ class GitHubOAuth2Callback(OAuthCallback): user_data = { 'username': info.get('login'), 'email': info.get('email', ''), - 'first_name': info.get('name'), + 'name': info.get('name'), 'password': None, } gh_user = user_get_or_create(**user_data) diff --git a/passbook/oauth_client/source_types/google.py b/passbook/oauth_client/source_types/google.py index e92731b2e..7cc49c74f 100644 --- a/passbook/oauth_client/source_types/google.py +++ b/passbook/oauth_client/source_types/google.py @@ -22,7 +22,7 @@ class GoogleOAuth2Callback(OAuthCallback): user_data = { 'username': info.get('email'), 'email': info.get('email', ''), - 'first_name': info.get('name'), + 'name': info.get('name'), 'password': None, } google_user = user_get_or_create(**user_data) diff --git a/passbook/oauth_client/source_types/reddit.py b/passbook/oauth_client/source_types/reddit.py index 87f175eb5..e5bf9ef61 100644 --- a/passbook/oauth_client/source_types/reddit.py +++ b/passbook/oauth_client/source_types/reddit.py @@ -61,7 +61,7 @@ class RedditOAuth2Callback(OAuthCallback): user_data = { 'username': info.get('name'), 'email': None, - 'first_name': info.get('name'), + 'name': info.get('name'), 'password': None, } reddit_user = user_get_or_create(**user_data) diff --git a/passbook/oauth_client/source_types/supervisr.py b/passbook/oauth_client/source_types/supervisr.py index a0e702502..28b316731 100644 --- a/passbook/oauth_client/source_types/supervisr.py +++ b/passbook/oauth_client/source_types/supervisr.py @@ -46,7 +46,7 @@ class SupervisrOAuthCallback(OAuthCallback): user_data = { 'username': info.get('username'), 'email': info.get('email', ''), - 'first_name': info.get('first_name'), + 'name': info.get('first_name'), 'password': None, } sv_user = user_get_or_create(**user_data) diff --git a/passbook/oauth_client/source_types/twitter.py b/passbook/oauth_client/source_types/twitter.py index 449268e93..f0e645911 100644 --- a/passbook/oauth_client/source_types/twitter.py +++ b/passbook/oauth_client/source_types/twitter.py @@ -38,7 +38,7 @@ class TwitterOAuthCallback(OAuthCallback): user_data = { 'username': info.get('screen_name'), 'email': info.get('email', ''), - 'first_name': info.get('name'), + 'name': info.get('name'), 'password': None, } tw_user = user_get_or_create(**user_data) diff --git a/passbook/pretend/views/github.py b/passbook/pretend/views/github.py index ea5277517..42a7d3a07 100644 --- a/passbook/pretend/views/github.py +++ b/passbook/pretend/views/github.py @@ -27,7 +27,7 @@ class GitHubUserView(View): "received_events_url": "", "type": "User", "site_admin": False, - "name": "%s %s" % (request.user.first_name, request.user.last_name), + "name": request.user.name, "company": "", "blog": "", "location": "", diff --git a/passbook/saml_idp/base.py b/passbook/saml_idp/base.py index a7d9f24b7..dcf03580f 100644 --- a/passbook/saml_idp/base.py +++ b/passbook/saml_idp/base.py @@ -157,7 +157,7 @@ class Processor: { 'FriendlyName': 'cn', 'Name': 'urn:oid:2.5.4.3', - 'Value': self._django_request.user.first_name, + 'Value': self._django_request.user.name, }, { 'FriendlyName': 'mail',