瀏覽代碼

Replace Rappasoft\LaravelAuthenticationLog by forked package

Bubka 1 年之前
父節點
當前提交
11ceb52286

+ 0 - 30
app/Models/Traits/AuthenticationLoggable.php

@@ -1,30 +0,0 @@
-<?php
-
-namespace App\Models\Traits;
-
-use Illuminate\Database\Eloquent\Builder;
-use Illuminate\Support\Carbon;
-use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog;
-use Rappasoft\LaravelAuthenticationLog\Traits\AuthenticationLoggable as TraitsAuthenticationLoggable;
-
-trait AuthenticationLoggable 
-{
-    use TraitsAuthenticationLoggable;
-
-    public function authentications()
-    {
-        return $this->morphMany(AuthenticationLog::class, 'authenticatable')->latest('id');
-    }
-
-    /**
-     * Get authentications for the provided timespan (in month)
-     */
-    public function authenticationsByPeriod(int $period = 1)
-    {
-        $from = Carbon::now()->subMonths($period);
-
-        return $this->authentications->filter(function (AuthenticationLog $authentication) use ($from) {
-            return $authentication->login_at >= $from || $authentication->logout_at >= $from;
-        });
-    }
-}

+ 16 - 1
app/Models/User.php

@@ -2,8 +2,8 @@
 
 namespace App\Models;
 
-use App\Models\Traits\AuthenticationLoggable;
 use App\Models\Traits\WebAuthnManageCredentials;
+use Bubka\LaravelAuthenticationLog\Traits\AuthenticationLoggable;
 use Illuminate\Auth\Events\PasswordReset;
 use Illuminate\Auth\Notifications\ResetPassword;
 use Illuminate\Contracts\Translation\HasLocalePreference;
@@ -41,6 +41,21 @@ use Laravel\Passport\HasApiTokens;
  * @property-read int|null $twofaccounts_count
  * @property-read \Illuminate\Database\Eloquent\Collection|\Laragear\WebAuthn\Models\WebAuthnCredential[] $webAuthnCredentials
  * @property-read int|null $web_authn_credentials_count
+ * @property string|null $oauth_id
+ * @property string|null $oauth_provider
+ * @property-read \Illuminate\Database\Eloquent\Collection<int, \Bubka\LaravelAuthenticationLog\Models\AuthenticationLog> $authentications
+ * @property-read int|null $authentications_count
+ * @property-read \Bubka\LaravelAuthenticationLog\Models\AuthenticationLog|null $latestAuthentication
+ * @method static \Illuminate\Database\Eloquent\Builder|User admins()
+ * @method \Illuminate\Support\Carbon|null latestAuthentication()
+ * @method \Illuminate\Support\Carbon|null lastLoginAt()
+ * @method \Illuminate\Support\Carbon|null lastSuccessfulLoginAt()
+ * @method \Illuminate\Support\Carbon|null lastLoginIp()
+ * @method \Illuminate\Support\Carbon|null lastSuccessfulLoginIp()
+ * @method \Illuminate\Support\Carbon|null previousLoginAt()
+ * @method \Illuminate\Support\Carbon|null previousLoginIp()
+ * @method \Illuminate\Support\Collection<int, \Bubka\LaravelAuthenticationLog\Models\AuthenticationLog> authenticationsByPeriod()
+ * @mixin \Eloquent
  */
 class User extends Authenticatable implements HasLocalePreference, WebAuthnAuthenticatable
 {

+ 10 - 1
app/Notifications/SignedInWithNewDevice.php

@@ -7,7 +7,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Notifications\Messages\MailMessage;
 use Illuminate\Notifications\Notification;
 use Jenssegers\Agent\Agent;
-use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog;
+use Bubka\LaravelAuthenticationLog\Models\AuthenticationLog;
 
 class SignedInWithNewDevice extends Notification implements ShouldQueue
 {
@@ -22,6 +22,9 @@ class SignedInWithNewDevice extends Notification implements ShouldQueue
      */
     protected $agent;
 
+    /**
+     * Create a new SignedInWithNewDevice instance
+     */
     public function __construct(AuthenticationLog $authenticationLog)
     {
         $this->authenticationLog = $authenticationLog;
@@ -29,11 +32,17 @@ class SignedInWithNewDevice extends Notification implements ShouldQueue
         $this->agent->setUserAgent($authenticationLog->user_agent);
     }
 
+    /**
+     * 
+     */
     public function via($notifiable)
     {
         return $notifiable->notifyAuthenticationLogVia();
     }
 
+    /**
+     * Wrap the notification to a mail envelop
+     */
     public function toMail($notifiable)
     {
         return (new MailMessage())

+ 11 - 2
composer.json

@@ -35,11 +35,20 @@
         "laravel/tinker": "^2.8",
         "laravel/ui": "^4.2",
         "paragonie/constant_time_encoding": "^2.6",
-        "rappasoft/laravel-authentication-log": "^4.0",
         "socialiteproviders/manager": "^4.4",
         "spatie/eloquent-sortable": "^4.0.1",
-        "spomky-labs/otphp": "^11.0"
+        "spomky-labs/otphp": "^11.0",
+        "bubka/laravel-authentication-log": "@dev"
     },
+    "repositories": [
+        {
+            "type": "path",
+            "url": "../packages/bubka/laravel-authentication-log",
+            "options": {
+                "symlink": true
+            }
+        }
+    ],
     "require-dev": {
         "barryvdh/laravel-ide-helper": "^2.13",
         "brianium/paratest": "^7.3",

文件差異過大導致無法顯示
+ 298 - 189
composer.lock


+ 8 - 6
config/authentication-log.php

@@ -13,14 +13,16 @@ return [
         'login' => \Illuminate\Auth\Events\Login::class,
         'failed' => \Illuminate\Auth\Events\Failed::class,
         'logout' => \Illuminate\Auth\Events\Logout::class,
-        'logout-other-devices' => \Illuminate\Auth\Events\OtherDeviceLogout::class,
+        // 'logout-other-devices' => \Illuminate\Auth\Events\OtherDeviceLogout::class,
+        // 'proxyUserAccess' => \App\Events\VisitedByProxyUser::class,
     ],
 
     'listeners' => [
-        'login' => \Rappasoft\LaravelAuthenticationLog\Listeners\LoginListener::class,
-        'failed' => \Rappasoft\LaravelAuthenticationLog\Listeners\FailedLoginListener::class,
-        'logout' => \Rappasoft\LaravelAuthenticationLog\Listeners\LogoutListener::class,
-        'logout-other-devices' => \Rappasoft\LaravelAuthenticationLog\Listeners\OtherDeviceLogoutListener::class,
+        'login' => \Bubka\LaravelAuthenticationLog\Listeners\LoginListener::class,
+        'failed' => \Bubka\LaravelAuthenticationLog\Listeners\FailedLoginListener::class,
+        'logout' => \Bubka\LaravelAuthenticationLog\Listeners\LogoutListener::class,
+        // 'logout-other-devices' => \Bubka\LaravelAuthenticationLog\Listeners\OtherDeviceLogoutListener::class,
+        // 'proxyUserAccess' => \App\Listeners\VisitedByProxyUserListener::class,
     ],
 
     'notifications' => [
@@ -42,7 +44,7 @@ return [
             'location' => false,
 
             // The Notification class to send
-            'template' => \Rappasoft\LaravelAuthenticationLog\Notifications\FailedLogin::class,
+            'template' => \Bubka\LaravelAuthenticationLog\Notifications\FailedLogin::class,
         ],
     ],
 

+ 1 - 0
database/migrations/2024_04_14_082519_create_authentication_log_table.php

@@ -18,6 +18,7 @@ return new class extends Migration
             $table->timestamp('logout_at')->nullable();
             $table->boolean('cleared_by_user')->default(false);
             $table->json('location')->nullable();
+            $table->string('auth_method', 40)->nullable();
         });
     }
 

+ 2 - 0
phpstan.neon

@@ -11,4 +11,6 @@ parameters:
         analyse:
             - app/Protobuf/*
     ignoreErrors:
+        -
+            message: '#.*geoip.*#'
     checkMissingIterableValueType: false

+ 1 - 1
routes/web.php

@@ -19,7 +19,7 @@ use Laravel\Passport\Http\Controllers\PersonalAccessTokenController;
 
 // use App\Models\User;
 // use App\Notifications\SignedInWithNewDevice;
-// use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog;
+// use Bubka\LaravelAuthenticationLog\Models\AuthenticationLog;
 
 /*
 |--------------------------------------------------------------------------

+ 19 - 0
tests/Api/v1/Controllers/UserManagerControllerTest.php

@@ -12,6 +12,7 @@ use Illuminate\Http\Request;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Carbon;
 use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Event;
 use Illuminate\Support\Facades\Hash;
 use Illuminate\Support\Facades\Notification;
 use Illuminate\Support\Facades\Password;
@@ -522,6 +523,24 @@ class UserManagerControllerTest extends FeatureTestCase
             ->assertForbidden();
     }
 
+    /**
+     * @test
+     */
+    public function test_authLog_events_are_listened_by_authLog_listeners()
+    {
+        Event::fake();
+
+        foreach (config('authentication-log.listeners') as $type => $listenerClass) {
+            Event::assertListening(
+                config('authentication-log.events.' . $type),
+                $listenerClass
+            );
+        }
+    }
+
+    /**
+     * Local feeder because Factory cannot be used here
+     */
     protected function feedAuthenticationLog() : int
     {
         // Do not change creation order

部分文件因文件數量過多而無法顯示