<commit_before><?php
namespace Auth\Controller;

use Application\Model\DynamicHashTable;
use Application\Service\MessageService;
use Auth\Form\EmailForm;
use Auth\Form\PWForgetForm;
use Auth\Model\AuthStorage;
use Auth\Model\UserTable;
use Auth\Utility\UserPassword;
use Exception;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Result;
use Zend\Mvc\Controller\AbstractActionController;
use Auth\Form\LoginForm;
use Auth\Form\UserForm;
use Auth\Model\User;
use Application\Service\TemplateTypes;

class AuthController extends AbstractActionController
{
    protected $form;

    /** @var AuthStorage */
    protected $storage;
    /** @var AuthenticationService  */
    protected $authService;
    /** @var UserTable  */
    protected $userTable;
    /** @var DynamicHashTable  */
    protected $dynamicHashTable;
    /** @var  MessageService */
    protected $msgService;

    function __construct(AuthStorage $storage, AuthenticationService $authService, UserTable $userTable, DynamicHashTable $dynamicHashTable, MessageService $msgService )
    {
        $this->storage = $storage;
        $this->authService = $authService;
        $this->userTable = $userTable;
        $this->dynamicHashTable = $dynamicHashTable;
        $this->msgService = $msgService;
    }

    private function getUserDetails($mail)
    {
        $users = $this->userTable->getUsers(array(
                        'email' => $mail
                    ), array(
                        'id' => 'id',
                        'email',
                        'name'
                    ));
        return $users;
    }

    private function login($ip, $email, $clearPass) {
        $valid = true;
        $result = new Result(500, null, ['Unexpected Server Error']);
        //check if user exists
        $userDetails = $this->userTable->getUsersForAuth($email);
        // email not found
        if (!$userDetails){
            $valid = false;
        }
        // email found but inactive
        elseif ($userDetails->status != 1) {
            $valid = false;
            $result = new Result(401, null, ['Zugang Verweigert::Dieser Account ist nicht Aktiviert']);
        }
        if ($valid) {
            $userPassword = new UserPassword();
            $encyptPass = $userPassword->create($clearPass);

            $this->authService->getAdapter()
                ->setIdentity($email)
                ->setCredential($encyptPass);

            $result = $this->authService->authenticate();
        }

        if ($valid && $result->isValid()) {
            $this->storage->setUserID($userDetails->id);
            $this->storage->setUserName($userDetails->name);
            $this->storage->setRoleName($userDetails->role_name);
            $this->storage->setIP($ip);
            // check if it has rememberMe : //@todo reimplement
//            if ($request->getPost('rememberme') == 1) {
//                $this->storage->setRememberMe(1);
//            }
            // set storage again
            $this->authService->setStorage($this->storage);
        }
        return $result;
    }
    public function loginAction()
    {
        $form = new LoginForm('Login');
        $request = $this->getRequest();
        $ip = $request->getServer('REMOTE_ADDR');
        $ref = $this->flashmessenger()->getMessagesFromNamespace('referer');
        if (count($ref) > 0) {
            $this->flashmessenger()->addMessage($ref[0], 'referer', 1);
        }
        if ($request->isPost()) {
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $data = $form->getData();

                $result = $this->login($ip, $data['email'], $data['password']);
                
                if ($result->isValid()) {
                    if (count($ref) > 0) {
                        return $this->redirect()->toUrl($ref[0]);
                    }
                    $this->flashmessenger()->addMessage("You've been logged in");
                    return $this->redirect()->toUrl($this->getReferer());
                } else {
                    foreach ($result->getMessages() as $message) {
                        // save message temporary into flashmessenger
                        $this->flashmessenger()->addMessage($message);
                    }
                    if (true) {//count($result->getMessages()) == 0) {
                        $form->get('email')->setMessages(['Email oder Passwort falsch']);
                        $form->get('password')->setMessages(['Email oder Passwort falsch']);
                    } else {
                        $form->get('password')->setMessages($result->getMessages());
                    }
                }
            }
        }
        return array(
            'form' => $form,
            'messages' => $this->flashmessenger()->getMessagesFromNamespace('auth')
        );<commit_msg>added and testet: if logged in from login page && no referring msg set ->reroute to home<commit_after><?php
namespace Auth\Controller;

use Application\Model\DynamicHashTable;
use Application\Service\MessageService;
use Auth\Form\EmailForm;
use Auth\Form\PWForgetForm;
use Auth\Model\AuthStorage;
use Auth\Model\UserTable;
use Auth\Utility\UserPassword;
use Exception;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Result;
use Zend\Mvc\Controller\AbstractActionController;
use Auth\Form\LoginForm;
use Auth\Form\UserForm;
use Auth\Model\User;
use Application\Service\TemplateTypes;

class AuthController extends AbstractActionController
{
    protected $form;

    /** @var AuthStorage */
    protected $storage;
    /** @var AuthenticationService  */
    protected $authService;
    /** @var UserTable  */
    protected $userTable;
    /** @var DynamicHashTable  */
    protected $dynamicHashTable;
    /** @var  MessageService */
    protected $msgService;

    function __construct(AuthStorage $storage, AuthenticationService $authService, UserTable $userTable, DynamicHashTable $dynamicHashTable, MessageService $msgService )
    {
        $this->storage = $storage;
        $this->authService = $authService;
        $this->userTable = $userTable;
        $this->dynamicHashTable = $dynamicHashTable;
        $this->msgService = $msgService;
    }

    private function getUserDetails($mail)
    {
        $users = $this->userTable->getUsers(array(
                        'email' => $mail
                    ), array(
                        'id' => 'id',
                        'email',
                        'name'
                    ));
        return $users;
    }

    private function login($ip, $email, $clearPass) {
        $valid = true;
        $result = new Result(500, null, ['Unexpected Server Error']);
        //check if user exists
        $userDetails = $this->userTable->getUsersForAuth($email);
        // email not found
        if (!$userDetails){
            $valid = false;
        }
        // email found but inactive
        elseif ($userDetails->status != 1) {
            $valid = false;
            $result = new Result(401, null, ['Zugang Verweigert::Dieser Account ist nicht Aktiviert']);
        }
        if ($valid) {
            $userPassword = new UserPassword();
            $encyptPass = $userPassword->create($clearPass);

            $this->authService->getAdapter()
                ->setIdentity($email)
                ->setCredential($encyptPass);

            $result = $this->authService->authenticate();
        }

        if ($valid && $result->isValid()) {
            $this->storage->setUserID($userDetails->id);
            $this->storage->setUserName($userDetails->name);
            $this->storage->setRoleName($userDetails->role_name);
            $this->storage->setIP($ip);
            // check if it has rememberMe : //@todo reimplement
//            if ($request->getPost('rememberme') == 1) {
//                $this->storage->setRememberMe(1);
//            }
            // set storage again
            $this->authService->setStorage($this->storage);
        }
        return $result;
    }
    public function loginAction()
    {
        $form = new LoginForm('Login');
        $request = $this->getRequest();
        $ip = $request->getServer('REMOTE_ADDR');
        $ref = $this->flashmessenger()->getMessagesFromNamespace('referer');
        if (count($ref) > 0) {
            $this->flashmessenger()->addMessage($ref[0], 'referer', 1);
        }
        if ($request->isPost()) {
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $data = $form->getData();

                $result = $this->login($ip, $data['email'], $data['password']);
                
                if ($result->isValid()) {
                    if (count($ref) > 0) {
                        if($ref == 'login')
                            bdump('sad');
                        return $this->redirect()->toUrl($ref[0]);
                    }
                    $this->flashmessenger()->addMessage("You've been logged in");
                    $referer = $this->getReferer();
                    if ($referer == 'login') $referer = 'home';
                    return $this->redirect()->toUrl($referer);
                } else {
                    foreach ($result->getMessages() as $message) {
                        // save message temporary into flashmessenger
                        $this->flashmessenger()->addMessage($message);
                    }
                    if (true) {//count($result->getMessages()) == 0) {
                        $form->get('email')->setMessages(['Email oder Passwort falsch']);
                        $form->get('password')->setMessages(['Email oder Passwort falsch']);
                    } else {
                        $form->get('password')->setMessages($result->getMessages());
                    }
                }
            }
        }
        return array(
            'form' => $form,
            'messages' => $this->flashmessenger()->getMessagesFromNamespace('auth')
        );
{'fraction_non_alphanumeric': 0.1423314103839268, 'fraction_numerical': 0.002983887010145216, 'mean_word_length': 1.8728571428571428, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 152, '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': '3012364', 'n_tokens_mistral': 2669, 'n_tokens_neox': 2555, 'n_words': 523}