diff --git a/app/Controller/Web/Session.php b/app/Controller/Api/Session.php old mode 100644 new mode 100755 similarity index 53% rename from app/Controller/Web/Session.php rename to app/Controller/Api/Session.php index 342a99f1..bcad8c07 --- a/app/Controller/Web/Session.php +++ b/app/Controller/Api/Session.php @@ -1,9 +1,10 @@ request = $request; $this->view = new View(); } - public function get() - { - $this->view->render("Login.php"); - } public function post() { - $input = $this->request->getUrlParams(); + $input = $this->request->getData(); + + $userModel = new UserModel(); + $userModel->setId($input['id']); + $userModel->setFirstName($input['firstName']); + $userModel->setLastName($input['lastName']); + $userModel->setEmail($input['email']); + $userModel->setPassword($input['password']); + $userModel->setCreatedOn($input['createdOn']); + $sessionService = new SessionService(); try { - $response = $sessionService->login($input); + $response = $sessionService->login($userModel); if ($response instanceof SessionModel) { setcookie('userId', $response->getUserId(), time() + (86400 * 30), "/"); setcookie('authToken', $response->getAuthToken(), time() + (86400 * 30), "/"); - $app = \Slim\Slim::getInstance('developer'); - $app->redirect("/notes"); + + $objResponse = new Response($response->toArray(), 1, "SUCCESS"); + + echo $objResponse->getResponse(); } - } catch (\InvalidArgumentException $error) { - $response = $error->getMessage(); - $objResponse = new Response($response); - $this->view->render("Login.php", $objResponse->getResponse()); } catch (ModelNotFoundException $error) { $response = $error->getMessage(); - $objResponse = new Response($response); - $this->view->render("Login.php", $objResponse->getResponse()); + $objResponse = new Response($response, 0, "FAILURE"); + echo $objResponse->getResponse(); } } } diff --git a/app/Controller/Web/Login.php b/app/Controller/Web/Login.php new file mode 100755 index 00000000..babfe4ac --- /dev/null +++ b/app/Controller/Web/Login.php @@ -0,0 +1,20 @@ +request = $request; + $this->view = new View(); + } + public function get() + { + $this->view->render("Login.php"); + } +} diff --git a/app/Controller/Web/User.php b/app/Controller/Web/User.php index cfe52dcd..de0b54a6 100644 --- a/app/Controller/Web/User.php +++ b/app/Controller/Web/User.php @@ -3,6 +3,7 @@ use Notes\View\View as View; use Notes\Service\User as UserService; +use Notes\Factory\User as UserFactory; use Notes\Model\User as UserModel; use Notes\Response\Response as Response; use Notes\Exception\ModelNotFoundException as ModelNotFoundException; @@ -23,6 +24,7 @@ public function get() public function post() { $input = $this->request->getUrlParams(); + $userService = new UserService(); try { $response = $userService->create($input); diff --git a/app/Domain/Session.php b/app/Domain/Session.php index 32d8a871..4a7208f5 100755 --- a/app/Domain/Session.php +++ b/app/Domain/Session.php @@ -11,6 +11,8 @@ use Notes\Validator\InputValidator as InputValidator; +use Notes\Factory\User as UserFactory; + class Session { public function __construct() @@ -18,61 +20,59 @@ public function __construct() $this->validator = new InputValidator(); } - public function create($userInput) + public function create($userModel) { $sessionModel = new sessionModel(); $userDomain = new UserDomain(); - $userModelRead = $userDomain->readByUserNameAndPassword($userInput); + $userModelRead = $userDomain->readByUserNameAndPassword($userModel); $sessionModel->setUserId($userModelRead->getId()); $randomNumber = rand(); - + $password = $userModelRead->getPassword(); $sessionModel->createAuthToken($password, $randomNumber); $sessionModel->setCreatedOn(date("Y-m-d H:i:s")); - if ($this->validator->notNull($sessionModel->getUserId()) - && $this->validator->validNumber($sessionModel->getUserId()) - && $this->validator->notNull($sessionModel->getAuthToken())) { + $this->validator->notNull($sessionModel->getUserId()); + $this->validator->validNumber($sessionModel->getUserId()); + $this->validator->notNull($sessionModel->getAuthToken()); + $sessionMapper = new SessionMapper(); $sessionModel = $sessionMapper->create($sessionModel); return $sessionModel; - } } public function read($sessionModel) { - if ($this->validator->notNull($sessionModel->getId()) - && $this->validator->validNumber($sessionModel->getId())) { + $this->validator->notNull($sessionModel->getId()); + $this->validator->validNumber($sessionModel->getId()); $sessionMapper = new SessionMapper(); $sessionModel = $sessionMapper->read($sessionModel); return $sessionModel; - } } public function getSessionByAuthTokenAndUserId($sessionModel) { - if ($this->validator->notNull($sessionModel->getUserId()) - && $this->validator->validNumber($sessionModel->getUserId()) - && $this->validator->notNull($sessionModel->getAuthToken())) { + $this->validator->notNull($sessionModel->getUserId()); + $this->validator->validNumber($sessionModel->getUserId()); + $this->validator->notNull($sessionModel->getAuthToken()); $sessionMapper = new SessionMapper(); $sessionModel = $sessionMapper->read($sessionModel); return $sessionModel; - } } public function delete($sessionModel) { - if ($this->validator->notNull($sessionModel->getId()) - && $this->validator->validNumber($sessionModel->getId()) - && $this->validator->notNull($sessionModel->getUserId()) - && $this->validator->validNumber($sessionModel->getUserId())) { + $this->validator->notNull($sessionModel->getId()); + $this->validator->validNumber($sessionModel->getId()); + $this->validator->notNull($sessionModel->getUserId()); + $this->validator->validNumber($sessionModel->getUserId()); $sessionModel->setIsExpired(1); $sessionModel->setExpiredOn(date("Y-m-d H:i:s")); $sessionMapper = new SessionMapper(); @@ -80,6 +80,5 @@ public function delete($sessionModel) $sessionModel = $sessionMapper->update($sessionModel); return $sessionModel; - } } } diff --git a/app/Domain/User.php b/app/Domain/User.php index c803876c..01e62c21 100755 --- a/app/Domain/User.php +++ b/app/Domain/User.php @@ -26,7 +26,10 @@ public function create($input) { $userFactory = new UserFactory(); $userModel = $userFactory->create($input); - $userModel->setCreatedOn(date("Y-m-d H:i:s")); + + $date = date("Y-m-d H:i:s"); + $userModel->setCreatedOn($date); + $userMapper = new UserMapper(); $userModel = $userMapper->create($userModel); return $userModel; @@ -44,15 +47,12 @@ public function read($input) } - public function readByUsernameandPassword($input) + public function readByUsernameandPassword($userModel) { + $userMapper = new UserMapper(); + $userModel = $userMapper->read($userModel); - $userFactory = new UserFactory(); - $userModel = $userFactory->create($input); - - $userMapper = new UserMapper(); - $userModel = $userMapper->read($userModel); - return $userModel; + return $userModel; } public function update($input) diff --git a/app/Model/Session.php b/app/Model/Session.php index ce121a46..84c9d831 100755 --- a/app/Model/Session.php +++ b/app/Model/Session.php @@ -4,12 +4,12 @@ class Session extends Model { - private $id; - private $userId; - private $authToken; - private $createdOn; - private $expiredOn; - private $isExpired; + protected $id; + protected $userId; + protected $authToken; + protected $createdOn; + protected $expiredOn; + protected $isExpired; public function setId($id) { diff --git a/app/Response/Response.php b/app/Response/Response.php index 396b8819..a88179fa 100644 --- a/app/Response/Response.php +++ b/app/Response/Response.php @@ -7,7 +7,7 @@ class Response private $status; private $message; private $version = '1.0.0'; - private $data; + protected $data; private $debugData; public function __construct($data = null, $status = '200', $message = 'OK', $debugData = null) diff --git a/app/Routes/Routes.php b/app/Routes/Routes.php old mode 100644 new mode 100755 index e70c14c0..58df5d70 --- a/app/Routes/Routes.php +++ b/app/Routes/Routes.php @@ -2,6 +2,8 @@ namespace Notes\Controller\Web; use Notes\Controller\Api\UserTag as UserTag; +use Notes\Controller\Api\Session as Session; + use Notes\Request\Request as Request; $application->get('/:route', function ($route) use ($application) { @@ -13,15 +15,18 @@ $application->get('/login', function () use ($application) { $request = new Request(); - $sessionController = new Session($request); - $sessionController->get(); + $loginController = new Login($request); + $loginController->get(); }); -$application->post('/login', function () use ($application) { +$application->post('/api/session', function () use ($application) { $request = $application->request(); $objRequest = new Request(); - $objRequest->setUrlParams($request->post()); + + $objRequest->setData($request->getBody()); + $objRequest->setHeaders($request->headers); + $objRequest->setCookies($request->cookies); $sessionController = new Session($objRequest); $sessionController->post(); diff --git a/app/Service/Session.php b/app/Service/Session.php index e5fcf804..bef98054 100644 --- a/app/Service/Session.php +++ b/app/Service/Session.php @@ -12,11 +12,11 @@ public function __construct() } - public function login($userInput) + public function login($userModel) { $sessionDomain = new SessionDomain(); - $session = $sessionDomain->create($userInput); + $session = $sessionDomain->create($userModel); return $session; } diff --git a/app/Service/User.php b/app/Service/User.php index 359cb28a..2b43b72f 100755 --- a/app/Service/User.php +++ b/app/Service/User.php @@ -11,11 +11,11 @@ public function __construct() { } - public function create($request) + public function create($input) { $userDomain=new UserDomain(); - $response=$userDomain->create($request); + $response=$userDomain->create($input); return $response; diff --git a/app/View/Create.php b/app/View/Create.php index 8a0c1aad..3b76dc1b 100644 --- a/app/View/Create.php +++ b/app/View/Create.php @@ -3,197 +3,39 @@
- - + + + + + + + + + - - - +