获取Apigility Resource中的当前用户信息

时间:2016-04-19 15:04:27

标签: 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);  
}

我已经做了很多搜索,但我不知道如何访问用户信息或访问令牌,我是否需要为此解析请求标头?

2 个答案:

答案 0 :(得分:5)

我也在寻找它。我没有找到任何相关文档。但答案很简单:

资源类继承已有方法import time import random hp=80 atk1=5 atk2=7 mp=40 smite1=12 smite2=15 def fmode(atkf1,atkf2,enemyatk1,enemyatk2,enemy,enemyhp,xp): hpf=hp mpf=mp while True: if enemyhp>0: yt=0 print("The "+str(enemy)+" attacks!") time.sleep(1) dmg=random.randint(enemyatk1,enemyatk2) hpf=hpf-dmg print("The "+str(enemy)+" hit you for "+str(dmg)) time.sleep(1) print("You are at "+str(hpf)+"hp") time.sleep(1) print("Your turn") time.sleep(1) yt=1 print("1)Attack "+" 2)Ability") while True: if yt==1: inpy=input("Select:") if inpy=="1": dmg=random.randint(atkf1,atkf2) print("You hit the "+str(enemy)+" for "+str(dmg)+"hp") enemyhp=enemyhp-dmg time.sleep(1) print("The "+str(enemy)+" is at "+str(enemyhp)+"hp") time.sleep(1) yt=0 break if inpy=="2": print("1)Backstab "+" 2)Heal "+str(mpf)+" mana left") while True: inpy1=input("Select:") if inpy1=="1": dmg=random.randint(smite1,smite2) enemyhp=enemyhp-dmg mpf=mpf-15 print("You stab the "+str(enemy)+" for "+str(dmg)) time.sleep(1) print("The "+str(enemy)+" is at "+str(enemyhp)+"hp") time.sleep(1) yt=0 break if yt==0: break if enemyhp<=0: print("The "+str(enemy)+" has been defeated.") time.sleep(1) print(str(xp)+"XP gained") time.sleep(1) return hpf,mpf fmode(5,7,1,3,"rat",20,15) print(hpf) print(mpf) 的{​​{1}}。

ZF\Rest\AbstractResourceListener

您还可以在RPC服务中使用getIdentity

我使用的是最新版本的apigility。

答案 1 :(得分:3)

我最终找到了获取用户ID的简短方法,只是为了完整性而将其添加为答案。
您可以获取@ {VininciusFagundes提到的identity对象$this->getIdentity(),此身份对象具有返回用户标识符的函数getRoleId()

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