Let's start to convert all calls to user_access() with the new AccountInterface::hasPermission() method.
Part of #2048171: [meta] Replace user_access() calls with $account->hasPermission() wherever possible.
Change records for this issue:
| Comment | File | Size | Author |
|---|---|---|---|
| #23 | interdiff-2062007-21-23.txt | 8.18 KB | herom |
| #23 | node_replace_user_access-2062007-23.patch | 20.37 KB | herom |
Comments
Comment #1
internetdevels commentedHere is the patch. In some cases Drupal::request()->attributes->get('_account') return NULL to account object. That why I added drupal_anonymous_user() function for fix the issue. But looks it requires additional investigation.
Comment #2
internetdevels commentedOops, forgot change status.
Comment #4
andypostThis could be fixed in #2048223: Add $account argument to AccessCheckInterface::access() method and use the current_user service
Use Drupal::currentUser() service https://drupal.org/node/2032447
Comment #5
sidharthapcorrected #4
Comment #7
tkuldeep17 commentedHi sidharthap
For using method of Drupal.php class, you should use
\Drupal::currentUser()->hasPermission('access content');instead ofDrupal::currentUser()->hasPermission('access content');As all the classes have some namespaces, but Drupal.php is defined in global namespace, so for using global namespace you have to prefix with '\'. For more about global name spaces.
Comment #8
tkuldeep17 commentedSubmitting my patch...
Comment #10
tkuldeep17 commentedResubmitting patch..
Comment #12
tkuldeep17 commentedVery Sorry, I was creating patch from old code base.. Now this patch is from new code base..
Comment #13
tkuldeep17 commentedComment #15
tkuldeep17 commentedFixing php syntax error..
Comment #17
tkuldeep17 commentedI am not getting, why drupal bot is giving php syntax error, I am using php 5.5. Drupal bot is saying 'unexpected ;'
return \Drupal::currentUser()->hasPermission(('revert revisions') || \Drupal::currentUser()->hasPermission('administer nodes');Please help me..
In this patch I am adding parenthesis
As in my local I am not getting error.. not able resolve it..
Comment #19
tkuldeep17 commentedSorry forgot to updated code base, so resubmitting patch..
Comment #21
herom commentedadding dependency injection for
$accountinNodeFormController.also, reverting changes in
node/Plugin/viewsfor now, since #2109433: Replace user_access() through injected user accounts in views. has a better solution for it.Comment #22
andypostindent is wrong, use 2 spaces
unnecessary change
Please use variable $current_user or $account to not make 2 calls to \Drupal wrapper
Also no reason in 2 blank lines
Use temporary variable $current_user or $account here
Comment #23
herom commentedcleanup done.
Comment #24
andypostThe coding standards does not prohibit wrapping long conditions https://drupal.org/coding-standards#linelength
I think more readable code always makes sense
It was used before patch so I think it's fine
Comment #25
xjmThanks for your work on this! See #2048171-17: [meta] Replace user_access() calls with $account->hasPermission() wherever possible..