簡體   English   中英

如何在fuelphp中維護前端登錄和后端登錄

[英]how to maintain frontend login and backend login in fuelphp

在fuelphp項目中,我為注冊用戶提供普通后端。 在前端我有一個登錄名,只有大學用戶可以從該名登錄后端(超級管理員也不能從前端登錄)。 我的問題是如果同時打開后端和前端,然后刷新后端,或者前端用戶從后端注銷。 我將后端登錄保持為默認的fuelphp登錄(admin.php)。 代碼:

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

其他); }另外,我還在不同的控制器(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;
        }
    }

我已經通過前端的ajax登錄了。 歡迎任何幫助/建議。

執行此操作的更簡單方法是為每種類型的用戶創建基本控制器。

例如,將其稱為Controller_Users和Controller_Admin。 這些可以擴展另一個控制器,例如Contoller_Template等。

在您的Controllers for Users中的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/'));
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM