FEATURE: #1425: add user log actions
This commit is contained in:
@@ -6,11 +6,22 @@ use App\Enumerations\UserRoles;
|
|||||||
use App\Enumerations\UserStates;
|
use App\Enumerations\UserStates;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Http\Requests;
|
use App\Http\Requests;
|
||||||
|
use App\Models\ActionLog;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
class UserDetail extends Controller
|
class UserDetail extends Controller
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var ActionLog
|
||||||
|
*/
|
||||||
|
private $log;
|
||||||
|
|
||||||
|
function __construct(ActionLog $log)
|
||||||
|
{
|
||||||
|
$this->log = $log;
|
||||||
|
}
|
||||||
|
|
||||||
public function getDetail($id = null)
|
public function getDetail($id = null)
|
||||||
{
|
{
|
||||||
// when no user specified, lets assume we want to edit "me"
|
// when no user specified, lets assume we want to edit "me"
|
||||||
@@ -21,13 +32,16 @@ class UserDetail extends Controller
|
|||||||
// try to find given user
|
// try to find given user
|
||||||
$user = User::findOrFail($id);
|
$user = User::findOrFail($id);
|
||||||
|
|
||||||
|
$actions = $this->log->getUserActions($id);
|
||||||
|
|
||||||
$roles = UserRoles::getWithTranslations();
|
$roles = UserRoles::getWithTranslations();
|
||||||
$states = UserStates::getWithTranslations();
|
$states = UserStates::getWithTranslations();
|
||||||
|
|
||||||
return view('admin.userManagement.detail', [
|
return view('admin.userManagement.detail', [
|
||||||
'user' => $user,
|
'user' => $user,
|
||||||
'states' => $states,
|
'states' => $states,
|
||||||
'roles' => $roles
|
'roles' => $roles,
|
||||||
|
'actions' => $actions
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ActionLog
|
* Class ActionLog
|
||||||
@@ -10,16 +11,71 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
class ActionLog extends Model
|
class ActionLog extends Model
|
||||||
{
|
{
|
||||||
protected $table = 'action_log';
|
protected $table = 'action_log';
|
||||||
|
|
||||||
public $timestamps = false;
|
public $timestamps = false;
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'type_id',
|
'type_id',
|
||||||
'user_id',
|
'user_id',
|
||||||
'parameters'
|
'parameters'
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Query factory for loading action logs with their type (therefore with name and value)
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getLogActionsQuery()
|
||||||
|
{
|
||||||
|
$actions = \DB::table('action_log')
|
||||||
|
->join('action_log_types', 'action_log_types.id', '=', 'action_log.type_id')
|
||||||
|
->orderBy('created_at', 'DESC');
|
||||||
|
|
||||||
|
return $actions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get last N user actions
|
||||||
|
*
|
||||||
|
* @param null $id
|
||||||
|
* @param int $limit
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getUserActions($id = null, $limit = 15)
|
||||||
|
{
|
||||||
|
// if user is not specified, use logged one
|
||||||
|
if (is_null($id)) {
|
||||||
|
$id = Auth::user()->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// load last N actions of selected user
|
||||||
|
$actions = $this->getLogActionsQuery()
|
||||||
|
->limit($limit)
|
||||||
|
->where('user_id', $id)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
// translate and return result
|
||||||
|
return $this->translateLogMessages($actions);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method used for translating and filling parameters into value of log.
|
||||||
|
*
|
||||||
|
* @param $messages
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function translateLogMessages($messages)
|
||||||
|
{
|
||||||
|
$log = [];
|
||||||
|
|
||||||
|
// translate name and value of each record
|
||||||
|
foreach ($messages as $action) {
|
||||||
|
$action->name = trans($action->name);
|
||||||
|
$action->value = trans($action->value, (array)json_decode($action->parameters));
|
||||||
|
$log[] = $action;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $log;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,23 +1,18 @@
|
|||||||
<?php
|
<?php
|
||||||
return [
|
return [
|
||||||
'login' => 'Logged in',
|
'login' => 'Logged in',
|
||||||
'logout' => 'Logged out',
|
'logout' => 'Logged out',
|
||||||
'selfUserEdit' => 'Updated self user',
|
'userEdit' => 'Updated user :name (:email)',
|
||||||
'userEdit' => 'Updated someone\'s profile',
|
'newCustomField' => 'Added new custom field with ID #:id and name ":name"',
|
||||||
'changePassword' => 'Changed password',
|
'editCustomField' => 'Edited custom field with ID #:id and name ":name"',
|
||||||
'newCustomField' => 'Added new custom field',
|
'deleteCustomField' => 'Deleted custom field ID #:id and name ":name"',
|
||||||
'editCustomField' => 'Edited custom field',
|
'changeSettings' => 'Changes settings',
|
||||||
'deleteCustomField' => 'Deleted custom field',
|
|
||||||
'changeSettings' => 'Changes settings',
|
|
||||||
|
|
||||||
// list of names
|
// list of names
|
||||||
'nameLogin' => 'Logged in',
|
'nameLogin' => 'Login',
|
||||||
'nameLogout' => 'Logged out',
|
'nameLogout' => 'Log out',
|
||||||
'nameSelfUserEdit' => 'Updated self user',
|
'nameUserEdit' => 'Edit user',
|
||||||
'nameUserEdit' => 'Updated someone\'s profile',
|
'nameNewCustomField' => 'New custom field',
|
||||||
'nameChangePassword' => 'Changed password',
|
'nameEditCustomField' => 'Edit custom field',
|
||||||
'nameNewCustomField' => 'Added new custom field',
|
'nameDeleteCustomField' => 'Delete custom field',
|
||||||
'nameEditCustomField' => 'Edited custom field',
|
'nameChangeSettings' => 'Change settings',
|
||||||
'nameDeleteCustomField' => 'Deleted custom field',
|
|
||||||
'nameChangeSettings' => 'Changes settings',
|
|
||||||
];
|
];
|
||||||
@@ -25,5 +25,5 @@ return [
|
|||||||
'update' => 'Update informations',
|
'update' => 'Update informations',
|
||||||
'passwordLeaveEmpty' => 'Leave empty, if you do not want to change it.',
|
'passwordLeaveEmpty' => 'Leave empty, if you do not want to change it.',
|
||||||
'userSuccessfullyEdited' => 'User successfully edited.',
|
'userSuccessfullyEdited' => 'User successfully edited.',
|
||||||
|
'noActions' => 'There are no actions yet.',
|
||||||
];
|
];
|
||||||
@@ -25,7 +25,21 @@
|
|||||||
<div class="callout">
|
<div class="callout">
|
||||||
<h3>@lang('admin/userManagement.usersActions')</h3>
|
<h3>@lang('admin/userManagement.usersActions')</h3>
|
||||||
|
|
||||||
<em>comming soon</em>
|
@if (!empty($actions))
|
||||||
|
<ul class="accordion" data-accordion>
|
||||||
|
@foreach($actions as $action)
|
||||||
|
<li class="accordion-item" data-accordion-item>
|
||||||
|
<a class="accordion-title">{{$action->name}}</a>
|
||||||
|
<div class="accordion-content" data-tab-content>
|
||||||
|
{{date('d.m.Y G:i', strtotime($action->created_at))}} <br />
|
||||||
|
{{$action->value}}
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
@endforeach
|
||||||
|
</ul>
|
||||||
|
@else
|
||||||
|
<em>@lang('admin/userManagement.noActions')</em>
|
||||||
|
@endif
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
@@ -49,5 +63,4 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@stop
|
@stop
|
||||||
Reference in New Issue
Block a user