M Farahat
M Farahat

Reputation: 89

Authorization for cakephp

I m still learning the cakephp. I m making a gallery so every page is public, I m not making accounts for users, but there is an admin account that posts the photos/videos/jobs I v searched but couldnt find something that matches my case.. here's my Appcontroller

    <?php
App::uses('Controller', 'Controller');
class AppController extends Controller {

    public $components = array('Auth' => array(
                'loginAction' => array(
                    'controller' => 'admins',
                    'action' => 'login'
                ),
                'loginRedirect' => array(
                        'controller' => 'pages',
                        'action' => 'display','adminpanel'
                ),
                    'logoutRedirect' => array(
                        'controller' => 'pages',
                        'action' => 'display',
                        'home'
                ),
                'authError' => 'Did you really think you are allowed to see that?',
                'authenticate' => array(
                    'Form' => array(
                        'fields' => array('username' => 'Username','password'=>'password')
                    )
                )
            ),'DebugKit.Toolbar');

   public function  beforefilter(){
       $this->Auth->authenticate = array('Form');
   }
}
?>

And here's my AdminsController

    <?php
App::uses('AppController', 'Controller');

class AdminsController extends AppController {


        public $uses='Admin';
        public $helpers = array('Html', 'Form', 'Session');
    public $components = array('Paginator','Session');


            //this can be changed later 
            //if the system has users and admins
        public function login()
        {
            $this->layout = 'login';
            if ($this->request->is('post')) 
            {
                if ($this->Auth->login()) 
                {
                    return $this->redirect($this->Auth->redirect());
                }
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }
}
?>

and there's the admins/login.ctp

<?php echo $this->Form->create('Admin',array('class'=>'form-signin')); ?>
        <h2 class="form-signin-heading"><?php echo __('Please sign in'); ?></h2>
        <?php echo $this->Form->input('Username',array('type'=>'text','placeholder'=>'Username','class'=>'form-control'));?>
        <?php echo $this->Form->input('password',array('type'=>'password','placeholder'=>'Password','class'=>'form-control'));?>
        <label class="checkbox">
          <input type="checkbox" value="remember-me"> Remember me
        </label>
        <?php echo $this->Form->end(array('value' => 'Sign in', 'class' => 'btn btn-lg btn-primary btn-block'));

now when i go to the website it redirects me to the admins login, and I dont want that. I just want to view the website as a public viewer and it can redirect me if i visited the website/admins or any page related to the admin

Thank you in advance

Upvotes: 0

Views: 1267

Answers (2)

shravan uchil
shravan uchil

Reputation: 64

in core.php enable admin prefixes.

Configure::write('Routing.prefixes', array('admin'));

then add Auth component.

Upvotes: 0

floriank
floriank

Reputation: 25698

This is well explained in the book, here.

You have to tell the auth component which actions are public accessible in each controller:

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow(array('index', 'view', '...')));
}

Upvotes: 1

Related Questions