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\Http\Controllers\Controller;
|
||||
use App\Http\Requests;
|
||||
use App\Models\ActionLog;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class UserDetail extends Controller
|
||||
{
|
||||
/**
|
||||
* @var ActionLog
|
||||
*/
|
||||
private $log;
|
||||
|
||||
function __construct(ActionLog $log)
|
||||
{
|
||||
$this->log = $log;
|
||||
}
|
||||
|
||||
public function getDetail($id = null)
|
||||
{
|
||||
// when no user specified, lets assume we want to edit "me"
|
||||
@@ -21,13 +32,16 @@ class UserDetail extends Controller
|
||||
// try to find given user
|
||||
$user = User::findOrFail($id);
|
||||
|
||||
$actions = $this->log->getUserActions($id);
|
||||
|
||||
$roles = UserRoles::getWithTranslations();
|
||||
$states = UserStates::getWithTranslations();
|
||||
|
||||
return view('admin.userManagement.detail', [
|
||||
'user' => $user,
|
||||
'states' => $states,
|
||||
'roles' => $roles
|
||||
'roles' => $roles,
|
||||
'actions' => $actions
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
/**
|
||||
* Class ActionLog
|
||||
@@ -10,16 +11,71 @@ use Illuminate\Database\Eloquent\Model;
|
||||
class ActionLog extends Model
|
||||
{
|
||||
protected $table = 'action_log';
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'type_id',
|
||||
'user_id',
|
||||
'parameters'
|
||||
];
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -2,22 +2,17 @@
|
||||
return [
|
||||
'login' => 'Logged in',
|
||||
'logout' => 'Logged out',
|
||||
'selfUserEdit' => 'Updated self user',
|
||||
'userEdit' => 'Updated someone\'s profile',
|
||||
'changePassword' => 'Changed password',
|
||||
'newCustomField' => 'Added new custom field',
|
||||
'editCustomField' => 'Edited custom field',
|
||||
'deleteCustomField' => 'Deleted custom field',
|
||||
'userEdit' => 'Updated user :name (:email)',
|
||||
'newCustomField' => 'Added new custom field with ID #:id and name ":name"',
|
||||
'editCustomField' => 'Edited custom field with ID #:id and name ":name"',
|
||||
'deleteCustomField' => 'Deleted custom field ID #:id and name ":name"',
|
||||
'changeSettings' => 'Changes settings',
|
||||
|
||||
// list of names
|
||||
'nameLogin' => 'Logged in',
|
||||
'nameLogout' => 'Logged out',
|
||||
'nameSelfUserEdit' => 'Updated self user',
|
||||
'nameUserEdit' => 'Updated someone\'s profile',
|
||||
'nameChangePassword' => 'Changed password',
|
||||
'nameNewCustomField' => 'Added new custom field',
|
||||
'nameEditCustomField' => 'Edited custom field',
|
||||
'nameDeleteCustomField' => 'Deleted custom field',
|
||||
'nameChangeSettings' => 'Changes settings',
|
||||
'nameLogin' => 'Login',
|
||||
'nameLogout' => 'Log out',
|
||||
'nameUserEdit' => 'Edit user',
|
||||
'nameNewCustomField' => 'New custom field',
|
||||
'nameEditCustomField' => 'Edit custom field',
|
||||
'nameDeleteCustomField' => 'Delete custom field',
|
||||
'nameChangeSettings' => 'Change settings',
|
||||
];
|
||||
@@ -25,5 +25,5 @@ return [
|
||||
'update' => 'Update informations',
|
||||
'passwordLeaveEmpty' => 'Leave empty, if you do not want to change it.',
|
||||
'userSuccessfullyEdited' => 'User successfully edited.',
|
||||
|
||||
'noActions' => 'There are no actions yet.',
|
||||
];
|
||||
@@ -25,7 +25,21 @@
|
||||
<div class="callout">
|
||||
<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>
|
||||
@@ -49,5 +63,4 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@stop
|
||||
Reference in New Issue
Block a user