Add Role ACL to add and update forms

This commit is contained in:
Kendrick Chan 2018-01-10 13:48:25 +08:00
parent a3307c4f14
commit 9bb3542c78
2 changed files with 63 additions and 1 deletions

View file

@ -10,8 +10,19 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use App\Menu\Generator as MenuGenerator;
use App\Access\Generator as ACLGenerator;
class RoleController extends BaseController
{
protected $acl_gen;
public function __construct(MenuGenerator $menu_gen, ACLGenerator $acl_gen)
{
$this->acl_gen = $acl_gen;
parent::__construct($menu_gen);
}
public function index()
{
$params = $this->initParameters('role_list');
@ -96,9 +107,18 @@ class RoleController extends BaseController
]);
}
protected function padACLHierarchy(&$params)
{
// get acl keys hierarchy
$acl_data = $this->acl_gen->getACL();
$params['acl_hierarchy'] = $acl_data['hierarchy'];
}
public function create()
{
$params = $this->initParameters('role_list');
$this->padACLHierarchy($params);
// response
return $this->render('role/form.html.twig', $params);
@ -114,6 +134,13 @@ class RoleController extends BaseController
$row->setID($req->request->get('id'))
->setName($req->request->get('name'));
// acl attributes
$acl_attribs = $req->request->get('acl');
foreach ($acl_attribs as $acl_key)
{
$row->addACLAccess($acl_key);
}
// validate
$errors = $validator->validate($row);
@ -147,6 +174,7 @@ class RoleController extends BaseController
public function update($id)
{
$params = $this->initParameters('role_list');
$this->padACLHierarchy($params);
// get row data
$em = $this->getDoctrine()->getManager();
@ -177,6 +205,20 @@ class RoleController extends BaseController
$row->setID($req->request->get('id'))
->setName($req->request->get('name'));
// don't update acl attributes for super user since they don't need it
if (!$row->isSuperAdmin())
{
// clear first
$row->clearACLAttributes();
// then add
$acl_attribs = $req->request->get('acl');
foreach ($acl_attribs as $acl_key)
{
$row->addACLAccess($acl_key);
}
}
// validate
$errors = $validator->validate($row);

View file

@ -54,6 +54,26 @@
<span class="m-form__help">Display name for this role</span>
</div>
</div>
<div class="form-group m-form__group row">
<label class="col-lg-3 col-form-label" data-field="name">
Acess Levels:
</label>
<div class="col-lg-9">
<div class="m-checkbox-list">
{% for acl_layer in acl_hierarchy %}
<b>{{ acl_layer.label }}</b><br>
{% for key, label in acl_layer.acls %}
<label class="m-checkbox">
<input type="checkbox" name="acl[]" value="{{ key }}" {{ row.hasACLAccess(key) ? 'checked' : '' }}>
{{ label }}
<span></span>
</label>
{% endfor %}
<br>
{% endfor %}
</div>
</div>
</div>
</div>
<div class="m-portlet__foot m-portlet__foot--fit">
<div class="m-form__actions m-form__actions--solid m-form__actions--right">
@ -139,4 +159,4 @@
}
});
</script>
{% endblock %}
{% endblock %}