diff --git a/app/Http/Controllers/Admin/UserDetail.php b/app/Http/Controllers/Admin/UserDetail.php index 0416f2b3..e734ab1c 100644 --- a/app/Http/Controllers/Admin/UserDetail.php +++ b/app/Http/Controllers/Admin/UserDetail.php @@ -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 ]); } } \ No newline at end of file diff --git a/app/Models/ActionLog.php b/app/Models/ActionLog.php index 809a43b7..911597e7 100644 --- a/app/Models/ActionLog.php +++ b/app/Models/ActionLog.php @@ -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; + } } \ No newline at end of file diff --git a/resources/lang/en/admin/actionLog.php b/resources/lang/en/admin/actionLog.php index 5b744e16..45b51677 100644 --- a/resources/lang/en/admin/actionLog.php +++ b/resources/lang/en/admin/actionLog.php @@ -1,23 +1,18 @@ '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', - 'changeSettings' => 'Changes settings', - + 'login' => 'Logged in', + 'logout' => 'Logged out', + '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', ]; \ No newline at end of file diff --git a/resources/lang/en/admin/userManagement.php b/resources/lang/en/admin/userManagement.php index a1cd63fe..ce2a81f5 100644 --- a/resources/lang/en/admin/userManagement.php +++ b/resources/lang/en/admin/userManagement.php @@ -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.', ]; \ No newline at end of file diff --git a/resources/views/admin/userManagement/detail.blade.php b/resources/views/admin/userManagement/detail.blade.php index 202db9db..ad2582f0 100644 --- a/resources/views/admin/userManagement/detail.blade.php +++ b/resources/views/admin/userManagement/detail.blade.php @@ -25,7 +25,21 @@

@lang('admin/userManagement.usersActions')

- comming soon + @if (!empty($actions)) + + @else + @lang('admin/userManagement.noActions') + @endif
@@ -49,5 +63,4 @@ - @stop \ No newline at end of file