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 Qt中实现子线程和管理线程类之间实时通信
  • ¥15 cacls 命令如何解除锁定文件夹?
  • ¥50 C++使用TWAIN协议如何实现A3幅面扫描仪扫描A4横向
  • ¥15 如何在sql server里完成筛选
  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?