Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let controllers specify what type a view to be rendered is #14309

Merged
merged 5 commits into from May 17, 2019

Conversation

diosmosis
Copy link
Member

Currently when normal non-admin views are rendered in admin controllers, it is assumed they are admin views. Settings for admin views are thus set, which can cause problems for normal views.

For example, say a matomo allows embedding pages, but not settings pages, then a user loads a site with no data. The siteWithoutData() action is invoked, and it is assumed to be an admin controller action, so setBasicVariablesAdminView() is called. This method assumes the view is a settings page so it will disable embedding pages since enable_framed_settings is not enabled.

This PR fixes this by allowing the view type to be specified to renderTemplate() and other methods. It's a bit odd looking, but it's hard to find a way to fix this properly given how many variables get set.

…not always assumed based on controller type.
@diosmosis diosmosis added the Needs Review PRs that need a code review label Apr 2, 2019
@diosmosis diosmosis added this to the 3.10.0 milestone Apr 2, 2019
@@ -576,11 +583,16 @@ protected function setMaxDateView(Date $maxDate, $view)
* Will exit on error.
*
* @param View $view
* @param string|null $viewType 'basic' or 'admin'. If null, set based on the type of controller.
Copy link
Member

Choose a reason for hiding this comment

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

if someone extends this controller, and passes admin it wouldn't work as expected right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you suggesting if admin is passed, we should only allow it if $this instanceof ControllerAdmin? Or something else?

Copy link
Member

Choose a reason for hiding this comment

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

I just meant might need to be updated in the docs ?
The check with instanceof might be useful though and prevent false expectations if admin is passed. This way a developer would directly notice to extend different class.

Copy link
Member Author

Choose a reason for hiding this comment

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

It would sort of work as expected if 'admin' is passed, since setBasicVariablesView() is overridden in ControllerAdmin. I think there would be many more variables set (and possibly an exception if there's no idsite or something like that).

We could simply keep it undocumented except for renderTemplate(), doesn't really need to be set anywhere else I think.

Copy link
Member

Choose a reason for hiding this comment

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

Or should we just move setBasicVariablesNoneAdminView etc to plugin\Controller and make it work even when it extends only Plugin\Controller and not ControllerAdmin? just a random thought, no preference in any direction here

Copy link
Member Author

Choose a reason for hiding this comment

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

The problem is with:

class SitesManagerController extends Plugin\AdminController {
    public function myNormalAction() {
        return $this->renderTemplate('bar.twig');
    }
}

Where ControllerAdmin::setBasicVariablesView() calls ControllerAdmin::setBasicVariablesAdminView(), when we don't want it to.

Copy link
Member Author

Choose a reason for hiding this comment

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

Or are you just talking about that specific case? I'm not sure if we should allow it, ie, should normal controllers be allowed to render admin pages?

Copy link
Member

Choose a reason for hiding this comment

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

should normal controllers be allowed to render admin pages?
No preference. It's just that renderTemplate() in Plugin\Controller has this parameter and it's documented to set admin optionally. So I would expect it to work even when Plugin\Controller is extended. We can also throw an exception in this case though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will throw an exception. We could change $viewType to $isBasicView or something, but then a dev could supply false.

This isn't an ideal solution, but I'm not too sure of another easy/quick one.

Copy link
Member

Choose a reason for hiding this comment

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

Will throw an exception.

👍

@tsteur
Copy link
Member

tsteur commented Apr 10, 2019

@diosmosis it seems this is actually breaking API: https://travis-ci.org/matomo-org/matomo/jobs/515032508#L925

The method renderTemplate is marked as @api preventing us from adding any variables because it would be impossible for a plugin to be compatible with previous and newer versions (older versions would not want to see the parameter while newer ones should have). Assuming this isn't used much in the wild we could possibly be fine with the breaking change but would need to adjust tag manager and increase the required Matomo version number for tag manager.

I will look through the detailed code changes later again.

} elseif ((empty($this->site) || empty($this->idSite))
&& $viewType == 'admin'
) {
$this->setBasicVariablesView($view, $viewType);
Copy link
Member

Choose a reason for hiding this comment

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

@diosmosis wouldn't we also call this when using viewType=view in some cases? basic variables does different things than general variables?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes seems so, I'm not sure why I made this change... I'll have to test again

@diosmosis diosmosis merged commit 7215ff1 into 3.x-dev May 17, 2019
@diosmosis diosmosis deleted the allow-specifying-view-type branch May 17, 2019 10:52
@mattab mattab added the c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. label Jun 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. Needs Review PRs that need a code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants