2016-03-30 40 views
0

fuelphpプロジェクトでは、私は登録されているユーザーのための通常のバックエンドを持っています。フロントエンドでは、大学のユーザーだけがバックエンドにログインできる場所からのログインがあります(スーパーユーザーもフロントエンドからログインできません)。 私の問題は、バックエンド、フロントエンドの両方を開き、バックエンドのリフレッシュ時またはフロントエンドユーザーがバックエンドからログアウトした場合です。 私はバックエンドログインをデフォルトのfuelphpログイン(admin.php)として維持しています。 コード:fuelphpでフロントエンドログインとバックエンドログインを維持する方法

public function action_login() { 
     // Already logged in 
     Auth::check() and Response::redirect('admin'); 

     $val = Validation::forge(); 

     if (Input::method() == 'POST') { 
      $val->add('email', 'Email or Username') 
       ->add_rule('required'); 
      $val->add('password', 'Password') 
       ->add_rule('required'); 

      if ($val->run()) { 
       $auth = Auth::instance(); 

       // check the credentials. This assumes that you have the previous table created 
       if (Auth::check() or $auth->login(Input::post('email'), Input::post('password'))) { 
        // credentials ok, go right in 
        if (Config::get('auth.driver', 'Simpleauth') == 'Ormauth') { 
         $current_user = Model\Auth_User::find_by_username(Auth::get_screen_name()); 
        } else { 
         $current_user = Model_User::find_by_username(Auth::get_screen_name()); 
        } 
        Session::set_flash('success', e('Welcome, ' . $current_user->fullname)); 
        Response::redirect('admin'); 
       } else { 
        $this->template->set_global('login_error', 'Login failed.'); 
       } 
      } 
     } 

     $this->template->title = 'Login'; 
     $this->template->content = View::forge('admin/login', array('val' => $val), f 

alse);私はフロントエンドでAJAXを介してログインを行っている別のコントローラ(welcome.php)

public function action_login_university() { 

     $val = Validation::forge(); 

     if (Input::method() == 'POST') { 
      $auth = Auth::instance(); 
      // check the credentials. This assumes that you have the previous table created 
      //if (Auth::check() and $auth->login(Input::post('email'), Input::post('password'))) { 
      if ($auth->login(Input::post('email'), Input::post('password'))) { 
       // credentials ok, go right in 
       if (Config::get('auth.driver', 'Simpleauth') == 'Ormauth') { 
        $current_user = Model\Auth_User::find_by_username(Auth::get_screen_name()); 
       } else { 
        $current_user = Model_User::find_by_username(Auth::get_screen_name()); 
       } 
       $group_id = $current_user->group_id; 
       $data['uname'] = Input::post('email'); 
       $data['psword'] = Input::post('password'); 
       $data['gid'] = $group_id; 
       if($current_user->group_id == '8') { 
        $data['msg'] = 'success'; 
        $data['url'] = Input::post('admin_url'); 
       } else { 
        $data['msg'] = 'error'; 
        $data['url'] = Input::post('current_url'); 
       } 
      } else { 
       $data['msg'] = 'error'; 
       $data['url'] = Input::post('current_url'); 
      } 

      echo json_encode($data); 
      die; 
     } 
    } 

に次のように }はまた、私はフロントエンドログインを維持しています。 ヘルプ/提案は大歓迎です。事前に感謝します。

+0

2つの使用differrent認証のインスタンス。 Auth静的メソッドはデフォルトのインスタンス上で便利なショートカットとして動作しますが、それぞれ独自の設定を持つ複数のauthインスタンスを作成できます。この場合、 'Auth :: check();'の代わりに '$ authinstance-> check();'を使います。 – WanWizard

答えて

0

これを行う簡単な方法は、ユーザーの種類ごとにベースコントローラーを作成することです。

たとえば、Controller_UsersとController_Adminを呼び出します。これらは、Contoller_Templateなどの別のコントローラを拡張することができます。

before()メソッドのユーザー用のコントローラーでは、アクセス権があることを確認してください。私はこのためにグループを使用するのが好きです。ユーザーがグループに属している場合は、何もしないでください。そうでない場合は、エラーページに送信してください。

ユーザー用のすべてのコントローラで、Controller_Usersが使用されています。以下のような

何か:

<?php 

/** 
    * The Users Controller. 
    * 
    * A controller to do user authentication for the Users section. 
    * 
    * @version  1.0.0 
    * @package  Controllers\Users 
    * @extends  Controller_Base 
    */ 
class Controller_Users extends Controller_Base 
{ 
    /** 
    * @var string $template The template to use for this controller 
    */ 
    public $template = 'template_users'; 

    public function before() { 

     parent::before(); 

     $auth = \Auth::instance(); 

     if ($auth->check()) { 
      $group_id = $auth->get_groups(); 
      if (!$group_id) { 
       \Session::set('error_msg', 'You do not have access to this application.'); 
       \Response::redirect(\Uri::generate('errors')); 
      } else { 
       if ($this->current_user->status === 'DISABLED') { 
        \Session::set('error_msg', 'Your user account has been disabled.'); 
        \Response::redirect(\Uri::generate('errors')); 
       } 
      } 
     } else { 
      \Session::set('redirect_url', \Uri::current()); 
      \Response::redirect(\Uri::generate('login/')); 
     } 
    } 
} 
関連する問題