-
Notifications
You must be signed in to change notification settings - Fork 15
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
API Add new Owner relation for handling permissions #127
API Add new Owner relation for handling permissions #127
Conversation
cd50e9b
to
6d1453d
Compare
src/Models/Link.php
Outdated
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::HASONE_IS_MULTIRECIPROCAL => true, | ||
], | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the associated framework PR and docs PR for more about this weird has_one
syntax.
src/Models/Link.php
Outdated
/** | ||
* 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. | ||
*/ | ||
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. | ||
$ownerRelationType = $owner->getRelationType($this->OwnerRelation); | ||
if ($ownerRelationType === 'has_one') { | ||
$idField = "{$this->OwnerRelation}ID"; | ||
if ($owner->$idField !== $this->ID) { | ||
return null; | ||
} | ||
} | ||
return $owner; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're double handling has_one
relations to link by storing the has_one
in the link as well.
This allows us to use the Owner
relation to check permissions, and to link to the owner in reports, etc.
The double handling is mitigated here by returning null
if the owner says it doesn't know about the link.
34646cf
to
974f6f8
Compare
src/Models/Link.php
Outdated
// See also the Owner method. | ||
'Owner' => [ | ||
'class' => DataObject::class, | ||
DataObjectSchema::HASONE_IS_MULTIRECIPROCAL => true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've suggested changing this in the framework PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
974f6f8
to
03b67c8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs rebase
03b67c8
to
8991e41
Compare
Rebased and conflicts resolved |
Dependencies
This PR requires silverstripe/silverstripe-framework#11084 to function and for tests to go green but an installer CI run is linked in the issue.
Issues