Monitoring access Private/reserved Area

Hi everybody!

I have a question for you:
I made a simple reserved area where only the registered users can access.
It is possible to monitoring the access at the area with a counter in the panel for each users or receive an email everytime?

I find this but it doesn’t work and it only send mail when a user logged in: Kirby Notifier - a panel security plugin

Thanks!

Somewhere in your code you are telling Kirby to login and redirect somewhere. Before redirecting, you can use something like this:

...

if($user->login($password)) {
  $page->increment('counter');

  go('/'):
}

Thank you for the answer, It’s a long time that I don’t work on this projet and now I have resumed it.
I try tu use this code and it works, but it only increment the value, it doesn’t tell me the name of the user who logs in.

What can I do to know the name of the user?

Thanks!

Well, you might as well send an email instead of incrementing the counter field, or use a structure field where you store the user name and the time the user logged in.

Thanks for the help and the patience but I still have doubts, this is the code of my controller:

<?php 

  return function($site, $pages, $page) {

   if($site->user()) go('tutti-i-file-area-riservata');

      if(r::is('post') and get('login')) {

      if($user = $site->user(get('username')) and $user->login(get('password'))) {

        $page->increment('counter');

        $email = email(array(
           'to'      => 'my-email@email.com',
           'from'    => 'access@email.com',
           'subject' => 'Sending emails with Kirby is easy',
           'body'    => 'Hey! This was really easy! senza echo?'
        ));

        if($email->send()) {
           echo 'The email has been sent';
        } else {
        echo $email->error();
        }



       go('tutti-i-file-area-riservata');
       } else {

      $error = true;
      }

  } else {
    $error = false;  
  }

  return array('error' => $error);

 };

It works but I have no Idea how to sand the username and the time/date, something like this?

'body'    => 'Username: {$user['username']}'

I know it is wrong but it’s a bit difficult for me…

return function($site, $pages, $page) {

   if($site->user()) go('tutti-i-file-area-riservata');

      if(r::is('post') and get('login')) {
      
      $username = get('username');
      $password = get('password');
      if($user = $site->user($username) and $user->login($password))) {

        $page->increment('counter');

        $email = email(array(
           'to'      => 'my-email@email.com',
           'from'    => 'access@email.com',
           'subject' => 'Sending emails with Kirby is easy',
           'body'    => 'Hey! This was really easy! The user ' . $username . ' logged in on ' . date('Y-m-d  H:i:s' , time()) . '.'
        ));

        if($email->send()) {
           echo 'The email has been sent';
        } else {
        echo $email->error();
        }



       go('tutti-i-file-area-riservata');
       } else {

      $error = true;
      }

  } else {
    $error = false;  
  }

  return array('error' => $error);

 };

Great! It works :sparkling_heart:

There is a little error here:

There is an extra parenthesis and the correct format is something like Y-m-d H:i:s

I think this lines of code is not necessary, but I try to remove and the email doesn’t arrive…

 if($email->send()) {
        echo 'The email has been sent';
     } else {
    echo $email->error();
    }

Well, you have to send the email after all and that is done within the if-statement… Instead of echoing success or error, you could write to a log file. Or just send the email:

$email->send();

without any confirmation…

Hi everybody,

I open again this threat because I am implementing the counter for the users who login in the panel.

I create a new custom user form fields like this in blueprints/users/client.yml:

fields: 
  counter: 
    label: Numero di accessi
    type: number
    width: 1/2

client.yml is one of the roles of the site.

<?php 

return function($site, $pages, $page) {

  if($site->user()) go('tutti-i-file-area-riservata');

  if(r::is('post') and get('login')) {


    $username = get('username');
    $password = get('password');
    if($user = $site->user(get('username')) and $user->login(get('password'))) {


      $email = email(array(
        'to'      => 'info@aaa.com',
        'from'    => 'access@aaa.com',
        'subject' => 'Accesso utente ' . $username . ' area riservata',
        'body'    => 'Hey! L\'utente ' . $username . ' ha effettuato l\'accesso all\'area riservata il ' . date('d-m-Y  H:i:s' , time()) . ' .'
      ));


      $email->send();

      $site->user(get('username'))->increment('counter');

      go('tutti-i-file-area-riservata');
    } else {
      $error = true;
    }

  } else {
    $error = false;  
  }

  return array('error' => $error);

};

The new code I have added is this, but it does’t work…

$site->user(get('username'))->increment('counter');

Can you help me to find what is wrong?

Thanks!

There is no increment() method for the user object, use $user->update() instead.

      $accessi = $site->user(get('username'))->get('counter');

      $site->user(get('username'))->update(array(
      'counter'     => $accessi++
      ));

Something like this?

I’m not very good in PHP… sorry

Not quite…

<?php

if($user = $site->user(get('username')) {
  $accessi = $user->counter();
  $user->update([
    'counter'     => $accessi+1
  ]);
}