Skip to content

Commit

Permalink
rights
Browse files Browse the repository at this point in the history
  • Loading branch information
schpill committed Mar 24, 2017
1 parent 92686dd commit 95cb183
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 44 deletions.
10 changes: 7 additions & 3 deletions lib/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3759,13 +3759,17 @@ function forward($what, $method = 'get', callable $before = null, callable $afte
list($controllerName, $action) = explode('#', $what, 2);
}

if (fnmatch('*:*', $what)) {
list($controllerName, $action) = explode(':', $what, 2);
}

$actualController = Registry::get('app.controller', null);

if (!empty($actualController)) {
if (is_object($actualController)) {
$classC = get_class($actualController);
$tab = explode('\\', $classC);
$namespace = array_shift($tab);
$classC = get_class($actualController);
$tab = explode('\\', $classC);
$namespace = array_shift($tab);

$controllerFile = path('app') . DS . 'controllers' . DS . $controllerName . '.php';

Expand Down
23 changes: 20 additions & 3 deletions lib/octalia.php
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,21 @@ public function __call($m, $a)
return $this->where([$field, $op, $value]);
}

if (fnmatch('getBy*', $m) && strlen($m) > 5) {
$field = callField($m, 'getBy');

$op = '=';

if (count($a) == 2) {
$op = array_shift($a);
$value = array_shift($a);
} else {
$value = array_shift($a);
}

return $this->where([$field, $op, $value]);
}

if (fnmatch('where*', $m) && strlen($m) > 5) {
$field = callField($m, 'where');

Expand Down Expand Up @@ -1212,7 +1227,7 @@ public function __call($m, $a)
}
}

$file = path('models') . '/' . $this->db . '/' . $this->table . '.php';
$file = path('models') . DS . $this->db . DS . $this->table . '.php';

if (file_exists($file)) {
$cbs = require_once $file;
Expand Down Expand Up @@ -1259,8 +1274,8 @@ public function collection()

public function slice($offset, $length = null)
{
$ids = array_values(array_slice((array) $this->iterator(), $offset, $length, true));
$this->ids = SplFixedArray::fromArray($ids);
$ids = array_values(array_slice((array) $this->iterator(), $offset, $length, true));
$this->ids = SplFixedArray::fromArray($ids);

return $this;
}
Expand Down Expand Up @@ -2767,13 +2782,15 @@ private function intersect($tab1, $tab2)

foreach ($tab1 as $row) {
$id = isAke($row, 'id', null);

if (strlen($id)) {
array_push($ids1, $id);
}
}

foreach ($tab2 as $row) {
$id = isAke($row, 'id', null);

if (strlen($id)) {
array_push($ids2, $id);
}
Expand Down
25 changes: 20 additions & 5 deletions lib/rights.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public static function can($resource, $action)
{
$role = role()->getLabel();

$inline = isAke(self::$inline, sha1($role . $resource . $action), false);
$inline = isAke(self::$inline, sha1($role . $resource . $action), 'octodummy');

if (true === $inline) {
return true;
if ('octodummy' != $inline) {
return value($inline);
}

$resources = isAke(self::$rules, $role, []);
Expand All @@ -43,7 +43,13 @@ public static function can($resource, $action)
}

if (is_array($actions)) {
return in_array($action, $action);
foreach ($actions as $ka => $va) {
if ($ka == $action) {
return value($va);
} elseif (is_numeric($ka) && $va == $action) {
return true;
}
}
}
}
}
Expand All @@ -64,6 +70,13 @@ public static function disallow($role, $resource, $action)

self::$inline[$key] = false;
}

public static function add($role, $resource, $action, $status = true)
{
$key = sha1($role . $resource . $action);

self::$inline[$key] = $status;
}
}

/*
Expand All @@ -73,7 +86,9 @@ public static function disallow($role, $resource, $action)
'admin' => '*',
'guest' => null,
'manager' => [
'compta' => ['read', 'delete']
'compta' => ['read' => function () {
return user()->business_unit == 'CTO';
}, 'delete']
],
]);
Expand Down
27 changes: 19 additions & 8 deletions lib/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,12 @@ private static function html($controller)
$layout = cut('<layout>', '</layout>', $content);

if (!empty($layout)) {
$content = Arrays::last(explode('</layout>', $content));
$layout = path('app') . DS . 'views' . DS . 'layouts' . DS . $layout . '.phtml';
$contents = Arrays::last(explode('</layout>', $content));
$layout = path('app') . DS . 'views' . DS . 'layouts' . DS . $layout . '.phtml';

if (File::exists($layout)) {
$layout = File::read($layout);

$content = str_replace('<content></content>', $content, $layout);
$layout = File::read($layout);
$content = str_replace('<content></content>', $content, $layout);
}
}

Expand Down Expand Up @@ -279,8 +278,12 @@ public static function compile($content)
array_shift($rows);

foreach ($rows as $row) {
$file = cut('"', '"', $row);
$content = str_replace('<partial file="' . $file . '">', '<?php $this->partial(\'' . str_replace('.', DS, $file) . '.phtml\'); ?>', $content);
$file = cut('"', '"', $row);
$content = str_replace(
'<partial file="' . $file . '">',
'<?php $this->partial(\'' . str_replace('.', DS, $file) . '.phtml\'); ?>',
$content
);
}

$content = str_replace(['{{', '}}'], ['<?php $controller->e("', '");?>'], $content);
Expand Down Expand Up @@ -384,7 +387,15 @@ public function handling($routes, $quit = true)
if (is_callable($route['callback'])) {
$this->route = call_user_func_array($route['callback'], $params);
} else {
$this->route = explode('@', $route['callback']);
if (fnmatch('*@*', $route['callback'])) {
$this->route = explode('@', $route['callback']);
} elseif (fnmatch('*#*', $route['callback'])) {
$this->route = explode('#', $route['callback']);
} elseif (fnmatch('*.*', $route['callback'])) {
$this->route = explode('.', $route['callback']);
} elseif (fnmatch('*:*', $route['callback'])) {
$this->route = explode(':', $route['callback']);
}
}
} else {
call_user_func_array($route['callback'], $params);
Expand Down
58 changes: 33 additions & 25 deletions lib/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,36 +160,36 @@ public static function __callStatic($m, $a)
$callback = array_shift($a);

if (!$callback instanceof \Closure) {
$callback = function () use ($callback) {
$render = null;

if (fnmatch('*#*', $callback)) {
if (fnmatch('*#*#*', $callback)) {
list($controller, $action, $render) = explode('#', $callback, 3);
} else {
list($controller, $action) = explode('#', $callback, 2);
}
} elseif (fnmatch('*@*', $callback)) {
if (fnmatch('*@*@*', $callback)) {
list($controller, $action, $render) = explode('@', $callback, 3);
} else {
list($controller, $action) = explode('@', $callback, 2);
}
} elseif (fnmatch('*.*', $callback)) {
if (fnmatch('*.*.*', $callback)) {
list($controller, $action, $render) = explode('.', $callback, 3);
} else {
list($controller, $action) = explode('.', $callback, 2);
}
$render = null;

if (fnmatch('*#*', $callback)) {
if (fnmatch('*#*#*', $callback)) {
list($controller, $action, $render) = explode('#', $callback, 3);
} else {
list($controller, $action) = explode('#', $callback, 2);
}
} elseif (fnmatch('*@*', $callback)) {
if (fnmatch('*@*@*', $callback)) {
list($controller, $action, $render) = explode('@', $callback, 3);
} else {
list($controller, $action) = explode('@', $callback, 2);
}
} elseif (fnmatch('*.*', $callback)) {
if (fnmatch('*.*.*', $callback)) {
list($controller, $action, $render) = explode('.', $callback, 3);
} else {
list($controller, $action) = explode('.', $callback, 2);
}
}

$render = empty($render);
$render = empty($render);

$callback = function () use ($controller, $action, $render) {
return [$controller, $action, $render];
};
}

$method = Strings::lower($m);
$method = Strings::lower($m);

$methods = [];

Expand Down Expand Up @@ -269,13 +269,19 @@ public static function __callStatic($m, $a)

$name = '.' === $name ? 'home' : $name;

$route = model('uri', [
$dataRoute = [
'name' => $name,
'uri' => $uri,
'url' => $url,
'method' => $method,
'params' => $params
]);
];

if (isset($controller)) $dataRoute["controller"] = $controller;
if (isset($action)) $dataRoute["action"] = $action;
if (isset($render)) $dataRoute["render"] = $render;

$route = model('uri', $dataRoute);

$route->macro('as', function ($name) use ($route) {
return $route->setName($name);
Expand All @@ -292,6 +298,8 @@ public static function __callStatic($m, $a)
list($controller, $action) = explode('#', $string, 2);
} elseif (fnmatch('*.*', $string)) {
list($controller, $action) = explode('.', $string, 2);
} elseif (fnmatch('*:*', $string)) {
list($controller, $action) = explode(':', $string, 2);
}

return $route
Expand Down

0 comments on commit 95cb183

Please sign in to comment.