<commit_before><?php

namespace rmatil\cms\Controller;

use DateTime;
use Doctrine\DBAL\DBALException;
use rmatil\cms\Constants\EntityNames;
use rmatil\cms\Constants\HttpStatusCodes;
use rmatil\cms\Entities\User;
use rmatil\cms\Utils\PasswordUtils;
use SlimController\SlimController;

class UserController extends SlimController {

    public function getUsersAction() {
        $entityManager   = $this->app->entityManager;
        $userRepository  = $entityManager->getRepository(EntityNames::USER);
        $users           = $userRepository->findAll();

        $this->app->expires(0);
        $this->app->response->header('Content-Type', 'application/json');
        $this->app->response->setStatus(HttpStatusCodes::OK);
        $this->app->response->setBody($this->app->serializer->serialize($users, 'json'));
    }

    public function getUserByIdAction($id) {
        $entityManager   = $this->app->entityManager;
        $userRepository  = $entityManager->getRepository(EntityNames::USER);
        $user            = $userRepository->findOneBy(array('id' => $id));

        if ($user === null) {
            $this->app->response->setStatus(HttpStatusCodes::NOT_FOUND);
            return;
        }

        // do not show lock if requested by the same user as currently locked
        if ($user->getIsLockedBy() !== null &&
            $user->getIsLockedBy()->getId() === $_SESSION['user_id']) {
            $user->setIsLockedBy(null);
        }

        $this->app->expires(0);
        $this->app->response->header('Content-Type', 'application/json');
        $this->app->response->setStatus(HttpStatusCodes::OK);
        $this->app->response->setBody($this->app->serializer->serialize($user, 'json'));

        $userRepository             = $entityManager->getRepository(EntityNames::USER);
        $origUser                   = $userRepository->findOneBy(array('id' => $_SESSION['user_id']));
        // set requesting user as lock
        $user->setIsLockedBy($origUser);
        
        // force update
        try {
            $entityManager->flush();
        } catch (DBALException $dbalex) {
            $now = new DateTime();
            $this->app->log->error(sprintf('[%s]: %s', $now->format('d-m-Y H:i:s'), $dbalex->getMessage()));
            $this->app->response->setStatus(HttpStatusCodes::CONFLICT);
            return;
        }
    }

    public function updateUserAction($userId) {
        $userObject                 = $this->app->serializer->deserialize($this->app->request->getBody(), EntityNames::USER, 'json');

        $entityManager              = $this->app->entityManager;
        $userRepository             = $entityManager->getRepository(EntityNames::USER);
        $origUser                   = $userRepository->findOneBy(array('id' => $userId));

        $userGroupRepository        = $entityManager->getRepository(EntityNames::USER_GROUP);
        $origUserGroup              = $userGroupRepository->findOneBy(array('id' => $origUser->getUserGroup()->getId()));
        $userObject->setUserGroup($origUserGroup);

        if ($userObject->getPlainPassword() === null ||
            $userObject->getPlainPassword() === '') {
            // user has not set a new password
            $userObject->setPasswordHash($origUser->getPasswordHash());
        } else {
            // hash provided plaintext password
            $userObject->setPasswordHash(PasswordUtils::hash($userObject->getPlainPassword()));
        }

        $origUser->update($userObject);
        // release lock on editing
        $origUser->setIsLockedBy(null);

        // force update
        try {
            $entityManager->flush();
        } catch (DBALException $dbalex) {
            $now = new DateTime();
            $this->app->log->error(sprintf('[%s]: %s', $now->format('d-m-Y H:i:s'), $dbalex->getMessage()));
            $this->app->response->setStatus(HttpStatusCodes::CONFLICT);
            return;
        }

        $this->app->expires(0);<commit_msg>Fix #28<commit_after><?php

namespace rmatil\cms\Controller;

use DateTime;
use Doctrine\DBAL\DBALException;
use rmatil\cms\Constants\EntityNames;
use rmatil\cms\Constants\HttpStatusCodes;
use rmatil\cms\Entities\User;
use rmatil\cms\Login\PasswordHandler;
use rmatil\cms\Utils\PasswordUtils;
use SlimController\SlimController;

class UserController extends SlimController {

    public function getUsersAction() {
        $entityManager   = $this->app->entityManager;
        $userRepository  = $entityManager->getRepository(EntityNames::USER);
        $users           = $userRepository->findAll();

        $this->app->expires(0);
        $this->app->response->header('Content-Type', 'application/json');
        $this->app->response->setStatus(HttpStatusCodes::OK);
        $this->app->response->setBody($this->app->serializer->serialize($users, 'json'));
    }

    public function getUserByIdAction($id) {
        $entityManager   = $this->app->entityManager;
        $userRepository  = $entityManager->getRepository(EntityNames::USER);
        $user            = $userRepository->findOneBy(array('id' => $id));

        if ($user === null) {
            $this->app->response->setStatus(HttpStatusCodes::NOT_FOUND);
            return;
        }

        // do not show lock if requested by the same user as currently locked
        if ($user->getIsLockedBy() !== null &&
            $user->getIsLockedBy()->getId() === $_SESSION['user_id']) {
            $user->setIsLockedBy(null);
        }

        $this->app->expires(0);
        $this->app->response->header('Content-Type', 'application/json');
        $this->app->response->setStatus(HttpStatusCodes::OK);
        $this->app->response->setBody($this->app->serializer->serialize($user, 'json'));

        $userRepository             = $entityManager->getRepository(EntityNames::USER);
        $origUser                   = $userRepository->findOneBy(array('id' => $_SESSION['user_id']));
        // set requesting user as lock
        $user->setIsLockedBy($origUser);
        
        // force update
        try {
            $entityManager->flush();
        } catch (DBALException $dbalex) {
            $now = new DateTime();
            $this->app->log->error(sprintf('[%s]: %s', $now->format('d-m-Y H:i:s'), $dbalex->getMessage()));
            $this->app->response->setStatus(HttpStatusCodes::CONFLICT);
            return;
        }
    }

    public function updateUserAction($userId) {
        $userObject                 = $this->app->serializer->deserialize($this->app->request->getBody(), EntityNames::USER, 'json');

        $entityManager              = $this->app->entityManager;
        $userRepository             = $entityManager->getRepository(EntityNames::USER);
        $origUser                   = $userRepository->findOneBy(array('id' => $userId));

        $userGroupRepository        = $entityManager->getRepository(EntityNames::USER_GROUP);
        $origUserGroup              = $userGroupRepository->findOneBy(array('id' => $origUser->getUserGroup()->getId()));
        $userObject->setUserGroup($origUserGroup);

        if ($userObject->getPlainPassword() === null ||
            $userObject->getPlainPassword() === '') {
            // user has not set a new password
            $userObject->setPasswordHash($origUser->getPasswordHash());
        } else {
            // hash provided plaintext password
	    $userObject->setPasswordHash(PasswordHandler::hash($userObject->getPlainPassword()));
        }

        $origUser->update($userObject);
        // release lock on editing
        $origUser->setIsLockedBy(null);

        // force update
        try {
            $entityManager->flush();
        } catch (DBALException $dbalex) {
            $now = new DateTime();
            $this->app->log->error(sprintf('[%s]: %s', $now->format('d-m-Y H:i:s'), $dbalex->getMessage()));
            $this->app->response->setStatus(HttpStatusCodes::CONFLICT);
            return;
        }

        $this->app->expires(0);
{'fraction_non_alphanumeric': 0.16072545340838024, 'fraction_numerical': 0.0010006253908692933, 'mean_word_length': 2.569642857142857, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 189, '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': '4617143', 'n_tokens_mistral': 2243, 'n_tokens_neox': 2231, 'n_words': 334}