Skip to content

Commit

Permalink
fix: fix token usage metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
kumy committed Oct 17, 2023
1 parent 11684bf commit 07bbe29
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GeoKrety\Model\EmailRevalidateToken;
use GeoKrety\Service\Smarty;
use Sugar\Event;

class UserEmailRevalidateToken extends Base {
public function get(\Base $f3) {
Expand Down Expand Up @@ -31,9 +32,11 @@ public function get(\Base $f3) {
$token->used = EmailRevalidateToken::TOKEN_VALIDATED;
$token->save();
\Flash::instance()->addMessage(_('You have successfully validated your email address.'), 'success');
Event::instance()->emit('email-revalidation.token.used', $token);
$f3->reroute(sprintf('@user_details(@userid=%d)', $token->user->id));
}
\Flash::instance()->addMessage(_('Sorry this token is not valid, already used or expired.'), 'danger');
Event::instance()->emit('email-revalidation.token.error', $token);
$token->token = $_token;
}

Expand Down
5 changes: 5 additions & 0 deletions website/app/GeoKrety/Model/EmailActivationToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use DB\SQL\Schema;
use Sugar\Event;

/**
* @property int|null id
Expand Down Expand Up @@ -259,6 +260,10 @@ public function __construct() {
// $this->beforeupdate(function ($self) {
// });

$this->afterinsert(function ($self) {
Event::instance()->emit('email.token.generated', $self);
});

$this->virtual('update_expire_on_datetime', function ($self): \DateTime {
$expire = $self->created_on_datetime ? clone $self->created_on_datetime : new \DateTime();

Expand Down
5 changes: 5 additions & 0 deletions website/app/GeoKrety/Model/EmailRevalidateToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DB\SQL\Schema;
use GeoKrety\Model\Traits\EmailField;
use Sugar\Event;

/**
* @property string token
Expand Down Expand Up @@ -113,6 +114,10 @@ public function __construct() {
}
});

$this->afterinsert(function ($self) {
Event::instance()->emit('email-revalidation.token.generated', $self);
});

$this->virtual('validate_expire_on_datetime', function ($self): \DateTime {
$expire = $self->created_on_datetime ? clone $self->created_on_datetime : new \DateTime();

Expand Down
16 changes: 14 additions & 2 deletions website/app/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,23 @@ function audit(string $event, $newObjectModel) {
});
$events->on('email.token.generated', function (GeoKrety\Model\EmailActivationToken $token) {
audit('email.token.generated', $token);
Metrics::counter('email_validation_token_created_total', 'Total number of email validation token created');
Metrics::counter('email_validation_token_total', 'Total number of email validation token created', ['verb'], ['created']);
});
$events->on('email.token.used', function (GeoKrety\Model\EmailActivationToken $token) {
audit('email.token.used', $token);
Metrics::counter('email_validation_token_used_total', 'Total number of email validation token used');
Metrics::counter('email_validation_token_total', 'Total number of email validation token used', ['verb'], ['used']);
});
$events->on('email-revalidation.token.generated', function (GeoKrety\Model\EmailRevalidateToken $token) {
audit('email-revalidation.token.generated', $token);
Metrics::counter('email_revalidation_token_total', 'Total number of email re-validation token created', ['verb'], ['generated']);
});
$events->on('email-revalidation.token.used', function (GeoKrety\Model\EmailRevalidateToken $token) {
audit('email-revalidation.token.used', $token);
Metrics::counter('email_revalidation_token_total', 'Total number of email re-validation token used', ['verb'], ['used']);
});
$events->on('email-revalidation.token.error', function (GeoKrety\Model\EmailRevalidateToken $token) {
audit('email-revalidation.token.error', $token);
Metrics::counter('email_revalidation_token_total', 'Total number of email re-validation token error', ['verb'], ['error']);
});
$events->on('user.secid.changed', function (GeoKrety\Model\User $user) {
audit('user.secid.changed', $user);
Expand Down

0 comments on commit 07bbe29

Please sign in to comment.