Laravel:手动保持登录(身份验证状态)

时间:2017-11-22 01:32:36

标签: php laravel authentication

我的网站有多种身份验证用于不同目的。这里的授权不是正确的,因为对于每种认证都需要不同的数据,因此对于各种认证,单独的数据库表使用。

电子。 G。所有管理员都需要的是电子邮件和密码:电子邮件用作身份验证的ID。现在,要查看他们当前属于我自己网站的开发中的网站,客户还需要订单ID,客户名称,网站名称和别名数据。例如:

  • 未来的网站域名:stivenson-gym.com
  • 网站名称:史蒂文森的健身房
  • 订单ID:StivensonGym
  • 客户:Michael Stivenson
  • 子网站首页的网址:www.webmaster-example.com/orders/stivenson-gym/index.html
  • 此订单的别名(以上网址的一部分):stivenson-gym

与管理员身份验证不同,此处订单ID (非客户名称)用于登录。

我已经意识到了:

  • 手动订单ID和密码检查
  • 使用每个订单ID的动态别名构建进行路由。

现在我需要在浏览他的网站时让客户登录。

为什么Auth::attempt(['orderID' => $orderID, 'password' => $password])不会使用?因为此方法引用Users表,但我需要Orders表。我无法更改Auth的全局表格,因为管理员需要不同的表进行身份验证。

在理论上,解决方案将是:

  • 在会话中写下某个客户当前正在浏览他的子网站。
  • 当客户更改其子网站的内部页面时,首先需要在会话中检查上述记录。
  • 当客户离开其子网站时 - 清除该部分。

我将在未经授权的情况下添加我当前的代码:

class LoginToSitesInDevelopmentZoneController extends Controller {

  const SITE_IN_DEVELOPMENT_TABLE_NAME = 'site_in_dev';

      const ORDER_ID_FIELD_NAME = 'order_id';
      const PASSWORD_FIELD_NAME = 'password';
      const SITENAME_FIELD_NAME = 'sitename';
      const CUSTOMER_NAME_FIELD_NAME = 'customer_name';
      const ALIAS_FIELD_NAME = 'alias';

  public function login(Request $request) {

    $inputtedOrderId = $request->input('order_id');       // from login form

    // manual check is this order exists
    $orderIdInDB = DB::table(self::SITE_IN_DEVELOPMENT_TABLE_NAME)
            ->where(self::ORDER_ID_FIELD_NAME, $inputtedOrderId)->first();

    if (is_null($orderIdInDB)) {
       return Redirect::route('loginToSitesInDevelopmentZonePage')->withErrors(['wrongOrderId' => true])->withInput();
    }

    // from login form       
    $inputtedPassword = $request->input('password');

    // manual check the order id and password compilance
    $bcryptedPasswordFromDB = DB::table(self::SITE_IN_DEVELOPMENT_TABLE_NAME)
            ->where(self::ORDER_ID_FIELD_NAME, $inputtedOrderId)->value(self::PASSWORD_FIELD_NAME);

    if (!Hash::check($inputtedPassword,$bcryptedPasswordFromDB)) {
        return Redirect::route('loginToSitesInDevelopmentZonePage')->withErrors(['wrongPassword' => true]);
    }


    // get respective alias
    $alias = DB::table(self::SITE_IN_DEVELOPMENT_TABLE_NAME)
            ->where(self::ORDER_ID_FIELD_NAME, $inputtedOrderId)->value(self::ALIAS_FIELD_NAME);

    // here I need to provide acess of the customer to all pages above 'sites_in_development_zone/'.$alias

    return redirect('sites_in_development_zone/'.$alias.'/index.html');

  }
}

提前感谢您的帮助。

0 个答案:

没有答案