Skip to content

Commit

Permalink
Display account status on admin page (#883)
Browse files Browse the repository at this point in the history
* fix: [admin] display accoutn status as text

* fix: fix token usage metrics
  • Loading branch information
kumy authored Oct 17, 2023
1 parent d998187 commit eed9d74
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions website/app-templates/smarty/blocks/user_finder_list.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<th class="text-right">Join date</th>
<th class="text-right">Last login</th>
<th class="text-right">Email valid</th>
<th class="text-right">Status</th>
<th class="text-right">Actions</th>
</tr>
</thead>
Expand All @@ -25,6 +26,7 @@
<td class="text-right">{if !is_null($user->joined_on_datetime)}{$user->joined_on_datetime|print_date nofilter}{/if}</td>
<td class="text-right">{if !is_null($user->last_login_datetime)}{$user->last_login_datetime|print_date nofilter}{/if}</td>
<td class="text-right">{if $user->email_invalid}{t}false{/t}{else}{t}true{/t}{/if}</td>
<td class="text-right">{$user->status_text()}</td>
<td class="text-right">
{block user_actions}{/block}
</td>
Expand Down
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
10 changes: 10 additions & 0 deletions website/app/GeoKrety/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class User extends Base implements \JsonSerializable {
public const USER_ACCOUNT_VALID = 1;
public const USER_ACCOUNT_IMPORTED = 2;

public const ACCOUNT_STATUS_TEXT = [
self::USER_ACCOUNT_INVALID => 'Non-Activated',
self::USER_ACCOUNT_VALID => 'Fully activated',
self::USER_ACCOUNT_IMPORTED => 'Imported',
];

public const USER_ACCOUNT_STATUS_INVALID = [
self::USER_ACCOUNT_INVALID,
self::USER_ACCOUNT_IMPORTED,
Expand Down Expand Up @@ -303,6 +309,10 @@ public function isAccountImported(): bool {
return $this->account_valid === self::USER_ACCOUNT_IMPORTED;
}

public function status_text(): string {
return self::ACCOUNT_STATUS_TEXT[$this->account_valid];
}

public function hasEmail(): bool {
return !is_null($this->_email_hash);
}
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 eed9d74

Please sign in to comment.