1

私は Cakephp の初心者で、ゼロからアプリケーションを構築しようとしています。私はマニュアルを読んでおり、チュートリアルも行いました。

現在、承認に関する問題に直面しています: ActionsAuthorize と ControllersAuthorize を使用して、ユーザーがアクション (編集、削除、...) を実行できるようにする方法ですが、それが自分のもの (投稿、イベント、...) である場合にのみ同時に。ばかげた質問かもしれませんが、初心者として、何かが足りないと感じています。

アプリケーションが単純であれば、ControllersAuthorize (isAuthorized()) を使用するだけで十分ですが、ActionsAuthorize は非常に便利です。

チュートリアル (ブログ) の例に従って、各投稿には作成者 (ユーザー) がいて、プロファイル (作成者、管理者、編集者など) に属し、一般にユーザーは投稿を編集できます ($this->Acl->allow ('author','controllers/Posts/edit')) ただし、自分の投稿のみ。

これまでのところ、次のコードがあります。

//User Model
public $actsAs = array('Acl' => array('type' => 'both'));
public $belongsTo = array(
    'Profile' => array(
        'className' => 'Profile',
        'foreignKey' => 'profile_id'
    )
);
//saving the aro alias => username   
public function aftersave($created) {
    if($created) {
        $this->Aro->save(array('alias'=>$this->data[$this->alias]['username']));
    }
}

public function parentNode() {
    if (!$this->id && empty($this->data)) {
        return null;
    }
    if (isset($this->data['User']['profile_id'])) {
        $profileId = $this->data['User']['profile_id'];
    } else {
        $profileId = $this->field('profile_id');
    }
    if (!$profileId) {
        return null;
    } else {
        return array('Profile' => array('id' => $profileId));
    }
}

//Profile Model
public $actsAs = array('Acl' => array('type' => 'both'));
public function parentNode() {
    return null;
}
//savin the aco alias => name
public function aftersave($created) {
    if($created) {
        $this->Aro->save(array('alias'=>$this->data[$this->alias]['name']));
    }
}

//Post Model
public function isOwnedBy($post, $user) {
    return $this->field('id', array('id' => $post, 'user_id' => $user)) === $post;
}

//Post Controller
public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('index');
}       
public function isAuthorized($user) {
    // All registered users can add posts
    if ($this->action === 'add' && isset($user)) {
        return true;
    }
    // The owner of a post can edit and delete it
    if (in_array($this->action, array('edit', 'delete'))) {
        $postId = $this->request->params['pass'][0];
        if ($this->Post->isOwnedBy($postId, $user['id'])) {
            //after knowing if the post is owned by the user check ACL
            return $this->Acl->check($user['profile_id'],$this->Acl->aco,$this->action);
        }
    }
    return parent::isAuthorized($user);
}

//AppController
public $components = array(
    'Acl',
    'Session',
    'Auth' => array(
        'authenticate' => array('Blowfish'),
        'authError' => 'You are not allow to perform this action, please login',
        'loginRedirect' => array('controller' => 'users', 'action' => 'login'),
        'logoutRedirect' => array('controller' => 'posts', 'action' => 'index')
    )
);
public function isAuthorized($user) {
    // Only admins can access admin functions
    if (isset($user['profile_id']) && isset($this->request->params['admin'])) {
        return (bool)($user['profile_id'] === '1');
    }
    // Admin can access every action
    if (isset($user['profile_id']) && $user['profile_id'] === '1') {
        return true;
    }       
    return false;
}       
public function beforeFilter() {
    $this->Auth->authorize = array('Controller');
    $this->Auth->allow('display');
}  
        // The owner of a post can edit and delete it
        if (in_array($this->action, array('edit', 'delete','view'))) {
            $postId = $this->request->params['pass'][0];
            if ($this->Post->isOwnedBy($postId, $user['id'])) {
                return $this->Acl->check($user['profile_id'],$this->Acl->aco,$this->action);
            }
        }
        return parent::isAuthorized($user);
    }

したがって、私の質問は、PostController:isAutorized() で使用している方法で承認を正しく使用しているかどうかです。最初にユーザーが投稿を所有しているかどうかを確認し、次にユーザーがアクションを許可されているかどうかを確認します。別の方法ですか?この方法でいいですか?もっと良い方法はありますか?何か不足していますか?

よろしくお願いします!

4

1 に答える 1

0

これを実現する 1 つの方法を次に示します。編集: ACL アプリを実行していることに気付きました。そうでない場合の答えはこれです。他に誰も応答していないので、それはまだ役立つかもしれないと考えました.

public function doStuff(){
      if(AuthComponent::user('role') == 'admin'){
      //do stuff 
      }elseif(AuthComponent::user('id') == $blogpost['Blog']['owner']){ 
      // do stuff 
      }else{ 
      $this->Html->redirect(array('action' => 'elsewhere'));
      }


 }
于 2013-09-11T14:44:31.173 に答える