re #25 allow rules edition + assignment

This commit is contained in:
Antonio Ramirez
2017-07-16 02:28:34 +02:00
parent f706ebb4fa
commit 84b009c5f0
17 changed files with 614 additions and 20 deletions

View File

@ -46,11 +46,9 @@ class AuthItemEditionService implements ServiceInterface
$item->description = $this->model->description;
if (!empty($this->model->rule)) {
$rule = $this->make($this->model->rule);
if (null === $this->getAuthManager()->getRule($rule->name)) {
$this->getAuthManager()->add($rule);
if (null !== $this->getAuthManager()->getRule($this->model->rule)) {
$item->ruleName = $this->model->rule;
}
$item->ruleName = $rule->name;
} else {
$item->ruleName = null;
}

View File

@ -0,0 +1,53 @@
<?php
/*
* This file is part of the 2amigos/yii2-usuario project.
*
* (c) 2amigOS! <http://2amigos.us/>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Da\User\Service;
use Da\User\Contracts\ServiceInterface;
use Da\User\Model\Rule;
use Da\User\Traits\AuthManagerAwareTrait;
use Da\User\Traits\ContainerAwareTrait;
use Exception;
class AuthRuleEditionService implements ServiceInterface
{
use AuthManagerAwareTrait;
use ContainerAwareTrait;
protected $model;
public function __construct(Rule $model)
{
$this->model = $model;
}
public function run()
{
if (!$this->model->validate() || (!in_array($this->model->scenario, ['create', 'update']))) {
return false;
}
$rule = $this->make($this->model->className, [], ['name' => $this->model->name]);
try {
if ($this->model->scenario == 'create') {
$this->getAuthManager()->add($rule);
} else {
$this->getAuthManager()->update($this->model->previousName, $rule);
}
$this->getAuthManager()->invalidateCache();
} catch (Exception $e) {
return false;
}
return true;
}
}