Создание веб-сервиса с CakePHP и AuthComponent?

Я ищу решение моей проблемы. Я пытаюсь создать веб-сервис, используя CakePHP 2.
Я создал CRUD и настроил с помощью AuthComponent для входа в систему. AuthComponent был настроен для использования формы. Когда я пытаюсь выполнить некоторую функцию контроллера, чтобы вернуть JSON не работает, и показать мне код страницы index.php
Я думаю, что если я действительно настраиваю Basic Auth работает, но когда я пытаюсь добавить Basic Auth в $components это открытый доступ, все действия могут быть доступны в браузере.

Как я могу настроить Basic и Form AuthComponent для совместной работы?

Я пытаюсь это, но не работает, потому что все действия открыты для доступа

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;
}
}

1

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector