Bas*_*ein 5 php zend-framework oauth-2.0 zend-framework2 apigility
我刚开始使用Apigility和oAuth2,我想知道从数据库中获取信息时是否有可能获得当前经过身份验证的"登录"用户.
我目前有以下代码:
/**
* Fetch all or a subset of resources
*
* @param array $params
* @return mixed
*/
public function fetchAll($params = array())
{
var_dump($params);
// Using Zend\Db's SQL abstraction
$sql = new \Zend\Db\Sql\Sql($this->db);
//I would like to get the currently logged in user here... but how?
$select = $sql->select('projects')->where(array('userid' => 1));;
// This provides paginated results for the given Select instance
$paged = new \Zend\Paginator\Adapter\DbSelect($select, $this->db);
// which we then pass to our collection
return new ProjectsCollection($paged);
}
Run Code Online (Sandbox Code Playgroud)
我已经做了很多搜索,但我不知道如何访问用户信息或访问令牌,我是否需要为此解析请求标头?
我也在寻找它.我没有找到任何关于这方面的文件.但答案很简单:
资源类继承ZF\Rest\AbstractResourceListener
已有方法的资源类getIdentity
.
/**
* Fetch all or a subset of resources
*
* @param array $params
* @return mixed
*/
public function fetchAll($params = array())
{
// if user isn't authenticated return nothing
if(!$this->getIdentity() instanceof ZF\MvcAuth\Identity\AuthenticatedIdentity) {
return [];
}
// this array returyour query here using $userIdns the authentication info
// in this case we need the 'user_id'
$identityArray= $this->getIdentity()->getAuthenticationIdentity();
// note, by default user_id is the email (username column in oauth_users table)
$userId = $identityArray['user_id'];
// fetch all using $userId
}
Run Code Online (Sandbox Code Playgroud)
您还可以getIdentity
在RPC服务中使用.
我正在使用最新版本的apigility.
归档时间: |
|
查看次数: |
924 次 |
最近记录: |