Answer the question
In order to leave comments, you need to log in
How can I log the actions of moderators on the site through events?
I want to log everything that the moderators do on the site, for example when they edited a post, or deleted a comment, created a forum thread, etc.
Tell me how to do it, I want to record who did what, when, where, what time.
To do it through standard events? afterSave afterDelete ? tell me more please.
As I see it, I just insert code into the desired action, like Log($model);
and somewhere it's all recorded in the database
Answer the question
In order to leave comments, you need to log in
Everywhere and everywhere insert Log($model); is not the right move. Logic is smeared across all models.
It is better to write a separate module and use events to solve everything.
As an example ..
in the log module component, which I connect in the config like this
'logs' => [
'class' => common\modules\logs\components\Log::className(),
],
namespace common\modules\logs\components;
class Log extends Event
{
/**
* @inheritdoc
*/
public function init()
{
// Слушаем события моделей. Если событие произошло, то обрабатываем его в builderUser
self::on(User::className(), User::EVENT_AFTER_LOGIN, [$this, 'builderUser']);
self::on(User::className(), User::EVENT_BEFORE_LOGOUT, [$this, 'builderUser']);
......
/**
* Построитель событий пользователей
*
* @param mixed $event Событие
*
* @return void
*/
public function builderUser($event = null)
{
switch ($event->name) {
case User::EVENT_AFTER_LOGIN: // Авторизация
if ($event->sender->identity->isAdmin()) { // Админ
$this->addEvents(LogEvents::EVENT_AUTHORIZATION_ADMINISTRATOR, $event->sender->identity);
} elseif ($event->sender->identity->isSeller()) { // Продавец
.....
} elseif (...) { // Другие пользователи системы
.....
}
break;
case User::EVENT_BEFORE_LOGOUT: // Выход из ЛК
if ($event->sender->identity->isAdmin()) { // Админ
$this->addEvents(LogEvents::EVENT_LOGOUT_ADMINISTRATOR, $event->sender->identity);
} elseif ($event->sender->identity->isSeller()) { // Продавец
......
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question