public function testHasRole() { $foo = new Rbac\Role('foo'); $this->rbac->addRole('bar'); $this->rbac->addRole($foo); $this->assertEquals(true, $this->rbac->hasRole($foo)); $this->assertEquals(true, $this->rbac->hasRole('bar')); $this->assertEquals(false, $this->rbac->hasRole('baz')); }
/** * @covers Zend\Permissions\Rbac\Rbac::hasRole() */ public function testHasRole() { $foo = new Rbac\Role('foo'); $snafu = new TestAsset\RoleTest('snafu'); $this->rbac->addRole('bar'); $this->rbac->addRole($foo); $this->rbac->addRole('snafu'); // check that the container has the same object $foo $this->assertTrue($this->rbac->hasRole($foo)); // check that the container has the same string "bar" $this->assertTrue($this->rbac->hasRole('bar')); // check that the container do not have the string "baz" $this->assertFalse($this->rbac->hasRole('baz')); // check that we can compare two different objects with same name $this->assertNotEquals($this->rbac->getRole('snafu'), $snafu); $this->assertTrue($this->rbac->hasRole($snafu)); }
/** * Load the requested resources into RBAC. * * @param Rbac $rbac * @param string $role * @param string|null $permission * @return \Doctrine\DBAL\Query\QueryBuilder */ protected function load($rbac, $role, $permission = null) { $options = $this->options; $builder = new QueryBuilder($this->connection); // Role always present $builder->select('node.name')->from($options->getRoleTable(), 'node')->from($options->getRoleTable(), 'parent')->where('node.lft BETWEEN parent.lft AND parent.rgt')->andWhere('parent.name = :role')->orderBy('node.lft'); $builder->setParameter('role', $role); // Permission optional if ($permission) { $builder->addSelect('permission.name AS permission')->leftJoin('node', 'role_permission', 'rp', 'node.id = rp.role_id')->leftJoin('node', 'permission', 'permission', 'rp.permission_id = permission.id')->andWhere('(permission.name = :permission OR permission.name IS NULL)'); $builder->setParameter('permission', $permission); } $parent = null; foreach ($builder->execute() as $row) { if ($parent) { if (!$rbac->hasRole($row['name'])) { $rbac->getRole($parent)->addChild($row['name']); } } elseif (!$rbac->hasRole($row['name'])) { $rbac->addRole($row['name']); } if ($permission) { if ($row['permission']) { $rbac->getRole($row['name'])->addPermission($row['permission']); } } $parent = $row['name']; } return $builder; }