<commit_before><?php

require_once("include/auth/secure-token.php");
require_once("include/ldap.php");
/**
 * A class for doing LDAP authentication using the secure token system
 * implements SecureTokenAuth
 * @author <NAME> <<EMAIL>>
 */
class LDAPAuth extends SecureTokenAuth
{
	private $ldapManager;

	public function __construct()
	{
		parent::__construct();
	}

	public function checkAuthentication($username, $password)
	{
		// empty usernames aren't valid in LDAP
		if (empty($username))
		{
			return false;
		}
		$config = Configuration::getInstance();
		$this->ldapManager = new LDAPManager($config->getConfig("ldap.host"), $username, $password);
		return $this->ldapManager->getAuthed();
	}

	public function getTeams($username)
	{
		ide_log(LOG_INFO, "Getting teams for '$username'.");
		$config = Configuration::getInstance();
		$ldapManager = new LDAPManager($config->getConfig("ldap.host"), "ide", $config->getConfig("ldap.ideuser.password"));
		$groupNamePrefix = $config->getConfig("ldap.team.prefix");
		$groups = $ldapManager->getGroupsForUser($username, $groupNamePrefix.'*');
		$teams = array();

		ide_log(LOG_INFO, "Using prefix '$groupNamePrefix'.");
		foreach ($groups as $group)
		{
			ide_log(LOG_DEBUG, "Got group '$group'.");
			$teams[] = substr($group, strlen($groupNamePrefix));
		}
		ide_log(LOG_INFO, "Got teams: ". print_r($teams, true));

		return $teams;
	}

	/**
	 * Returns whether or not the user is in the requested group.
	 * @param group: The group to search for.
	 */
	private function inGroup($group)
	{
		$config = Configuration::getInstance();
		$user = $this->ldapManager->getUser();
		$IDEldapManager = new LDAPManager($config->getConfig("ldap.host"), "ide", $config->getConfig("ldap.ideuser.password"));
		$groups = $IDEldapManager->getGroupsForUser($user, $group);
		// should either be 0 or 1 responses...
		$inGroup = count($groups) > 0;
		return $inGroup;
	}

	public function isCurrentUserAdmin()
	{
		$config = Configuration::getInstance();
		$adminName = $config->getConfig("ldap.admin_group");
		$isAdmin = $this->inGroup($adminName);
		return $isAdmin;
	}

	public function displayNameForUser($user)
	{
		if ($this->ldapManager->getAuthed())
		{
			$info = $this->ldapManager->getUserInfo($user);
			return $info["name.first"] . " " . $info["name.last"];
		}
		else
		{
			throw new Exception("you aren't authed to ldap", E_LDAP_NOT_AUTHED);
		}
	}
<commit_msg>Modify internal signature to be more flexible.<commit_after><?php

require_once("include/auth/secure-token.php");
require_once("include/ldap.php");
/**
 * A class for doing LDAP authentication using the secure token system
 * implements SecureTokenAuth
 * @author <NAME> <<EMAIL>>
 */
class LDAPAuth extends SecureTokenAuth
{
	private $ldapManager;

	public function __construct()
	{
		parent::__construct();
	}

	public function checkAuthentication($username, $password)
	{
		// empty usernames aren't valid in LDAP
		if (empty($username))
		{
			return false;
		}
		$config = Configuration::getInstance();
		$this->ldapManager = new LDAPManager($config->getConfig("ldap.host"), $username, $password);
		return $this->ldapManager->getAuthed();
	}

	public function getTeams($username)
	{
		ide_log(LOG_INFO, "Getting teams for '$username'.");
		$config = Configuration::getInstance();
		$ldapManager = new LDAPManager($config->getConfig("ldap.host"), "ide", $config->getConfig("ldap.ideuser.password"));
		$groupNamePrefix = $config->getConfig("ldap.team.prefix");
		$groups = $ldapManager->getGroupsForUser($username, $groupNamePrefix.'*');
		$teams = array();

		ide_log(LOG_INFO, "Using prefix '$groupNamePrefix'.");
		foreach ($groups as $group)
		{
			ide_log(LOG_DEBUG, "Got group '$group'.");
			$teams[] = substr($group, strlen($groupNamePrefix));
		}
		ide_log(LOG_INFO, "Got teams: ". print_r($teams, true));

		return $teams;
	}

	/**
	 * Returns whether or not the user is in the requested group.
	 * @param user: The user to check membership for.
	 * @param group: The group to search for.
	 */
	private function inGroup($user, $group)
	{
		$config = Configuration::getInstance();
		$IDEldapManager = new LDAPManager($config->getConfig("ldap.host"), "ide", $config->getConfig("ldap.ideuser.password"));
		$groups = $IDEldapManager->getGroupsForUser($user, $group);
		// should either be 0 or 1 responses...
		$inGroup = count($groups) > 0;
		return $inGroup;
	}

	public function isCurrentUserAdmin()
	{
		$config = Configuration::getInstance();
		$adminName = $config->getConfig("ldap.admin_group");
		$user = $this->ldapManager->getUser();
		$isAdmin = $this->inGroup($user, $adminName);
		return $isAdmin;
	}

	public function displayNameForUser($user)
	{
		if ($this->ldapManager->getAuthed())
		{
			$info = $this->ldapManager->getUserInfo($user);
			return $info["name.first"] . " " . $info["name.last"];
		}
		else
		{
			throw new Exception("you aren't authed to ldap", E_LDAP_NOT_AUTHED);
		}
	}

{'fraction_non_alphanumeric': 0.1645748987854251, 'fraction_numerical': 0.0012145748987854252, 'mean_word_length': 4.812941176470588, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, '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': '1266611', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1598, 'n_words': 358}