diff --git a/api/controllers/user.php b/api/controllers/user.php index 66fdf2f4..5aa9cfeb 100644 --- a/api/controllers/user.php +++ b/api/controllers/user.php @@ -1,40 +1,11 @@ group('/user', function () use ($app) { +include 'user/login.php'; +include 'user/signup.php'; - $app->get('/get/(:by)/(:value)', function () use ($app) { - echo "Returns the user with $by = $value as a json"; - }); +$userControllers = new ControllerGroup(); +$userControllers->setGroupPath('/user'); - //TODO: THIS METHOD CAN BE ONLY USED IF IT IS LOGIN AS ADMIN - $app->get('/add/:email/:pass', function ($email, $pass) use ($app) { - $userInstance = new User(); - $userInstance->setProperties(array( - 'email' => $email, - 'password' => $pass, - 'admin' => 0 - )); - $id = $userInstance->store(); - Response::respondSuccess(array( - 'id' => $id - )); - }); +$userControllers->addController(new LoginController); +$userControllers->addController(new SignUpController); - $app->post('/login', function () use ($app) { - $email = Controller::request('email'); - $password = Controller::request('password'); - - if ($userInstance = User::getUser($email, 'email')) { - $pass = $userInstance->password; - } - else { - Response::respondError(ERRORS::INVALID_CREDENTIALS); - } - - if ($userInstance->password === $password) { - Response::respondSuccess(); - } - else { - Response::respondError(ERRORS::INVALID_CREDENTIALS); - } - }); -}); \ No newline at end of file +$userControllers->finalize(); diff --git a/api/controllers/user/login.php b/api/controllers/user/login.php new file mode 100644 index 00000000..9e2c51ee --- /dev/null +++ b/api/controllers/user/login.php @@ -0,0 +1,20 @@ +password !== $password) { + Response::respondError(ERRORS::INVALID_CREDENTIALS); + return; + } + + Response::respondSuccess(); + return; + } +} diff --git a/api/controllers/user/signup.php b/api/controllers/user/signup.php new file mode 100644 index 00000000..b3629915 --- /dev/null +++ b/api/controllers/user/signup.php @@ -0,0 +1,22 @@ +setProperties(array( + 'email' => $email, + 'password' => $password, + 'admin' => 0 + )); + $id = $userInstance->store(); + + Response::respondSuccess(array( + 'id' => $id + )); + } +} diff --git a/api/index.php b/api/index.php index 39e84480..ba5ea782 100644 --- a/api/index.php +++ b/api/index.php @@ -20,7 +20,9 @@ spl_autoload_register(function ($class) { }); // LOAD CONTROLLERS -include 'libs/Controller.php'; +include_once 'libs/Controller.php'; +include_once 'libs/ControllerGroup.php'; + foreach (glob('controllers/*.php') as $controller) { include $controller; } diff --git a/api/libs/Controller.php b/api/libs/Controller.php index ab6834c7..11770fc7 100644 --- a/api/libs/Controller.php +++ b/api/libs/Controller.php @@ -1,8 +1,20 @@ handler(); + }; + } + public static function request($key) { - $app = \Slim\Slim::getInstance(); + $app = self::getAppInstance(); return $app->request()->post($key); } @@ -27,4 +39,8 @@ class Controller { public static function checkAdminLogged() { return self::checkUserLogged() && (self::getLoggedUser()->admin === 2); } + + public static function getAppInstance() { + return \Slim\Slim::getInstance(); + } } \ No newline at end of file diff --git a/api/libs/ControllerGroup.php b/api/libs/ControllerGroup.php new file mode 100644 index 00000000..b91c8a72 --- /dev/null +++ b/api/libs/ControllerGroup.php @@ -0,0 +1,24 @@ +groupPath = $groupPath; + } + + public function addController($controller) { + array_push($this->controllers, $controller); + } + + public function finalize() { + $app = Controller::getAppInstance(); + $controllers = $this->controllers; + + $app->group($this->groupPath, function () use ($app, $controllers) { + foreach ($controllers as $controller) { + $app->post($controller::PATH, $controller->getHandler()); + } + }); + } +}