/** * Returns ancestor of item * * @param Permission $item permission instance * @return array */ public function fetchAncestor(Permission $item) { $key = $this->getCacheKey($item->siteKey, $item->name, 'ancestor'); if (!($ancestors = $this->cache->get($key))) { $ancestors = $this->repo->fetchAncestor($item); if (!empty($ancestors)) { $this->cache->put($key, $ancestors); } } return $ancestors; }
/** * Set a parent Registered to given Registered * * @param Registered $registered registered instance * * @return void */ protected function setAncestor(Registered &$registered) { $ancestors = $this->repo->fetchAncestor($registered); uasort($ancestors, [$this, 'cmp']); foreach ($ancestors as $ancestor) { $registered->addParent($ancestor); } }
/** * Returns ancestor of item * * @param Permission $item permission instance * @return array */ public function fetchAncestor(Permission $item) { return $this->repo->fetchAncestor($item); }
/** * Set permission's ancestor to permission * * @param Permission $permission permission instance * @return void */ protected function setAncestor(Permission $permission) { $ancestors = $this->repo->fetchAncestor($permission); usort($ancestors, function (Permission $a, Permission $b) { if ($a->getDepth() == $b->getDepth()) { return 0; } return $a->getDepth() > $b->getDepth() ? -1 : 1; }); foreach ($ancestors as $ancestor) { $permission->addParent($ancestor); } }
public function testFetchAncestor() { list($conn, $query) = $this->getMocks(); $mockRegistered = m::mock('Xpressengine\\Permission\\Registered'); $mockRegistered->siteKey = 'default'; $mockRegistered->type = 'instance'; $mockRegistered->name = 'board.notice.b1'; $conn->shouldReceive('table')->andReturn($query); $query->shouldReceive('where')->once()->with('siteKey', 'default')->andReturn($query); $query->shouldReceive('where')->once()->with('type', 'instance')->andReturn($query); $query->shouldReceive('whereRaw')->once()->with("'board.notice.b1' like concat(`name`, '.', '%')")->andReturn($query); $query->shouldReceive('where')->once()->with('name', '<>', 'board.notice.b1')->andReturn($query); $query->shouldReceive('get')->once()->withNoArgs()->andReturn((object) [['id' => 1, 'type' => 'instance', 'name' => 'board', 'grants' => '{"access":{"type":"power","value":"guest"},"create":{"type":"power","value":"member"}}'], ['id' => 2, 'type' => 'instance', 'name' => 'board.notice', 'grants' => '{"access":{"type":"power","value":"guest"},"create":{"type":"power","value":"super"}}']]); $instance = new PermissionRepository($conn); $registereds = $instance->fetchAncestor($mockRegistered); $this->assertEquals(2, count($registereds)); $this->assertEquals(['type' => 'power', 'value' => 'member'], $registereds[0]['create']); $this->assertEquals('board', $registereds[0]->name); $this->assertEquals(['type' => 'power', 'value' => 'super'], $registereds[1]['create']); $this->assertEquals('board.notice', $registereds[1]->name); }