Get current user information in the Apigility resource

I just started with Apigility and oAuth2, and I was wondering if it is possible to get the currently authenticated user "loggedin" while retrieving information from the database.

I currently have the following code:

/**
 * Fetch all or a subset of resources
 *
 * @param  array $params
 * @return mixed
 */
public function fetchAll($params = array())
{
    var_dump($params);
    // Using Zend\Db 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);  
}

I’ve searched a lot already, but don’t know how to access user information or access token, do I need to parse the request header for this?

+4
source share
2 answers

I also looked for him. I did not find any documentation about this. But the answer is pretty simple:

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
}

getIdentity RPC.

apigility.

+3

, .
identity, @ViníciusFagundes, $this->getIdentity(), getRoleId(), .

$user_id = $this->getIdentity()->getRoleId();
+3

All Articles