Updated: Comment #0
Problem/Motivation
#2048171: [meta] Replace user_access() calls with $account->hasPermission() wherever possible. is moving calls from user_access to instead access the _account from the request.
But I think this is crummy DX
- if (!user_access('access tour')) {
+ $account = Drupal::request()->attributes->get('_account');
+ if (!$account->hasPermission('access tour')) {
As to is this:
- global $user;
+ $user = Drupal::request()->attributes->get('_account');
Proposed resolution
Add Drupal::userAccess and let procedural code use that instead. Plugins and other Object oriented code should inject the request and just access it directly, but I think procedural code needs a convenience wrapper.
Similarly add Drupal::currentAccount() to replace global $user
Remaining tasks
Review it.
Decide on action.
User interface changes
None.
API changes
New convenience methods on \Drupal
Related Issues
#2048171: [meta] Replace user_access() calls with $account->hasPermission() wherever possible.
#2062031: Replace user_access() calls with $account->hasPermission() in tour module
#2047951: [META] Remove calls to deprecated global $user and $GLOBALS['user']
#1945024: Remove subrequests from central controllers and use the controller resolver directly.
#2062151: Create a current user service to ensure that current account is always available
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | user-access-dx.7.interdiff.txt | 797 bytes | larowlan |
| #9 | user-access-dx.7.patch | 1.92 KB | larowlan |
| #3 | user-access-dx.3.interdiff.txt | 732 bytes | larowlan |
| #3 | user-access-dx.3.patch | 1.92 KB | larowlan |
| user-access-dx.patch | 1.44 KB | larowlan |
Comments
Comment #1
larowlanComment #2
damien tournoud commentedGiven that we also have many stupid patches like this in the queue:
.... I suggest we just do a
Drupal::currentAccount()(orDrupal::account()).Comment #3
larowlanLike so?
Comment #4
larowlanAdded #2047951: [META] Remove calls to deprecated global $user and $GLOBALS['user'] to summary
Comment #4.0
larowlanUpdated issue summary.
Comment #5
larowlanUpdated issue summary to include #2 and #3
Comment #6
Crell commentedNull. Not false: http://www.garfieldtech.com/blog/empty-return-values
Comment #7
larowlan@Crell for ::currentAccount() only right?
Comment #8
Crell commentedCorrect. Sorry, I didn't realize the dreditor snippet would be that useless. :-)
A method should return "an object or NULL if there isn't one" or "TRUE or FALSE and nothing else". But the absence of a valid object is not untrue, it's the absence of a value. Absence of a value is NULL, not untrue.
Any time you use the value FALSE, mentally translate it to "untrue". If it doesn't make sense, then you shouldn't be using it. :-)
Comment #9
larowlanHere tis
Comment #10
Crell commentedI do hope we're not shooting ourselves in the foot with all of these "make procedural code pretty" patches...
Comment #11
catchI'm starting to question why we have _account as a custom property on the request attributes and this being the only way to get at it. What about having a service which does this? Then we wouldn't need that actual logic in \Drupal. This is related to #2040065: Remove _account from request and use the current user service instead. just feels like we're piling hacks on at the moment.
Comment #12
larowlanIs that this:
#1858196: [meta] Leverage Symfony Session components
But yeah, questioning the why as well as the how is a good idea.
Comment #13
catchIt might well be that. In which case we either need to postpone all these issues on that one, or find an API (regardless of injected code or not) that's going to be forwards compatible with that patch so we're not doing ->get('_account') much less ->set('_account') all over the place.
Comment #14
larowlank, will postpone the two meta's
Comment #15
msonnabaum commentedI think this patch is showing that we dont need userAccess, we need a CurrentUser service:
Comment #16
larowlanAdded #2062151: Create a current user service to ensure that current account is always available and #1945024: Remove subrequests from central controllers and use the controller resolver directly. to related issues in summary.
Comment #16.0
larowlanUpdated issue summary.
Comment #17
larowlanPostponed on #2062151: Create a current user service to ensure that current account is always available
Comment #18
larowlanFixed in #2062151: Create a current user service to ensure that current account is always available
Comment #18.0
larowlanUpdated issue summary