Let's start to convert all calls to user_access() with the new AccountInterface::hasPermission() method.

Commit message from all closed issues #8

Issue #2061977 by kim.pepper, InternetDevels, rhm50, naveenvalecha, andypost, mandar.harkare, sergeypavlenko, sidharthap, herom, SIz, tkuldeep17: Replace user_access() calls with $account->hasPermission() in all core modules except user.

Part of #2048171: [meta] Replace user_access() calls with $account->hasPermission() wherever possible.

Change records for this issue:

Comments

internetdevels’s picture

Status: Active » Needs review
StatusFileSize
new3.96 KB

Patch attached.

andypost’s picture

Component: user system » contact.module
  1. +++ b/core/modules/contact/contact.pages.inc
    @@ -24,7 +24,8 @@
    +  $user = Drupal::request()->attributes->get('_account');
    

    Use Drupal::currentUser() service https://drupal.org/node/2032447

  2. +++ b/core/modules/contact/contact.pages.inc
    @@ -24,7 +24,8 @@
    +  if (!$user->hasPermission('administer contact forms')) {
    
    @@ -36,7 +37,7 @@ function contact_site_page(Category $category = NULL) {
    +      if ($user->hasPermission('administer contact forms')) {
    
    @@ -71,7 +72,7 @@ function contact_personal_page($recipient) {
       global $user;
    ...
    +  if (!$user->hasPermission('administer contact forms') && !$user->hasPermission('administer users')) {
    

    This will need re-roll after #1938390: Convert contact_site_page and contact_person_page to a new-style Controller

kim.pepper’s picture

This is the only occurrence of user_access left in the contact module.

andypost’s picture

Priority: Minor » Normal
Status: Needs review » Reviewed & tested by the community

Yep, the one one left

xjm’s picture

Title: Replace user_access() calls with $account->hasPermission() in contact module » Replace user_access() calls with $account->hasPermission() in all core modules except user
Status: Reviewed & tested by the community » Needs work

Thanks for your work on this!

See #2048171-17: [meta] Replace user_access() calls with $account->hasPermission() wherever possible.. Expanding the scope of this issue; please update this patch to include fixes to all modules except user.module and then confirm that no instances remain in core/modules that are not in core/modules/user before RTBCing.

xjm’s picture

Oh, and please help make sure everyone who worked on the duplicates for other core modules gets credit for this patch! Put a commit message in the summary and final RTBC comment for core maintainers.

kim.pepper’s picture

Hi Jess,

There are only 3 other issues left for this (including user module issue). 23 have already been committed.

I'm not sure how much efficiency we'll get out of combining them?

Kim

kim.pepper’s picture

Status: Needs work » Needs review
StatusFileSize
new54.37 KB

OK. Here's a combined patch of all those listed above.

andypost’s picture

Issue summary: View changes

Compiled list of people involved from all duplicate issues #8:

InternetDevels, rhm50
naveenvalecha, InternetDevels
rhm50, InternetDevels, andypost
InternetDevels, mandar.harkare, sergeypavlenko
sidharthap, InternetDevels
andypost, InternetDevels, kim.pepper, naveenvalecha
InternetDevels, SIz, sidharthap
tkuldeep17, herom, sidharthap, InternetDevels
rhm50, InternetDevels
herom, InternetDevels

Issue #2061977 by kim.pepper, InternetDevels, rhm50, naveenvalecha, andypost, mandar.harkare, sergeypavlenko, sidharthap, herom, SIz, tkuldeep17: Replace user_access() calls with $account->hasPermission() in all core modules except user.

added to summary

andypost’s picture

Status: Needs review » Needs work
+++ b/core/authorize.php
@@ -58,7 +58,7 @@ function authorize_access_denied_page() {
+  return settings()->get('allow_authorize_operations', TRUE) && Drupal::currentUser()->hasPermission('administer software updates');

+++ b/core/includes/bootstrap.inc
@@ -2860,13 +2860,13 @@ function drupal_classloader_register($name, $path) {
+ * function datetime_default_format_type() {

+++ b/core/includes/menu.inc
@@ -599,10 +599,10 @@ function _menu_check_access(&$item, $map) {
+    // As call_user_func_array is quite slow and user_access is a very common ¶

+++ b/core/lib/Drupal/Core/Extension/UpdateModuleHandler.php
@@ -46,7 +46,9 @@ public function getImplementations($hook) {
+      // Those are needed by

This changes should be fixed in #2062043: Replace user_access() calls with $account->hasPermission() in core files

kim.pepper’s picture

Status: Needs work » Needs review
StatusFileSize
new51.33 KB
new3.06 KB

Re-roll plus remove core includes as per #11

andypost’s picture

Status: Needs review » Reviewed & tested by the community

RTBC if green

The last submitted patch, 12: 2061977-user-access-12.patch, failed testing.

herom’s picture

Component: contact.module » other
Status: Reviewed & tested by the community » Needs work

not green...

and a couple of things I noticed, that should be fixed with the next patch:

  1. +++ b/core/modules/views/views.api.php
    @@ -377,7 +377,9 @@ function hook_views_form_substitutions() {
    +  $account = Drupal::currentUser();
    +
    +  if ($view->name == 'my_special_view' && $account->hasPermission('my special permission') && $display_id == 'public_display') {
    
    @@ -444,7 +446,9 @@ function hook_views_pre_execute(ViewExecutable $view) {
    +  $account = Drupal::currentUser();
    +
    +  if (count($view->query->tables) > 2 && $account->hasPermission('administer views')) {
    
    +++ b/core/modules/views/views.module
    @@ -332,7 +332,8 @@ function views_page_alter(&$page) {
    +  $account = Drupal::currentUser();
    +  if (!$account || !$account->hasPermission('access contextual links')) {
    

    Drupal:: called without "\" prefix. And no need for local variable here, you can call \Drupal::currentUser()->hasPermission() directly.
    Also, !$account || is unnecessary, since that would always be false here.

  2. +++ b/core/update.php
    @@ -70,7 +70,7 @@ function update_helpful_links() {
    -  if (user_access('access administration pages')) {
    +  if (Drupal::currentUser()->hasPermission('access administration pages')) {
    

    this also belongs to the core files issue.

herom’s picture

Status: Needs work » Needs review
StatusFileSize
new6.04 KB
new50.52 KB

fixing the tests, points in #15, and a few extra $account params.

Status: Needs review » Needs work

The last submitted patch, 16: 2061977-user-access-16.patch, failed testing.

herom’s picture

Status: Needs work » Needs review
StatusFileSize
new525 bytes
new50.52 KB

oops.

andypost’s picture

18: 2061977-user-access-18.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 18: 2061977-user-access-18.patch, failed testing.

herom’s picture

Status: Needs work » Needs review
StatusFileSize
new49.87 KB

reroll.

Status: Needs review » Needs work

The last submitted patch, 21: 2061977-user-access-21.patch, failed testing.

herom’s picture

Status: Needs work » Needs review

21: 2061977-user-access-21.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 21: 2061977-user-access-21.patch, failed testing.

herom’s picture

the fails don't seem relevant, and are different than previous test.
doing another retest, to see what comes up.

herom’s picture

Status: Needs work » Needs review

21: 2061977-user-access-21.patch queued for re-testing.

ianthomas_uk’s picture

StatusFileSize
new19.16 KB
new43.08 KB

Reroll, plus replacing occurrences that have slipped in this patch. Interdiff shows the new occurrences I replaced after completing my reroll.

Status: Needs review » Needs work

The last submitted patch, 27: 2061977-user-access-27.patch, failed testing.

herom’s picture

Status: Needs work » Needs review
StatusFileSize
new41.75 KB
new652 bytes

rerolled + a one line fix.

herom’s picture

StatusFileSize
new41.75 KB

re-uploading the patch, since it didn't show up in the issue summary Files table.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

I think it is fine to not do injection for now.

+++ b/core/modules/node/node.api.php
@@ -562,23 +562,23 @@ function hook_node_load($nodes) {
-    if ($op == 'create' && user_access('create ' . $type . ' content', $account)) {
+    if ($op == 'create' && $account->hasPermission('create ' . $type . ' content')) {

The new way for this example is actually much nicer code!

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

  • webchick committed 25afa81 on 8.x
    Issue #2061977 by InternetDevels, kim.pepper, ianthomas_uk, herom:...

  • webchick committed 686b47c on 8.x
    Issue #2061977 by InternetDevels, kim.pepper, ianthomas_uk, herom, rhm50...
  • webchick committed a07a491 on 8.x
    Revert "Issue #2061977 by InternetDevels, kim.pepper, ianthomas_uk,...
webchick’s picture

Sorry, that last bit was to fix commit credit. I missed the nicely formatted commit message in the issue summary because I was trying to commit patches and talk on the phone at the same time. :P Thanks to herom for the alert!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

jthorson’s picture

jthorson’s picture