From 804ca2f64909d93de6de3476aa3efcbb399f8698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien=20De=CC=81le=CC=80ze?= Date: Thu, 4 Jun 2020 11:48:27 +0200 Subject: [PATCH] users: create user record on registration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Creates a user record when a new account is registered. * Disables `invenio-userprofiles` module. * Exposes and uses user record for displaying user information in templates. * Gives role `user` by default when a new record is created. * Removes `organisation` from required fields for users. * Hides `applications` and `security` menu entries. * Creates a new page for editing user data. * Removes useless CSS rules * Adds CSS rules for standalone editor. * Customizes settings page. Co-Authored-by: Sébastien Délèze --- sonar/config.py | 2 +- sonar/modules/ext.py | 9 ++- sonar/modules/users/cli.py | 10 ++- .../users/jsonschemas/users/user-v1.0.0.json | 1 - sonar/modules/users/signals.py | 44 ++++++++++++ sonar/theme/assets/scss/common/_theme.scss | 25 ++----- .../templates/sonar/accounts/profile.html | 36 ++++++++++ .../templates/sonar/accounts/signup.html | 2 - .../theme/templates/sonar/page_settings.html | 69 +++++++++---------- .../sonar/partial/dropdown_user.html | 6 +- .../theme/templates/sonar/partial/navbar.html | 2 +- sonar/theme/views.py | 37 +++++++++- sonar/translations/de/LC_MESSAGES/messages.po | 12 ++-- sonar/translations/en/LC_MESSAGES/messages.po | 12 ++-- sonar/translations/fr/LC_MESSAGES/messages.po | 12 ++-- sonar/translations/it/LC_MESSAGES/messages.po | 12 ++-- sonar/translations/messages.pot | 9 +-- .../test_shibboleth_handlers.py | 2 +- .../test_shibboleth_views_client.py | 2 +- tests/ui/test_views.py | 26 ++++++- tests/ui/users/test_users_cli.py | 1 + tests/ui/users/test_users_signals.py | 32 +++++++++ 22 files changed, 267 insertions(+), 96 deletions(-) create mode 100644 sonar/modules/users/signals.py create mode 100644 sonar/theme/templates/sonar/accounts/profile.html create mode 100644 tests/ui/users/test_users_signals.py diff --git a/sonar/config.py b/sonar/config.py index 2d056797..2b0729d8 100644 --- a/sonar/config.py +++ b/sonar/config.py @@ -128,7 +128,7 @@ def _(x): # User profiles # ============= -USERPROFILES_EXTEND_SECURITY_FORMS = True +USERPROFILES = False # Celery configuration # ==================== diff --git a/sonar/modules/ext.py b/sonar/modules/ext.py index e3338cdf..8c0e9cf6 100644 --- a/sonar/modules/ext.py +++ b/sonar/modules/ext.py @@ -22,10 +22,13 @@ import jinja2 from flask_assets import Bundle, Environment from flask_bootstrap import Bootstrap +from flask_security import user_registered from flask_wiki import Wiki from sonar.modules.permissions import has_admin_access, has_publisher_access, \ has_superuser_access +from sonar.modules.users.api import current_user_record +from sonar.modules.users.signals import user_registered_handler from sonar.modules.utils import get_switch_aai_providers, get_view_code from . import config @@ -39,7 +42,8 @@ def utility_processor(): has_superuser_access=has_superuser_access, ui_version=config.SONAR_APP_UI_VERSION, aai_providers=get_switch_aai_providers, - view_code=get_view_code()) + view_code=get_view_code(), + current_user_record=current_user_record) class Sonar(object): @@ -82,6 +86,9 @@ def init_app(self, app): app.context_processor(utility_processor) + # Connect to signal sent when a user is created in Flask-Security. + user_registered.connect(user_registered_handler, weak=False) + def init_config(self, app): """Initialize configuration.""" for k in dir(config): diff --git a/sonar/modules/users/cli.py b/sonar/modules/users/cli.py index 581bd40c..f7f1342b 100644 --- a/sonar/modules/users/cli.py +++ b/sonar/modules/users/cli.py @@ -67,9 +67,15 @@ def import_users(infile): password = hash_password(password) del user_data['password'] + if not user_data.get('roles'): + user_data['roles'] = [UserRecord.ROLE_USER] + roles = user_data.get('roles', []).copy() - if not roles or not isinstance(roles, list): - roles = [] + + for role in roles: + if not datastore.find_role(role): + datastore.create_role(name=role) + datastore.commit() # Create account and activate it datastore.create_user(email=email, password=password, roles=roles) diff --git a/sonar/modules/users/jsonschemas/users/user-v1.0.0.json b/sonar/modules/users/jsonschemas/users/user-v1.0.0.json index 55b172aa..f36239a3 100644 --- a/sonar/modules/users/jsonschemas/users/user-v1.0.0.json +++ b/sonar/modules/users/jsonschemas/users/user-v1.0.0.json @@ -142,7 +142,6 @@ "full_name", "email", "roles", - "organisation", "$schema" ] } diff --git a/sonar/modules/users/signals.py b/sonar/modules/users/signals.py new file mode 100644 index 00000000..edf2ec49 --- /dev/null +++ b/sonar/modules/users/signals.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# +# Swiss Open Access Repository +# Copyright (C) 2019 RERO +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +"""Signals for users.""" + +from sonar.modules.users.api import UserRecord, datastore + + +def user_registered_handler(app, user, confirm_token): + """Called when a new user is registered. + + :param app: App context. + :param user: User account. + """ + # Add a default role to user + role = datastore.find_role(UserRecord.ROLE_USER) + datastore.add_role_to_user(user, role) + datastore.commit() + + # Create user record + user_record = UserRecord.get_user_by_email(user.email) + if not user_record: + user_record = UserRecord.create( + { + 'full_name': user.email, + 'email': user.email, + 'roles': [UserRecord.ROLE_USER] + }, + dbcommit=True) + user_record.reindex() diff --git a/sonar/theme/assets/scss/common/_theme.scss b/sonar/theme/assets/scss/common/_theme.scss index c1165fe1..14a9ddaa 100644 --- a/sonar/theme/assets/scss/common/_theme.scss +++ b/sonar/theme/assets/scss/common/_theme.scss @@ -37,25 +37,8 @@ img.logo { @extend .text-light } -// CSS code to allow `ng-csp="no-inline-css"` -[ng\:cloak], -[ng-cloak], -[data-ng-cloak], -[x-ng-cloak], -.ng-cloak, -.x-ng-cloak, -.ng-hide:not(.ng-hide-animate) { - display: none !important; -} - -ng\:form { - display: block; -} - -.ng-animate-shim { - visibility:hidden; -} - -.ng-anchor { - position:absolute; +.standalone-editor { + legend, button.btn-outline-danger { + display: none; + } } diff --git a/sonar/theme/templates/sonar/accounts/profile.html b/sonar/theme/templates/sonar/accounts/profile.html new file mode 100644 index 00000000..cf656067 --- /dev/null +++ b/sonar/theme/templates/sonar/accounts/profile.html @@ -0,0 +1,36 @@ +{# -*- coding: utf-8 -*- + Swiss Open Access Repository + Copyright (C) 2019 RERO + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +#} + +{% extends 'sonar/page_settings.html' %} + +{%- block settings_body scoped %} +
+ +
+{%- endblock settings_body %} + +{%- block javascript %} + + + + + + +{%- endblock javascript %} diff --git a/sonar/theme/templates/sonar/accounts/signup.html b/sonar/theme/templates/sonar/accounts/signup.html index 6dd207b6..d080d756 100644 --- a/sonar/theme/templates/sonar/accounts/signup.html +++ b/sonar/theme/templates/sonar/accounts/signup.html @@ -30,8 +30,6 @@

{{ form_errors(form) }} {{ form.hidden_tag() }} - {{ render_field(form.profile.full_name, errormsg=False) }} - {{ render_field(form.profile.username, errormsg=False) }} {{ render_field(form.email, autofocus=True, errormsg=False) }} {{ render_field(form.password, errormsg=False) }} {%- if form.password_confirm %} diff --git a/sonar/theme/templates/sonar/page_settings.html b/sonar/theme/templates/sonar/page_settings.html index 7b4c68c3..077d36f6 100755 --- a/sonar/theme/templates/sonar/page_settings.html +++ b/sonar/theme/templates/sonar/page_settings.html @@ -20,48 +20,43 @@ {%- block body scoped %}
-
- {%- block settings_menu scoped %} -
-
-
{{ _('Settings') }}
-
    - {%- for item in current_menu.submenu('settings').children if item.visible %} - {%- block settings_menu_item scoped %} - - {{ item.text|safe }} - +
    + {%- block settings_menu scoped %} +
      + {%- for item in current_menu.submenu('settings').children if item.visible %} + {%- block settings_menu_item scoped %} + + {{ item.text|safe }} + + {%- endblock %} + {%- endfor %} +
    + {%- endblock %} +
    +
    + {%- block settings_content scoped %} +
    +
    +
    + {%- block settings_content_title scoped %} + {%- if panel_icon %} + {%- block settings_content_title_icon scoped %} + + {%- endblock %} + {%- endif %} + {{ panel_title|default("") }} {%- endblock %} - {%- endfor %} -
-
- {%- endblock %} -
-
- {%- block settings_content scoped %} -
-
-
- {%- block settings_content_title scoped %} - {%- if panel_icon %} - {%- block settings_content_title_icon scoped %} - - {%- endblock %} - {%- endif %} - {{ panel_title|default("") }} - {%- endblock %} -
- {%- block settings_body scoped %} -
- {%- block settings_form scoped %} - {%- endblock settings_form %} -
- {%- endblock settings_body %} + {%- block settings_body scoped %} +
+ {%- block settings_form scoped %} + {%- endblock settings_form %}
+ {%- endblock settings_body %}
- {%- endblock %}
+ {%- endblock %} +
{%- endblock %} diff --git a/sonar/theme/templates/sonar/partial/dropdown_user.html b/sonar/theme/templates/sonar/partial/dropdown_user.html index f83bf928..d01e2e00 100644 --- a/sonar/theme/templates/sonar/partial/dropdown_user.html +++ b/sonar/theme/templates/sonar/partial/dropdown_user.html @@ -16,12 +16,10 @@ #}