From 9e6ff4ee15119fcc4509cd0eceaae3ff14d11c35 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Tue, 22 May 2018 20:57:50 +0800 Subject: [PATCH] Add rider username and password to forms and database #119 --- src/Controller/RiderController.php | 41 +++++++++++++++++++++++++++++- src/Entity/Rider.php | 37 +++++++++++++++++++++++++++ templates/rider/form.html.twig | 30 ++++++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) diff --git a/src/Controller/RiderController.php b/src/Controller/RiderController.php index 84e2f21d..16b9bb8c 100644 --- a/src/Controller/RiderController.php +++ b/src/Controller/RiderController.php @@ -7,6 +7,7 @@ use App\Ramcar\DayOfWeek; use App\Entity\Rider; use App\Entity\RiderSchedule; use App\Entity\Hub; +use App\Entity\User; use App\Service\FileUploader; use Doctrine\ORM\Query; @@ -14,6 +15,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; use Symfony\Component\Validator\Validator\ValidatorInterface; +use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; use DateTime; @@ -154,7 +156,8 @@ class RiderController extends BaseController ->setContactNumber($req->request->get('contact_no')) ->setPlateNumber($req->request->get('plate_number')) ->setImageFile($req->request->get('image_file')) - ->setActive($req->request->get('flag_active') ? true : false); + ->setActive($req->request->get('flag_active') ? true : false) + ->setUsername($req->request->get('username')); } public function addSubmit(Request $req, EncoderFactoryInterface $ef, ValidatorInterface $validator) @@ -176,6 +179,24 @@ class RiderController extends BaseController // initialize error list $error_array = []; + // get password inputs + $password = $req->request->get('password'); + $confirm_password = $req->request->get('confirm_password'); + + // custom validation for password fields + if (!$password) { + $error_array['password'] = 'This value should not be blank.'; + } else if ($password != $confirm_password) { + $error_array['confirm_password'] = 'Passwords do not match.'; + } else { + // encode password + $enc = $ef->getEncoder(new User()); + $encoded_password = $enc->encodePassword($req->request->get('password'), ''); + + // set password + $obj->setPassword($encoded_password); + } + // custom validation for associations $hub_id = $req->request->get('hub'); @@ -303,6 +324,24 @@ class RiderController extends BaseController // initialize error list $error_array = []; + // get password inputs + $password = $req->request->get('password'); + $confirm_password = $req->request->get('confirm_password'); + + // custom validation for password fields + if ($password || $confirm_password) { + if ($password != $confirm_password) { + $error_array['confirm_password'] = 'Passwords do not match.'; + } else { + // encode password + $enc = $ef->getEncoder(new User()); + $encoded_password = $enc->encodePassword($req->request->get('password'), ''); + + // set password + $obj->setPassword($encoded_password); + } + } + // custom validation for associations $hub_id = $req->request->get('hub'); diff --git a/src/Entity/Rider.php b/src/Entity/Rider.php index d637bc99..8b3dba37 100644 --- a/src/Entity/Rider.php +++ b/src/Entity/Rider.php @@ -92,6 +92,18 @@ class Rider */ protected $flag_active; + // username for rider api + /** + * @ORM\Column(type="string", length=80, unique=true, nullable=true) + */ + protected $username; + + // password for rider api + /** + * @ORM\Column(type="string", length=64) + */ + protected $password; + public function __construct() { $this->job_orders = new ArrayCollection(); @@ -99,6 +111,8 @@ class Rider $this->curr_rating = 0; $this->flag_available = true; $this->flag_active = true; + $this->username = null; + $this->password = ''; } public function getID() @@ -253,4 +267,27 @@ class Rider { return $this->flag_active; } + + public function setUsername($username) + { + $this->username = $username; + return $this; + } + + public function getUsername() + { + return $this->username; + } + + public function setPassword($pass) + { + // they have to pass the encoded password + $this->password = $pass; + return $this; + } + + public function getPassword() + { + return $this->password; + } } diff --git a/templates/rider/form.html.twig b/templates/rider/form.html.twig index c6501ca9..c08c250c 100644 --- a/templates/rider/form.html.twig +++ b/templates/rider/form.html.twig @@ -35,6 +35,36 @@
+ +
+

+ Rider App User +

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

Rider Details