Skip to content

Commit

Permalink
feat: add user page with summary of posting rights
Browse files Browse the repository at this point in the history
Refs: RW-1161
  • Loading branch information
orakili committed Jan 29, 2025
1 parent b0c39f8 commit 7433e4d
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
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,106 @@
<?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) {
$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($source->field_user_posting_rights->job)],
['data' => $this->formatPostingRights($source->field_user_posting_rights->training)],
['data' => $this->formatPostingRights($source->field_user_posting_rights->report)],
];
}

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',
],
];
}

}

0 comments on commit 7433e4d

Please sign in to comment.