简体   繁体   English

如何在fuelphp中维护前端登录和后端登录

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

In fuelphp project, I have a normal backend for users who are registered. 在fuelphp项目中,我为注册用户提供普通后端。 In frontend I have a login from where only the university user can login to backend(superadmin also can't login from frontend). 在前端我有一个登录名,只有大学用户可以从该名登录后端(超级管理员也不能从前端登录)。 My problem here is if I open both backend, frontend and then on refresh of backend or frontend user is logged out of backend. 我的问题是如果同时打开后端和前端,然后刷新后端,或者前端用户从后端注销。 I have maintained backend login as default fuelphp login(admin.php). 我将后端登录保持为默认的fuelphp登录(admin.php)。 code : 代码:

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); 其他); } Also I have maintained frontend login as follows in different controller(welcome.php) }另外,我还在不同的控制器(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;
        }
    }

I have done the login via ajax in frontend. 我已经通过前端的ajax登录了。 Any help/suggestions are welcome.Thanks in advance. 欢迎任何帮助/建议。

The easier way to do this is to create base controllers for each type of user. 执行此操作的更简单方法是为每种类型的用户创建基本控制器。

For instance call it Controller_Users and Controller_Admin. 例如,将其称为Controller_Users和Controller_Admin。 These can extend another controller such as Contoller_Template and so on. 这些可以扩展另一个控制器,例如Contoller_Template等。

In your Controllers for Users in your before() method. 在您的Controllers for Users中的before()方法中。 Make sure they have access. 确保他们有权访问。 I like using groups for this. 我喜欢为此使用组。 If they belong to group Users, then do nothing. 如果它们属于用户组,则什么也不做。 If them do not, send them to an error page. 如果没有,请将其发送到错误页面。

Now in all of the controllers for Users entend Controller_Users. 现在,在所有用于用户的控制器中输入Controller_Users。

Something like: 就像是:

<?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