Skip to content

Commit

Permalink
Tried and failed with PlantUML
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Nov 29, 2019
1 parent 3e5b550 commit ab25890
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
33 changes: 33 additions & 0 deletions docs/items.md
@@ -0,0 +1,33 @@
@startuml

legend right
|<#FFDDDD> <b>E</b> | <b>Role</b> |
|<#DDFFDD> <b>P</b> | <b>Permission</b> |
endlegend

!define Role(name,desc) component "desc" as name << (R,#EEEEEE) >> #FFDDDD
!define Permission(name,desc) () name as "desc" #DDFFDD
hide methods
hide stereotypes

package "Client" {

Role(client_manager, "client.manager")

Permission(client_create, "client.create")
Permission(client_update, "client.update")
Permission(client_get_note, "client.get-note")
Permission(client_set_note, "client.set-note")
}


client_create -> client_manager
client_update -> client_manager
client_get_note -> client_manager
client_set_note -> client_manager

client_create -[hidden]-> client_update
client_update -[hidden]-> client_get_note
client_get_note -[hidden]-> client_set_note

@enduml
108 changes: 108 additions & 0 deletions src/console/PlantUML.php
@@ -0,0 +1,108 @@
<?php
/**
* RBAC implementation for HiPanel
*
* @link https://github.com/hiqdev/hipanel-rbac
* @package hipanel-rbac
* @license BSD-3-Clause
* @copyright Copyright (c) 2016-2019, HiQDev (http://hiqdev.com/)
*/

namespace hipanel\rbac\console;

use yii\rbac\PhpManager;
use yii\rbac\Item;

class PlantUML
{
/**
* @var PhpManager
*/
protected $auth;

public function __construct($auth)
{
$this->auth = $auth;
}

public function build()
{
$path = dirname(__DIR__, 2) . '/docs/test.md';

$header = "@startuml";
$footer = "@enduml";
$legend = '
legend right
|<#FFDDDD> <b>E</b> | <b>Role</b> |
|<#DDFFDD> <b>P</b> | <b>Permission</b> |
endlegend
';

$defs = '
!define Role(name,desc) component "desc" as name << (R,#EEEEEE) >> #FFDDDD
!define Permission(name,desc) () name as "desc" #DDFFDD
hide methods
hide stereotypes
';

$items = [];
$links = [];
$hidds = [];

$permissions = $this->auth->getPermissions();
foreach ($permissions as $name => $perm) {
if ($this->isDeny($name)) {
continue;
}

$id = $this->toId($name);
$items[] = "Permission($id, \"$name\")";

if (!empty($old_id)) {
$hidds[] = "$old_id -[hidden]-> $id";
}
$old_id = $id;
}

$items[] = '';

foreach ($this->auth->getRoles() as $parent => $role) {
$parent_id = $this->toId($parent);

$items[] = "Role($parent_id, \"$parent\")";
foreach ($this->auth->getChildren($parent) as $child => $item) {
$child_id = $this->toId($child);
$links[] = "$child_id -> $parent_id";
}
}

$items = $this->joinClientOnly($items);
$links = $this->joinClientOnly($links);
$hidds = $this->joinClientOnly($hidds);
$res = implode("\n", [$header, $legend, $defs, $items, '', $links, '', $hidds, '', $footer, '']);

return $res;
}

private function isDeny(string $name): bool
{
return strncmp($name, 'deny', 4) === 0;
}

private function toId(string $name): string
{
return strtr($name, ':.-', '___');
}

private function joinClientOnly($arr)
{
$res = [];
foreach ($arr as $str) {
if (strpos($str, 'client') !== false) {
$res[] = $str;
}
}

return implode("\n", $res);
}
}
12 changes: 12 additions & 0 deletions src/console/RbacController.php
Expand Up @@ -70,4 +70,16 @@ public function actionShow()
printf(" %-12s %s\n", "$userId:", $roles);
}
}

public function actionPlantuml()
{
$path = dirname(__DIR__, 2) . '/docs/test.txt';

$auth = yii::getApp()->get('authManager');
$plant = new PlantUML($auth);

$uml = $plant->build();

file_put_contents($path, $uml);
}
}

0 comments on commit ab25890

Please sign in to comment.