Add routes for API roles and user. Add controller for API role. Display list of API roles. #194

This commit is contained in:
Korina Cordero 2019-05-08 10:27:53 +00:00
parent ecec2d07d7
commit 2bd703fc5c
6 changed files with 412 additions and 0 deletions

View file

@ -34,6 +34,36 @@ access_keys:
label: Update
- id: role.delete
label: Delete
- id: apiuser
label: API User Access
acls:
- id: apiuser.menu
label: Menu
- id: apiuser.list
label: List
- id: apiuser.add
label: Add
- id: apiuser.update
label: Update
- id: apiuser.delete
label: Delete
- id: apiuser.role.sadmin
label: Super Admin Role
- id: apiuser.profile
label: User Profile
- id: apirole
label: API Role Access
acls:
- id: apirole.menu
label: Menu
- id: apirole.list
label: List
- id: apirole.add
label: Add
- id: apirole.update
label: Update
- id: apirole.delete
label: Delete
- id: logistics
label: Logistics Access
acls:

View file

@ -16,6 +16,19 @@ main_menu:
label: Roles
parent: user
- id: apiuser
acl: apiuser.menu
label: API User
icon: flaticon-users
- id: api_user_list
acl: apiuser.list
label: API Users
parent: apiuser
- id: api_role_list
acl: apirole.list
label: API Roles
parent: apiuser
- id: logistics
acl: logistics.menu
label: Logistics

View file

@ -0,0 +1,33 @@
api_role_list:
path: /api-roles
controller: App\Controller\APIRoleController::index
api_role_rows:
path: /api-roles/rows
controller: App\Controller\APIRoleController::rows
methods: [POST]
api_role_create:
path: /api-roles/create
controller: App\Controller\APIRoleController::addForm
methods: [GET]
api_role_create_submit:
path: /api-roles/create
controller: App\Controller\APIRoleController::addSubmit
methods: [POST]
api_role_update:
path: /api-roles/{id}
controller: App\Controller\APIRoleController::updateForm
methods: [GET]
api_role_update_submit:
path: /api-roles/{id}
controller: App\Controller\APIRoleController::updateSubmit
methods: [POST]
api_role_delete:
path: /api-roles/{id}
controller: App\Controller\APIRoleController::destroy
methods: [DELETE]

View file

@ -0,0 +1,44 @@
api_user_list:
path: /apiusers
controller: App\Controller\APIUserController::index
api_user_rows:
path: /apiusers/rows
controller: App\Controller\APIUserController::rows
methods: [POST]
api_user_create:
path: /apiusers/create
controller: App\Controller\APIUserController::addForm
methods: [GET]
api_user_create_submit:
path: /apiusers/create
controller: App\Controller\APIUserController::addSubmit
methods: [POST]
api_user_update:
path: /apiusers/{id}
controller: App\Controller\APIUserController::updateForm
methods: [GET]
api_user_update_submit:
path: /apiusers/{id}
controller: App\Controller\APIUserController::updateSubmit
methods: [POST]
api_user_delete:
path: /apiusers/{id}
controller: App\Controller\APIUserController::destroy
methods: [DELETE]
api_user_profile:
path: /apiprofile
controller: App\Controller\APIUserController::profileForm
methods: [GET]
api_user_profile_submit:
path: /apiprofile
controller: App\Controller\APIUserController::profileSubmit
methods: [POST]

View file

@ -0,0 +1,151 @@
<?php
namespace App\Controller;
use App\Ramcar\BaseController;
use Catalyst\APIBundle\Entity\Role as APIRole;
use Doctrine\ORM\Query;
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 APIRoleController 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()
{
$this->denyAccessUnlessGranted('apirole.list', null, 'No access.');
$params = $this->initParameters('api_role_list');
// response
return $this->render('api-role/list.html.twig', $params);
}
public function rows(Request $req)
{
$this->denyAccessUnlessGranted('apirole.list', null, 'No access.');
// build query
$qb = $this->getDoctrine()
->getRepository(APIRole::class)
->createQueryBuilder('q');
// get datatable params
$datatable = $req->request->get('datatable');
// count total records
$tquery = $qb->select('COUNT(q)');
// add filters to count query
$this->setQueryFilters($datatable, $tquery);
$total = $tquery->getQuery()
->getSingleScalarResult();
// get current page number
$page = $datatable['pagination']['page'] ?? 1;
$perpage = $datatable['pagination']['perpage'];
$offset = ($page - 1) * $perpage;
// add metadata
$meta = [
'page' => $page,
'perpage' => $perpage,
'pages' => ceil($total / $perpage),
'total' => $total,
'sort' => 'asc',
'field' => 'id'
];
// build query
$query = $qb->select('q');
// add filters to query
$this->setQueryFilters($datatable, $query);
// check if sorting is present, otherwise use default
if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) {
$order = $datatable['sort']['sort'] ?? 'asc';
$query->orderBy('q.' . $datatable['sort']['field'], $order);
} else {
$query->orderBy('q.id', 'asc');
}
// get rows for this page
$obj_rows = $query->setFirstResult($offset)
->setMaxResults($perpage)
->getQuery()
->getResult();
// process rows
$rows = [];
foreach ($obj_rows as $orow) {
// add row data
$row['id'] = $orow->getID();
$row['name'] = $orow->getName();
// add row metadata
$row['meta'] = [
'update_url' => '',
'delete_url' => ''
];
// check if they have access to super admin users
if (!$this->isGranted('user.role.sadmin') && $orow->isSuperAdmin())
{
$rows[] = $row;
continue;
}
// add crud urls
if ($this->isGranted('user.update'))
$row['meta']['update_url'] = $this->generateUrl('api_role_update', ['id' => $row['id']]);
if ($this->isGranted('user.delete'))
$row['meta']['delete_url'] = $this->generateUrl('api_role_delete', ['id' => $row['id']]);
$rows[] = $row;
}
// response
return $this->json([
'meta' => $meta,
'data' => $rows
]);
}
protected function padACLHierarchy(&$params)
{
// get acl keys hierarchy
$acl_data = $this->acl_gen->getACL();
$params['acl_hierarchy'] = $acl_data['hierarchy'];
}
// check if datatable filter is present and append to query
protected function setQueryFilters($datatable, &$query)
{
if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search']))
{
$query->where('q.id LIKE :filter')
->orWhere('q.name LIKE :filter')
->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%');
}
}
}

View file

@ -0,0 +1,141 @@
{% extends 'base.html.twig' %}
{% block body %}
<!-- BEGIN: Subheader -->
<div class="m-subheader">
<div class="d-flex align-items-center">
<div class="mr-auto">
<h3 class="m-subheader__title">
API Roles
</h3>
</div>
</div>
</div>
<!-- END: Subheader -->
<div class="m-content">
<!--Begin::Section-->
<div class="row">
<div class="col-xl-12">
<div class="m-portlet m-portlet--mobile">
<div class="m-portlet__body">
<div class="m-form m-form--label-align-right m--margin-top-20 m--margin-bottom-30">
<div class="row align-items-center">
<div class="col-xl-8 order-2 order-xl-1">
<div class="form-group m-form__group row align-items-center">
<div class="col-md-4">
<div class="m-input-icon m-input-icon--left">
<input type="text" class="form-control m-input m-input--solid" placeholder="Search..." id="data-rows-search">
<span class="m-input-icon__icon m-input-icon__icon--left">
<span><i class="la la-search"></i></span>
</span>
</div>
</div>
</div>
</div>
<div class="col-xl-4 order-1 order-xl-2 m--align-right">
<a href="{{ url('api_role_create') }}" class="btn btn-focus m-btn m-btn--custom m-btn--icon m-btn--air m-btn--pill">
<span>
<i class="la la-key"></i>
<span>New API Role</span>
</span>
</a>
<div class="m-separator m-separator--dashed d-xl-none"></div>
</div>
</div>
</div>
<!--begin: Datatable -->
<div id="data-rows"></div>
<!--end: Datatable -->
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block scripts %}
<script>
$(function() {
var options = {
data: {
type: 'remote',
source: {
read: {
url: '{{ url("api_role_rows") }}',
method: 'POST'
}
},
saveState: {
cookie: false,
webstorage: false
},
pageSize: 10,
serverPaging: true,
serverFiltering: true,
serverSorting: true
},
columns: [
{
field: 'id',
title: 'ID'
},
{
field: 'name',
title: 'Name'
},
{
field: 'Actions',
width: 110,
title: 'Actions',
sortable: false,
overflow: 'visible',
template: function (row, index, datatable) {
var actions = '';
if (row.meta.update_url != '') {
actions += '<a href="' + row.meta.update_url + '" class="m-portlet__nav-link btn m-btn m-btn--hover-accent m-btn--icon m-btn--icon-only m-btn--pill btn-edit" data-id="' + row.username + '" title="Edit"><i class="la la-edit"></i></a>';
}
if (row.meta.delete_url != '') {
actions += '<a href="' + row.meta.delete_url + '" class="m-portlet__nav-link btn m-btn m-btn--hover-danger m-btn--icon m-btn--icon-only m-btn--pill btn-delete" data-id="' + row.username + '" title="Delete"><i class="la la-trash"></i></a>';
}
return actions;
},
}
],
search: {
onEnter: false,
input: $('#data-rows-search'),
delay: 400
}
};
var table = $("#data-rows").mDatatable(options);
$(document).on('click', '.btn-delete', function(e) {
var url = $(this).prop('href');
var id = $(this).data('id');
var btn = $(this);
e.preventDefault();
swal({
title: 'Confirmation',
html: 'Are you sure you want to delete <strong>' + id + '</strong>?',
type: 'warning',
showCancelButton: true
}).then((result) => {
if (result.value) {
$.ajax({
method: "DELETE",
url: url
}).done(function(response) {
table.row(btn.parents('tr')).remove();
table.reload();
});
}
});
});
});
</script>
{% endblock %}