douyi7283 2015-12-12 23:19
浏览 159
已采纳

使用laravel为admin用户创建中间件

I build an simple app and I have Users, but some users need to have admin privilegies, so I try to create this:

  1. First at migration file I add boolean type - admin:

    public function up()

     {
            Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table->boolean('admin');
                $table->string('username');
                $table->string('email')->unique();
                $table->string('password', 60);
                $table->rememberToken();
                $table->timestamps();
            });
        }
    

after that I create new middleware:

class RedirectIfNotAManager
{

    public function handle($request, Closure $next)
    {
        if (! $request->user()->IsATeamManager()){

            return redirect('articles');

        }
        return $next($request);
    }
}

offcource Now I need to create function IsATeamManager() at User model file:

public function IsATeamManager(){
        if ($this->is('admin') {
            return true;
        }
        return false;

    }

at Kernel.php I add manager:

 protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'manager' => \App\Http\Middleware\RedirectIfNotAManager::class,

    ];

At route I add:

Route::get('foo', ['middleware'=>'manager', function(){

    return 'This Page is Only for Managers';

}]);

Manually at phpMyAdmin I change the user admin column for some users at '1'... I log as that user,

But when I try to go at: localhost:8888/foo I get this error:

BadMethodCallException in Builder.php line 2071: Call to undefined method Illuminate\Database\Query\Builder::is()

What I also try is to chech is authenticated user admin when i make request so I write:

public function store(Requests\ArticleRequest $request)
    {
        $article = new Article($request->all());

        Auth::user()->is('admin')->articles()->save($article);

        return redirect('articles');
    }

but that doesn work and I dont know why... so my request is if authenticated users have admin column true then save article...

What you suggest? How to implement user admin at my app?

  • 写回答

1条回答 默认 最新

  • duanhuang4841 2015-12-12 23:34
    关注

    I think you should replace this:

    Auth::user()->is('admin')->articles()->save($article);
    

    with this:

    Auth::user()->where('admin', 1)->articles()->save($article);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名