0

Auth コンポーネントを使用しています。現在、ログインしていないユーザーが「管理者」ページ (adminhome.ctp) にアクセスできないようにすることができます。しかし、isAuthorized()管理者以外がページにアクセスできないようにする方法がわかりません。

AppController の内部:

public function beforeFilter() {
    $this->Auth->allow('index', 'view', 'login', 'logout', 'display');
    $this->Auth->authorize = array('Controller'); 
    //$this->Auth->autoRedirect = false;
}

public function isAuthorized($user_id) {
    $this->loadModel('User');
    $user = $this->User->findById($this->Auth->user());
    if ( $user['User']['role'] === 'admin') {
        $this->Session->setFlash('isAuthorized');
        return true;
    }
    $this->Session->setFlash('!isAuthorized');
    return false;
}

PagesController の beforeFilter() は次のとおりです。

function beforeFilter() {
    $this->Auth->deny('adminhome');
}

私たちは何を間違っていますか?

4

3 に答える 3

2

アーリア人の回答を使用しましたが、他の人に役立つ可能性のある小さな変更をいくつか加えました。

if($this->Session->read('Auth.User.role') != 'admin')
    {
        $this->Session->setFlash('You are not authorized to visit this page');
        $this->redirect('/');
    }
于 2015-03-28T22:31:49.803 に答える
1

Auth->deny() を使用してメソッドへのアクセスを制限する必要があり、adminhome は PagesController のメソッドではないため、あなたのやり方ではうまくいかないと思います。これを試して:

# in app/controller/PagesController.php
public function display() {
  $page = empty($this->request->params['pass'][0]) ? null : $this->request->params['pass'][0];
  if($page == 'adminhome' || $this->User->isAuthorized()) {
    $this->render($page);
  } else {
    # redirect the user somewhere else
  }
}

これが役立つことを願っています

于 2013-04-26T00:19:43.763 に答える