Allow reverting a completed token to an un-complete state (undo "deny" scenario)

This commit is contained in:
2022-02-15 14:56:45 +00:00
parent 6cb5d759b4
commit e029728d69

View File

@@ -264,6 +264,54 @@ abstract class BasicTokenRepository
return $model;
}
/**
* Reverts a completed token request to its previously incomplete state.
*
* @param TokenOwnerInterface $tokenOwner The owner of the token to revert.
*
* @return Model|false
*/
public function revert(TokenOwnerInterface $tokenOwner)
{
if ($this->tokenLogger) {
$this->tokenLogger->debug('Reverting token for {{owner}}', [
'owner' => $tokenOwner
]);
}
// Find an unexpired, incomplete tokens for the specified owner.
// Using first() works because owners can only have at most 1 active token
$model = $this->classMapper->getClassMapping($this->modelIdentifier)::query()
->where('owner_id', $tokenOwner->getId())
->where('completed', true)
->where(function($query) {
return $query->where('expires_at', '>', Carbon::now())->orWhereNull('expires_at');
})
->first();
if ($model === null) {
if ($this->tokenLogger) {
$this->tokenLogger->warn('Token not found!');
}
return false;
}
if ($this->tokenLogger) {
$this->tokenLogger->debug('Found:', [
'model' => $model
]);
}
$model->fill([
'completed' => false,
'completed_at' => null,
]);
$model->save();
return $model;
}
/**
* Determine if a specified token owner has an incomplete and unexpired token.
*