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

API Add new Owner relation for handling permissions #127

Merged

Conversation

GuySartorelli
Copy link
Member

@GuySartorelli GuySartorelli commented Dec 4, 2023

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

@GuySartorelli GuySartorelli marked this pull request as draft December 4, 2023 04:53
@GuySartorelli GuySartorelli force-pushed the pulls/4/ownership-model branch 2 times, most recently from cd50e9b to 6d1453d Compare December 5, 2023 03:50
Comment on lines 38 to 47
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,
],
];
Copy link
Member Author

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.

Comment on lines 295 to 313
/**
* 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;
}
Copy link
Member Author

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.

@GuySartorelli GuySartorelli force-pushed the pulls/4/ownership-model branch 3 times, most recently from 34646cf to 974f6f8 Compare December 5, 2023 21:26
@GuySartorelli GuySartorelli marked this pull request as ready for review December 5, 2023 21:28
README.md Show resolved Hide resolved
// See also the Owner method.
'Owner' => [
'class' => DataObject::class,
DataObjectSchema::HASONE_IS_MULTIRECIPROCAL => true,
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

@emteknetnz emteknetnz left a comment

Choose a reason for hiding this comment

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

Needs rebase

@GuySartorelli
Copy link
Member Author

Rebased and conflicts resolved

@emteknetnz emteknetnz merged commit 7f65d4f into silverstripe:4 Dec 12, 2023
10 checks passed
@emteknetnz emteknetnz deleted the pulls/4/ownership-model branch December 12, 2023 01:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants