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

[3.x] user activity #2063

Merged
merged 8 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@
'modules' => [],
'analytics' => ['enabled' => false],
'search_endpoint' => 'twill.search',

/*
|--------------------------------------------------------------------------
| Twill Auth activity related configuration
|--------------------------------------------------------------------------
|
*/
'auth_activity_log' => [
'login' => false,
'logout' => false,
],
'auth_activity_causer' => 'users',
];
42 changes: 39 additions & 3 deletions docs/content/1_documentation/8_dashboard/index.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Dashboard

Once you have created and configured multiple CRUD modules in your Twill's admin console, you can configure Twill's dashboard in `config/twill.php`.
Once you have created and configured multiple CRUD modules in your Twill's admin console, you can configure Twill's
dashboard in `config/twill.php`.

For each module that you want to enable in a part or all parts of the dashboad, add an entry to the `dashboard.modules` array, like in the following example:
## Model activity

For each module that you want to enable in a part or all parts of the dashboad, add an entry to the `dashboard.modules`
array, like in the following example:

```php
return [
Expand All @@ -27,6 +31,8 @@ return [
];
```

## Google analytics

You can also enable a Google Analytics module:

```php
Expand All @@ -44,4 +50,34 @@ return [

It is using Spatie's [Laravel Analytics](https://github.com/spatie/laravel-analytics) package.

Follow [Spatie's documentation](https://github.com/spatie/laravel-analytics#how-to-obtain-the-credentials-to-communicate-with-google-analytics) to setup a Google service account and download a json file containing your credentials, and provide your Analytics view ID using the `ANALYTICS_VIEW_ID` environment variable.
Follow [Spatie's documentation](https://github.com/spatie/laravel-analytics#how-to-obtain-the-credentials-to-communicate-with-google-analytics)
to setup a Google service account and download a json file containing your credentials, and provide your Analytics view
ID using the `ANALYTICS_VIEW_ID` environment variable.

## User activity

In addition to model activity, you can also enable user login/logout activity.

This feature is enabled by default and can be enabled by setting the following config keys:

```
twill.dashboard.auth_activity_log.login => true
twill.dashboard.auth_activity_log.logout => true
```

:::filename:::
`config/twill.php`
:::#filename:::

```php
<?php

return [
'dashboard' => [
'auth_activity_log' => [
'login' => true,
'logout' => true
]
]
]
```
3 changes: 3 additions & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'reset-password' => 'Reset password',
'reset-send' => 'Send password reset link',
'verify-login' => 'Verify login',
'auth-causer' => 'Authentication'
],
'buckets' => [
'intro' => 'What would you like to feature today?',
Expand Down Expand Up @@ -56,6 +57,8 @@
'unfeatured' => 'Unfeatured',
'restored' => 'Restored',
'deleted' => 'Deleted',
'login' => 'Login action',
'logout' => 'Logout action'
],
'activity-row' => [
'edit' => 'Edit',
Expand Down
28 changes: 27 additions & 1 deletion src/Http/Controllers/Admin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ private function getEnabledActivities(): array
}
}

if (config('twill.dashboard.auth_activity_log.login', false)) {
$listActivities[] = config('twill.dashboard.auth_activity_causer', 'users');
}

if (config('twill.dashboard.auth_activity_log.logout', false)) {
$listActivities[] = config('twill.dashboard.auth_activity_causer', 'users');
}

return $listActivities;
}

Expand Down Expand Up @@ -200,6 +208,10 @@ private function getLoggedInUserActivities(): Collection
*/
private function formatActivity($activity)
{
if ($activity->subject_type === config('twill.auth_activity_causer', 'users')) {
return $this->formatAuthActivity($activity);
}

$dashboardModule = $this->config->get('twill.dashboard.modules.' . $activity->subject_type);

if (! $dashboardModule || ! $dashboardModule['activity'] ?? false) {
Expand All @@ -220,7 +232,7 @@ private function formatActivity($activity)
'date' => $activity->created_at->toIso8601String(),
'author' => $activity->causer->name ?? twillTrans('twill::lang.dashboard.unknown-author'),
'name' => $activity->subject->titleInDashboard ?? $activity->subject->title,
'activity' => twillTrans('twill::lang.dashboard.activities.' . $activity->description),
'activity' => twillTrans('twill::lang.dashboard.activities.' . $activity->description, $activity->properties->toArray()),
] + (classHasTrait($activity->subject, HasMedias::class) ? [
'thumbnail' => $activity->subject->defaultCmsImage(['w' => 100, 'h' => 100]),
] : []) + (! $activity->subject->trashed() ? [
Expand All @@ -235,6 +247,20 @@ private function formatActivity($activity)
] : []);
}

private function formatAuthActivity(Activity $activity): array
{
return [
'id' => $activity->id,
'type' => twillTrans('twill::lang.auth.auth-causer'),
'date' => $activity->created_at->toIso8601String(),
'author' => $activity->causer->name ?? twillTrans('twill::lang.dashboard.unknown-author'),
'name' => ucfirst($activity->description) ?? '',
'activity' => twillTrans('twill::lang.dashboard.activities.' . $activity->description, $activity->properties->toArray()),
] + (classHasTrait($activity->subject, HasMedias::class) ? [
'thumbnail' => $activity->subject->defaultCmsImage(['w' => 100, 'h' => 100]),
] : []);
}

/**
* @return array|\Illuminate\Support\Collection
*/
Expand Down
17 changes: 12 additions & 5 deletions src/Http/Controllers/Admin/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace A17\Twill\Http\Controllers\Admin;

use A17\Twill\Http\Requests\Admin\OauthRequest;
use A17\Twill\Models\User;
use A17\Twill\Repositories\UserRepository;
use Carbon\Carbon;
use Illuminate\Auth\AuthManager;
use Illuminate\Config\Repository as Config;
use Illuminate\Encryption\Encrypter;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\App;
Expand Down Expand Up @@ -110,11 +112,12 @@ public function showLogin2FaForm()
return $this->viewFactory->make('twill::auth.2fa');
}

/**
* @return \Illuminate\Http\RedirectResponse
*/
public function logout(Request $request)
public function logout(Request $request): RedirectResponse
{
if (config('twill.dashboard.auth_activity_log.logout', false)) {
activity()->performedOn($this->guard()->user())->causedBy($this->guard()->user())->log('logout');
}

$this->guard()->logout();

$request->session()->invalidate();
Expand All @@ -133,7 +136,7 @@ protected function authenticated(Request $request, $user)
return $this->afterAuthentication($request, $user);
}

protected function afterAuthentication(Request $request, $user)
protected function afterAuthentication(Request $request, $user): RedirectResponse
{
if ($user->google_2fa_secret && $user->google_2fa_enabled) {
$this->guard()->logout();
Expand All @@ -155,6 +158,10 @@ protected function afterAuthentication(Request $request, $user)
]);
}

if (config('twill.dashboard.auth_activity_log.login', false)) {
activity()->performedOn($user)->causedBy($user)->log('login');
}

return $this->redirector->intended($this->redirectTo);
}

Expand Down