示例#1
0
 /**
  * @Route ("/profile/authtoken/create")
  * @HttpMethod ({"POST"})
  * @Secure ({"USER"})
  *
  * @param array $params
  * @param Request $request
  * @return string
  * @throws \Exception
  */
 public function profileAuthTokenCreate(array $params, Request $request)
 {
     if (!isset($params['g-recaptcha-response']) || empty($params['g-recaptcha-response'])) {
         throw new Exception('You must solve the recaptcha.');
     }
     $googleRecaptchaHandler = new GoogleRecaptchaHandler();
     $googleRecaptchaHandler->resolve(Config::$a['g-recaptcha']['secret'], $params['g-recaptcha-response'], $request->ipAddress());
     $apiAuthService = ApiAuthenticationService::instance();
     $userId = Session::getCredentials()->getUserId();
     $tokens = $apiAuthService->getAuthTokensByUserId($userId);
     if (count($tokens) >= 5) {
         throw new Exception('You have reached the maximum [5] allowed login keys.');
     }
     $log = Application::instance()->getLogger();
     $conn = Application::instance()->getConnection();
     $conn->beginTransaction();
     try {
         $token = $apiAuthService->createAuthToken($userId);
         $apiAuthService->addAuthToken($userId, $token);
         $conn->commit();
     } catch (\Exception $e) {
         $log->critical("Error creating auth token");
         $conn->rollBack();
         throw $e;
     }
     Session::set('modelSuccess', 'Auth token created!');
     return 'redirect: /profile/authentication';
 }
示例#2
0
 /**
  * @Route ("/register")
  * @HttpMethod ({"POST"})
  * @Transactional
  *
  * Handle the confirmation request
  * @param array $params
  * @throws Exception
  */
 public function registerProcess(array $params, ViewModel $model, Request $request)
 {
     $userService = UserService::instance();
     $authService = AuthenticationService::instance();
     $authCreds = $this->getSessionAuthenticationCredentials($params);
     $username = isset($params['username']) && !empty($params['username']) ? $params['username'] : '';
     $email = isset($params['email']) && !empty($params['email']) ? $params['email'] : '';
     $country = isset($params['country']) && !empty($params['country']) ? $params['country'] : '';
     $rememberme = isset($params['rememberme']) && !empty($params['rememberme']) ? true : false;
     $authCreds->setUsername($username);
     $authCreds->setEmail($email);
     try {
         if (!isset($params['g-recaptcha-response']) || empty($params['g-recaptcha-response'])) {
             throw new Exception('You must solve the recaptcha.');
         }
         $googleRecaptchaHandler = new GoogleRecaptchaHandler();
         $googleRecaptchaHandler->resolve(Config::$a['g-recaptcha']['secret'], $params['g-recaptcha-response'], $request->ipAddress());
         $authService->validateUsername($username);
         $authService->validateEmail($email);
         if (!empty($country)) {
             $countryArr = Country::getCountryByCode($country);
             if (empty($countryArr)) {
                 throw new Exception('Invalid country');
             }
             $country = $countryArr['alpha-2'];
         }
         $user = array();
         $user['username'] = $username;
         $user['email'] = $email;
         $user['userStatus'] = 'Active';
         $user['country'] = $country;
         $user['userId'] = $userService->addUser($user);
         $userService->addUserAuthProfile(array('userId' => $user['userId'], 'authProvider' => $authCreds->getAuthProvider(), 'authId' => $authCreds->getAuthId(), 'authCode' => $authCreds->getAuthCode(), 'authDetail' => $authCreds->getAuthDetail()));
         Session::set('authSession');
         $authCredHandler = new AuthenticationRedirectionFilter();
         return $authCredHandler->execute($authCreds);
     } catch (Exception $e) {
         $model->title = 'Register Error';
         $model->username = $username;
         $model->email = $email;
         $model->follow = isset($params['follow']) ? $params['follow'] : '';
         $model->authProvider = $authCreds->getAuthProvider();
         $model->code = $authCreds->getAuthCode();
         $model->error = $e;
         return 'register';
     }
 }