cakePHP – logowanie autoryzacji

app.php w sekcji Log => [

'auth' => [
            'className' => 'Cake\Log\Engine\FileLog',
            'path' => LOGS,
            'file' => 'auth',
            'url' => env('LOG_AUTH_URL', null),
            'scopes' => ['auth'],
        ],

Kontroler Users / login()

use Cake\Log\Log;

if( $this->request->is(['post']) )
{
  $user = $this->Auth->identify();
            
  $login  = $this->request->getData()['username'];
  $ip     = $this->request->clientIp();
  $domain = $this->request->host();
  $agent  = $this->request->getEnv('HTTP_USER_AGENT');
  $langs  = $this->request->getEnv('HTTP_ACCEPT_LANGUAGE');
  $lang   = substr($langs, 0, 2);

  $info = "Login: " . $login.' | '.$ip.' | '.$domain.' | '.$lang.' | '.$agent;
            
  if($user)
  {
     $this->Auth->setUser($user);
     Log::info($info, ['auth']);
     $this->redirect( $this->Auth->redirectUrl() );
  } else {
     $this->Flash->error(__('Błędne dane logowania'));
     Log::error('Błąd logowania: '.$info, ['auth']);
     $this->redirect(['action' => 'login']);
  }
}

Kontroler Users / logout()

Log::info("Logout: " .$this->Auth->user('username'), ['auth']);

Sprawdzenie logów

# tailf /var/www/project/logs/auth.log

2018-10-22 16:26:12 Info: login admin| 37.252.202.32 | example.pl | pl | Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0

2018-10-22 16:26:14 Info: admin logout

Dodaj komentarz