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

[RW-1161] Add user page with summary of posting rights #999

Merged
merged 1 commit into from
Jan 29, 2025
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
1 change: 1 addition & 0 deletions config/user.role.editor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ permissions:
- 'access reliefweb private files'
- 'access reliefweb user posting rights field validation'
- 'access user contact forms'
- 'access user posting rights page'
- 'access user profiles'
- 'add content to books'
- 'administer community topics'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
reliefweb_users.posting_rights:
route_name: reliefweb_users.posting_rights
title: 'Posting Rights'
base_route: entity.user.canonical
8 changes: 8 additions & 0 deletions html/modules/custom/reliefweb_users/reliefweb_users.module
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,11 @@ function reliefweb_users_get_email_confirmation_hash(UserInterface $user, $times
$data = implode('/', [$user->id(), $timestamp, $email, $confirmed]);
return Crypt::hmacBase64($data, Settings::getHashSalt() . $email);
}

/**
* Implements hook_entity_type_alter().
*/
function reliefweb_users_entity_type_alter(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
$entity_types['user']->setLinkTemplate('posting-rights', '/user/{user}/posting-rights');
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ manage user roles:
title: 'Manage user roles'
description: 'Allow users to manager user roles on other accounts.'

access user posting rights page:
title: 'Access posting user rights page'
description: 'Allow users to access the user posting rights page.'

permission_callbacks:
- Drupal\reliefweb_users\UserPermissions::permissions
11 changes: 11 additions & 0 deletions html/modules/custom/reliefweb_users/reliefweb_users.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ user.email.confirmation.process:
_maintenance_access: FALSE
no_cache: TRUE

reliefweb_users.posting_rights:
path: '/user/{user}/posting-rights'
defaults:
_controller: '\Drupal\reliefweb_users\Controller\UserPostingRightsController::content'
_title: 'Posting Rights'
requirements:
_permission: 'access user posting rights page'
options:
parameters:
user:
type: entity:user
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Drupal\reliefweb_users\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;

/**
* Controller for the Posting Rights page.
*/
class UserPostingRightsController extends ControllerBase {

/**
* Builds the content for the Posting Rights page.
*
* @param \Drupal\Core\Session\AccountInterface $user
* The user account for which to display posting rights.
*
* @return array
* A render array representing the page content.
*/
public function content(AccountInterface $user): array {
$sources = $this->entityTypeManager()
->getStorage('taxonomy_term')
->loadByProperties([
'vid' => 'source',
'field_user_posting_rights.id' => $user->id(),
]);

$header = [
$this->t('Source'),
$this->t('Job'),
$this->t('Training'),
$this->t('Report'),
];

$rows = [];
foreach ($sources as $source) {
$rights = NULL;
// Retrieve the rights corresponding to the user.
foreach ($source->field_user_posting_rights as $item) {
if ($item->id == $user->id()) {
$rights = $item->toArray();
break;
}
}
if (empty($rights)) {
continue;
}

$rows[] = [
[
'data' => [
'#type' => 'link',
'#title' => $source->label(),
'#url' => Url::fromRoute(
'reliefweb_fields.taxonomy_term.user_posting_rights_form',
['taxonomy_term' => $source->id()],
['fragment' => ':~:text=' . $user->id()]
),
],
],
['data' => $this->formatPostingRights($rights['job'] ?? 0)],
['data' => $this->formatPostingRights($rights['training'] ?? 0)],
['data' => $this->formatPostingRights($rights['report'] ?? 0)],
];
}

return [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this->t('No posting rights found.'),
'#attached' => [
'library' => [
'common_design_subtheme/rw-user-posting-right',
],
],
];
}

/**
* Formats the posting rights value for display.
*
* @param int $right
* The numeric value of the posting right.
*
* @return array
* A render array for the formatted posting right.
*/
protected function formatPostingRights(int $right): array {
$rights = [
0 => 'unverified',
1 => 'blocked',
2 => 'allowed',
3 => 'trusted',
];

$labels = [
0 => $this->t('Unverified'),
1 => $this->t('Blocked'),
2 => $this->t('Allowed'),
3 => $this->t('Trusted'),
];

return [
'#type' => 'html_tag',
'#tag' => 'span',
'#value' => $labels[$right] ?? $this->t('unknown'),
'#attributes' => [
'class' => ['rw-user-posting-right', 'rw-user-posting-right--large'],
'data-user-posting-right' => $rights[$right] ?? 'unknown',
],
];
}

}