Skip to content

Commit 9c01e35

Browse files
committed
Upgrade Drupal 7 unl_access code module to Durpal 10. Fixes unlcms/project-herbie#821. Fixes #2.
1 parent 1eff620 commit 9c01e35

26 files changed

+1270
-172
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- node
6+
id: add_any_affiliation_to_node
7+
label: "Add 'Any Affiliation'"
8+
type: node
9+
plugin: add_any_affiliation_to_node
10+
configuration: { }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- node
6+
id: add_faculty_emeriti_affiliation_to_node
7+
label: "Add 'Faculty/Emeriti Affiliation'"
8+
type: node
9+
plugin: add_faculty_emeriti_affiliation_to_node
10+
configuration: { }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- node
6+
id: add_guest_affiliation_to_node
7+
label: "Add 'Guest Affiliation'"
8+
type: node
9+
plugin: add_guest_affiliation_to_node
10+
configuration: { }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- node
6+
id: add_staff_affiliation_to_node
7+
label: "Add 'Staff Affiliation'"
8+
type: node
9+
plugin: add_staff_affiliation_to_node
10+
configuration: { }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- node
6+
id: add_student_affiliation_to_node
7+
label: "Add 'Student Affiliation'"
8+
type: node
9+
plugin: add_student_affiliation_to_node
10+
configuration: { }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- node
6+
id: make_node_public
7+
label: 'Make public'
8+
type: node
9+
plugin: make_node_public
10+
configuration: { }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- node
6+
id: remove_any_affiliation_to_node
7+
label: "Remove 'Any Affiliation'"
8+
type: node
9+
plugin: remove_any_affiliation_to_node
10+
configuration: { }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- node
6+
id: remove_faculty_emeriti_affiliation_to_node
7+
label: "Remove 'Faculty/Emeriti Affiliation'"
8+
type: node
9+
plugin: remove_faculty_emeriti_affiliation_to_node
10+
configuration: { }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- node
6+
id: remove_guest_affiliation_to_node
7+
label: "Remove 'Guest Affiliation'"
8+
type: node
9+
plugin: remove_guest_affiliation_to_node
10+
configuration: { }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- node
6+
id: remove_staff_affiliation_to_node
7+
label: "Remove 'Staff Affiliation'"
8+
type: node
9+
plugin: remove_staff_affiliation_to_node
10+
configuration: { }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- node
6+
id: remove_student_affiliation_to_node
7+
label: "Remove 'Student Affiliation'"
8+
type: node
9+
plugin: remove_student_affiliation_to_node
10+
configuration: {}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Drupal\unl_access\Plugin\Action;
4+
5+
use Drupal\Core\Action\ActionBase;
6+
use Drupal\Core\Session\AccountInterface;
7+
use Drupal\node\NodeInterface;
8+
use Drupal\Core\Database\DatabaseException;
9+
10+
/**
11+
* Push term in front.
12+
*
13+
* @Action(
14+
* id = "add_any_affiliation_to_node",
15+
* label = @Translation("Add 'Any Affiliation'"),
16+
* type = "node"
17+
* )
18+
*/
19+
class AddAnyAffiliationToNode extends ActionBase {
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function execute($entity = NULL) {
25+
/** @var \Drupal\node\NodeInterface $entity */
26+
if ($entity instanceof NodeInterface) {
27+
$node_id = $entity->id();
28+
$node_title = $entity->getTitle();
29+
$affiliation_type = 'Any Affiliation';
30+
31+
try {
32+
$any_affiliation_access = \Drupal::database()->select('unl_access_node_affiliation', 'a')
33+
->fields('a', array('nid', 'affiliation'))
34+
->condition('nid', $node_id, 'IN')
35+
->condition('affiliation', UNL_AFFILIATION_ANY) // Additional condition for status.
36+
->execute()
37+
->fetchAll();
38+
39+
if (!$any_affiliation_access) {
40+
$inserted_id = \Drupal::database()->insert('unl_access_node_affiliation')
41+
->fields(array('nid', 'affiliation'))
42+
->values(array($node_id, UNL_AFFILIATION_ANY))
43+
->execute();
44+
if ($inserted_id || $inserted_id == 0) {
45+
$access_records = unl_access_node_access_records($entity);
46+
\Drupal::service('node.grant_storage')->write($entity, $access_records);
47+
48+
\Drupal::messenger()->addStatus(t('@affiliation_type access has been added to the page "@title". Anyone with a UNL affiliation has viewing access.', [
49+
'@title' => $node_title,
50+
'@affiliation_type' => $affiliation_type,
51+
52+
]));
53+
} else {
54+
\Drupal::messenger()->addError(t('Failed to add @affiliation_type access to the page "@title".', [
55+
'@title' => $node_title,
56+
'@affiliation_type' => $affiliation_type,
57+
]));
58+
}
59+
} else {
60+
\Drupal::messenger()->addStatus(t('Page "@name" already has @affiliation_type access assigned to it.', [
61+
'@name' => $node_title,
62+
'@affiliation_type' => $affiliation_type,
63+
]));
64+
}
65+
} catch (DatabaseException $e) {
66+
// Log the error and show a user-friendly message.
67+
\Drupal::logger('unl_access')->error('Error inserting @affiliation_type records for node @nid: @message', [
68+
'@nid' => $node_id,
69+
'@message' => $e->getMessage(),
70+
'@affiliation_type' => $affiliation_type,
71+
]);
72+
\Drupal::messenger()->addError(t('An error occurred while attempting to insert @affiliation_type records for node @nid. Please try again later.', ['@nid' => $node_id]));
73+
}
74+
}
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
81+
/** @var \Drupal\node\NodeInterface $entity */
82+
// Ensure that the object is a valid node before proceeding.
83+
if (!$object instanceof NodeInterface) {
84+
return FALSE; // Early return if not a Node.
85+
}
86+
$result = $object->access('update', $account, TRUE);
87+
return $return_as_object ? $result : $result->isAllowed();
88+
}
89+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Drupal\unl_access\Plugin\Action;
4+
5+
use Drupal\Core\Action\ActionBase;
6+
use Drupal\Core\Session\AccountInterface;
7+
use Drupal\node\NodeInterface;
8+
use Drupal\Core\Database\DatabaseException;
9+
10+
/**
11+
* Push term in front.
12+
*
13+
* @Action(
14+
* id = "add_faculty_emeriti_affiliation_to_node",
15+
* label = @Translation("Add 'Faculty/Emeriti Affiliation'"),
16+
* type = "node"
17+
* )
18+
*/
19+
class AddFacultyEmeritiAffiliationToNode extends ActionBase {
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function execute($entity = NULL) {
25+
/** @var \Drupal\node\NodeInterface $entity */
26+
if ($entity instanceof NodeInterface) {
27+
$node_id = $entity->id();
28+
$node_title = $entity->getTitle();
29+
$affiliation_type = 'Faculty/Emeriti Affiliation';
30+
31+
try {
32+
$any_affiliation_access = \Drupal::database()->select('unl_access_node_affiliation', 'a')
33+
->fields('a', array('nid', 'affiliation'))
34+
->condition('nid', $node_id)
35+
->condition('affiliation', UNL_AFFILIATION_FACULTY) // Additional condition for status.
36+
->execute()
37+
->fetchAll();
38+
39+
if (!$any_affiliation_access) {
40+
$inserted_id = \Drupal::database()->insert('unl_access_node_affiliation')
41+
->fields(array('nid', 'affiliation'))
42+
->values(array($node_id, UNL_AFFILIATION_FACULTY))
43+
->execute();
44+
if ($inserted_id || $inserted_id == 0) {
45+
$access_records = unl_access_node_access_records($entity);
46+
\Drupal::service('node.grant_storage')->write($entity, $access_records);
47+
48+
\Drupal::messenger()->addStatus(t('@affiliation_type access has been added to page "@title".', [
49+
'@title' => $node_title,
50+
'@affiliation_type' => $affiliation_type,
51+
]));
52+
} else {
53+
\Drupal::messenger()->addError(t('Failed to add @affiliation_type access to page "@title".', [
54+
'@title' => $node_title,
55+
'@affiliation_type' => $affiliation_type,
56+
]));
57+
}
58+
} else {
59+
\Drupal::messenger()->addStatus(t('Page "@name" already has @affiliation_type access assigned to it.', [
60+
'@name' => $node_title,
61+
'@affiliation_type' => $affiliation_type,
62+
]));
63+
}
64+
} catch (DatabaseException $e) {
65+
// Log the error and show a user-friendly message.
66+
\Drupal::logger('unl_access')->error('Error inserting @affiliation_type records for node @nid: @message', [
67+
'@nid' => $node_id,
68+
'@message' => $e->getMessage(),
69+
'@affiliation_type' => $affiliation_type,
70+
]);
71+
\Drupal::messenger()->addError(t(
72+
'An error occurred while attempting to insert @affiliation_type records for node @nid. Please try again later.',
73+
[
74+
'@nid' => $node_id,
75+
'@affiliation_type' => $affiliation_type,
76+
77+
]
78+
));
79+
}
80+
}
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
87+
/** @var \Drupal\node\NodeInterface $entity */
88+
// Ensure that the object is a valid node before proceeding.
89+
if (!$object instanceof NodeInterface) {
90+
return FALSE; // Early return if not a Node.
91+
}
92+
93+
// Use the recommended access check for nodes
94+
$result = $object->access('update', $account, TRUE);
95+
return $return_as_object ? $result : $result->isAllowed();
96+
}
97+
}

0 commit comments

Comments
 (0)