使用CakePHP和AuthComponent创建Web服务?

时间:2015-08-14 22:00:54

标签: php web-services cakephp cakephp-2.7

我正在寻找解决问题的方法。我正在尝试使用CakePHP 2创建一个Web服务。 我创建了一个CRUD并使用AuthComponent进行配置登录。 AuthComponent配置为使用Form。当我尝试执行一些控制器的函数来返回一个JSON不起作用,并显示页面index.php的代码 我想如果我确实配置Basic Auth工作,但是当我尝试在$components中添加Basic Auth时它可以在浏览器上访问,所有操作都可以在浏览器上访问。

如何将AuthComponent的Basic和Form配置为一起工作?

我正在尝试此操作,但不起作用,因为所有操作都已打开以进行访问

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 个答案:

没有答案