-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API Add new Owner relation for handling permissions (#127)
- Loading branch information
1 parent
9010e73
commit 7f65d4f
Showing
8 changed files
with
227 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ public function getDefaultTitle(): string | |
'File missing', | ||
); | ||
} | ||
|
||
return (string) $this->getDescription(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,17 @@ | |
use SilverStripe\Forms\RequiredFields; | ||
use SilverStripe\LinkField\Type\Registry; | ||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\ORM\DataObjectSchema; | ||
use SilverStripe\ORM\FieldType\DBHTMLText; | ||
use SilverStripe\Versioned\Versioned; | ||
|
||
/** | ||
* A Link Data Object. This class should be treated as abstract. You should never directly interact with a plain Link | ||
* instance | ||
* | ||
* Note that links should be added via a has_one or has_many relation, NEVER a many_many relation. This is because | ||
* some functionality such as the can* methods rely on having a single Owner. | ||
* | ||
* @property string $Title | ||
* @property bool $OpenInNew | ||
*/ | ||
|
@@ -31,6 +35,17 @@ class Link extends DataObject | |
'OpenInNew' => 'Boolean', | ||
]; | ||
|
||
private static array $has_one = [ | ||
// Note that this handles one-to-many relations AND one-to-one relations. | ||
// Any has_one pointing at Link will be intentionally double handled - this allows us to use the owner | ||
// for permission checks and to link back to the owner from reports, etc. | ||
// See also the Owner method. | ||
'Owner' => [ | ||
'class' => DataObject::class, | ||
DataObjectSchema::HAS_ONE_MULTI_RELATIONAL => true, | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
GuySartorelli
Author
Member
|
||
], | ||
]; | ||
|
||
private static array $extensions = [ | ||
Versioned::class, | ||
]; | ||
|
@@ -300,6 +315,33 @@ public function getVersionedState(): string | |
return 'published'; | ||
} | ||
|
||
/** | ||
* Get the owner of this link, if there is one. | ||
* | ||
* Returns null if the reciprocal relation is a has_one which no longer contains this link | ||
* or if there simply is no actual owner record in the db. | ||
*/ | ||
public function Owner(): ?DataObject | ||
{ | ||
$owner = $this->getComponent('Owner'); | ||
|
||
// Since the has_one is being stored in two places, double check the owner | ||
// actually still owns this record. If not, return null. | ||
if ($this->OwnerRelation && $owner->getRelationType($this->OwnerRelation) === 'has_one') { | ||
$idField = "{$this->OwnerRelation}ID"; | ||
if ($owner->$idField !== $this->ID) { | ||
return null; | ||
} | ||
} | ||
|
||
// Return null if there simply is no owner | ||
if (!$owner || !$owner->isInDB()) { | ||
return null; | ||
} | ||
|
||
return $owner; | ||
} | ||
|
||
/** | ||
* Get all link types except the generic one | ||
* | ||
|
@@ -327,7 +369,7 @@ public function getDisplayTitle(): string | |
if ($this->Title) { | ||
return $this->Title; | ||
} | ||
|
||
$defaultLinkTitle = $this->getDefaultTitle(); | ||
|
||
$this->extend('updateDefaultLinkTitle', $defaultLinkTitle); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Tests\Form; | ||
|
||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\LinkField\Form\LinkField; | ||
use SilverStripe\LinkField\Models\Link; | ||
use SilverStripe\LinkField\Tests\Models\LinkTest\LinkOwner; | ||
|
||
class LinkFieldTest extends SapphireTest | ||
{ | ||
protected $usesDatabase = true; | ||
|
||
protected static $extra_dataobjects = [ | ||
LinkOwner::class, | ||
]; | ||
|
||
/** | ||
* When we save a link into the has_one of a record, we also need to save | ||
* the Owner has_one on the link itself. | ||
*/ | ||
public function testSaveInto() | ||
{ | ||
// Prepare fixtures (need new records for this) | ||
$field = new LinkField('Link'); | ||
$link = new Link(); | ||
$link->write(); | ||
$owner = new LinkOwner(); | ||
$owner->write(); | ||
|
||
// Save link into owner | ||
$field->setValue($link->ID); | ||
$field->saveInto($owner); | ||
// Get the link again - the new values are in the DB. | ||
$link = Link::get()->byID($link->ID); | ||
|
||
// Validate | ||
$this->assertSame($link->ID, $owner->LinkID); | ||
$this->assertSame($owner->ID, $link->OwnerID); | ||
$this->assertSame($owner->ClassName, $link->OwnerClass); | ||
$this->assertSame('Link', $link->OwnerRelation); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Tests\Models\LinkTest; | ||
|
||
use SilverStripe\Dev\TestOnly; | ||
use SilverStripe\LinkField\Models\Link; | ||
use SilverStripe\ORM\DataObject; | ||
|
||
class LinkOwner extends DataObject implements TestOnly | ||
{ | ||
private static array $has_one = [ | ||
'Link' => Link::class, | ||
]; | ||
|
||
private static array $has_many = [ | ||
'LinkList' => Link::class . '.Owner', | ||
]; | ||
} |
@GuySartorelli I think this constant does not exist in current stable version of 5.1