doumao8803 2014-06-16 11:52
浏览 44
已采纳

在过滤器之前挂钩-laravel 4

I have the following table

id  email            password  role
1   someemail        password  admin
2   someemail        password  guest

Guest is the one who can access few privileged section of front end (eg: commenting blog post etc)

Guest is normally registered via facebook using oauth. I have blocked guest from admin login like below

if (Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password'),'role'=>'admin'))) {  

But problem is that , when ever user login from facebook , the session is shared and can access admin as well.

facebook login code is as below

 //$result hold facebook information
    $user->firstname = $result['first_name'];
                $user->lastname = $result['last_name'];
                $user->email = $result['email'];
                $user->password ='sample';
                $user->role='facebook';
                //check user with same email is already there
                $usr = User::where('email', '=', $result['email'])->count();
                if($usr==0)
                    $user->save();
                //automatically login the registered user
                $user = User::where('email', '=', $user->email)->where('role', '=', 'facebook')->first();
                Auth::login($user);

Now in each controller i have called following function on constructor

public function __construct()   {
        $this->beforeFilter('auth');
    }

This is not sufficient to stop the session sharing from guest user.. any help will be appreciated

  • 写回答

1条回答 默认 最新

  • duan0818 2014-06-16 16:35
    关注

    Create a new filter in app/filters.php that checks if the user is an admin.

    Route::filter('auth.admin', function()
    {
        if (Auth::guest())
            return Redirect::guest('/');
    
        if (Auth::user()->role != 'admin')
            return Redirect::to('/');
    });
    

    Now in your admin controllers call the new filter

    public function __construct()
    {
        $this->beforeFilter('auth.admin');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类