<commit_before>package net.devrieze.chatterbox.server;

import static net.devrieze.util.db.DBHelper.*;

import java.security.Principal;
import java.sql.SQLException;

import javax.servlet.ServletRequest;
import javax.sql.DataSource;

import net.devrieze.util.db.DBHelper;

public class UserManager {

  static final String RESOURCE_REF = "java:/comp/env/jdbc/webauth";

  private static final String APPNAME = "chatterbox";

  private static final String COL_APP = "`app`";

  private static final String COL_USER = "`user`";

  private static final String TABLE_PERMS = "`app_perms`";

  private static final String SQL_ADD_APP_PERM = "INSERT IGNORE INTO "+TABLE_PERMS+" SET "+COL_USER+" = ?, "+COL_APP+" = ?";
  private static final int SQL_I_ADD_APP_PERM_COL_USER = 1;
  private static final int SQL_I_ADD_APP_PERM_COL_APPNAME = 2;

  private static final String SQL_CHECK_APP_PERM = "SELECT "+COL_USER+" FROM "+TABLE_PERMS+" WHERE "+COL_USER+"=? AND "+COL_APP+" = ?";
  private static final int SQL_I_CHECK_APP_PERM_COL_USER = 1;
  private static final int SQL_I_CHECK_APP_PERM_COL_APPNAME = 2;

  static DataSource aDataSource;

  public static void addAllowedUser(Principal pPrincipal, ServletRequest pKey) throws SQLException {
    DBHelper dbHelper = getDbHelper(RESOURCE_REF, pKey);
    try {
      dbHelper.makeInsert(SQL_ADD_APP_PERM, "Failure to register access permission in database")
          .addParam(SQL_I_ADD_APP_PERM_COL_USER, pPrincipal.getName())
          .addParam(SQL_I_ADD_APP_PERM_COL_APPNAME, APPNAME)
          .execCommit();
    } finally {
      dbHelper.close();
    }
  }

  public static boolean isAllowedUser(Principal pPrincipal, ServletRequest pKey) throws SQLException {
    DBHelper dbHelper = getDbHelper(RESOURCE_REF, pKey);
    try {
      return dbHelper.makeQuery(SQL_CHECK_APP_PERM, "Failure to verify access permission in database")
          .addParam(SQL_I_CHECK_APP_PERM_COL_USER, pPrincipal.getName())
          .addParam(SQL_I_CHECK_APP_PERM_COL_APPNAME, APPNAME)
          .execQueryNotEmpty();
    } finally {
      dbHelper.close();
    }
  }

  public static String createLogoutURL(String pURI) {
    return "/accounts/logout";
  }

  public static String getCurrentUserEmail(Principal p) {
    return <EMAIL>";
  }

  public static void destroy() {
    DBHelper.closeAllConnections(RESOURCE_REF);
  }

}<commit_msg>Get sender email from the DarwinPrincipal if the principal is a DarwinPrincipal.<commit_after>package net.devrieze.chatterbox.server;

import static net.devrieze.util.db.DBHelper.*;

import java.security.Principal;
import java.sql.SQLException;

import javax.servlet.ServletRequest;
import javax.sql.DataSource;

import uk.ac.bournemouth.darwin.catalina.realm.DarwinUserPrincipal;
import net.devrieze.util.db.DBHelper;

public class UserManager {

  static final String RESOURCE_REF = "java:/comp/env/jdbc/webauth";

  private static final String APPNAME = "chatterbox";

  private static final String COL_APP = "`app`";

  private static final String COL_USER = "`user`";

  private static final String TABLE_PERMS = "`app_perms`";

  private static final String SQL_ADD_APP_PERM = "INSERT IGNORE INTO "+TABLE_PERMS+" SET "+COL_USER+" = ?, "+COL_APP+" = ?";
  private static final int SQL_I_ADD_APP_PERM_COL_USER = 1;
  private static final int SQL_I_ADD_APP_PERM_COL_APPNAME = 2;

  private static final String SQL_CHECK_APP_PERM = "SELECT "+COL_USER+" FROM "+TABLE_PERMS+" WHERE "+COL_USER+"=? AND "+COL_APP+" = ?";
  private static final int SQL_I_CHECK_APP_PERM_COL_USER = 1;
  private static final int SQL_I_CHECK_APP_PERM_COL_APPNAME = 2;

  static DataSource aDataSource;

  public static void addAllowedUser(Principal pPrincipal, ServletRequest pKey) throws SQLException {
    DBHelper dbHelper = getDbHelper(RESOURCE_REF, pKey);
    try {
      dbHelper.makeInsert(SQL_ADD_APP_PERM, "Failure to register access permission in database")
          .addParam(SQL_I_ADD_APP_PERM_COL_USER, pPrincipal.getName())
          .addParam(SQL_I_ADD_APP_PERM_COL_APPNAME, APPNAME)
          .execCommit();
    } finally {
      dbHelper.close();
    }
  }

  public static boolean isAllowedUser(Principal pPrincipal, ServletRequest pKey) throws SQLException {
    DBHelper dbHelper = getDbHelper(RESOURCE_REF, pKey);
    try {
      return dbHelper.makeQuery(SQL_CHECK_APP_PERM, "Failure to verify access permission in database")
          .addParam(SQL_I_CHECK_APP_PERM_COL_USER, pPrincipal.getName())
          .addParam(SQL_I_CHECK_APP_PERM_COL_APPNAME, APPNAME)
          .execQueryNotEmpty();
    } finally {
      dbHelper.close();
    }
  }

  public static String createLogoutURL(String pURI) {
    return "/accounts/logout";
  }

  public static String getCurrentUserEmail(Principal p) {
    if (p instanceof DarwinUserPrincipal) {
      return ((DarwinUserPrincipal) p).getEmail().toString();
    } 
    return p.getName()+"@<EMAIL>";
  }

  public static void destroy() {
    DBHelper.closeAllConnections(RESOURCE_REF);
  }

}
{'fraction_non_alphanumeric': 0.0940870954854175, 'fraction_numerical': 0.0015980823012385138, 'mean_word_length': 4.337953091684435, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3949743', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1575, 'n_words': 374}