public function actionAuthenticateUser()
 {
     $params = array('username', 'password');
     try {
         $isRequestValid = $this->_validator->validateRequest($params);
         if ($isRequestValid) {
             $username = $this->_request->getParam('username', null);
             $password = $this->_request->getParam('password', null);
             $objDbUserauth = new \models\Users();
             // check if user is authenticated or not
             $isUserAuthenticatedStatus = $objDbUserauth->authenticate($username, $password);
             if ($isUserAuthenticatedStatus === true) {
                 $userRow = \R::findOne('users', "user_name = :un OR email = :un", array(':un' => $username));
                 $this->_response->renderJson(array('message' => 'User successfully authenticated', 'status' => 1, 'userdata' => array('cimba_auth_id' => $userRow->id, 'email' => $userRow->email, 'firstname' => $userRow->first_name, 'lastname' => $userRow->last_name, 'username' => $userRow->user_name)));
                 exit;
             } elseif ($isUserAuthenticatedStatus === \models\Users::ERROR_USER_PWD_EXPIRED) {
                 $this->_response->renderJson(array('message' => 'Invalid Credentials', 'status' => 0, 'status_code' => 'password_expired'));
             } elseif ($isUserAuthenticatedStatus === \models\Users::IDENTITY_NOT_FOUND) {
                 $this->_response->renderJson(array('message' => 'Invalid Credentials', 'status' => 0, 'status_code' => 'false'));
             } elseif ($isUserAuthenticatedStatus === \models\Users::ERROR_USER_LOCKED) {
                 $this->_response->renderJson(array('message' => 'Invalid Credentials', 'status' => 0, 'status_code' => 'user_locked'));
             } else {
                 $this->_response->renderJson(array('message' => 'Invalid Credentials', 'status' => 0, 'status_code' => 'failure'));
             }
         } else {
             $this->_response->renderJson(array('message' => 'Request cannot be validated'), 400);
         }
     } catch (\Exception $e) {
         $this->_response->renderJson(array('message' => $e->getMessage()), 500);
     }
 }
 public function actionSignup()
 {
     $objValidator = new helpers\Validation();
     $params = array('first_name', 'last_name', 'email', 'city', 'country', 'password', 'longitude', 'latitude', 'gender', 'phone', 'address', 'devicetoken', 'devicetype');
     try {
         $isRequestValid = $objValidator->validateRequest($params);
         if ($isRequestValid) {
             $email = $this->_request->getPost('email', null);
             $password = $this->_request->getPost('password', null);
             $objUserAuthMdl = new \models\Users();
             $dataToInsert = array('first_name' => $this->_request->getPost('first_name', null), 'last_name' => $this->_request->getPost('last_name', null), 'email' => $email, 'password' => md5($password), 'city' => $this->_request->getPost('city', null), 'country' => $this->_request->getPost('country', null), 'longitude' => $this->_request->getPost('longitude', null), 'latitude' => $this->_request->getPost('latitude', null), 'gender' => $this->_request->getPost('gender', null), 'phone' => $this->_request->getPost('phone', null), 'address' => $this->_request->getPost('address', null), 'websiteurl' => $this->_request->getPost('websiteurl', null), 'device_token' => $this->_request->getPost('devicetoken', null), 'device_type' => $this->_request->getPost('devicetype', null));
             // check if user is valid or not
             $return = $objUserAuthMdl->insertUser($dataToInsert);
             if ($return && $return > 0) {
                 $profilePic = $this->_request->getPost('profilepic', null);
                 $isProfilePicPosted = $profilePic !== null ? true : false;
                 $isProfilePicSaved = true;
                 if ($isProfilePicPosted) {
                     // handle profile picture
                     $userId = $return;
                     $objImageProcessor = new helpers\ImageProcessor();
                     $dpPath = __DIR__ . '/../images/' . $userId . '_pp.jpg';
                     $isProfilePicSaved = $objImageProcessor->convertBase64ToImage($profilePic, $dpPath);
                 }
                 if ($isProfilePicSaved) {
                     // authenticate user and return token
                     $userAuthRow = $objUserAuthMdl->authenticate($email, $password);
                     $this->_request->sendSuccessResponse('User successfully registered', array('token' => $userAuthRow->token));
                 } else {
                     $this->_request->sendErrorResponse(404, 404, 'User registered but profile picture not saved');
                 }
             } else {
                 if ($return == -1) {
                     $this->_request->sendErrorResponse(404, 404, 'Email alredy exists');
                 } else {
                     if ($return == -2) {
                         $this->_request->sendErrorResponse(404, 404, 'Invalid email address');
                     } else {
                         $this->_request->sendErrorResponse(404, 404, 'Error registering user please try later');
                     }
                 }
             }
         } else {
             $this->_request->sendErrorResponse(403, 403, 'Request cannot be validated');
         }
     } catch (\Exception $e) {
         echo $e->getMessage();
         $this->_request->sendErrorResponse(404, 404, $e->getMessage());
     }
 }