<commit_before><?php

namespace AbaLookup;

use
	Lookup\Entity\UserType,
	AbaLookup\Form\LoginForm,
	AbaLookup\Form\ProfileEditForm,
	AbaLookup\Form\RegisterForm,
	InvalidArgumentException,
	Zend\View\Model\ViewModel
;

/**
 * Handles all actions involving users
 */
class UsersController extends AbaLookupController
{
	/**
	 * Registers the user or shows a registration form
	 *
	 * Shows the registration form, validates the form data, creates a user from
	 * the submitted data, and then adds the user into the database.
	 *
	 * @return array|Zend\Http\Response
	 */
	public function registerAction()
	{
		// If a user is already logged in
		$user = $this->currentSessionUser();
		if (isset($user)) {
			// Redirect the user to their profile page
			$params = [
				'id' => $user->getId(),
				'action' => 'profile',
			];
			return $this->redirect()->toRoute('users', $params);
		}
		// Prepare the view layout
		$this->prepareLayout($this->layout());
		// Get the user type from the URL
		// Create a registration form
		$type = $this->params('type');
		$form = new RegisterForm($type);
		// If the user has not submitted a POST request
		if (!$this->request->isPost()) {
			// The user has not submitted the form
			return [
				'form' => $form,
				'type' => $type,
			];
		}
		// The user has submitted via POST
		// Pass the data along to the form
		// Terms of Service
		// Show previous data to user
		$data = $this->params();
		try {
			$id = $this->getApi('UserAccount')->put(
				$data->fromPost($form::ELEMENT_NAME_EMAIL_ADDRESS),
				$data->fromPost($form::ELEMENT_NAME_PASSWORD),
				$data->fromPost($form::ELEMENT_NAME_DISPLAY_NAME),
				$data->fromPost($form::ELEMENT_NAME_USER_TYPE),
				$data->fromPost($form::ELEMENT_NAME_POSTAL_CODE),
				$data->fromPost()
			);
		} catch (Lookup\Api\Exception\InvalidDataException $s) {
			return [
				'error' => $form->getMessage(),
				'form'  => $form,
				'type'  => $type,
			];
		}
		$this->setUserSession($id);
		// Redirect the user to their profile page
		$params = [
			'id'     => $id,
			'action' => 'profile',
		];
		return $this->redirect()->toRoute('users', $params);
	}

	/**
	 * Logs the user in
	 *
	 * Verify the user credentials and if valid login user in.
	 *
	 * @return array|Zend\Http\Response
	 */
	public function loginAction()
	{
		// If a user is already logged in
		$user = $this->currentSessionUser();
		if (isset($user)) {
			// Show the user their profile page
			$params = [
				'id'     => $user->getId(),
				'action' => 'profile',
			];
			return $this->redirect()->toRoute('users', $params);
		}
		// Prepare the view layout
		$this->prepareLayout($this->layout());
		// Create a login form
		$form = new LoginForm();
		// If the user has not submitted a POST request
		if (!$this->request->isPost()) {
			// Show the login form
			return [
				'form' => $form,
			];
		}
		// The user has submitted via POST
		// Pass the data along to the form
		$form->setData($this->request->getPost());
		if (!$form->isValid()) {
			// Show the user the error message
			return [
				'form' => $form,
				'error' => $form->getMessage(),
			];
		}
		// Retrieve the validated values
		$emailAddress = $form->getEmailAddress();
		$password     = $form->getPassword();
		$user         = $this->getUserByEmail($emailAddress);
		// If the given email address yields no user
		// Or the user entered the wrong password
		if (!$user || !$user->verifyPassword($password)) {
			// Show the user a error message
			return [
				'form' => $form,
				'error' => 'The entered credentials are not valid.',
			];
		}
		// Create a session for the user
		$this->setUserSession($user, $form->rememberMe());
		$params = [
			'id'     => $user->getId(),
			'action' => 'profile',
		];
		return $this->redirect()->toRoute('users', $params);
	}

	/**
	 * Logs the user out
	 *
	 * If a user is logged in, log them out. Invalidates the session.
	 * Reroutes the user to the home page.
	 *
	 * @return Zend\Http\Response
	 */
	public function logoutAction()
	{
		if ($this->isUserInSession()) {
			$this->unsetUserSession();
		}
		return $this->redirect()->toRoute('home');
	}

	/**
	 * Displays the user's profile
	 *
	 * Edits to the user's profile arrive via POST with
	 * a parameter of 'mode' => 'edit'.
	 *
	 * @return array|Zend\Http\Response<commit_msg>Updated login action to use API.<commit_after><?php

namespace AbaLookup;

use
	AbaLookup\Form\LoginForm,
	AbaLookup\Form\ProfileEditForm,
	AbaLookup\Form\RegisterForm,
	Zend\View\Model\ViewModel
;

/**
 * Handles all actions involving users
 */
class UsersController extends AbaLookupController
{
	/**
	 * Registers the user or shows a registration form
	 *
	 * Shows the registration form, validates the form data, creates a user from
	 * the submitted data, and then adds the user into the database.
	 *
	 * @return array|Zend\Http\Response
	 */
	public function registerAction()
	{
		// If a user is already logged in
		$user = $this->currentSessionUser();
		if (isset($user)) {
			// Redirect the user to their profile page
			$params = [
				'id' => $user->getId(),
				'action' => 'profile',
			];
			return $this->redirect()->toRoute('users', $params);
		}
		// Prepare the view layout
		$this->prepareLayout($this->layout());
		// Get the user type from the URL
		// Create a registration form
		$type = $this->params('type');
		$form = new RegisterForm($type);
		// If the user has not submitted a POST request
		if (!$this->request->isPost()) {
			// The user has not submitted the form
			return [
				'form' => $form,
				'type' => $type,
			];
		}
		// The user has submitted via POST
		// TODO - Validate Terms of Service
		// TODO - Show previous data to user
		$data = $this->params();
		try {
			$id = $this->getApi('UserAccount')->put(
				$data->fromPost($form::ELEMENT_NAME_EMAIL_ADDRESS),
				$data->fromPost($form::ELEMENT_NAME_PASSWORD),
				$data->fromPost($form::ELEMENT_NAME_DISPLAY_NAME),
				$data->fromPost($form::ELEMENT_NAME_USER_TYPE),
				$data->fromPost($form::ELEMENT_NAME_POSTAL_CODE),
				$data->fromPost()
			);
		} catch (Lookup\Api\Exception\InvalidDataException $e) {
			return [
				'error' => $e->getMessage(),
				'form'  => $form,
				'type'  => $type,
			];
		}
		$this->setUserSession($id);
		// Redirect the user to their profile page
		$params = [
			'id'     => $id,
			'action' => 'profile',
		];
		return $this->redirect()->toRoute('users', $params);
	}

	/**
	 * Logs the user in
	 *
	 * Verify the user credentials and if valid login user in.
	 *
	 * @return array|Zend\Http\Response
	 */
	public function loginAction()
	{
		// If a user is already logged in
		$user = $this->currentSessionUser();
		if (isset($user)) {
			// Show the user their profile page
			$params = [
				'id'     => $user->getId(),
				'action' => 'profile',
			];
			return $this->redirect()->toRoute('users', $params);
		}
		// Prepare the view layout
		$this->prepareLayout($this->layout());
		// Create a login form
		$form = new LoginForm();
		// If the user has not submitted a POST request
		if (!$this->request->isPost()) {
			// Show the login form
			return [
				'form' => $form,
			];
		}
		// The user has submitted via POST
		$data = $this->request->getPost();
		try {
			$user = $this->getApi('User')->get(
				$data->fromPost($form::ELEMENT_NAME_EMAIL_ADDRESS),
				$data->fromPost($form::ELEMENT_NAME_PASSWORD),
				$data->fromPost()
			);
		} catch (Lookup\Api\Exception\InvalidDataException $e) {
			return [
				'error' => $e->getMessage(),
				'form'  => $form,
			];
		}
		// Create a session for the user
		$id = $user->getId();
		$this->setUserSession($id, $form->rememberMe());
		$params = [
			'id'     => $id,
			'action' => 'profile',
		];
		return $this->redirect()->toRoute('users', $params);
	}

	/**
	 * Logs the user out
	 *
	 * If a user is logged in, log them out. Invalidates the session.
	 * Reroutes the user to the home page.
	 *
	 * @return Zend\Http\Response
	 */
	public function logoutAction()
	{
		if ($this->isUserInSession()) {
			$this->unsetUserSession();
		}
		return $this->redirect()->toRoute('home');
	}

	/**
	 * Displays the user's profile
	 *
	 * Edits to the user's profile arrive via POST with
	 * a parameter of 'mode' => 'edit'.
	 *
	 * @return array|Zend\Http\Response
{'fraction_non_alphanumeric': 0.1606474211861336, 'fraction_numerical': 0.0, 'mean_word_length': 3.0969816922315685, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 134, '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': '4223781', 'n_tokens_mistral': 3068, 'n_tokens_neox': 2666, 'n_words': 745}