]> BookStack Code Mirror - bookstack/commitdiff
Added latest activity into users list view 2360/head
authorDan Brown <redacted>
Fri, 20 Nov 2020 20:10:18 +0000 (20:10 +0000)
committerDan Brown <redacted>
Fri, 20 Nov 2020 20:10:18 +0000 (20:10 +0000)
app/Auth/User.php
app/Auth/UserRepo.php
resources/lang/en/settings.php
resources/views/users/index.blade.php

index 6bf656be383614d8839d461aa72b427eed40ec29..5feb269e2bd1ce7fc230070faac1b6f067a55e93 100644 (file)
@@ -1,5 +1,6 @@
 <?php namespace BookStack\Auth;
 
+use BookStack\Actions\Activity;
 use BookStack\Api\ApiToken;
 use BookStack\Interfaces\Loggable;
 use BookStack\Model;
@@ -12,6 +13,7 @@ use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
 use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Database\Eloquent\Relations\HasOne;
 use Illuminate\Notifications\Notifiable;
 
 /**
@@ -230,6 +232,14 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
         return $this->hasMany(ApiToken::class);
     }
 
+    /**
+     * Get the latest activity instance for this user.
+     */
+    public function latestActivity(): HasOne
+    {
+        return $this->hasOne(Activity::class)->latest();
+    }
+
     /**
      * Get the url for editing this user.
      */
index fdb8c0923882cabe08e03f4db68e6b89609e1468..5ae2ed2a278e90f7e1e976ab5859c94600ab5ad6 100644 (file)
@@ -10,6 +10,7 @@ use BookStack\Exceptions\UserUpdateException;
 use BookStack\Uploads\Image;
 use Exception;
 use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Pagination\LengthAwarePaginator;
 use Images;
 use Log;
 
@@ -56,13 +57,19 @@ class UserRepo
 
     /**
      * Get all the users with their permissions in a paginated format.
-     * @param int $count
-     * @param $sortData
-     * @return Builder|static
      */
-    public function getAllUsersPaginatedAndSorted($count, $sortData)
+    public function getAllUsersPaginatedAndSorted(int $count, array $sortData): LengthAwarePaginator
     {
-        $query = $this->user->with('roles', 'avatar')->orderBy($sortData['sort'], $sortData['order']);
+        $sort = $sortData['sort'];
+        if ($sort === 'latest_activity') {
+            $sort = \BookStack\Actions\Activity::query()->select('created_at')
+                ->whereColumn('activities.user_id', 'users.id')
+                ->latest()
+                ->take(1);
+        }
+
+        $query = $this->user->with(['roles', 'avatar', 'latestActivity'])
+            ->orderBy($sort, $sortData['order']);
 
         if ($sortData['search']) {
             $term = '%' . $sortData['search'] . '%';
index 52919d44d43dc9acc379b70ddfcfba9dde132684..dce3454263bf160598f9442944a9de012b6fded0 100755 (executable)
@@ -157,6 +157,7 @@ return [
     'user_profile' => 'User Profile',
     'users_add_new' => 'Add New User',
     'users_search' => 'Search Users',
+    'users_latest_activity' => 'Latest Activity',
     'users_details' => 'User Details',
     'users_details_desc' => 'Set a display name and an email address for this user. The email address will be used for logging into the application.',
     'users_details_desc_no_email' => 'Set a display name for this user so others can recognise them.',
index da373c1618b563fddcc9768644b7d1ac608e29cf..4b5bad0fd5bb3db369d47dfe2250ed00f5e40c58 100644 (file)
@@ -27,7 +27,6 @@
                 </div>
             </div>
 
-            {{--TODO - Add last login--}}
             <table class="table">
                 <tr>
                     <th></th>
@@ -37,6 +36,9 @@
                         <a href="{{ sortUrl('/settings/users', $listDetails, ['sort' => 'email']) }}">{{ trans('auth.email') }}</a>
                     </th>
                     <th>{{ trans('settings.role_user_roles') }}</th>
+                    <th class="text-right">
+                        <a href="{{ sortUrl('/settings/users', $listDetails, ['sort' => 'latest_activity']) }}">{{ trans('settings.users_latest_activity') }}</a>
+                    </th>
                 </tr>
                 @foreach($users as $user)
                     <tr>
                                 <small><a href="{{ url("/settings/roles/{$role->id}") }}">{{$role->display_name}}</a>@if($index !== count($user->roles) -1),@endif</small>
                             @endforeach
                         </td>
+                        <td class="text-right text-muted">
+                            @if($user->latestActivity)
+                                <small title="{{ $user->latestActivity->created_at->format('Y-m-d H:i:s') }}">{{ $user->latestActivity->created_at->diffForHumans() }}</small>
+                            @endif
+                        </td>
                     </tr>
                 @endforeach
             </table>