Skip to content

Commit

Permalink
Merge branch '4.0' into 4
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Dec 12, 2024
2 parents 8a893c8 + 6cccd25 commit 6f5fd98
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
47 changes: 39 additions & 8 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,25 +25,55 @@ 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
*/
public function getShareDraftLinkAction()
{
if ($this->owner->config()->get('url_segment')) {
return $this->owner->Link('MakeShareDraftLink');
$owner = $this->getOwner();
if (!$owner->config()->get('url_segment')) {
return '';
}
$id = $this->getRecordID();
if (!$id) {
return '';
}
return $owner->Link(Controller::join_links('MakeShareDraftLink', $id));
}

private function getRecordID(): ?int
{
$owner = $this->getOwner();
if ($owner->hasMethod('currentRecordID')) {
return $owner->currentRecordID();
}
// Could be a non-LeftAndMain controller, since the extension is applied directly to Controller
if ($owner->hasMethod('CurrentPage')) {
return $owner->CurrentPage()?->ID;
}
return '';
return null;
}
}
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 %>"
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"
>
</span>

Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2177,9 +2177,9 @@ cosmiconfig@^9.0.0:
parse-json "^5.2.0"

cross-spawn@^7.0.2, cross-spawn@^7.0.3:
version "7.0.5"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.5.tgz#910aac880ff5243da96b728bc6521a5f6c2f2f82"
integrity sha512-ZVJrKKYunU38/76t0RMOulHOnUcbU9GbpWKAOZ0mhjr7CX6FVrH+4FrAapSOekrgFQ3f/8gwMEuIft0aKq6Hug==
version "7.0.6"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.6.tgz#8a58fe78f00dcd70c370451759dfbfaf03e8ee9f"
integrity sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==
dependencies:
path-key "^3.1.0"
shebang-command "^2.0.0"
Expand Down

0 comments on commit 6f5fd98

Please sign in to comment.