<commit_before>package com.renomad.qarma;

import java.io.IOException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.renomad.qarma.Database_access;
import com.renomad.qarma.Utilities;

public class Security {

  public static int check_login(String username, String password) {
    return Database_access.check_login(username, password);
  }

  public static void register_user(int user_id,
      String ip_address) {
    Database_access.register_details_on_user_login(user_id, ip_address);
  }

  /**
    * tries logging out the user.  If successful, return true
    * @param the user id in question
    * @returns true if successful
    */
  public static boolean logout_user(int user_id) {
    return Database_access.set_user_not_logged_in(user_id);
  }

  private static int get_int_from_cookie(Cookie c) {
    return Utilities.parse_int(c.getValue());
  }


  /**
    * we just look up the user.  If they are logged in, we
    * return the user id.  if failed, return -1;
    * @param the request object
    * @returns a valid user id if allowd.  -1 otherwise.
    */
  public static int check_if_allowed(HttpServletRequest r) {
    Cookie[] cookies = r.getCookies();
    Cookie c = find_our_cookie(cookies);
    int user_id = get_int_from_cookie(c);
    if (user_id == Integer.MIN_VALUE) { return -1; }
    boolean is_logged_in = Database_access.user_is_logged_in(user_id);
    if (is_logged_in) {
      return user_id;
    }
    return -1; //-1 means not allowed or failure.
  }

  public static Cookie find_our_cookie(Cookie[] all_cookies) {
    if (all_cookies == null) {
      return null;
    }
    for (Cookie cookie : all_cookies) {
      if (cookie.getName().equals("qarma_cookie")) {
        return cookie;
      }
    }
    return null;
  }<commit_msg>correcting a defect.  When there is no cookie, we should not try parsing the value.  Just return -1 (no user)<commit_after>package com.renomad.qarma;

import java.io.IOException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.renomad.qarma.Database_access;
import com.renomad.qarma.Utilities;

public class Security {

  public static int check_login(String username, String password) {
    return Database_access.check_login(username, password);
  }

  public static void register_user(int user_id,
      String ip_address) {
    Database_access.register_details_on_user_login(user_id, ip_address);
  }

  /**
    * tries logging out the user.  If successful, return true
    * @param the user id in question
    * @returns true if successful
    */
  public static boolean logout_user(int user_id) {
    return Database_access.set_user_not_logged_in(user_id);
  }

  private static int get_int_from_cookie(Cookie c) {
    return Utilities.parse_int(c.getValue());
  }


  /**
    * we just look up the user.  If they are logged in, we
    * return the user id.  if failed, return -1;
    * @param the request object
    * @returns a valid user id if allowd.  -1 otherwise.
    */
  public static int check_if_allowed(HttpServletRequest r) {
    Cookie[] cookies = r.getCookies();
    Cookie c = find_our_cookie(cookies);
    if (c == null) {
      return -1;
    }
    int user_id = get_int_from_cookie(c);
    if (user_id == Integer.MIN_VALUE) { return -1; }
    boolean is_logged_in = Database_access.user_is_logged_in(user_id);
    if (is_logged_in) {
      return user_id;
    }
    return -1; //-1 means not allowed or failure.
  }

  public static Cookie find_our_cookie(Cookie[] all_cookies) {
    if (all_cookies == null) {
      return null;
    }
    for (Cookie cookie : all_cookies) {
      if (cookie.getName().equals("qarma_cookie")) {
        return cookie;
      }
    }
    return null;
  }
{'fraction_non_alphanumeric': 0.09533678756476684, 'fraction_numerical': 0.0031088082901554403, 'mean_word_length': 3.299554565701559, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1669611', 'n_tokens_mistral': 1254, 'n_tokens_neox': 1230, 'n_words': 336}