Skip to content

Commit

Permalink
Hotfix infinite loop
Browse files Browse the repository at this point in the history
When a user is retrieved code like `Craft::$app->getUsers()->unlockUser($this);` is called on user init. This can update the user record which causes an infinite loop when the code in `handleUserElementChanges()` queries for the user again.
  • Loading branch information
lukeholder committed Nov 7, 2024
1 parent 71830b3 commit 557459c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Stripe

## 1.2.0.1 - 2024-11-07

- Fixed an infinite loop that could occur when querying for a user.

## 1.2.0 - 2024-11-06

> [!NOTE]
Expand Down
11 changes: 5 additions & 6 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,14 +575,14 @@ function(DefineMenuItemsEvent $event) {
private function handleUserElementChanges(): void
{
// if email address got changed - update stripe
Event::on(UserRecord::class, UserRecord::EVENT_BEFORE_UPDATE, function(ModelEvent $event) {
$userRecord = $event->sender;
Event::on(User::class, User::EVENT_BEFORE_SAVE, function(ModelEvent $event) {
/** @var User|StripeCustomerBehavior $user */
$user = Craft::$app->getUsers()->getUserById($userRecord->id);
$user = $event->sender;
$userRecord = UserRecord::findOne($user->id);
$settings = $this->getSettings();
if ($user->isCredentialed && $settings['syncChangedUserEmailsToStripe']) {
$oldEmail = $userRecord->getOldAttribute('email');
$newEmail = $userRecord->getAttribute('email');
$oldEmail = $userRecord->getAttribute('email');
$newEmail = $user->email;
if ($oldEmail != $newEmail) {
$customers = $user->getStripeCustomers();
if ($customers->isNotEmpty()) {
Expand Down Expand Up @@ -671,7 +671,6 @@ public function getCpNavItem(): ?array
];
}


return $ret;
}

Expand Down

0 comments on commit 557459c

Please sign in to comment.