简体   繁体   English

Lithium Auth ::通过关系检查用户,关系数据库

[英]Lithium Auth::check user with relationships, relational database

I am using Auth::check('user', $this->request), where 'user' is the name of my Auth::config configuration, to successfully authenticate and login users against a Users table. 我正在使用Auth :: check('user',$ this-> request)(其中'user'是我的Auth :: config配置的名称)来针对Users表成功验证和登录用户。 Upon successful user login, the user information is stored in the session and can be accessed via Auth::check('user'). 成功登录用户后,用户信息将存储在会话中,并可通过Auth :: check('user')访问。

I have a relational design in place where the Users table has a many-to-one relationship with a table named Sites. 我有一个关系设计,其中Users表与名为Sites的表具有多对一关系。 When Auth::check queries the Users table to authenticate a user logging in, it only returns information from the Users table. 当Auth :: check查询Users表以验证登录的用户时,它仅返回Users表中的信息。 I need Auth:check to return the User with its related data from the Sites table. 我需要Auth:check才能从Sites表中返回用户及其相关数据。 Both the User and Site models have the appropriate hasOne and hasMany relationships defined. User和Site模型都定义了适当的hasOne和hasMany关系。 How can I ensure that the user is queried and returned with its corresponding related data via Auth::check()? 如何确保通过Auth :: check()查询用户并返回相应的相关数据?

While it's generally inadvisable to store more session information than is absolutely necessary, it's possible to do this through the 'query' configuration key of the Form auth adapter. 虽然通常不建议存储比绝对必要更多的会话信息,但是可以通过Form auth适配器的'query'配置键来实现。 Just create a wrapper method in your Users model that can take a query configuration array, and return a user object with associated relationships. 只需在Users模型中创建一个包装方法即可采用查询配置数组,然后返回具有关联关系的用户对象。 Probably something like this: 大概是这样的:

public static function authenticate (array $query) {
    return static::all($query + array('with' => 'Sites'));
}

Then, add 'query' => 'authenticate' to your 'user' auth configuration. 然后,将'query' => 'authenticate''user' user'auth配置中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM