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

ENH Update code to avoid calling deprecated code #271

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/Extensions/ShareDraftContentControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SilverStripe\Control\Controller;
use SilverStripe\Core\Extension;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;

/**
Expand All @@ -24,17 +25,29 @@ class ShareDraftContentControllerExtension extends Extension
public function MakeShareDraftLink()
{
if ($member = Security::getCurrentUser()) {
if ($this->owner->hasMethod('CurrentPage') && $this->owner->CurrentPage()->canView($member)) {
return $this->owner->CurrentPage()->ShareTokenLink();
}
if ($this->owner->hasMethod('canView') && $this->owner->canView($member)) {
return $this->owner->ShareTokenLink();
if ($this->owner->hasMethod('currentRecord')) {
$link = $this->getShareTokenLink($this->owner->currentRecord(), $member);
} elseif ($this->owner->hasMethod('CurrentPage')) {
// Could be a non-LeftAndMain controller, since the extension is applied directly to Controller
$link = $this->getShareTokenLink($this->owner->CurrentPage(), $member);
}
$link ??= $this->getShareTokenLink($this->owner, $member);
}
if ($link) {
return $link;
}

return Security::permissionFailure();
}

private function getShareTokenLink(object $record, Member $member): ?string
{
if ($record->hasMethod('canView') && $record->canView($member)) {
return $record->ShareTokenLink();
}
return null;
}

/**
* @return string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%-- Placeholder is re-rendered by the ShareDraftContent React component --%>
<span class="share-draft-content__placeholder"
data-url="<% if $CurrentPage.ShareDraftLinkAction %>{$CurrentPage.ShareDraftLinkAction}<% else %>{$Controller.CurrentPage.ShareDraftLinkAction}<% end_if %>"
Copy link
Member Author

Choose a reason for hiding this comment

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

Note these references must not be updated to currentRecord.

CurrentPage is not actually accessing the CurrentPage() method on the LeftAndMain (or in this case CMSMainPageEditController) instance. Instead, because of how this template gets rendered, it's falling back to the global template provider $CurrentPage which calls Controller::curr().

ShareDraftLinkAction() then gets called on the controller, not on the page record.

data-helpurl="https://userhelp.silverstripe.org/en/4/optional_features/share_draft_content"
data-helpurl="https://userhelp.silverstripe.org/en/optional_features/share_draft_content"
Copy link
Member Author

Choose a reason for hiding this comment

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

Not related but should be updated

>
</span>

Expand Down
Loading