Ho usato Auth, ma sfortunatamente, sembra che funzioni solo con Session. Voglio che se l'utente controlla la casella di controllo "Ricordami", userei Cookie e lui verrebbe loggato per 2 settimane. Non riesco a trovare nulla nel libro ufficiale e in Google ho trovato solo pochi e non ottimi post sul blog. C'è un modo per implementare questo senza riscrivere il nucleo?CakePHP mi ricordo con Auth
risposta
Ricordarsi di me non è altro che la sessione identificata con un cookie, ma la durata del cookie è impostata su infinito. Guarda Config/core.php per la durata del cookie di sessione.
La durata dei cookie non è sufficiente. Dovresti anche fare in modo che le sessioni durino così a lungo sul server. – nIcO
Vedere questo URL penso che sia molto utile per voi.
http://lecterror.com/articles/view/cakephp-and-the-infamous-remember-me-cookie
o provare questo
function login() {
if ($this->Auth->user()) {
if (!empty($this->data) && $this->data['User']['remember_me']) {
$cookie = array();
$cookie['username'] = $this->data['User']['username'];
$cookie['password'] = $this->data['User']['password'];
$this->Cookie->write('Auth.User', $cookie, true, COOKIE_EXPIRE);
unset($this->data['User']['remember_me']);
}
$this->LogDetail->Write('activity','has logged IN');
$this->redirect($this->Auth->redirect());
}
if (empty($this->data)) {
$cookie = $this->Cookie->read('Auth.User');
if (!is_null($cookie)) {
if ($this->Auth->login($cookie)) {
$this->Session->destroy('Message.Auth'); # clear auth message, just in case we use it.
$this->LogDetail->Write('activity','has been authenticated via cookie and is now logged IN');
$this->redirect($this->Auth->redirect());
} else {
$this->LogDetail->Write('activity','attempted to gain access with an invalid cookie');
$this->Cookie->destroy('Auth.User'); # delete invalid cookie
$this->Session->setFlash('Invalid cookie');
$this->redirect('login');
}
}
}
}
Mi sentirei piuttosto a disagio sapendo che la mia password, anche crittografata, galleggia da qualche parte in un cookie. Penso che in tal caso, è sufficiente memorizzare il nome utente. – nIcO
nel controller utente:
public function beforeFilter() {
$this->Auth->allow(array('login', 'register'));
parent::beforeFilter();
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
// did they select the remember me checkbox?
if ($this->request->data['User']['remember_me'] == 1) {
// remove "remember me checkbox"
unset($this->request->data['User']['remember_me']);
// hash the user's password
$this->request->data['User']['password'] = $this->Auth->password($this->request->data['User']['password']);
// write the cookie
$this->Cookie->write('remember_me_cookie', $this->request->data['User'], true, '2 weeks');
}
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Username or password is incorrect.'));
}
}
$this->set(array(
'title_for_layout' => 'Login'
));
}
public function logout() {
// clear the cookie (if it exists) when logging out
$this->Cookie->delete('remember_me_cookie');
return $this->redirect($this->Auth->logout());
}
Nella vista login:
<h1>Login</h1>
<?php echo $this->Form->create('User'); ?>
<?php echo $this->Form->input('username'); ?>
<?php echo $this->Form->input('password'); ?>
<?php echo $this->Form->checkbox('remember_me'); ?> Remember Me
<?php echo $this->Form->end('Login'); ?>
Nel vostro AppController:
public $components = array(
'Session',
'Auth',
'Cookie'
);
public $uses = array('User');
public function beforeFilter() {
// set cookie options
$this->Cookie->key = 'qSI232qs*&[email protected][email protected]*(XSL#$%)[email protected][email protected]#HKis~#^';
$this->Cookie->httpOnly = true;
if (!$this->Auth->loggedIn() && $this->Cookie->read('remember_me_cookie')) {
$cookie = $this->Cookie->read('remember_me_cookie');
$user = $this->User->find('first', array(
'conditions' => array(
'User.username' => $cookie['username'],
'User.password' => $cookie['password']
)
));
if ($user && !$this->Auth->login($user['User'])) {
$this->redirect('/users/logout'); // destroy session & cookie
}
}
}
Mi sentirei piuttosto a disagio sapendo che la mia password, anche crittografata, galleggia da qualche parte in un cookie. Penso che in tal caso, è sufficiente memorizzare il nome utente. – nIcO
Ricorda che è l'hash crittografato, salato, della tua password.Se questo ti tiene ancora al limite, la tua scommessa migliore è quella di avere un token generato casualmente per accompagnare il nome utente. Personalmente non mi baserei solo sul nome utente. – Hoff
Hai ragione, in realtà l'ho capito dopo. Ma comunque, preferirei non archiviare nulla relativo alla password in un cookie. Non sono sicuro se sia una specie di allergia, ma questa idea mi dà la pelle d'oca ;-) – nIcO
uso dell'adattatore CookeAuthenticate:
https://github.com/ceeram/Authenticate/blob/master/Controller/Component/Auth/CookieAuthenticate.php
qui maggiori informazioni:
penso che c'è da sapere sui livelli CakePHP sicurezza. Cerca di ridurre la sicurezza del tuo cakePHP. Variabili di configurazione di CakePHP documentation. Avevo scritto un blog su di esso anche molto tempo fa.
E 'stato un po' di tempo da quando la domanda è stata risolta, ma spero che questo possa aiutare quelli che verranno dopo di me.
ho scritto breve procedura dettagliata su come configurare 'ricordati di me' funzionalità utilizzando Auhenticate Plugin da Ceeram
Maggiori informazioni qui: http://mirkoborivojevic.com/posts/2013/08/10/setup-remember-me-functionality-in-cakephp/
si può provare questo
if ($this->Auth->login())
{
if (!empty($this->data['User']['remember']))
{
$cookie = array();
$cookie['login'] = $this->data['User']['login'];
$cookie['password'] = $this->data['User']['password'];
$cookie['language'] =$this->data['User']['language'];
$this->Cookie->write('Auth.projectname', $cookie, true, '+1 years');
unset($this->data['User']['remember']);
public function admin_login() {
$this->layout = 'admin_login';
if (count($this->Session->read("Auth.User"))) {
$usr = $this->Session->read("Auth.User");
if ($usr['role'] == 'A' || $usr['role'] == 'RA' || $usr['role'] == 'MAfA' || $usr['role'] == 'Af' || $usr['role'] == 'FAA')
return $this->redirect(array('controller' => 'dashboard', 'action' => 'view'));
}
if ($this->request->is('post')) {
if ($this->request->data['User']['remember_me']=="1") {
// pr($this->request->data);
// die('sdd');
$this->Cookie->write('username', $this->request->data['User']['username'], true, '1 year');
$this->Cookie->write('password', $this->request->data['User']['password'], true, '1 year');
} else {
$this->Cookie->destroy();
}
/*
* Check if email or username is passed in form
*/
$uname = $this->request->data['User']['username'];
//login via email
if (filter_var($uname, FILTER_VALIDATE_EMAIL)) {
$u = $this->User->findByemail($uname);
} else { //login via username
$u = $this->User->findByusername($uname);
}
if ($u) {
$this->request->data['User']['username'] = $u['User']['username'];
/* * *
* Error if user is not active
*/
if ($u['User']['user_status'] != 'active') {
$this->Session->setFlash(__('Sorry! Your account is not active.'), 'default', array('class' => 'alert alert-danger'));
} elseif ($this->Auth->login()) { //if logged in
$user_caps = $this->fetchCapabilitiesByRole($u['User']['role']);
$this->Session->write("Auth.User.privileges", array('capabilities' => $user_caps['capabilities'], 'geo_areas' => array()));
if ($u['User']['role'] == 'A' || $u['User']['role'] == 'RA' || $u['User']['role'] == 'Af' || $u['User']['role'] == 'MAfA' || $u['User']['role'] == 'FAA')
return $this->redirect(array('controller' => 'dashboard', 'action' => 'view'));
return $this->redirect($this->Auth->redirect());
}else { //if invalid
$this->Session->setFlash(__('Invalid username or password.'), 'default', array('class' => 'alert alert-danger'));
}
} else {//if user does not exists
$this->Session->setFlash(__('User does not exists.'), 'default', array('class' => 'alert alert-danger'));
}
}
}
- 1. Cakephp 2.0 mock auth
- 2. Laravel 4 Mi ricordo che scade il tempo
- 3. Accesso alla sessione cakephp (auth) dall'esterno cakephp
- 4. Come mi ricordo differire dal timeout della sessione
- 5. Auth di Cakephp con più tabelle "Utenti"
- 6. CakePHP Auth Component utilizzando 2 tabelle
- 7. Errore di reindirizzamento componente CakePHP Auth
- 8. CakePHP 2.x Auth con due login separati
- 9. Come aumentare cakephp sessione componente Auth scadono tempo
- 10. Componente auth CakePHP reindirizza per accedere dopo logOUT
- 11. CakePHP Auth come consentire controller e azioni specifiche
- 12. git non ricordo la password
- 13. Auth con Unirest JAVA
- 14. Utilizzando Auth con endpoint
- 15. CakePHP OAuth con Google
- 16. cakePHP isAuthorized not working
- 17. query UPDATE con CakePHP
- 18. Applicazione CakePHP/CakePHP 2.4
- 19. CakePHP 2.0 con Twitter Bootstrap
- 20. Come integrare Wordpress con Cakephp?
- 21. http basic auth con swashbuckle documentazione api
- 22. Cakephp e Laravel coesistenti
- 23. Sails REST API con semplice AUTH
- 24. Come ottenere l'autenticazione con django-auth-ldap?
- 25. Autenticazione gitweb con Gitosis senza LDAP Auth?
- 26. Twitter Application-Solo Auth con "portatore Token"
- 27. Non ricordo la mia password debug.keystore per android
- 28. django-rest-auth: accesso social con google
- 29. CakePHP con Bootstrap (da Twitter)
- 30. Modello CakePHP con "Between dates"
Non sei più interessato a rispondere a questa domanda o cosa? Non hai dato feedback a nessuna delle risposte. – Hoff
Si consiglia di dare un'occhiata a https://github.com/delight-im/PHP-Auth, che è sia agnostico rispetto al database che indipendente dal database. – caw