Created
July 23, 2013 15:07
-
-
Save cyrilf/6063097 to your computer and use it in GitHub Desktop.
BjyAuthorize issue #170
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* BjyAuthorize Module (https://github.com/bjyoungblood/BjyAuthorize) | |
* | |
* @link https://github.com/bjyoungblood/BjyAuthorize for the canonical source repository | |
* @license http://framework.zend.com/license/new-bsd New BSD License | |
*/ | |
namespace BjyAuthorize\Provider\Identity; | |
use BjyAuthorize\Exception\InvalidRoleException; | |
use Zend\Db\Adapter\Adapter; | |
use Zend\Db\Sql\Where; | |
use Zend\Db\Sql\Sql; | |
use Zend\Permissions\Acl\Role\RoleInterface; | |
use ZfcUser\Service\User; | |
/** | |
* Identity provider based on {@see \Zend\Db\Adapter\Adapter} | |
* | |
* @author Ben Youngblood <[email protected]> | |
*/ | |
class ZfcUserZendDb implements ProviderInterface | |
{ | |
/** | |
* @var User | |
*/ | |
protected $userService; | |
/** | |
* @var string|\Zend\Permissions\Acl\Role\RoleInterface | |
*/ | |
protected $defaultRole; | |
/** | |
* @var string | |
*/ | |
protected $tableName = 'user_role_linker'; | |
/** | |
* @param \Zend\Db\Adapter\Adapter $adapter | |
* @param \ZfcUser\Service\User $userService | |
*/ | |
public function __construct(Adapter $adapter, User $userService) | |
{ | |
$this->adapter = $adapter; | |
$this->userService = $userService; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getIdentityRoles() | |
{ | |
$authService = $this->userService->getAuthService(); | |
if (! $authService->hasIdentity()) { | |
return array($this->getDefaultRole()); | |
} | |
// get roles associated with the logged in user | |
$sql = new Sql($this->adapter); | |
/* Now we join the user role table (I don't know how to not hard code the 'user_role' string | |
and get it from config?) */ | |
$select = $sql->select()->from(array('role_linker' => $this->tableName)) | |
->join(array('user_role' => 'user_role'), | |
'role_linker.role_id = user_role.id'); | |
$where = new Where(); | |
$where->equalTo('user_id', $authService->getIdentity()->getId()); | |
$results = $sql->prepareStatementForSqlObject($select->where($where))->execute(); | |
$roles = array(); | |
foreach ($results as $i) { | |
// here we retrieve the roleId (i.e admin) | |
$roles[] = $i['roleId']; | |
//$roles[] = $i['role_id']; | |
} | |
return $roles; | |
} | |
/** | |
* @return string|\Zend\Permissions\Acl\Role\RoleInterface | |
*/ | |
public function getDefaultRole() | |
{ | |
return $this->defaultRole; | |
} | |
/** | |
* @param string|\Zend\Permissions\Acl\Role\RoleInterface $defaultRole | |
* | |
* @throws \BjyAuthorize\Exception\InvalidRoleException | |
*/ | |
public function setDefaultRole($defaultRole) | |
{ | |
if (! ($defaultRole instanceof RoleInterface || is_string($defaultRole))) { | |
throw InvalidRoleException::invalidRoleInstance($defaultRole); | |
} | |
$this->defaultRole = $defaultRole; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment