A
A
akula222016-08-02 11:38:55
Yii
akula22, 2016-08-02 11:38:55

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

1 answer(s)
M
melnikov_m, 2016-08-25
@melnikov_m

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(),
        ],

writing...
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']);
......

The builderUser function looks like this..
/**
     * Построитель событий пользователей
     *
     * @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()) {  // Продавец
                 ......
                }

and the addEvents function directly writes the event type, the event and the log itself to the
LogEvents database - this is a regular model with a list of events and event types.
The code was taken out of context, there is no way to give a complete list, but I think the essence is clear.
1) We listen to the event of the model
2) we process the event depending on the user's rights and the type of event (entry, exit, purchase, removal of goods, etc.)
UPD
module structure
a35e5f669a014cf7bda8727238929f88.jpg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question