Add support for update and delete API User. #194
This commit is contained in:
parent
d42e1b1ed5
commit
35321203f1
2 changed files with 108 additions and 2 deletions
|
|
@ -141,7 +141,7 @@ class APIUserController extends BaseController
|
||||||
$this->denyAccessUnlessGranted('apiuser.add', null, 'No access.');
|
$this->denyAccessUnlessGranted('apiuser.add', null, 'No access.');
|
||||||
|
|
||||||
// create new row
|
// create new row
|
||||||
// API and secret keys are generated with the call to new APIUser()
|
// API and secret keys are generated with the call to new APIUser()
|
||||||
$em = $this->getDoctrine()->getManager();
|
$em = $this->getDoctrine()->getManager();
|
||||||
$obj = new APIUser();
|
$obj = new APIUser();
|
||||||
|
|
||||||
|
|
@ -198,6 +198,112 @@ class APIUserController extends BaseController
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function updateForm($id)
|
||||||
|
{
|
||||||
|
$this->denyAccessUnlessGranted('apiuser.update', null, 'No access.');
|
||||||
|
|
||||||
|
$params = $this->initParameters('api_ser_list');
|
||||||
|
$params['mode'] = 'update';
|
||||||
|
|
||||||
|
// get row data
|
||||||
|
$em = $this->getDoctrine()->getManager();
|
||||||
|
$obj = $em->getRepository(APIUser::class)->find($id);
|
||||||
|
|
||||||
|
// make sure this row exists
|
||||||
|
if (empty($obj))
|
||||||
|
throw $this->createNotFoundException('The item does not exist');
|
||||||
|
|
||||||
|
// get roles
|
||||||
|
$params['roles'] = $em->getRepository(APIRole::class)->findAll();
|
||||||
|
|
||||||
|
$params['obj'] = $obj;
|
||||||
|
|
||||||
|
// response
|
||||||
|
return $this->render('api-user/form.html.twig', $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateSubmit(Request $req, EncoderFactoryInterface $ef, ValidatorInterface $validator, $id)
|
||||||
|
{
|
||||||
|
$this->denyAccessUnlessGranted('apiuser.update', null, 'No access.');
|
||||||
|
|
||||||
|
// get row data
|
||||||
|
$em = $this->getDoctrine()->getManager();
|
||||||
|
$obj = $em->getRepository(APIUser::class)->find($id);
|
||||||
|
|
||||||
|
// make sure this row exists
|
||||||
|
if (empty($obj))
|
||||||
|
throw $this->createNotFoundException('The item does not exist');
|
||||||
|
|
||||||
|
// set and save values
|
||||||
|
$obj->setName($req->request->get('name'))
|
||||||
|
->setEnabled($req->request->get('enabled') ? true : false)
|
||||||
|
->clearRoles();
|
||||||
|
|
||||||
|
// set roles
|
||||||
|
$roles = $req->request->get('roles');
|
||||||
|
|
||||||
|
if (!empty($roles)) {
|
||||||
|
foreach ($roles as $role_id) {
|
||||||
|
// check if role exists
|
||||||
|
$role = $em->getRepository(APIRole::class)->find($role_id);
|
||||||
|
|
||||||
|
if (!empty($role))
|
||||||
|
$obj->addRole($role);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// validate
|
||||||
|
$errors = $validator->validate($obj);
|
||||||
|
|
||||||
|
// initialize error list
|
||||||
|
$error_array = [];
|
||||||
|
|
||||||
|
// add errors to list
|
||||||
|
foreach ($errors as $error) {
|
||||||
|
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if any errors were found
|
||||||
|
if (!empty($error_array)) {
|
||||||
|
// return validation failure response
|
||||||
|
return $this->json([
|
||||||
|
'success' => false,
|
||||||
|
'errors' => $error_array
|
||||||
|
], 422);
|
||||||
|
} else {
|
||||||
|
// validated! save the entity
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
|
// return successful response
|
||||||
|
return $this->json([
|
||||||
|
'success' => 'Changes have been saved!'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy($id)
|
||||||
|
{
|
||||||
|
$this->denyAccessUnlessGranted('apiuser.delete', null, 'No access.');
|
||||||
|
|
||||||
|
$params = $this->initParameters('api_user_list');
|
||||||
|
|
||||||
|
// get row data
|
||||||
|
$em = $this->getDoctrine()->getManager();
|
||||||
|
$obj = $em->getRepository(APIUser::class)->find($id);
|
||||||
|
|
||||||
|
if (empty($obj))
|
||||||
|
throw $this->createNotFoundException('The item does not exist');
|
||||||
|
|
||||||
|
// delete this row
|
||||||
|
$em->remove($obj);
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
|
// response
|
||||||
|
$response = new Response();
|
||||||
|
$response->setStatusCode(Response::HTTP_OK);
|
||||||
|
$response->send();
|
||||||
|
}
|
||||||
|
|
||||||
// check if datatable filter is present and append to query
|
// check if datatable filter is present and append to query
|
||||||
protected function setQueryFilters($datatable, &$query) {
|
protected function setQueryFilters($datatable, &$query) {
|
||||||
if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) {
|
if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) {
|
||||||
|
|
|
||||||
|
|
@ -117,7 +117,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
if (row.meta.delete_url != '') {
|
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.id + '" title="Delete"><i class="la la-trash"></i></a>';
|
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.name + '" title="Delete"><i class="la la-trash"></i></a>';
|
||||||
}
|
}
|
||||||
|
|
||||||
return actions;
|
return actions;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue