Skip to content

Commit

Permalink
Merge pull request #214 from dof-dss/D8NID-1518
Browse files Browse the repository at this point in the history
D8 nid 1518
  • Loading branch information
omahm authored Jun 28, 2022
2 parents 46cda26 + 235c5a9 commit 0f0829f
Show file tree
Hide file tree
Showing 11 changed files with 407 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ jobs:
name: Fetch phpcs and dependencies
command: |
composer require drupal/coder --prefer-stable --no-interaction --optimize-autoloader
composer require slevomat/coding-standard --prefer-stable --no-interaction --optimize-autoloader
# Commenting out this dependency due to version constraint issues.
# composer require slevomat/coding-standard --prefer-stable --no-interaction --optimize-autoloader
# Move vendor directory up a level as we don't want to code-check all of that.
mv vendor ../
- run:
Expand Down
8 changes: 8 additions & 0 deletions origins_qa/origins_qa.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: 'Origins Quality Assurance'
type: module
description: 'Tools for QA/testing teams'
core_version_requirement: ^8 || ^9
package: 'Origins'
dependencies:
- drupal:user

63 changes: 63 additions & 0 deletions origins_qa/origins_qa.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* Implements hook_install().
*/
function origins_qa_install() {
$qa_role = \Drupal::entityTypeManager()->getStorage('user_role')->load('qa');

// Create a new 'qa' role as one wasn't found.
if (empty($qa_role)) {
$qa_role = \Drupal\user\Entity\Role::create([
'id' => 'qa',
'label' => 'Quality Assurance',
]);

$qa_role->save();
}
}

use Drupal\Core\Link;

/**
* Implements hook_requirements().
*/
function origins_qa_requirements($phase) {
$requirements = [];

if ($phase == 'runtime') {
$accounts = \Drupal::entityTypeManager()
->getListBuilder('user')
->getStorage()
->loadByProperties([
'roles' => 'qa'
]);

if (!empty($accounts)) {
$active = 0;
foreach ($accounts as $account) {
($account->isActive()) ? $active++ : NULL;
}

$summary = t('@active active of @total QA accounts.', [
'@active' => $active,
'@total' => count($accounts),
]);

$description = [
'#markup' => t('These should be removed or disabled on production sites. @link', [
'@link' => Link::createFromRoute('QA account list', 'origins_qa.manager.list')->toString()
])
];

$requirements['origins_qa'] = [
'title' => t('Origins QA accounts'),
'severity' => REQUIREMENT_WARNING,
'value' => $summary,
'description' => $description,
];
}
}

return $requirements;
}
7 changes: 7 additions & 0 deletions origins_qa/origins_qa.libraries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
origins_qa:
js:
js/origins_qa.js: {}
dependencies:
- core/jquery
- core/drupal.dialog.ajax
- core/jquery.form
14 changes: 14 additions & 0 deletions origins_qa/origins_qa.links.action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
origins_qa.manager.enable_all:
title: 'Enable all'
route_name: origins_qa.manager.toggleAll
route_parameters:
action: 'enable'
appears_on:
- origins_qa.manager.list
origins_qa.manager.disable_all:
title: 'Disable all'
route_name: origins_qa.manager.toggleAll
route_parameters:
action: 'disable'
appears_on:
- origins_qa.manager.list
5 changes: 5 additions & 0 deletions origins_qa/origins_qa.links.task.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
origins_qa.manager.list:
title: 'QA accounts'
route_name: origins_qa.manager.list
base_route: entity.user.collection
weight: 15
21 changes: 21 additions & 0 deletions origins_qa/origins_qa.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* @file
* Contains origins_qa.module.
*/

use Drupal\Core\Routing\RouteMatchInterface;

/**
* Implements hook_help().
*/
function origins_qa_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.origins_qa':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t("This module provides management operations for QA accounts. User accounts must be assigned the role 'qa' (Quality Assurance) to appear on the 'QA Accounts' section under 'People'.") . '</p>';
return $output;
}
}
4 changes: 4 additions & 0 deletions origins_qa/origins_qa.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
manage qa accounts:
description: 'View the QA account list and bulk manage QA accounts.'
title: 'Manage QA accounts'
restrict access: TRUE
27 changes: 27 additions & 0 deletions origins_qa/origins_qa.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
origins_qa.manager.list:
path: '/admin/origins/qa/list'
defaults:
_title: 'Quality Assurance accounts'
_controller: '\Drupal\origins_qa\Controller\QaAccountsManager::list'
requirements:
_permission: 'manage qa accounts'
origins_qa.manager.toggleAll:
path: '/admin/origins/qa/toggle-all/{action}'
defaults:
_title: 'Enable / disable all QA accounts'
_controller: '\Drupal\origins_qa\Controller\QaAccountsManager::toggleAll'
requirements:
_permission: 'manage qa accounts'
options:
parameters:
action:
type: string
origins_qa.manager.password_form_modal:
path: '/admin/origins/qa/password-set-modal'
defaults:
_title: 'Modal Form'
_controller: '\Drupal\origins_qa\Controller\QaAccountsManager::displayPasswordForm'
requirements:
_permission: 'manage qa accounts'
options:
_admin_route: TRUE
163 changes: 163 additions & 0 deletions origins_qa/src/Controller/QaAccountsManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

namespace Drupal\origins_qa\Controller;

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormBuilder;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Controller for Origins QA.
*/
class QaAccountsManager extends ControllerBase {

/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilder
*/
protected $formBuilder;

/**
* {@inheritdoc}
*
* @param \Drupal\Core\Form\FormBuilder $formBuilder
* The form builder.
*/
public function __construct(FormBuilder $formBuilder) {
$this->formBuilder = $formBuilder;
}

/**
* {@inheritdoc}
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The Drupal service container.
*
* @return static
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('form_builder')
);
}

/**
* Returns a list of QA accounts.
*/
public function list() {
$build = [];

// Fetch all the accounts belonging to the 'qa' role.
$accounts = $this->entityTypeManager()
->getListBuilder('user')
->getStorage()
->loadByProperties([
'roles' => 'qa'
]);

$header = [
'username' => $this->t('Username'),
'status' => $this->t('Status'),
'last_access' => $this->t('Last access'),
'operations' => $this->t('Operations'),
];

$rows = [];

foreach ($accounts as $account) {
$rows[] = [
$account->label(),
($account->isActive()) ? 'Enabled' : 'Disabled',
($account->getLastAccessedTime() == 0) ? 'Never' : date('d F Y', $account->getLastAccessedTime()),
[
'data' => [
'#type' => 'dropbutton',
'#links' => [
'edit' => [
'title' => $this->t('Edit'),
'url' => Url::fromRoute('entity.user.edit_form', ['user' => $account->id()]),
],

],
],
],
];
}

$build['qa_accounts'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this->t("There are no accounts on this site associated with the 'qa' (Quality Assurance) role. You will need to assign test accounts to that role for them to show up in this table."),
];

if (!empty($accounts)) {
$build['open_modal'] = [
'#type' => 'link',
'#title' => $this->t('Set passwords for all QA accounts'),
'#url' => Url::fromRoute('origins_qa.manager.password_form_modal'),
'#attributes' => [
'class' => [
'use-ajax',
'button',
'button-action',
'button--primary'
],
],
];

$build['#attached']['library'][] = 'core/drupal.dialog.ajax';
}

return $build;
}

/**
* Toggles all QA accounts to either active or blocked.
*/
public function toggleAll($action) {
$accounts = $this->entityTypeManager()
->getListBuilder('user')
->getStorage()
->loadByProperties([
'roles' => 'qa'
]);

foreach ($accounts as $account) {

if ($action === 'enable') {
if (!$account->isActive()) {
$account->activate();
$account->save();
}
}
else {
if ($account->isActive()) {
$account->block();
$account->save();
}
}
}

$this->messenger()->addMessage('Updated all QA accounts.');

return $this->redirect('origins_qa.manager.list');
}

/**
* Ajax callback for displaying the password form.
*/
public function displayPasswordForm() {
$response = new AjaxResponse();

$modal_form = $this->formBuilder->getForm('Drupal\origins_qa\Form\QaPasswordSetForm');
$response->addCommand(new OpenModalDialogCommand('QA Password form', $modal_form, ['width' => '300']));

return $response;
}

}
Loading

0 comments on commit 0f0829f

Please sign in to comment.