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

LOOP-1009: User login #166

Merged
merged 3 commits into from
Jul 16, 2021
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
27 changes: 27 additions & 0 deletions config/sync/block.block.userlogin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
uuid: 11fc8127-6a14-44b8-865f-fef3e55a34f2
langcode: en
status: true
dependencies:
module:
- user
theme:
- os2loop_theme
id: userlogin
theme: os2loop_theme
region: content
weight: -7
provider: null
plugin: user_login_block
settings:
id: user_login_block
label: 'User login'
provider: user
label_display: '0'
visibility:
user_role:
id: user_role
roles:
anonymous: anonymous
negate: false
context_mapping:
user: '@user.current_user_context:current_user'
18 changes: 0 additions & 18 deletions config/sync/user.role.anonymous.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,4 @@ label: 'Anonymous user'
weight: 0
is_admin: false
permissions:
- 'access content'
- 'hide format selection for comment'
- 'hide format selection for flagging'
- 'hide format selection for message'
- 'hide format selection for node'
- 'hide format selection for paragraph'
- 'hide format selection for taxonomy_term'
- 'hide format selection for user'
- 'hide format tips'
- 'hide more format tips link'
- 'use search_api_autocomplete for os2loop_search_db'
- 'use text format os2loop_post_comment_plain_text'
- 'use text format os2loop_post_comment_rich_text'
- 'use text format os2loop_question_answer_plain_text'
- 'use text format os2loop_question_answer_rich_text'
- 'use text format os2loop_question_plain_text'
- 'use text format os2loop_question_rich_text'
- 'view media'
- 'view sp metadata'
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Drupal\Core\Form\FormStateInterface;
use Drupal\user\UserInterface;

/**
* Implements hook_form_alter().
Expand All @@ -24,3 +25,10 @@ function os2loop_user_login_form_alter(&$form, FormStateInterface $form_state, $
function os2loop_user_login_menu_local_tasks_alter(&$data, $route_name) {
Drupal::service('os2loop_user_login.helper')->alterLocalTasks($data, $route_name);
}

/**
* Implements hook_user_login().
*/
function os2loop_user_user_login(UserInterface $account) {
Drupal::service('os2loop_user_login.helper')->userLogin($account);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\os2loop_user_login\Form\SettingsForm;
use Drupal\os2loop_settings\Settings;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\user\UserInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
Expand Down Expand Up @@ -80,13 +82,11 @@ public function __construct(Settings $settings, ModuleHandlerInterface $module_h
*/
public function alterForm(&$form, FormStateInterface $form_state, $form_id) {
if ('openid_connect_login_form' === $form_id) {
$form['#submit'][] = [$this, 'redirect'];
if (!$this->config->get('show_oidc_login')) {
$form['#access'] = FALSE;
}
}
elseif ('user_login_form' === $form_id) {
$form['#submit'][] = [$this, 'redirect'];
if (!$this->config->get('show_drupal_login')) {
$form['#attached']['library'][] = 'os2loop_user_login/user-login-form';

Expand Down Expand Up @@ -141,59 +141,47 @@ public function alterLocalTasks(array &$data, string $route_name) {
}

/**
* Redirect users after login.
* Implements hook_user_login().
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The state of the form.
* Show a message to the user about incomplete profile.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* Forcing the user to go to the profile page using a redirect will be too
* hard to implement and maintain, så we do this the Drupal way (cf.
* user_user_login()).
*
* @see user_user_login()
*/
public function redirect(array $form, FormStateInterface $form_state) {
$url = Url::fromRoute('<front>');
$parameters = $form_state->getRedirect()->getRouteParameters();

// Check if there are empty required fields on the user that is
// attempting to login.
if (isset($parameters['user']) && is_numeric($parameters['user']) && $this->userHasEmptyRequiredFields($parameters['user'])) {
$url = Url::fromRoute('entity.user.edit_form', $parameters);
$this->messenger->addWarning($this->t('Please fill the required fields and save your profile.'));
}

// Check if a destination is already set.
$request = $this->requestStack->getCurrentRequest();
if (!$request->request->has('destination')) {
$form_state->setRedirectUrl($url);
}
else {
$request->query->set('destination', $request->request->get('destination'));
public function userLogin(AccountInterface $account) {
if (($account instanceof UserInterface) && $this->userHasEmptyRequiredFields($account)) {
$this->messenger->addWarning(
$this->t('Your user profile is not complete. Please go to <a href=":user-edit">your profile page</a> and fill in the required fields.',
[
':user-edit' => $account->toUrl('edit-form')->toString(),
])
);
}
}

/**
* Check if a user has empty required fields.
*
* @param int $uid
* The id of the user to check.
* @param \Drupal\Core\Session\AccountInterface $account
* The account to check.
*
* @return bool
* True if the user has empty required fields.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
private function userHasEmptyRequiredFields(int $uid): bool {
private function userHasEmptyRequiredFields(AccountInterface $account): bool {
/** @var \Drupal\user\Entity\User $user */
$user = $this->entityTypeManager->getStorage('user')->load($uid);
$user = $this->entityTypeManager->getStorage('user')->load($account->id());
$fields = $this->entityFieldManager->getFieldDefinitions('user', 'user');

foreach ($fields as $field_name => $field) {
if ($field->isRequired()) {
if (empty($user->get($field_name)->getValue())) {
return TRUE;
}
if ($field->isRequired() && empty($user->get($field_name)->getValue())) {
return TRUE;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{#
/**
* @file
* Default theme implementation to display a block.
*
* Available variables:
* - plugin_id: The ID of the block implementation.
* - label: The configured label of the block if visible.
* - configuration: A list of the block's configuration values.
* - label: The configured label for the block.
* - label_display: The display settings for the label.
* - provider: The module or other provider that provided this block plugin.
* - Block plugin specific settings will also be stored here.
* - content: The content of this block.
* - attributes: array of HTML attributes populated by modules, intended to
* be added to the main container tag of this template.
* - id: A valid HTML ID and guaranteed unique.
* - title_attributes: Same as attributes, except applied to the main title
* tag that appears in the template.
* - title_prefix: Additional output populated by modules, intended to be
* displayed in front of the main title tag that appears in the template.
* - title_suffix: Additional output populated by modules, intended to be
* displayed after the main title tag that appears in the template.
*
* @see template_preprocess_block()
*
* @ingroup themeable
*/
#}
<div{{ attributes }}>
{{ title_prefix }}
{% if label %}
<h2{{ title_attributes }}>{{ label }}</h2>
{% endif %}
{{ title_suffix }}
{% block content %}
{# Hide “Create new account”, “Reset your password” &c. #}
{{ content|without('user_links') }}
{% endblock %}
</div>