Thursday, April 30, 2009

Re: Auth Component require login in every page

I think you have forgotten to add this to controller beforeFilter():

function beforeFilter() {
parent::beforeFilter();
$this->Auth->allowedActions = array('logout', 'initDB');
}

http://book.cakephp.org/view/643/Preparing-to-Add-Auth

also, I'm not sure but I've read somewhere that 'login' cannot be in allow() array (the auth logic takes care of it) because it can make Auth behave oddly.

DatacenterHellas pisze:
> Hello all ! ! !
>
> I'm new to CakePHP and I already have create a login system based on
> Auth Component. All are fine with that component, but the problem that
> I have is that if the user logou the system then he can't see any
> page. The Auth redirects automatic to login form. What can I do, for
> desplay pages that are not require login ? ? ?
>
> ie : index, about, contact us must be public pages. Do not require
> login.
> add new article, edit articles, manage users must be private.
>
> How to select witch page must be private and witch must be public.
>
> My Code :
>
> AppController :
>
> class AppController extends Controller
> {
> var $name = "App";
> var $helpers = array('javascript','html','form');
> var $components = array('Auth','newArticle');
>
> function beforeRender()
> {
> $this->set('articles', $this->newArticle->getLatest());
> }
> }
>
> class UsersController extends AppController
> {
> var $name = "Users";
>
> function beforeFilter()
> {
> $this->Auth->allow('register', 'login', 'logout');
> }
>
> function register()
> {
> if(!empty($this->data))
> {
> $this->data['User']['password_confirm'] = $this->Auth->password
> ($this->data['User']['password_confirm']);
> $this->User->create();
> if($this->User->save($this->data))
> {
> $this->redirect(array('controller'=>'Pages','action'=>'index'));
> }
> }
> }
>
> function login()
> {
> if(!empty($this->data))
> {
> $usr = $this->Auth->login($this->data);
> if(!empty($usr))
> {
> $this->set('data',$this->Auth->user());
> $this->redirect(array('controller'=>'Pages','action'=>'index'));
> }
> else
> {
> $this->Session->setFlash("Η είσοδος απέτιχε");
> $this->redirect(array('controller'=>'Users','action'=>'login'));
> }
> }
> }
>
> function logout()
> {
> $this->Auth->logout();
> $this->redirect(array('controller'=>'Pages','action'=>'index'));
> }
>
> }
>
> class ArticlesController extends AppController
> {
> var $name = "Articles";
> var $paginate = array(
> 'limit' => 5,
> 'order' => array(
> 'Article.id' => 'DESC'
> )
> );
>
> function add()
> {
> if(!empty($this->data))
> {
> $this->Article->create();
>
> if($this->Article->save($this->data))
> {
> $this->redirect(array('controller'=>'Pages','action'=>'index'));
> }
> else
> {
> $this->Session->setFlash('Σφάλμα κατά την αποθήκευση');
> }
> }
> }
>
> function view()
> {
> $artcl = $this->Article->find('first', array('conditions'=>array
> ('Article.id'=>$this->params['named']['artid'])));
> $this->set('article', $artcl['Article']);
> }
>
> function showAll()
> {
> $artcl = $this->paginate('Article');
> $this->set('data', $artcl);
> }
> }
>
> class ProfilesController extends AppController
> {
> var $name = "Profiles";
>
> function view()
> {
> $pfl = $this->Profile->find('first', array('conditions'=>array
> ('User.id'=>$this->Session->read('Auth.User.id'))));
> $this->set('data', $pfl);
> }
>
> function edit()
> {
> if(!empty($this->data))
> {
> if($this->Profile->save($this->data))
> {
> $this->redirect(array('controller'=>'Profiles','action'=>'view'));
> }
> else
> {
> $this->Session->setFlash('Error');
> }
> }
> else
> {
> $pfl = $this->Profile->find('first', array('conditions'=>array
> ('User.id'=>$this->Session->read('Auth.User.id'))));
> $this->set('data', $pfl);
> }
> }
>
> function add()
> {
> if(!empty($this->data))
> {
> if($this->Profile->save($this->data))
> {
> $this->redirect(array('controller'=>'Profiles','action'=>'view'));
> }
> else
> {
> $this->Session->setFlash('Error');
> }
> }
> else
> {
> $pfl = $this->Profile->User->find('all', array('conditions'=>array
> ('User.id'=>$this->Session->read('Auth.User.id'))));
> $this->set('data', $pfl);
> }
> }
> }
>
> class PagesController extends AppController {
>
> var $name = 'Pages';
> var $uses = null;
>
> function index()
> {
> }
>
> }
>
> class ArticlesController extends AppController
> {
> var $name = "Articles";
> var $paginate = array(
> 'limit' => 5,
> 'order' => array(
> 'Article.id' => 'DESC'
> )
> );
>
> function add()
> {
> if(!empty($this->data))
> {
> $this->Article->create();
>
> if($this->Article->save($this->data))
> {
> $this->redirect(array('controller'=>'Pages','action'=>'index'));
> }
> else
> {
> $this->Session->setFlash('Σφάλμα κατά την αποθήκευση');
> }
> }
> }
>
> function view()
> {
> $artcl = $this->Article->find('first', array('conditions'=>array
> ('Article.id'=>$this->params['named']['artid'])));
> $this->set('article', $artcl['Article']);
> }
>
> function showAll()
> {
> $artcl = $this->paginate('Article');
> $this->set('data', $artcl);
> }
> }
>
> >
>
>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: