首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在fuelphp中维护前台登录和后端登录

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

Stack Overflow用户
提问于 2016-03-30 20:20:34
回答 1查看 486关注 0票数 0

在fuelphp项目中,我为注册的用户提供了一个普通的后端。在前台我有一个登录,只有大学用户可以登录到后端(超级管理员也不能从前台登录)。我这里的问题是,如果我同时打开后端和前端,然后刷新后端或前端用户从后端注销。我维护了后端登录作为默认的fuelphp登录(admin.php)。代码:

代码语言:javascript
复制
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)中维护了如下的前端登录

代码语言:javascript
复制
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登录了。任何帮助/建议都是提前welcome.Thanks的。

EN

回答 1

Stack Overflow用户

发布于 2016-04-09 01:00:12

更简单的方法是为每种类型的用户创建基本控制器。

例如,称其为Controller_Users和Controller_Admin。这些可以扩展另一个控制器,比如Contoller_Template等等。

在您的控制器中为用户在您的for ()方法中。确保他们有访问权限。我喜欢使用群组来解决这个问题。如果它们属于组用户,则不执行任何操作。如果没有,请将它们发送到错误页面。

现在,在用户的所有控制器中,entend Controller_Users。

类似于:

代码语言:javascript
复制
<?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/'));
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36308712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档