0

重複の可能性:
CodeIgniter 認証 + ユーザー権限

私は5つのユーザータイプを持っていて、異なるユーザーに異なる権限を与える権限テーブルを持っています。is_view、is_delete、is_add などの許可。ユーザーは、これらの権限に従って機能にアクセスします。

データベースを完成させます。コントローラーが呼び出される前に、各ページでユーザーに与えられた許可を確認したい。

4

3 に答える 3

1

auth-logicをコントローラーのコンストラクターに配置する必要があります

また

ベースコントローラーのコンストラクター内(すべてのコントローラーでロジックを繰り返す必要がないため、より多くのDRY)。

于 2012-10-11T06:47:07.803 に答える
1

コアコントローラーを拡張する新しいコントローラーを作成します。このファイルをに配置しますapplication/core/

class MY_AuthController extends CI_Controller {
    public function __construct() {
        // Do your auth check in here, redirect if not logged in
    }
}

次に、認証が必要なすべてのページで、この新しいコントローラーを継承します。このファイルは、通常のコントローラーフォルダーに配置するだけです

class Admin extends MY_AuthController {
    // All your controller goodness in here..
}
于 2012-10-11T07:00:34.507 に答える
0

次の 2 つの記事を読むことをお勧めします。

1. Keeping It Dryに関する Phil Sturgeon の投稿。

Phil は、コンストラクターにセッションと場合によってはデータベース ロジックが含まれる親コントローラーを作成する方法を紹介します。その後作成するすべてのコントローラは、ネイティブ コントローラではなく、カスタム コントローラから継承する必要がありますCI_Controller

に続く....

2. Shane Pearson のCodeIgniter Base Classes Revisited .

Shane の記事では、Phil のテクニックを改良し、カスタム コントローラーを から/coreに移動/baseし、より優れたものを使用しています__autoload()。この実装により、たとえば、CodeIgniter の CLI クラスを使用できるようになりましたが、Phil はバグアウトしました。


アイデアを提供するために、コードは完了すると次のようになります。

/base/MY_In_Controller.php

<?php
class MY_In_Controller extends CI_Controller{
    function __construct(){
        parent::__construct();
        //things like:
        //is the user even logged in? thank heavens I don't have to check this in every controller now. redirect if the session doesnt exist.
        //query the database and grab the permissions for the user. persist them with $this->load->vars();
        $this->data['perms'] = some_database_function();
        $this->load->vars($this->data);
    }
}

controllers/manage.php

<?php
class Manage extends MY_In_Controller{
    function __construct(){
        parent::__construct();
    }
    function index(){
        $this->load->view('manage');
        //and I can still access their permissions here and in the view.
        print_r($this->data['perms']);
    }
}
于 2012-10-12T03:56:07.853 に答える