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

Allow users to share draft files to non members #250

Open
wants to merge 1 commit into
base: 3
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Name: sharedraftcontent
After: framework/routes#coreroutes
---
SilverStripe\Assets\File:
extensions:
- SilverStripe\ShareDraftContent\Extensions\ShareDraftContentSiteTreeExtension
SilverStripe\CMS\Model\SiteTree:
extensions:
- SilverStripe\ShareDraftContent\Extensions\ShareDraftContentSiteTreeExtension
Expand Down
79 changes: 75 additions & 4 deletions src/Controllers/ShareDraftController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use BadMethodCallException;
use PageController;
use SilverStripe\Assets\File;
use SilverStripe\Core\Environment;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPRequestBuilder;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPStreamResponse;
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
use SilverStripe\Control\Session;
use SilverStripe\Core\Injector\Injector;
Expand Down Expand Up @@ -70,9 +72,27 @@ public function preview(HTTPRequest $request)
{
// Ensure this URL doesn't get picked up by HTTP caches
HTTPCacheControlMiddleware::singleton()->disableCache();

$key = $request->param('Key');
$token = $request->param('Token');

$shareToken = ShareToken::get()->filter('Token', $token)->first();

if (!$shareToken) {
return $this->errorPage();
}

if ($shareToken->PageID) {
return $this->previewPage($shareToken);
}

if ($shareToken->FileID) {
return $this->previewFile($shareToken);
}
}

public function previewFile($shareToken)
{
$key = $this->request->param('Key');

try {
$session = $this->getRequest()->getSession();
} catch (BadMethodCallException $e) {
Expand All @@ -81,12 +101,63 @@ public function preview(HTTPRequest $request)
->setSession(Injector::inst()->create(Session::class, []))
->getSession();
}
$shareToken = ShareToken::get()->filter('Token', $token)->first();

if (!$shareToken) {
/** @var File|ShareDraftContentSiteTreeExtension $dataObject */
$dataObject = Versioned::get_by_stage(File::class, Versioned::DRAFT)
->byID($shareToken->FileID);

$this->extend('updatePage', $dataObject);

$latest = Versioned::get_latest_version(File::class, $shareToken->FileID);

if (!$shareToken->isExpired() && $dataObject->generateKey($shareToken->Token) === $key) {
Requirements::css('silverstripe/sharedraftcontent: client/dist/styles/bundle-frontend.css');
// Temporarily un-secure the draft site and switch to draft
$oldSecured = $this->getIsDraftSecured($session);
$oldMode = Versioned::get_default_reading_mode();
static::$isViewingPreview = true;

// Process page inside an unsecured draft container
try {
$this->setIsDraftSecured($session, false);
Versioned::set_default_reading_mode('Stage.Stage');

} finally {
$this->setIsDraftSecured($session, $oldSecured);
// Use set_default_reading_mode() instead of set_reading_mode() because that's
// what's used in Versioned::choose_site_stage()
Versioned::set_default_reading_mode($oldMode);
static::$isViewingPreview = false;
}

$stream = $dataObject->getStream();
$size = $dataObject->getSize();
$mime = $dataObject->getMimeType();
$response = HTTPStreamResponse::create($stream, $size)
->addHeader('Content-Type', $mime);

$response->addHeader('Cache-Control', 'private');

return $response;
} else {
return $this->errorPage();
}

}

public function previewPage($shareToken)
{
$key = $this->request->param('Key');

try {
$session = $this->getRequest()->getSession();
} catch (BadMethodCallException $e) {
// Create a new session
$session = $this->getRequest()
->setSession(Injector::inst()->create(Session::class, []))
->getSession();
}

/** @var SiteTree|ShareDraftContentSiteTreeExtension $page */
$page = Versioned::get_by_stage(SiteTree::class, Versioned::DRAFT)
->byID($shareToken->PageID);
Expand Down
4 changes: 3 additions & 1 deletion src/Models/ShareToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\ShareDraftContent\Models;

use Page;
use SilverStripe\Assets\File;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBDatetime;

Expand All @@ -26,7 +27,8 @@ class ShareToken extends DataObject
* @var array
*/
private static $has_one = array(
'Page' => Page::class
'Page' => Page::class,
'File' => File::class,
);

/**
Expand Down