Skip to content

Conversation

AlexMaxHorkun
Copy link
Contributor

It is unsafe to use sessions anywhere but controllers and blocks due to multiple web APIs in Magento

It is unsafe to use sessions anywhere but controllers and blocks due to multiple web APIs in Magento
@buskamuza
Copy link
Contributor

6.2.2. Request, Response, Session, Store Manager and Cookie objects MUST be used only in the Presentation layer.

https://devdocs.magento.com/guides/v2.2/coding-standards/technical-guidelines.html

So, yes, this is correct and is in guidelines. And it would be great to cover it with a test.

#### What has to be done
Add PHPMD rule which will be triggered when a class has a constructor parameter of type
SessionManagerInterface (and any of the implementations) and is not an instance of
ActionInterface or AbstractBlock.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Also a plugin for a block/controller should be OK to use sessions.
  2. Would be good to expand the rule to cover "6.2.2. Request, Response, Session, Store Manager and Cookie objects MUST be used only in the Presentation layer.". Can be implemented as separate rules.
  3. Also, data providers for UI components are part of presentation layer, so they can depend on session.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. makes sense, but I don't know how a block plugin can be identified in a PHPMD rule. Controller plugin can be sort of identified by having before/after/aroundExecute/Dispatch but I'm not sure if that would be correct. I think it would be OK if devs would just have to put SuppressWarnings for those cases
  2. Ok, I'll update the proposal
  3. Sure, instances of Magento\Framework\View\Element\UiComponent\DataProvider\Document and Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface will be ignored by the rule then

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The plugin has $subject which can go through the same checks on whether it's a block or controller.

@AlexMaxHorkun
Copy link
Contributor Author

I don't think a warning should be issued when using store manager - it can be used outside of presentation layer.
My goal with this proposal is to prevent unexpected behavior when using the same model-layer classes for both HTML and web API, and issuing warnings for request/response objects goes outside of my intent

Copy link
Contributor

@buskamuza buskamuza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved from my side.
Though the proposed rules don't cover all the rules from the Tech Guidelines, they bring a good improvement and allow simpler implementation w/o dealing with exceptional cases. So it makes sense to implement it as described for now.

@Vinai
Copy link

Vinai commented Oct 24, 2018

In general I agree, but I do have to say that \Magento\Customer\Model\Authorization\CustomerSessionUserContext makes REST API usage on the store front very convenient.

I think It should stick around for at least a few years still, until the GraphQL integration is mature enough and has effectively replaces the frontend REST API uses in extensions.

@AlexMaxHorkun
Copy link
Contributor Author

@Vinai CustomerSessionUserContext is an exception case when we can't identify automatically whether a session object is used properly - CustomerSessionUserContext is fine and will stay

@buskamuza buskamuza merged commit be8731a into magento:master Oct 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants