2

MY_Controller で、ユーザーが現在のページへのアクセスを許可されているかどうかを確認できるように、アプリケーションにいくつかの変更を追加しようとしています。これは私のコントローラーの 1 つの例です。私のすべてには、読み取り、編集、作成、削除機能があります。すべての関数でifステートメントを実行する以外に、ユーザーが関数にアクセスすることを許可または禁止するアクセス許可をグローバルに設定する方法を理解する必要があります。

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Content_pages extends MY_Controller
{  
    /**
     * Account::__construct()
     * 
     * Load the parent construct and any additional models, helper, libraries available. 
     * 
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->model('content_page_model', 'content_page');
    }

    /**
     * Content_pages::read()
     * 
     * @return
     */
    public function read()
    {     
        //vardump($this->user_data);
        // Checks to see if the user has a role id of four and if they do then it shows the admin dashboard and if not then shows the user dashboard.
        if ($this->user_data->access_level_id >= 4)
        {
            // Retrieve all the users from the database that handle characters and assign it to the users variable.
            $content_pages = $this->content_page->get_all();

            // Place to dump the users array to verify it is the expected value.
            // vardump($users);

            // Checks to verify that there is data inside of the users array and that it is not empty.
            if (!empty($content_pages))
            {
                $this->template->set('content_pages', $content_pages);
            }

            // Add the breadcrumbs to the view.
            $this->breadcrumb->add_crumb('<li><a href="' . base_url() . 'wrestling-manager/control-panel" class="glyphicons home"><i></i> Control Panel</a></li>');
            $this->breadcrumb->add_crumb('<li><i></i> Content Pages</li>');
            $this->breadcrumb->change_link('<li class="divider"></li>');

            // Sets all the properites for the template view.
            $this->template
                ->set_theme('smashing')
                ->set_layout('control_panel_view')
                ->set_partial('header', 'partials/header')
                ->set_partial('sidebar','partials/sidebar')
                ->set_partial('footer', 'partials/footer')
                ->title('Content Pages')
                ->set('user_data', $this->user_data)
                ->build('content_pages_view');    
        }
        else
        {
            echo 'haha';
            //redirect('wrestling-manager/control-panel');
        }
    }

    /**
     * Content_pages::edit()
     * 
     * @return void
     */
    public function create()
    {
        echo 'testing for create function';
    }

    /**
     * Content_pages::edit()
     * 
     * @return void
     */
    public function edit($content_page_id)
    {
        vardump($content_page_id);
    }

    public function delete($content_page_id)
    {
        vardump($content_page_id);
    }

    /**
     * Content_pages::save()
     *  
     * @return
     */
    public function save()
    {
        echo 'testing for save function';
    }

    /**
     * Content_pages::update()
     * 
     * @return
     */
    public function update()
    {
        echo 'testing for update function';
    }
}
4

2 に答える 2

1

それがMY_Controllerのすべてです-そこですべてのチェックを行う必要があります-URLおよび/またはGET/POST/SESSIONパラメーターに従って、そこで関数を作成し、コンストラクターで呼び出します。ユーザーにアクセス権がない場合は、フラグまたはエラー テキストを SESSION に追加します。次に、メインコントローラーでその SESSION フラグのみをチェックします。

于 2013-08-29T10:41:26.980 に答える
1

構成ファイルまたはデータベースで権限を設定できます。

パーミッションチェックを使用すると、コントローラーを呼び出す直前にインターセプター/フィルターを使用する方がよいでしょう。

コントローラーについては、通常は CRUD 操作を実行するためのものではなく、はるかに高いレベルでのドメイン固有の操作 (または、より低いレベルの場合は単一の共通操作) を実行することを意図していないため、少し間違っていると言わざるを得ません。handleRequest方法)。

  • 役割のリストと権限のリストを用意します。(例: GUEST、USER、ADMIN ロール。権限はドメイン固有)
  • 必要に応じてロールを権限に関連付けます。
  • 各ユーザーに役割を割り当てる

その後、 を介してAuthorizationService、現在のユーザーが何かを実行できるかどうかを確認できます。たとえば、このサービスは、現在のユーザーのロールがそれらを持っていることを確認するために、特定の操作に必要なすべてのアクセス許可を反復処理できます。例えば:

class AuthorizationFilter {

    public function verifyAccess($user, $request) {
        $role = $user->getRole();
        $permissions = $authorization->getPermissionsFor($request);
        $allowed = true;    // true as a missing permission will later set it to false
        for ($i = 0; $i < size($permissions); $i++) {
            $allowed &= $role->hasPermission($permissions[$i]);
        }
        return $allowed;
    }
}

その後、承認の結果に基づいて、元のリクエストまたは「フォールバック」リクエストのコントローラーを呼び出すことができます。

class RequestDispatcher {

    public function dispatch() {
        // ...
        if ($authFilter->verifyAccess($user, $request)) {
            // invoke proper request controller
        } else {
            // invoke "you're not allowed to do this" controller
        }
        // ...
    }
}

警告: 上記のコードは単なるサンプル コードであり、決して完全ではなく、本番環境に適しているわけでもありません!!!

于 2013-08-29T11:02:45.210 に答える