From 98592424009a4c96260c96197ee4cf619811556c Mon Sep 17 00:00:00 2001 From: Ramon Gutierrez Date: Sun, 7 Jan 2018 19:21:37 +0800 Subject: [PATCH] Add basic user crud --- config/routes.yaml | 30 +++++ src/Controller/RoleController.php | 4 + src/Controller/UserController.php | 188 +++++++++++++++++++++++++++++- templates/role/form.html.twig | 52 ++++----- templates/user/form.html.twig | 157 +++++++++++++++++++++++++ templates/user/list.html.twig | 179 ++++++++++++++++++++++++++++ 6 files changed, 580 insertions(+), 30 deletions(-) create mode 100644 templates/user/form.html.twig create mode 100644 templates/user/list.html.twig diff --git a/config/routes.yaml b/config/routes.yaml index 97da2f00..4f74a5b1 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -22,6 +22,36 @@ user_list: path: /users controller: App\Controller\UserController::index +user_rows: + path: /users/rows + controller: App\Controller\UserController::rows + methods: [POST] + +user_create: + path: /users/create + controller: App\Controller\UserController::create + methods: [GET] + +user_create_submit: + path: /users/create + controller: App\Controller\UserController::createSubmit + methods: [POST] + +user_update: + path: /users/{id} + controller: App\Controller\UserController::update + methods: [GET] + +user_update_submit: + path: /users/{id} + controller: App\Controller\UserController::updateSubmit + methods: [POST] + +user_delete: + path: /users/{id} + controller: App\Controller\UserController::destroy + methods: [DELETE] + # roles role_list: diff --git a/src/Controller/RoleController.php b/src/Controller/RoleController.php index 3749a079..cb1bf79a 100644 --- a/src/Controller/RoleController.php +++ b/src/Controller/RoleController.php @@ -58,6 +58,8 @@ class RoleController extends BaseController public function createSubmit(Request $req) { + // TODO: validation + // create new row $em = $this->getDoctrine()->getManager(); $row = new Role(); @@ -99,6 +101,8 @@ class RoleController extends BaseController public function updateSubmit(Request $req, $id) { + // TODO: validation + // get row data $em = $this->getDoctrine()->getManager(); $row = $em->getRepository(Role::class)->find($id); diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 61803a6a..127756dd 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -3,6 +3,13 @@ namespace App\Controller; use App\Ramcar\BaseController; +use App\Entity\User; +use App\Entity\Role; + +use Doctrine\ORM\Query; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; class UserController extends BaseController { @@ -10,6 +17,185 @@ class UserController extends BaseController { $params = $this->initParameters('user_list'); - return $this->render('home.html.twig', $params); + $qb = $this->getDoctrine() + ->getRepository(User::class) + ->createQueryBuilder('q') + ->getQuery(); + + // get all rows + $rows = $qb->getResult(Query::HYDRATE_ARRAY); + + // response + return $this->render('user/list.html.twig', $params); + } + + public function rows() + { + // build query + $qb = $this->getDoctrine() + ->getRepository(User::class) + ->createQueryBuilder('q') + ->getQuery(); + + // get all rows + $rows = $qb->getResult(Query::HYDRATE_ARRAY); + + // add crud urls + foreach ($rows as $index => $row) { + $rows[$index]['update_url'] = $this->generateUrl('user_update', ['id' => $row['id']]); + $rows[$index]['delete_url'] = $this->generateUrl('user_delete', ['id' => $row['id']]); + } + + // response + return $this->json(['data' => $rows]); + } + + public function create() + { + $params = $this->initParameters('user_list'); + + // get roles + $em = $this->getDoctrine()->getManager(); + $params['roles'] = $em->getRepository(Role::class)->findAll(); + + // response + return $this->render('user/form.html.twig', $params); + } + + public function createSubmit(Request $req, EncoderFactoryInterface $ef) + { + // TODO: validation, compare password fields + + // create new row + $em = $this->getDoctrine()->getManager(); + $row = new User(); + + // encode password + $enc = $ef->getEncoder($row); + $password = $enc->encodePassword($req->request->get('password'), $row->getSalt()); + + // set and save values + $row->setUsername($req->request->get('username')) + ->setFirstName($req->request->get('first_name')) + ->setLastName($req->request->get('last_name')) + ->setPassword($password) + ->setEmail($req->request->get('email')) + ->setContactNumber($req->request->get('contact_no')) + ->setEnabled($req->request->get('enabled') ? true : false); + + // set roles + foreach ($req->request->get('roles') as $role_id) { + // check if role exists + $role = $em->getRepository(Role::class)->find($role_id); + + if (!empty($role)) + $row->addRole($role); + } + + $em->persist($row); + $em->flush(); + + // set success + $this->addFlash( + 'success', + 'Changes have been saved!' + ); + + // response + return $this->redirectToRoute('user_list'); + } + + public function update($id) + { + $params = $this->initParameters('user_list'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(User::class)->find($id); + + if (empty($row)) + throw $this->createNotFoundException('The item does not exist'); + + // get roles + $em = $this->getDoctrine()->getManager(); + $params['roles'] = $em->getRepository(Role::class)->findAll(); + + $params['row'] = $row; + $params['values'] = []; + + // response + return $this->render('user/form.html.twig', $params); + } + + public function updateSubmit(Request $req, EncoderFactoryInterface $ef, $id) + { + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(User::class)->find($id); + + if (empty($row)) + throw $this->createNotFoundException('The item does not exist'); + + // set and save values + $row->setUsername($req->request->get('username')) + ->setFirstName($req->request->get('first_name')) + ->setLastName($req->request->get('last_name')) + ->setEmail($req->request->get('email')) + ->setContactNumber($req->request->get('contact_no')) + ->setEnabled($req->request->get('enabled') ? true : false) + ->clearRoles(); + + // update password if provided + $password_input = $req->request->get('password'); + $confirm_password_input = $req->request->get('confirm_password'); + + if ($password_input || $confirm_password_input) { + // encode password + $enc = $ef->getEncoder($row); + $password = $enc->encodePassword($req->request->get('password'), $row->getSalt()); + + $row->setPassword($password); + } + + // set roles + foreach ($req->request->get('roles') as $role_id) { + // check if role exists + $role = $em->getRepository(Role::class)->find($role_id); + + if (!empty($role)) + $row->addRole($role); + } + + $em->flush(); + + // set success + $this->addFlash( + 'success', + 'Changes have been saved!' + ); + + // response + return $this->redirectToRoute('user_list'); + } + + public function destroy($id) + { + $params = $this->initParameters('user_list'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(User::class)->find($id); + + if (empty($row)) + throw $this->createNotFoundException('The item does not exist'); + + // delete this row + $em->remove($row); + $em->flush(); + + // response + $response = new Response(); + $response->setStatusCode(Response::HTTP_OK); + $response->send(); } } diff --git a/templates/role/form.html.twig b/templates/role/form.html.twig index 903e1a38..2b80257c 100644 --- a/templates/role/form.html.twig +++ b/templates/role/form.html.twig @@ -13,7 +13,7 @@
-
+
@@ -21,9 +21,6 @@ - - -

{% if row is defined %} Edit Role @@ -35,36 +32,33 @@

-
+
-
-
- -
- - {% if error.id is defined %} - - {% endif %} - Unique identifier for this role -
+
+ +
+ + {% if error.id is defined %} + + {% endif %} + Unique identifier for this role
-
- -
- - {% if error.name is defined %} - - {% endif %} - Display name for this role -
-
-
+
+
+ +
+ + {% if error.name is defined %} + + {% endif %} + Display name for this role +
+
-
+
-
-
+
Cancel
diff --git a/templates/user/form.html.twig b/templates/user/form.html.twig new file mode 100644 index 00000000..2e5971b5 --- /dev/null +++ b/templates/user/form.html.twig @@ -0,0 +1,157 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Users

+
+
+
+ +
+ +
+
+
+
+
+
+ + + +

+ {% if row is defined %} + Edit User + {{ row.getUsername() }} + {% else %} + New User + {% endif %} +

+
+
+
+ + +
+
+ +
+ + {% if error.username is defined %} + + {% endif %} + Unique alias for this user +
+
+
+ +
+ + {% if error.first_name is defined %} + + {% endif %} +
+ +
+ + {% if error.last_name is defined %} + + {% endif %} +
+
+
+ +
+ + {% if error.password is defined %} + + {% endif %} + {% if row is defined %} + Leave both fields blank for unchanged + {% endif %} +
+ +
+ + {% if error.confirm_password is defined %} + + {% endif %} +
+
+
+ +
+ + {% if error.email is defined %} + + {% endif %} +
+ +
+ + {% if error.contact_no is defined %} + + {% endif %} +
+
+
+ +
+
+ {% for role in roles %} + + {% endfor %} +
+ Check all roles that apply +
+
+
+ + + + +
+
+
+
+
+
+ + Cancel +
+
+
+
+ +
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/user/list.html.twig b/templates/user/list.html.twig new file mode 100644 index 00000000..79229e11 --- /dev/null +++ b/templates/user/list.html.twig @@ -0,0 +1,179 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

+ Users +

+
+
+
+ +
+ +
+
+
+
+ {% for message in app.flashes('success') %} + + {% endfor %} +
+
+
+
+
+
+ + + + +
+
+
+
+ +
+
+ +
+ +
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} \ No newline at end of file