Merge branch 'dev' into user_management
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace App\Enumerations;
|
||||
|
||||
use App\Models\ActionLogType;
|
||||
|
||||
class ActionLogTypes extends BaseEnumeration
|
||||
{
|
||||
/**
|
||||
* Init list of all action log types
|
||||
*
|
||||
* @throws Exceptions\EnumerationExistsException
|
||||
*/
|
||||
protected static function init()
|
||||
{
|
||||
$types = ActionLogType::all(['id', 'name']);
|
||||
|
||||
foreach ($types as $type) {
|
||||
self::push($type->id, $type->name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace App\Enumerations;
|
||||
|
||||
use App\Models\ActionLogType;
|
||||
|
||||
class ActionLogTypesForLogger extends BaseEnumeration
|
||||
{
|
||||
protected static function init()
|
||||
{
|
||||
$types = ActionLogType::all(['string_id', 'id']);
|
||||
|
||||
foreach ($types as $type) {
|
||||
self::push($type->string_id, $type->id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,11 @@ abstract class BaseEnumeration
|
||||
$className::init();
|
||||
}
|
||||
|
||||
// it can happen that even after init no enumerations are available
|
||||
if (empty(self::$items[$className])) {
|
||||
self::$items[$className] = [];
|
||||
}
|
||||
|
||||
self::$initiliazed[] = $className;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace App\Enumerations;
|
||||
|
||||
use App\Models\Content;
|
||||
|
||||
class ContentStatuses extends BaseEnumeration
|
||||
{
|
||||
protected static function init()
|
||||
{
|
||||
$roles = [
|
||||
Content::STATUS_DRAFT => 'admin/contents.status_' . Content::STATUS_DRAFT,
|
||||
Content::STATUS_DELETED => 'admin/contents.status_' . Content::STATUS_DELETED,
|
||||
Content::STATUS_PROTECTED => 'admin/contents.status_' . Content::STATUS_PROTECTED,
|
||||
Content::STATUS_PUBLIC => 'admin/contents.status_' . Content::STATUS_PUBLIC
|
||||
];
|
||||
|
||||
foreach ($roles as $role => $translation) {
|
||||
self::push($role, $translation);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace App\Enumerations;
|
||||
|
||||
use App\Models\Module;
|
||||
|
||||
class Modules extends BaseEnumeration
|
||||
{
|
||||
/**
|
||||
* Init list of all installed modules
|
||||
*
|
||||
* @throws Exceptions\EnumerationExistsException
|
||||
*/
|
||||
protected static function init()
|
||||
{
|
||||
$modules = Module::all(['handler', 'name']);
|
||||
|
||||
foreach ($modules as $module) {
|
||||
self::push($module->handler, $module->name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Events\Event;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class ChangeSettings extends Event
|
||||
{
|
||||
use SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Events\Event;
|
||||
use App\Models\CustomField;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class CreateNewCustomField extends Event
|
||||
{
|
||||
use SerializesModels;
|
||||
/**
|
||||
* @var CustomField
|
||||
*/
|
||||
public $customField;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @param CustomField $customField
|
||||
*/
|
||||
public function __construct(CustomField $customField)
|
||||
{
|
||||
$this->customField = $customField;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Events\Event;
|
||||
use App\Models\User;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class CreateNewUser extends Event
|
||||
{
|
||||
use SerializesModels;
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
public $user;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public function __construct(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Events\Event;
|
||||
use App\Models\CustomField;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class DeleteCustomField extends Event
|
||||
{
|
||||
use SerializesModels;
|
||||
/**
|
||||
* @var CustomField
|
||||
*/
|
||||
public $customField;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @param CustomField $customField
|
||||
*/
|
||||
public function __construct(CustomField $customField)
|
||||
{
|
||||
$this->customField = $customField;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Events\Event;
|
||||
use App\Models\CustomField;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class EditCustomField extends Event
|
||||
{
|
||||
use SerializesModels;
|
||||
/**
|
||||
* @var CustomField
|
||||
*/
|
||||
public $customField;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @param CustomField $customField
|
||||
*/
|
||||
public function __construct(CustomField $customField)
|
||||
{
|
||||
$this->customField = $customField;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class EditUser extends Event
|
||||
{
|
||||
use SerializesModels;
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
public $user;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public function __construct(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Session\TokenMismatchException;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
@@ -45,6 +46,11 @@ class Handler extends ExceptionHandler
|
||||
*/
|
||||
public function render($request, Exception $e)
|
||||
{
|
||||
if ($e instanceof TokenMismatchException){
|
||||
//redirect to form an example of how I handle mine
|
||||
return redirect($request->fullUrl())->with('error', trans('validation.csrf'));
|
||||
}
|
||||
|
||||
return parent::render($request, $e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
namespace App\Handlers;
|
||||
|
||||
use App\Models\Content;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
/**
|
||||
* Class Handler
|
||||
*
|
||||
* Handler is a class which handles module-specific behaviour which cannot be done by module itself, resp. cannot be done by some abstract magic.
|
||||
*
|
||||
* @package App\Handlers
|
||||
*/
|
||||
abstract class Handler
|
||||
{
|
||||
/**
|
||||
* Render given module into visitor area.
|
||||
*
|
||||
* @param Content $content DB model from table contents
|
||||
* @param array $parameters parameters used for modifications/custom settings of given model
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function render(Content $content, $parameters);
|
||||
|
||||
/**
|
||||
* Register admin menu (or multiple ones)
|
||||
*
|
||||
* This cannot be in ServiceProvider because Session is started at middleware. And ServiceProvider is called before
|
||||
* starting any middleware..
|
||||
*
|
||||
* @param User $user model of logged user
|
||||
*/
|
||||
public static function registerAdminMenu(User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,10 @@ namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Enumerations\CustomFieldResources;
|
||||
use App\Enumerations\CustomFieldTypes;
|
||||
use App\Events\CreateNewCustomField as CreateNewCustomFieldEvent;
|
||||
use App\Http\Requests\NewCustomFieldRequest;
|
||||
use App\Models\CustomField;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
|
||||
class CreateNewCustomField extends Controller
|
||||
{
|
||||
@@ -59,6 +61,9 @@ class CreateNewCustomField extends Controller
|
||||
|
||||
$customField->save();
|
||||
|
||||
// fire event about creating new custom field
|
||||
Event::fire(new CreateNewCustomFieldEvent($customField));
|
||||
|
||||
return redirect()
|
||||
->route('customfields.new')
|
||||
->with('success', trans('admin/customFields.successfullyCreated'));
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Enumerations\UserRoles;
|
||||
use App\Events\CreateNewUser as CreateNewUserEvent;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests;
|
||||
use App\Http\Requests\CreateNewUserRequest;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class CreateNewUser extends Controller
|
||||
@@ -51,6 +52,9 @@ class CreateNewUser extends Controller
|
||||
// store user into database
|
||||
$user->save();
|
||||
|
||||
// fire event
|
||||
Event::fire(new CreateNewUserEvent($user));
|
||||
|
||||
// refresh page with information about successfull registration
|
||||
return redirect()
|
||||
->route('users.new')
|
||||
|
||||
@@ -4,10 +4,13 @@ namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Enumerations\CustomFieldResources;
|
||||
use App\Enumerations\CustomFieldTypes;
|
||||
use App\Events\DeleteCustomField;
|
||||
use App\Events\EditCustomField as EditCustomFieldEvent;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests;
|
||||
use App\Http\Requests\EditCustomFieldRequest;
|
||||
use App\Models\CustomField;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
|
||||
class EditCustomField extends Controller
|
||||
{
|
||||
@@ -60,6 +63,9 @@ class EditCustomField extends Controller
|
||||
// save changes into database
|
||||
$customField->save();
|
||||
|
||||
// fire event about editing custom field
|
||||
Event::fire(new EditCustomFieldEvent($customField));
|
||||
|
||||
return redirect()
|
||||
->route('customfields.edit', ['id' => $id])
|
||||
->with('success', trans('admin/customFields.successfullyEdited'));
|
||||
@@ -89,6 +95,9 @@ class EditCustomField extends Controller
|
||||
// delete item
|
||||
$field->delete();
|
||||
|
||||
// fire event about editing custom field
|
||||
Event::fire(new DeleteCustomField($field));
|
||||
|
||||
// redirect back to overview
|
||||
return redirect()
|
||||
->route('customfields.overview')
|
||||
|
||||
@@ -4,11 +4,13 @@ namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Enumerations\UserRoles;
|
||||
use App\Enumerations\UserStates;
|
||||
use App\Events\EditUser as EditUserEvent;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests;
|
||||
use App\Http\Requests\EditUserRequest;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class EditUser extends Controller
|
||||
@@ -81,6 +83,9 @@ class EditUser extends Controller
|
||||
|
||||
$user->save();
|
||||
|
||||
// fire event about editing user
|
||||
Event::fire(new EditUserEvent($user));
|
||||
|
||||
return redirect()
|
||||
->route('users.detail', ['id' => $id])
|
||||
->with('success', trans('admin/userManagement.userSuccessfullyEdited'));
|
||||
|
||||
@@ -29,6 +29,7 @@ class Kernel extends HttpKernel
|
||||
\Illuminate\Session\Middleware\StartSession::class,
|
||||
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
|
||||
\App\Http\Middleware\VerifyCsrfToken::class,
|
||||
\App\Http\Middleware\ModuleRegistrator::class
|
||||
],
|
||||
|
||||
'api' => [
|
||||
@@ -45,6 +46,7 @@ class Kernel extends HttpKernel
|
||||
*/
|
||||
protected $routeMiddleware = [
|
||||
'auth' => \App\Http\Middleware\Authenticate::class,
|
||||
'onlyAdmin' => \App\Http\Middleware\RequireAdminRole::class,
|
||||
// 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
|
||||
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
|
||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
|
||||
class ModuleRegistrator
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
$modules = \Module::enabled();
|
||||
|
||||
// iterate over all enabled modules
|
||||
foreach ($modules as $key => $module) {
|
||||
$handler = config($module->getName() . '.handler');
|
||||
|
||||
// try to register menus
|
||||
if (method_exists($handler, 'registerAdminMenu') && \Auth::check()) {
|
||||
call_user_func([$handler, 'registerAdminMenu'], \Auth::user());
|
||||
}
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Models\User;
|
||||
use Closure;
|
||||
|
||||
class RequireAdminRole
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
if (\Auth::user()->role != User::ROLE_ADMIN) {
|
||||
return redirect()
|
||||
->back()
|
||||
->with('error', trans('auth.accessDenied'));
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
+43
-37
@@ -28,22 +28,55 @@ Route::group([
|
||||
'prefix' => 'admin'
|
||||
], function () {
|
||||
|
||||
// these routes are available only for user with role admin
|
||||
Route::group(['middleware' => 'onlyAdmin'], function () {
|
||||
// display form for creating new user
|
||||
Route::get('users/new', 'CreateNewUser@getNewUser')
|
||||
->name('users.new');
|
||||
|
||||
// POST methods
|
||||
// display form for creating new user
|
||||
Route::post('users/new', 'CreateNewUser@postRegistration')
|
||||
->name('users.newPost');
|
||||
|
||||
// display form for edit of existing user
|
||||
Route::get('users/edit/{id}', 'EditUser@getEditExistingUser')
|
||||
->name('users.edit');
|
||||
|
||||
// form for adding new custom field
|
||||
Route::get('customfields/new', 'CreateNewCustomField@getNew')
|
||||
->name('customfields.new');
|
||||
|
||||
// form for editing custom field
|
||||
Route::get('customfields/edit/{id}', 'EditCustomField@getEditCustomField')
|
||||
->name('customfields.edit');
|
||||
|
||||
// form for adding new custom field
|
||||
Route::post('customfields/new', 'CreateNewCustomField@postNew')
|
||||
->name('customfields.newPost');
|
||||
|
||||
// form for editing custom field
|
||||
Route::post('customfields/edit/{id}', 'EditCustomField@postEditCustomField')
|
||||
->name('customfields.editPost');
|
||||
|
||||
Route::get('customfields/delete/{id}', 'EditCustomField@deleteCustomField')
|
||||
->name('customfields.delete');
|
||||
});
|
||||
|
||||
// these routes are available for all logged (authenticated) users
|
||||
|
||||
// display dashboard with some usefull information
|
||||
Route::get('/', 'Dashboard@getIndex')
|
||||
->name('admin.dashboard');
|
||||
|
||||
// display form for creating new user
|
||||
Route::get('users/new', 'CreateNewUser@getNewUser')
|
||||
->name('users.new');
|
||||
|
||||
// display form for edit of existing user
|
||||
Route::get('users/edit/{id}', 'EditUser@getEditExistingUser')
|
||||
->name('users.edit');
|
||||
|
||||
// display user detail in admin
|
||||
Route::get('users/detail/{id?}', 'UserDetail@getDetail')
|
||||
->name('users.detail');
|
||||
|
||||
// method for saving edited data of user
|
||||
Route::post('users/edit/{id}', 'EditUser@postEdit')
|
||||
->name('users.editPost');
|
||||
|
||||
// display list of all users registered into our system
|
||||
Route::get('users/overview/{state?}/{perPage?}', 'UsersOverview@getOverview')
|
||||
->name('users.overview');
|
||||
@@ -55,25 +88,6 @@ Route::group([
|
||||
Route::get('customfields/overview', 'CustomFieldsOverview@getOverview')
|
||||
->name('customfields.overview');
|
||||
|
||||
// form for adding new custom field
|
||||
Route::get('customfields/new', 'CreateNewCustomField@getNew')
|
||||
->name('customfields.new');
|
||||
|
||||
// form for editing custom field
|
||||
Route::get('customfields/edit/{id}', 'EditCustomField@getEditCustomField')
|
||||
->name('customfields.edit');
|
||||
|
||||
// form for adding new custom field
|
||||
Route::post('customfields/new', 'CreateNewCustomField@postNew')
|
||||
->name('customfields.newPost');
|
||||
|
||||
// form for editing custom field
|
||||
Route::post('customfields/edit/{id}', 'EditCustomField@postEditCustomField')
|
||||
->name('customfields.editPost');
|
||||
|
||||
Route::get('customfields/delete/{id}', 'EditCustomField@deleteCustomField')
|
||||
->name('customfields.delete');
|
||||
|
||||
// logout method
|
||||
Route::get('logout', function () {
|
||||
|
||||
@@ -84,14 +98,6 @@ Route::group([
|
||||
return redirect()
|
||||
->route('login')
|
||||
->with('success', trans('admin/common.logoutSuccessfull'));
|
||||
})->name('logout');
|
||||
|
||||
// POST methods
|
||||
// display form for creating new user
|
||||
Route::post('users/new', 'CreateNewUser@postRegistration')
|
||||
->name('users.newPost');
|
||||
|
||||
// method for saving edited data of user
|
||||
Route::post('users/edit/{id}', 'EditUser@postEdit')
|
||||
->name('users.editPost');
|
||||
})
|
||||
->name('logout');
|
||||
});
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\ChangeSettings;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class LogChangeSettings extends Logger
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param ChangeSettings $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(ChangeSettings $event)
|
||||
{
|
||||
$parameters = [];
|
||||
|
||||
$this->saveLog($parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for action name. Used only internally
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getName()
|
||||
{
|
||||
return 'changeSettings';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\CreateNewCustomField;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class LogCreateCustomField extends Logger
|
||||
{
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param CreateNewCustomField $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(CreateNewCustomField $event)
|
||||
{
|
||||
$parameters = [
|
||||
'id' => $event->customField->id,
|
||||
'name' => $event->customField->name
|
||||
];
|
||||
|
||||
$this->saveLog($parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for action name. Used only internally
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getName()
|
||||
{
|
||||
return 'newCustomField';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\CreateNewUser;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class LogCreatedUser extends Logger
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param CreateNewUser $event
|
||||
*/
|
||||
public function handle(CreateNewUser $event)
|
||||
{
|
||||
$parameters = [
|
||||
'id' => $event->user->id,
|
||||
'name' => $event->user->name,
|
||||
'email' => $event->user->email
|
||||
];
|
||||
|
||||
$this->saveLog($parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for action name. Used only internally
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getName()
|
||||
{
|
||||
return 'createUser';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\DeleteCustomField;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class LogDeleteField extends Logger
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param DeleteCustomField $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(DeleteCustomField $event)
|
||||
{
|
||||
$parameters = [
|
||||
'id' => $event->customField->id,
|
||||
'name' => $event->customField->name
|
||||
];
|
||||
|
||||
$this->saveLog($parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for action name. Used only internally
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getName()
|
||||
{
|
||||
return 'deleteCustomField';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\EditCustomField;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class LogEditCustomField extends Logger
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param EditCustomField $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(EditCustomField $event)
|
||||
{
|
||||
$parameters = [
|
||||
'id' => $event->customField->id,
|
||||
'name' => $event->customField->name
|
||||
];
|
||||
|
||||
$this->saveLog($parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for action name. Used only internally
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getName()
|
||||
{
|
||||
return 'editCustomField';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\EditUser;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class LogEditUser extends Logger
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param EditUser $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(EditUser $event)
|
||||
{
|
||||
$parameters = [
|
||||
'id' => $event->user->id,
|
||||
'name' => $event->user->name,
|
||||
'email' => $event->user->email
|
||||
];
|
||||
|
||||
$this->saveLog($parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for action name. Used only internally
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getName()
|
||||
{
|
||||
return 'userEdit';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Illuminate\Auth\Events\Login;
|
||||
|
||||
class LogLogin extends Logger
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param Login $event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Login $event)
|
||||
{
|
||||
$parameters = [
|
||||
'id' => $event->user->id,
|
||||
'name' => $event->user->name,
|
||||
'email' => $event->user->email
|
||||
];
|
||||
|
||||
$this->saveLog($parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for action name. Used only internally
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getName()
|
||||
{
|
||||
return 'login';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Illuminate\Auth\Events\Logout;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class LogLogout extends Logger
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param Logout $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Logout $event)
|
||||
{
|
||||
$parameters = [
|
||||
'id' => $event->user->id,
|
||||
'name' => $event->user->name,
|
||||
'email' => $event->user->email
|
||||
];
|
||||
|
||||
$this->saveLog($parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for action name. Used only internally
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getName()
|
||||
{
|
||||
return 'logout';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Enumerations\ActionLogTypesForLogger;
|
||||
use App\Events\Event;
|
||||
use App\Models\ActionLog;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
abstract class Logger
|
||||
{
|
||||
private $actionTypes = [];
|
||||
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->actionTypes = ActionLogTypesForLogger::getWithTranslations();
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcut for loging events
|
||||
*
|
||||
* @param array $parameters
|
||||
*/
|
||||
protected function saveLog($parameters = [])
|
||||
{
|
||||
// try to translate string id into integer id
|
||||
$typeId = $this->getTypeId();
|
||||
|
||||
// is user logged in and exists given type?
|
||||
if (!Auth::check() || is_null($typeId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// get user id
|
||||
$userId = Auth::user()->id;
|
||||
|
||||
// setup object for insertion
|
||||
$data = [
|
||||
'user_id' => $userId,
|
||||
'type_id' => $typeId,
|
||||
'parameters' => json_encode((array)$parameters)
|
||||
];
|
||||
|
||||
// create new logged event
|
||||
ActionLog::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate string id to integer id
|
||||
*
|
||||
* @return int|null ID of action type when exists, null otherwise
|
||||
*/
|
||||
private function getTypeId()
|
||||
{
|
||||
$name = $this->getName();
|
||||
if (!isset($this->actionTypes[$name])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->actionTypes[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for action name. Used only internally
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function getName();
|
||||
}
|
||||
@@ -9,8 +9,9 @@ use Illuminate\Database\Eloquent\Model;
|
||||
*/
|
||||
class ActionLog extends Model
|
||||
{
|
||||
protected $table = 'action_log';
|
||||
|
||||
public $timestamps = true;
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'type_id',
|
||||
|
||||
@@ -14,6 +14,7 @@ class ActionLogType extends Model
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'string_id',
|
||||
'value'
|
||||
];
|
||||
|
||||
|
||||
+97
-4
@@ -3,15 +3,14 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Class Content
|
||||
*/
|
||||
class Content extends Model
|
||||
{
|
||||
|
||||
public $timestamps = true;
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'module_handler',
|
||||
@@ -26,8 +25,102 @@ class Content extends Model
|
||||
'module_settings',
|
||||
'status'
|
||||
];
|
||||
|
||||
protected $guarded = [];
|
||||
const STATUS_DELETED = 'deleted';
|
||||
const STATUS_DRAFT = 'draft';
|
||||
const STATUS_PROTECTED = 'protected';
|
||||
const STATUS_PUBLIC = 'public';
|
||||
|
||||
|
||||
/**
|
||||
* Getter for overview of all content pages of given handler (module type)
|
||||
*
|
||||
* @param $handler
|
||||
* @param bool $joinAuthor
|
||||
*
|
||||
*/
|
||||
public function getByType($handler, $joinAuthor = false)
|
||||
{
|
||||
$query = DB::table('contents')
|
||||
->select('contents.*')
|
||||
->where('contents.status', '<>', self::STATUS_DELETED)
|
||||
->where('module_handler', $handler)
|
||||
->orderBy('created_at');
|
||||
|
||||
// join user email and user name to results when needed
|
||||
if ($joinAuthor) {
|
||||
$query = $query->join('users', 'users.id', '=', 'contents.user_id')
|
||||
->select('contents.*', 'users.name AS author_name', 'users.email AS author_email');
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates current model with information about changed columns
|
||||
*
|
||||
* @param array $fields
|
||||
* @param array $request
|
||||
*
|
||||
* @return int number of changed columns
|
||||
*/
|
||||
public function updateWithHistory(array $fields, array $request)
|
||||
{
|
||||
$editBatch = uniqid();
|
||||
$changed = 0;
|
||||
|
||||
foreach ($fields as $fieldDB => $fieldRequest) {
|
||||
// ignore non-existing fields
|
||||
if (!isset($this->$fieldDB) || !isset($request[$fieldRequest])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// remove any whitespaces around real content
|
||||
$request[$fieldRequest] = trim($request[$fieldRequest]);
|
||||
|
||||
if ($this->$fieldDB != $request[$fieldRequest]) {
|
||||
|
||||
// setup history object
|
||||
$history = [
|
||||
'user_id' => \Auth::user()->id,
|
||||
'content_id' => $this->id,
|
||||
'edit_batch' => $editBatch,
|
||||
'column' => $fieldRequest,
|
||||
'old_value' => $this->$fieldDB,
|
||||
'new_value' => $request[$fieldRequest]
|
||||
];
|
||||
|
||||
// save history
|
||||
ContentHistory::create($history);
|
||||
|
||||
// save change into database
|
||||
$this->$fieldDB = $request[$fieldRequest];
|
||||
|
||||
$changed++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->save();
|
||||
|
||||
return [$editBatch, $changed];
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark relationship with its historical records
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function history()
|
||||
{
|
||||
return $this->hasMany('App\Models\ContentHistory');
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark relationship with users table
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function author()
|
||||
{
|
||||
return $this->belongsTo('App\Models\User', 'user_id');
|
||||
}
|
||||
}
|
||||
@@ -9,13 +9,14 @@ use Illuminate\Database\Eloquent\Model;
|
||||
*/
|
||||
class ContentHistory extends Model
|
||||
{
|
||||
protected $table = 'content_history';
|
||||
|
||||
public $timestamps = true;
|
||||
// this table does not have created_at and updated_at fields
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'content_id',
|
||||
'changed_at',
|
||||
'edit_batch',
|
||||
'column',
|
||||
'new_value',
|
||||
|
||||
@@ -10,7 +10,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
class Module extends Model
|
||||
{
|
||||
|
||||
public $timestamps = true;
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'handler',
|
||||
|
||||
@@ -13,8 +13,30 @@ class EventServiceProvider extends ServiceProvider
|
||||
* @var array
|
||||
*/
|
||||
protected $listen = [
|
||||
'App\Events\SomeEvent' => [
|
||||
'App\Listeners\EventListener',
|
||||
'App\Events\CreateNewUser' => [
|
||||
'App\Listeners\LogCreatedUser',
|
||||
],
|
||||
'App\Events\EditUser' => [
|
||||
'App\Listeners\LogEditUser',
|
||||
],
|
||||
'App\Events\CreateNewCustomField' => [
|
||||
'App\Listeners\LogCreateCustomField',
|
||||
],
|
||||
'App\Events\EditCustomField' => [
|
||||
'App\Listeners\LogEditCustomField',
|
||||
],
|
||||
'App\Events\DeleteCustomField' => [
|
||||
'App\Listeners\LogDeleteField',
|
||||
],
|
||||
'App\Events\ChangeSettings' => [
|
||||
'App\Listeners\LogChangeSettings',
|
||||
],
|
||||
// internal Laravel events - should work as auth.login and auth.logout, however it doesn't...
|
||||
'Illuminate\Auth\Events\Login' => [
|
||||
'App\Listeners\LogLogin'
|
||||
],
|
||||
'Illuminate\Auth\Events\Logout' => [
|
||||
'App\Listeners\LogLogout'
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user