pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      <bdo id='2cOWi'></bdo><ul id='2cOWi'></ul>
    <tfoot id='2cOWi'></tfoot>
    1. <legend id='2cOWi'><style id='2cOWi'><dir id='2cOWi'><q id='2cOWi'></q></dir></style></legend>
        <i id='2cOWi'><tr id='2cOWi'><dt id='2cOWi'><q id='2cOWi'><span id='2cOWi'><b id='2cOWi'><form id='2cOWi'><ins id='2cOWi'></ins><ul id='2cOWi'></ul><sub id='2cOWi'></sub></form><legend id='2cOWi'></legend><bdo id='2cOWi'><pre id='2cOWi'><center id='2cOWi'></center></pre></bdo></b><th id='2cOWi'></th></span></q></dt></tr></i><div class="dlltjx7" id='2cOWi'><tfoot id='2cOWi'></tfoot><dl id='2cOWi'><fieldset id='2cOWi'></fieldset></dl></div>
      1. <small id='2cOWi'></small><noframes id='2cOWi'>

        Laravel 身份驗證與雄辯的角色驅動程序

        laravel authentication with eloquent driver on roles(Laravel 身份驗證與雄辯的角色驅動程序)

          1. <tfoot id='MQV3p'></tfoot>
            <i id='MQV3p'><tr id='MQV3p'><dt id='MQV3p'><q id='MQV3p'><span id='MQV3p'><b id='MQV3p'><form id='MQV3p'><ins id='MQV3p'></ins><ul id='MQV3p'></ul><sub id='MQV3p'></sub></form><legend id='MQV3p'></legend><bdo id='MQV3p'><pre id='MQV3p'><center id='MQV3p'></center></pre></bdo></b><th id='MQV3p'></th></span></q></dt></tr></i><div class="pn77xj7" id='MQV3p'><tfoot id='MQV3p'></tfoot><dl id='MQV3p'><fieldset id='MQV3p'></fieldset></dl></div>

                <tbody id='MQV3p'></tbody>
                <bdo id='MQV3p'></bdo><ul id='MQV3p'></ul>
                • <small id='MQV3p'></small><noframes id='MQV3p'>

                  <legend id='MQV3p'><style id='MQV3p'><dir id='MQV3p'><q id='MQV3p'></q></dir></style></legend>
                • 本文介紹了Laravel 身份驗證與雄辯的角色驅動程序的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試對我的 Laravel 應用程序中的用戶進行身份驗證.

                  I am trying to authenticate users in my Laravel application.

                  我遇到了以下問題:

                  • 在 auth.php 中使用驅動程序數據庫:我可以使用 auth::attempt() 登錄,并且 auth::check 正在運行,但我無法驗證登錄用戶是否具有特定角色.
                  • 在 auth.php 中使用驅動程序 eloquent:我可以使用 auth::attempt() 登錄,但 auth::check 不起作用.但是,我可以檢查登錄用戶的角色.
                  • using driver database in auth.php: I can login using auth::attempt(), and auth::check is working, but I can't validate if the logged in user has a certain role.
                  • using driver eloquent in auth.php: I can login using auth::attempt(), but auth::check is not working. I can however check the role of the logged in user.

                  編輯(問題):我該如何解決這個問題,以便僅使用一個驅動程序,我就可以進行完整的身份驗證和角色檢查?

                  edit (question): How can I fix this so that with only one of the drivers, i can do a complete authentication and role check?

                  遷移表:

                  Schema::create('users', function ($table) {
                          $table->increments('id');
                          $table->integer('group_id')->unsigned();
                          $table->string('name', 64);
                          $table->string('email', 64)->unique();
                          $table->string('username', 64)->unique();
                          $table->string('phone', 13);
                          $table->string('address', 64);
                          $table->boolean('isresponsible');
                          $table->string('password', 64);
                          $table->rememberToken()->nullable();
                      });
                  Schema::create('roles', function ($table) {
                          $table->increments('id');
                          $table->string('name');
                      });
                  
                  Schema::create('users_roles', function ($table) {
                              $table->integer('user_id')->unsigned();
                              $table->integer('role_id')->unsigned();
                          }
                      );
                  Schema::table('users_roles', function($table){
                          $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
                          $table->foreign('role_id')->references('id')->on('roles');
                      });
                  

                  模型類用戶

                  <?php
                  use IlluminateAuthUserTrait;`
                  use IlluminateAuthUserInterface;`
                  use IlluminateAuthRemindersRemindableTrait;
                  use IlluminateAuthRemindersRemindableInterface;
                  
                  class User extends Eloquent implements UserInterface, RemindableInterface {
                  
                  
                  use UserTrait, RemindableTrait;
                  
                  /**
                   * The database table used by the model.
                   *
                   * @var string
                   */
                  protected $table = 'users';
                  public $timestamps = false;
                  
                  public static $rules = ['name' => 'required', 'group_id' => 'required', 'email' => 'required', 'phone' => 'required'];
                  protected $fillable = ['name', 'group_id', 'email', 'phone', 'address', 'isresponsible', 'password'];
                  
                  /**
                   * The attributes excluded from the model's JSON form.
                   *
                   * @var array
                   */
                  protected $hidden = array('password', 'remember_token');
                  
                  public function group()
                  {
                      return $this->belongsTo('Group');
                  }
                  
                  public function userroles(){
                      return $this->hasMany('Userrole');
                  }
                  
                  public function roles()
                  {
                      return $this->belongsToMany('Role', 'users_roles');
                  }
                  
                  public function hasRole($check)
                  {
                      dd($this->roles->toArray());
                      return in_array($check, array_fetch($this->roles->toArray(), 'name'));
                  }
                  
                  public function setBasicPassword($id){
                      $user = User::find($id);
                      $user->password = Hash::make('changeme');
                      $user->save();
                  }
                  
                  public function isValid()
                  {
                      $validation = Validator::make($this->attributes, static::$rules);
                      if ($validation->passes()) return true;
                      $this->messages = $validation->messages();
                      return false;
                  }
                  
                  
                  /**
                   * Get the e-mail address where password reminders are sent.
                   *
                   * @return string
                   */
                  public function getReminderEmail()
                  {
                      // TODO: Implement getReminderEmail() method.
                  }
                  
                  /**
                   * Get the unique identifier for the user.
                   *
                   * @return mixed
                   */
                  public function getAuthIdentifier()
                  {
                      return $this->email;
                  }
                  
                  /**
                   * Get the password for the user.
                   *
                   * @return string
                   */
                  public function getAuthPassword()
                  {
                      return $this->password;
                  }
                  
                  /**
                   * Get the token value for the "remember me" session.
                   *
                   * @return string
                   */
                  public function getRememberToken()
                  {
                      return $this->remember_token;
                  }
                  
                  public function setRememberToken($value)
                  {
                      $this->remember_token = $value;
                  }
                  
                  public function getRememberTokenName()
                  {
                      return 'remember_token';
                  }
                  }
                  

                  模型類角色

                  class Role extends Eloquent
                  {
                  
                  protected $table = 'roles';
                  public $timestamps = false;
                  
                  public static $rules = ['role_id' => 'required', 'name' => 'required'];
                  protected $fillable = ['name'];
                  
                  /**
                   * Get users with a certain role
                   */
                  public function userroles()
                  {
                      return $this->belongsToMany('User', 'users_roles');
                  }
                  }
                  

                  HomeController 認證功能

                  HomeController authentication function

                   public function authenticate(){
                      $rules = array(
                          'email'    => 'required|email',
                          'password' => 'required|alphaNum|min:3'
                      );
                      $validator = Validator::make(Input::all(), $rules);
                      if ($validator->fails()) {
                          return Redirect::to('login')
                              ->withErrors($validator)
                              ->withInput(Input::except('password'));
                      } else {
                          $userdata = array(
                              'email' => Input::get('email'),
                              'password' => Input::get('password')
                          );
                          if (Auth::attempt($userdata, true)) {
                              return Redirect::action('HomeController@index');
                  
                          } else {
                              return Redirect::action('HomeController@login')->withInput();
                          }
                      }
                  }
                  

                  使用數據庫驅動程序
                  - auth:attempt() 和 auth::check 正在工作

                  USING THE DATABASE DRIVER
                  - auth:attempt() and auth::check are working

                  $this->beforeFilter('admin', ['only' => ['index']]); //filter in controller
                  //filter in filters;php
                  Route::filter('admin', function()
                  {
                  if(!Auth::check()) return Redirect::action('HomeController@index');
                  if(!Auth::user()->hasRole('admin')) return View::make('errors.401');
                  });
                  

                  這會因調用未定義的方法 IlluminateAuthGenericUser::hasRole()"而失敗

                  This fails with 'Call to undefined method IlluminateAuthGenericUser::hasRole()'

                  EDIT 數據庫 驅動程序返回一個 GenericUser 對象,我需要自己的 User 對象.不知道哪里可以改.

                  EDIT The database driver return a GenericUser Object, and I need my own User object. Don't know where I can change this.

                  解決方法:我不想使用這個,丑陋的代碼和過濾器(或視圖)不應該需要這樣做

                  Workaround:I'd rather not use this, ugly code and filters (or views) should not need to do this

                  Route::filter('admin', function()
                  {
                      if(!Auth::check()) return Redirect::action('HomeController@index');
                      $user = User::find((Auth::user()->id));
                      if(!$user->hasRole('admin')){ return View::make('errors.401');}
                  });
                  

                  使用 ELOQUENT 驅動程序

                  USING THE ELOQUENT DRIVER

                  • auth::attempt() 成功
                  • auth::check() 失敗
                  • 過濾器沒有錯誤

                  推薦答案

                  問題在于您對 getAuthIdentifier() 的實現.此方法實際上應該返回表的主鍵,而不是用于登錄的用戶名.

                  The problem is your implementation of getAuthIdentifier(). This method should actually return the primary key of your table and not the username that's used for logging in.

                  所以你的應該是這樣的:

                  So yours should look like this:

                  public function getAuthIdentifier(){
                      return $this->id;
                  }
                  

                  或者實際上,我建議您多清理一下模型,因為所有 getSomeAuthStuff 方法都在兩個特征中實現.

                  Or actually, I recommend you clean up your model a bit more since all of the getSomeAuthStuff methods are implemented in the two traits.

                  使用 github 上的默認模型一個基礎并添加所有自定義代碼(角色方法、規則等)

                  Use the default model on github as a base and add all your custom code (roles methods, rules etc)

                  getAuthIdentifier() 返回的值將存儲在會話中.
                  之后使用 check() 時,將在 UserProvider 上調用 retrieveById.而 EloquentUserProvider 就是這樣做的:

                  The value returned from getAuthIdentifier() will be stored in the session.
                  When using check() afterwards, retrieveById will be called on the UserProvider. And the EloquentUserProvider does this:

                  public function retrieveById($identifier)
                  {
                      return $this->createModel()->newQuery()->find($identifier);
                  }
                  

                  它使用 find() 它通過它的主鍵(通常是 id)搜索模型

                  It uses find() which searches for the model by it's primary key (usually id)

                  這篇關于Laravel 身份驗證與雄辯的角色驅動程序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數)
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“localhost的訪問被拒絕)

                  <small id='fKfMy'></small><noframes id='fKfMy'>

                  <tfoot id='fKfMy'></tfoot>
                      <tbody id='fKfMy'></tbody>

                    <legend id='fKfMy'><style id='fKfMy'><dir id='fKfMy'><q id='fKfMy'></q></dir></style></legend>

                          <bdo id='fKfMy'></bdo><ul id='fKfMy'></ul>
                            <i id='fKfMy'><tr id='fKfMy'><dt id='fKfMy'><q id='fKfMy'><span id='fKfMy'><b id='fKfMy'><form id='fKfMy'><ins id='fKfMy'></ins><ul id='fKfMy'></ul><sub id='fKfMy'></sub></form><legend id='fKfMy'></legend><bdo id='fKfMy'><pre id='fKfMy'><center id='fKfMy'></center></pre></bdo></b><th id='fKfMy'></th></span></q></dt></tr></i><div class="x5z5bnd" id='fKfMy'><tfoot id='fKfMy'></tfoot><dl id='fKfMy'><fieldset id='fKfMy'></fieldset></dl></div>

                          • 主站蜘蛛池模板: 缠绕机|缠绕膜包装机|缠绕包装机-上海晏陵智能设备有限公司 | 餐饮小吃技术培训-火锅串串香培训「何小胖培训」_成都点石成金[官网] | 纸张环压仪-纸张平滑度仪-杭州纸邦自动化技术有限公司 | 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 安徽控制器-合肥船用空调控制器-合肥家电控制器-合肥迅驰电子厂 安徽净化板_合肥岩棉板厂家_玻镁板厂家_安徽科艺美洁净科技有限公司 | 垃圾处理设备_餐厨垃圾处理设备_厨余垃圾处理设备_果蔬垃圾处理设备-深圳市三盛环保科技有限公司 | 电解抛光加工_不锈钢电解抛光_常州安谱金属制品有限公司 | 卫生纸复卷机|抽纸机|卫生纸加工设备|做卫生纸机器|小型卫生纸加工需要什么设备|卫生纸机器设备多少钱一台|许昌恒源纸品机械有限公司 | 防水套管厂家-柔性防水套管-不锈钢|刚性防水套管-天翔管道 | 金环宇|金环宇电线|金环宇电缆|金环宇电线电缆|深圳市金环宇电线电缆有限公司|金环宇电缆集团 | 合肥风管加工厂-安徽螺旋/不锈钢风管-通风管道加工厂家-安徽风之范 | 防水接头-电缆防水接头-金属-电缆密封接头-不锈钢电缆接头 | 颗粒机,颗粒机组,木屑颗粒机-济南劲能机械有限公司 | 转向助力泵/水泵/发电机皮带轮生产厂家-锦州华一精工有限公司 | 集菌仪厂家_全封闭_封闭式_智能智能集菌仪厂家-上海郓曹 | 成都网站建设制作_高端网站设计公司「做网站送优化推广」 | 贴片电容-贴片电阻-二三极管-国巨|三星|风华贴片电容代理商-深圳伟哲电子 | 西门子气候补偿器,锅炉气候补偿器-陕西沃信机电工程有限公司 | 铝箔袋,铝箔袋厂家,东莞铝箔袋,防静电铝箔袋,防静电屏蔽袋,防静电真空袋,真空袋-东莞铭晋让您的产品与众不同 | 不锈钢水管-不锈钢燃气管-卫生级不锈钢管件-不锈钢食品级水管-广东双兴新材料集团有限公司 | 包头市鑫枫装饰有限公司 | 济南ISO9000认证咨询代理公司,ISO9001认证,CMA实验室认证,ISO/TS16949认证,服务体系认证,资产管理体系认证,SC食品生产许可证- 济南创远企业管理咨询有限公司 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 | 手板_手板模型制作_cnc手板加工厂-东莞天泓| 大流量卧式砂磨机_强力分散机_双行星双动力混合机_同心双轴搅拌机-莱州市龙跃化工机械有限公司 | 四探针电阻率测试仪-振实密度仪-粉末流动性测定仪-宁波瑞柯微智能 | 巨野电机维修-水泵维修-巨野县飞宇机电维修有限公司 | 山东限矩型液力偶合器_液力耦合器易熔塞厂家-淄博市汇川源机械厂 | 发电机价格|发电机组价格|柴油发电机价格|柴油发电机组价格网 | 酒精检测棒,数显温湿度计,酒安酒精测试仪,酒精检测仪,呼气式酒精检测仪-郑州欧诺仪器有限公司 | 环氧乙烷灭菌器_压力蒸汽灭菌器_低温等离子过氧化氢灭菌器 _低温蒸汽甲醛灭菌器_清洗工作站_医用干燥柜_灭菌耗材-环氧乙烷灭菌器_脉动真空压力蒸汽灭菌器_低温等离子灭菌设备_河南省三强医疗器械有限责任公司 | 螺杆式冷水机-低温冷水机厂家-冷冻机-风冷式-水冷式冷水机-上海祝松机械有限公司 | 锌合金压铸-铝合金压铸厂-压铸模具-冷挤压-誉格精密压铸 | 小型铜米机-干式铜米机-杂线全自动铜米机-河南鑫世昌机械制造有限公司 | 河北码上网络科技|邯郸小程序开发|邯郸微信开发|邯郸网站建设 | ZHZ8耐压测试仪-上海胜绪电气有限公司| 一航网络-软件测评官网 | 宿舍管理系统_智慧园区系统_房屋/房产管理系统_公寓管理系统 | 聚氨酯保温钢管_聚氨酯直埋保温管道_聚氨酯发泡保温管厂家-沧州万荣防腐保温管道有限公司 | 济南货架定做_仓储货架生产厂_重型货架厂_仓库货架批发_济南启力仓储设备有限公司 | 混合气体腐蚀试验箱_盐雾/硫化氢/气体腐蚀试验箱厂家-北京中科博达 | ◆大型吹塑加工|吹塑加工|吹塑代加工|吹塑加工厂|吹塑设备|滚塑加工|滚塑代加工-莱力奇塑业有限公司 |