Esempio n. 1
0
 /**
  * Creates a native user in Omegaup and returns the DAO populated
  *
  * @param string $username optional
  * @param string $password optional
  * @param string $email optional
  * @return user (DAO)
  */
 public static function createUser($username = null, $password = null, $email = null, $verify = true)
 {
     // If data is not provided, generate it randomly
     if (is_null($username)) {
         $username = Utils::CreateRandomString();
     }
     if (is_null($password)) {
         $password = Utils::CreateRandomString();
     }
     if (is_null($email)) {
         $email = Utils::CreateRandomString() . '@mail.com';
     }
     // Populate a new Request to pass to the API
     UserController::$permissionKey = uniqid();
     $r = new Request(array('username' => $username, 'name' => $username, 'password' => $password, 'email' => $email, 'permission_key' => UserController::$permissionKey));
     // Call the API
     $response = UserController::apiCreate($r);
     // If status is not OK
     if (strcasecmp($response['status'], 'ok') !== 0) {
         throw new Exception('UserFactory::createUser failed');
     }
     // Get user from db
     $user = UsersDAO::FindByUsername($username);
     if ($verify) {
         UserController::$redirectOnVerify = false;
         $user = self::verifyUser($user);
     } else {
         $user->verified = 0;
         UsersDAO::save($user);
     }
     // Password came hashed from DB. Set password in plaintext
     $user->setPassword($password);
     return $user;
 }
Esempio n. 2
0
 /**
  * Verifies the user given its verification id
  *
  * @param Request $r
  * @return type
  * @throws ApiException
  * @throws InvalidDatabaseOperationException
  * @throws NotFoundException
  */
 public static function apiVerifyEmail(Request $r)
 {
     $user = null;
     // Admin can override verification by sending username
     if (isset($r['usernameOrEmail'])) {
         self::authenticateRequest($r);
         if (!Authorization::IsSystemAdmin($r['current_user_id'])) {
             throw new ForbiddenAccessException();
         }
         self::$log->info('Admin verifiying user...' . $r['usernameOrEmail']);
         Validators::isStringNonEmpty($r['usernameOrEmail'], 'usernameOrEmail');
         $user = self::resolveUser($r['usernameOrEmail']);
         self::$redirectOnVerify = false;
     } else {
         // Normal user verification path
         Validators::isStringNonEmpty($r['id'], 'id');
         try {
             $users = UsersDAO::search(new Users(array('verification_id' => $r['id'])));
             $user = is_array($users) && count($users) > 0 ? $users[0] : null;
         } catch (Exception $e) {
             throw new InvalidDatabaseOperationException($e);
         }
     }
     if (is_null($user)) {
         throw new NotFoundException('verificationIdInvalid');
     }
     try {
         $user->setVerified(1);
         UsersDAO::save($user);
     } catch (Exception $e) {
         throw new InvalidDatabaseOperationException($e);
     }
     self::$log->info('User verification complete.');
     self::registerToSendy($user);
     if (self::$redirectOnVerify) {
         die(header('Location: /login/'));
     }
     return array('status' => 'ok');
 }