Creating a webservice with CakePHP and AuthComponent?

142 Views Asked by At

I'm looking for a solution to my problem. I'm trying create a webservice using CakePHP 2. I created a CRUD and configured using AuthComponent to login. The AuthComponent was configured to use Form. When I try execute some function of controller to return a JSON doesn't works and show me code of page index.php I think if I do configure Basic Auth works, but when I try add Basic Auth in $components it's opened access, all actions can be access on browser.

How could I configure Basic and Form of AuthComponent to works together ?

I'm trying this, but doesn't works, because all actions are opened to access

class AppController extends Controller {    

public $components = array("RequestHandler", "Auth", "Session");


    public function beforeFilter(){       
        $this->Auth->authenticate = array(
            'Basic' => array('userModel' => 'User',
                                'fields'=> array(
                                    'username' => 'email',
                                    'password' => 'senha'
                                ),
                                'scope' => array(
                                    'User.status' => 1
                                )
                            ),            
            'Form' => array('userModel' => 'User',
                                'fields'=> array(
                                    'username' => 'email',
                                    'password' => 'senha'
                                ),
                                'scope' => array(
                                    'User.status' => 1
                                )
                            ),                 
        );

        $this->Auth->loginAction = array(
            'controller' => 'users', 
            'action' => 'login'            
        );

        $this->Auth->loginRedirect = array(
            'controller' => 'matriculas', 
            'action' => 'index'            
        );

        $this->Auth->logoutRedirect = array(
            'controller' => 'users', 
            'action' => 'login'            
        );

        $this->Auth->authorize = "Controller";
        $this->Auth->authError = "Efetue login de acesso";
        $this->Auth->allow("login");
    }

    public function isAuthorized($user) {
        if (isset($user['role']) && $user['role'] === 'admin') {
            return true; // Admin pode acessar todas actions
        }
        return false; // Os outros usuários não podem
    }


}

UsersController

class UsersController extends AppController {

    public $components = array('Paginator');     


    public function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->Paginator->paginate());
    }

    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
    }

    public function edit($id = null) {
        if (!$this->User->exists($id)) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is(array('post', 'put'))) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
            $this->request->data = $this->User->find('first', $options);
        }
    }


        public function login(){
            $this->layout = "layout";            
            if($this->request->is("post")){                  
                if ($this->Auth->login()) {                       
                        $this->redirect($this->Auth->redirect());                                
                }else{
                    $this->Session->setFlash(__('Usuário ou senha inválido'));
                }                 
            }              
        }

        public function logout(){
            $this->redirect($this->Auth->logout());            
        }

        /********** WEB SERVICES FUNCTIONS *********/

        /** return all users **/
        public function findAll(){
            $this->set("users", $this->User->find('all'));
            $this->set(array(
               "_serialize" => 'users',
            ));          
        }

        /** add new user from app **/
        public function addUserFromApp(){
            $this->layout=null;
            $data = $this->request->input("json_decode", true);
            echo $data;
        }
}
0

There are 0 best solutions below