[英]Get current user information in Apigility Resource
我剛開始使用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);
}
我已經做了很多搜索,但我不知道如何訪問用戶信息或訪問令牌,我是否需要為此解析請求標頭?
我也在尋找它。 我沒有找到任何關於這方面的文件。 但答案很簡單:
資源類繼承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
}
您還可以在RPC服務中使用getIdentity
。
我正在使用最新版本的apigility。
我最終找到了獲取用戶標識的簡短方法,只是為了完整性而將其添加為答案。
你可以像@ViníciusFagundes那樣提到identity
對象$this->getIdentity()
,這個身份對象有getRoleId()
函數,它返回用戶的標識符。
$user_id = $this->getIdentity()->getRoleId();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.