Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
David Kudera committed Jan 9, 2015
2 parents 569b933 + 1b3b832 commit f3381a3
Show file tree
Hide file tree
Showing 44 changed files with 172 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea/
vendor
composer.lock
composer.lock
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ before_script:
- composer self-update
- composer install --no-interaction --prefer-source --dev

script: VERBOSE=true sh ./tests/run-tests.sh -s tests/DKTests/
script: VERBOSE=true sh ./tests/run-tests.sh -s tests/DKTests/
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 David Kudera
Copyright (c) 2014 - 2015 David Kudera

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ class BasePresenter extends Nette\Application\UI\Presenter
{control menu}
```

## Render sitemap.xml

Router
```
$router[] = new Route('sitemap.xml', 'Homepage:sitemap');
```

Homepage/sitemap.latte
```
{extends none}
{control menu:sitemapXml}
```

## Authorization

You can hide some links for example for users without specific role, guest users, actions in other module or when
Expand Down Expand Up @@ -122,6 +135,12 @@ menu:
allow:
parameters:
[allowed: maybe]
Authors:
target: Authors:default
allow:
acl:
resource: authors
permission: view #optional - 'view' is default permission
```

or whole menu can be allowed for example just logged users:
Expand Down Expand Up @@ -336,6 +355,10 @@ class BookPresenter extends BasePresenter

## Changelog

* 1.1.0
+ Support for ACL permissions (thanks [whipsterCZ](https://github.com/whipsterCZ))
+ Support for generating sitemap (thanks [whipsterCZ](https://github.com/whipsterCZ))

* 1.0.4
+ Added `hasIcon` and `hasCounter` methods
+ Support for "absolute" targets [#6](https://github.com/sakren/nette-menu/issues/6)
Expand All @@ -351,4 +374,4 @@ class BookPresenter extends BasePresenter
+ Added support for translatable titles [#2](https://github.com/sakren/nette-menu/issues/2)

* 1.0.0
+ First version
+ First version
2 changes: 1 addition & 1 deletion src/DK/Menu/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,4 @@ public function getCurrentItem()
return null;
}

}
}
13 changes: 12 additions & 1 deletion src/DK/Menu/DI/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Extension extends CompilerExtension
'roles' => array(),
'module' => null,
'parameters' => array(),
'acl' => array(),
),
'items' => array(),
);
Expand Down Expand Up @@ -189,6 +190,16 @@ private static function addItemsToParent(Container $parent, array $items)
if (count($data['allow']['parameters']) > 0) {
$item->setAllowedForParameters($data['allow']['parameters']);
}

if (count($data['allow']['acl']) >0) {
if ( isset($data['allow']['acl']['resource']) ) {
$permission = null;
if ( isset($data['allow']['acl']['permission'])) {
$permission = $data['allow']['acl']['permission'];
}
$item->setAllowedForAcl($data['allow']['acl']['resource'], $permission);
}
}

if (count($data['items']) > 0) {
self::addItemsToParent($item, $data['items']);
Expand All @@ -207,4 +218,4 @@ public static function register(Configurator $configurator)
};
}

}
}
2 changes: 1 addition & 1 deletion src/DK/Menu/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
class InvalidArgumentException extends \InvalidArgumentException
{

}
}
65 changes: 59 additions & 6 deletions src/DK/Menu/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Item extends Container

const ALLOWED_FOR_PARAMETERS = 'parameters';

const ALLOWED_FOR_ACL = 'acl';


/** @var \DK\Menu\Menu */
private $menu;
Expand All @@ -45,11 +47,13 @@ class Item extends Container
/** @var array */
private $allowedFor = array(
self::ALLOWED_FOR_LOGGED_IN => null,
self::ALLOWED_FOR_ACL => array(),
self::ALLOWED_FOR_ROLES => array(),
self::ALLOWED_FOR_MODULE => null,
self::ALLOWED_FOR_PARAMETERS => array(),
);


private $defaultAclPermission = 'view';

/** @var bool */
private $allowed;
Expand Down Expand Up @@ -380,7 +384,7 @@ public function getLink()
public function hasAllowedForLoggedIn()
{
return $this->allowedFor[self::ALLOWED_FOR_LOGGED_IN] !== null;
}
}


/**
Expand Down Expand Up @@ -430,7 +434,7 @@ public function setAllowedForRoles(array $roles)
$this->allowedFor[self::ALLOWED_FOR_ROLES] = $roles;
return $this;
}


/**
* @return bool
Expand Down Expand Up @@ -488,6 +492,54 @@ public function setAllowedForParameters(array $parameters)
$this->allowedFor[self::ALLOWED_FOR_PARAMETERS] = $parameters;
return $this;
}

/**
* @return bool
*/
public function hasAllowedForAcl()
{
return count($this->allowedFor[self::ALLOWED_FOR_ACL]) > 0;
}


/**
* @return string
*/
public function getAllowedForAcl()
{
return $this->allowedFor[self::ALLOWED_FOR_ACL];
}


/**
* @param string $resource
* @param string $permission
* @return \DK\Menu\Item
*/
public function setAllowedForAcl($resource, $permission = null)
{
$this->allowedFor[self::ALLOWED_FOR_ACL]['resource'] = $resource;
if ( $permission) {
$this->allowedFor[self::ALLOWED_FOR_ACL]['permission'] = $permission;
}

return $this;
}

/**
* @param array $acl
* @return bool
*/
public function isAllowedForAcl($acl)
{
if (count($acl) === 0) {
return true;
}
$resource = $acl['resource'];
$permission = isset($acl['permission']) ? $acl['permission'] : $this->defaultAclPermission;
return $this->getMenu()->getUser()->isAllowed($resource, $permission);

}


/**
Expand Down Expand Up @@ -541,7 +593,7 @@ public function isAllowedForModule($module)

return mb_substr($name, 0, $pos) === ucfirst($module);
}


/**
* @param array $parameters
Expand Down Expand Up @@ -570,11 +622,12 @@ public function isAllowedForParameters(array $parameters)
public function isAllowed()
{
if ($this->allowed === null) {
if (!$this->hasAllowedForLoggedIn() && !$this->hasAllowedForRoles() && !$this->hasAllowedForModule() && !$this->hasAllowedForParameters()) {
if (!$this->hasAllowedForAcl() && !$this->hasAllowedForLoggedIn() && !$this->hasAllowedForRoles() && !$this->hasAllowedForModule() && !$this->hasAllowedForParameters()) {
return $this->allowed = true;
}

$this->allowed =
$this->isAllowedForAcl($this->getAllowedForAcl()) &&
$this->isAllowedForLoggedIn($this->getAllowedForLoggedIn()) &&
$this->isAllowedForRoles($this->getAllowedForRoles()) &&
(
Expand Down Expand Up @@ -636,4 +689,4 @@ public function isActive()
return $this->active;
}

}
}
2 changes: 1 addition & 1 deletion src/DK/Menu/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,4 @@ public function getLastCurrentItem()
return $item === false ? null : $item;
}

}
}
18 changes: 12 additions & 6 deletions src/DK/Menu/UI/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class Control extends BaseControl
{


/** @var \DK\Menu\Menu */
/** @var \DK\Menu\Menu */
private $menu;

/** @var string */
/** @var string */
private $menuTemplate;

/** @var string */
/** @var string */
private $breadcrumbTemplate;


Expand All @@ -32,8 +32,8 @@ public function __construct(Menu $menu)

$this->menu = $menu;

$this->menuTemplate = __DIR__. '/menu.latte';
$this->breadcrumbTemplate = __DIR__. '/breadcrumb.latte';
$this->menuTemplate = __DIR__ . '/menu.latte';
$this->breadcrumbTemplate = __DIR__ . '/breadcrumb.latte';
}


Expand Down Expand Up @@ -101,4 +101,10 @@ public function renderBreadcrumb()
$this->template->render();
}

}
public function renderSitemapXml()
{
$this->template->setFile(__DIR__. '/sitemapXml.latte');
$this->template->menu = $this->menu;
$this->template->render();
}
}
2 changes: 1 addition & 1 deletion src/DK/Menu/UI/IControlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ interface IControlFactory
*/
public function create();

}
}
2 changes: 1 addition & 1 deletion src/DK/Menu/UI/breadcrumb.latte
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{foreach $menu->getPath() as $item}
<a href="{$item->getLink()}">{$item->getTranslatedTitle()}</a>{sep} / {/sep}
{/foreach}
{/foreach}
2 changes: 1 addition & 1 deletion src/DK/Menu/UI/menu.latte
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
<a href="{$item->getLink()}" n:class="$item->isActive() ? active">{$item->getTranslatedTitle()}</a>
{include branch, menuParent => $item}
</li>
</ul>
</ul>
25 changes: 25 additions & 0 deletions src/DK/Menu/UI/sitemapXml.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{contentType text/xml}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">


{if !isset($menuParent)}
{var $menuParent = $menu}
{/if}
{var $priority = 1}

{block branch}

{if $menuParent->hasVisualItems()}
{foreach $menuParent->getItems() as $item}
<url n:if="$item->isVisual() && $item->isAllowed()">
<loc>{$baseUri}{$item->getLink()}</loc>
<priority>{$priority}</priority>
</url>
{include branch, menuParent => $item, priority => $priority - 0.1}
{/foreach}
{/if}

{/block}

</urlset>
2 changes: 1 addition & 1 deletion tests/DKTests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
coverage.dat
*.actual
*.expected
*.expected
2 changes: 1 addition & 1 deletion tests/DKTests/Menu/Extension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,4 @@ interface ICustomControlFactory
}


\run(new ExtensionTest);
\run(new ExtensionTest);
2 changes: 1 addition & 1 deletion tests/DKTests/Menu/Menu.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,4 @@ class Translator implements ITranslator
}


\run(new MenuTest);
\run(new MenuTest);
2 changes: 1 addition & 1 deletion tests/DKTests/Menu/MockUserStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ function getLogoutReason()

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
class HomepagePresenter extends BasePresenter
{

}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{layout ../../../templates/@layout.latte}
{layout ../../../templates/@layout.latte}
2 changes: 1 addition & 1 deletion tests/DKTests/app/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ services:
router:
class: Nette\Application\Routers\RouteList
setup:
- '$service[] = ?'(Nette\Application\Routers\Route('<presenter>/<action>[/<id>]', 'Homepage:default'))
- '$service[] = ?'(Nette\Application\Routers\Route('<presenter>/<action>[/<id>]', 'Homepage:default'))
2 changes: 1 addition & 1 deletion tests/DKTests/app/config/customControl.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ menu:

default:
controlClass: DKTests\Menu\CustomControl
controlInterface: DKTests\Menu\ICustomControlFactory
controlInterface: DKTests\Menu\ICustomControlFactory
Loading

0 comments on commit f3381a3

Please sign in to comment.