<commit_before><?php

namespace app\controllers\admin;

use app\components\{mail\Tools as MailTools, UrlHelper, UserGroupAdminMethods};
use app\models\db\{Consultation, ConsultationUserGroup, EMailLog, User};
use app\models\exceptions\UserEditFailed;
use app\models\settings\AntragsgruenApp;
use yii\base\ExitException;
use yii\web\Response;

class UsersController extends AdminBase
{
    private UserGroupAdminMethods $userGroupAdminMethods;

    public function beforeAction($action): bool
    {
        $result = parent::beforeAction($action);

        if ($result) {
            $this->userGroupAdminMethods = new UserGroupAdminMethods();
            $this->userGroupAdminMethods->setRequestData($this->consultation, $this->getHttpRequest(), $this->getHttpSession());
        }

        return $result;
    }

    private function getConsultationAndCheckAdminPermission(): Consultation
    {
        $consultation = $this->consultation;

        if (!User::havePrivilege($consultation, ConsultationUserGroup::PRIVILEGE_CONSULTATION_SETTINGS)) {
            $this->showErrorpage(403, \Yii::t('admin', 'no_access'));
            throw new ExitException();
        }

        return $consultation;
    }

    private function getUsersWidgetData(Consultation $consultation): array
    {
        $usersArr = array_map(function (User $user) use ($consultation): array {
            return $user->getUserAdminApiObject($consultation);
        }, $consultation->getUsersInAnyGroup());
        $groupsArr = array_map(function (ConsultationUserGroup $group): array {
            return $group->getUserAdminApiObject();
        }, $consultation->getAllAvailableUserGroups());

        return [
            'users' => $usersArr,
            'groups' => $groupsArr,
        ];
    }

    public function actionAddSingleInit(): string
    {
        $this->getConsultationAndCheckAdminPermission();

        $user = User::findByAuthTypeAndName($this->getPostValue('type'), $this->getPostValue('username'));
        if ($user) {
            $thisConRoles = $user->getConsultationUserGroupIds($this->consultation);
            $response = ['exists' => true, 'already_member' => (count($thisConRoles) > 0)];
        } else {
            $response = ['exists' => false];
        }

        return $this->returnRestResponse(200, json_encode($response, JSON_THROW_ON_ERROR));
    }

    public function actionAddSingle(): string
    {
        $this->getConsultationAndCheckAdminPermission();

        $user = User::findByAuthTypeAndName($this->getPostValue('authType'), $this->getPostValue('authUsername'));
        if ($user) {
            $thisConRoles = $user->getConsultationUserGroupIds($this->consultation);
            if (count($thisConRoles) > 0) {
                $this->getHttpSession()->setFlash('error', 'This user already has permissions for this consultation');
            }
        }

        $toAssignGroupIds = array_map('intval', $this->getPostValue('userGroups', []));
        $toAssignGroups = [];
        foreach ($this->consultation->getAllAvailableUserGroups() as $userGroup) {
            if (in_array($userGroup->id, $toAssignGroupIds)) {
                $toAssignGroups[] = $userGroup;
            }
        }
        if (count($toAssignGroups) === 0) {
            $this->getHttpSession()->setFlash('error', 'You need to provide at least one user group');
            return $this->redirect(UrlHelper::createUrl('/admin/users/index'));
        }

        if ($this->isPostSet('sendEmail')) {
            $emailText = $this->getPostValue('emailText');
        } else {
            $emailText = null;
        }

        if ($user) {
            $this->userGroupAdminMethods->addSingleDetailedUser($user, $toAssignGroups, $emailText);
            $this->getHttpSession()->setFlash('success', \Yii::t('admin', 'siteacc_user_added_single'));
        } else {
            if ($this->isPostSet('generatePassword')) {
                $password = null;
            } else {
                $password = $this->getPostValue('password');
            }
            $this->userGroupAdminMethods->createSingleDetailedUser(
                $this->getPostValue('authType'),
                $this->getPostValue('authUsername'),
                $password,
                $this->getPostValue('nameGiven'),
                $this->getPostValue('nameFamily'),
                $this->getPostValue('organization'),
                $toAssignGroups,
                $emailText
            );
            $this->getHttpSession()->setFlash('success', \Yii::t('admin', 'siteacc_user_created_single'));
        }

        foreach ($this->consultation->screeningUsers as $screeningUser) {
            if ($screeningUser->userId === $user->id) {
                $screeningUser->delete();
            }
        }

        return $this->redirect(UrlHelper::createUrl('/admin/users/index'));
    }

    public function actionAddMultipleWw(): string
    {
        $this->getConsultationAndCheckAdminPermission();
<commit_msg>Prevent a crash after creating a user account<commit_after><?php

namespace app\controllers\admin;

use app\components\{mail\Tools as MailTools, UrlHelper, UserGroupAdminMethods};
use app\models\db\{Consultation, ConsultationUserGroup, EMailLog, User};
use app\models\exceptions\UserEditFailed;
use app\models\settings\AntragsgruenApp;
use yii\base\ExitException;
use yii\web\Response;

class UsersController extends AdminBase
{
    private UserGroupAdminMethods $userGroupAdminMethods;

    public function beforeAction($action): bool
    {
        $result = parent::beforeAction($action);

        if ($result) {
            $this->userGroupAdminMethods = new UserGroupAdminMethods();
            $this->userGroupAdminMethods->setRequestData($this->consultation, $this->getHttpRequest(), $this->getHttpSession());
        }

        return $result;
    }

    private function getConsultationAndCheckAdminPermission(): Consultation
    {
        $consultation = $this->consultation;

        if (!User::havePrivilege($consultation, ConsultationUserGroup::PRIVILEGE_CONSULTATION_SETTINGS)) {
            $this->showErrorpage(403, \Yii::t('admin', 'no_access'));
            throw new ExitException();
        }

        return $consultation;
    }

    private function getUsersWidgetData(Consultation $consultation): array
    {
        $usersArr = array_map(function (User $user) use ($consultation): array {
            return $user->getUserAdminApiObject($consultation);
        }, $consultation->getUsersInAnyGroup());
        $groupsArr = array_map(function (ConsultationUserGroup $group): array {
            return $group->getUserAdminApiObject();
        }, $consultation->getAllAvailableUserGroups());

        return [
            'users' => $usersArr,
            'groups' => $groupsArr,
        ];
    }

    public function actionAddSingleInit(): string
    {
        $this->getConsultationAndCheckAdminPermission();

        $user = User::findByAuthTypeAndName($this->getPostValue('type'), $this->getPostValue('username'));
        if ($user) {
            $thisConRoles = $user->getConsultationUserGroupIds($this->consultation);
            $response = ['exists' => true, 'already_member' => (count($thisConRoles) > 0)];
        } else {
            $response = ['exists' => false];
        }

        return $this->returnRestResponse(200, json_encode($response, JSON_THROW_ON_ERROR));
    }

    public function actionAddSingle(): string
    {
        $this->getConsultationAndCheckAdminPermission();

        $user = User::findByAuthTypeAndName($this->getPostValue('authType'), $this->getPostValue('authUsername'));
        if ($user) {
            $thisConRoles = $user->getConsultationUserGroupIds($this->consultation);
            if (count($thisConRoles) > 0) {
                $this->getHttpSession()->setFlash('error', 'This user already has permissions for this consultation');
            }
        }

        $toAssignGroupIds = array_map('intval', $this->getPostValue('userGroups', []));
        $toAssignGroups = [];
        foreach ($this->consultation->getAllAvailableUserGroups() as $userGroup) {
            if (in_array($userGroup->id, $toAssignGroupIds)) {
                $toAssignGroups[] = $userGroup;
            }
        }
        if (count($toAssignGroups) === 0) {
            $this->getHttpSession()->setFlash('error', 'You need to provide at least one user group');
            return $this->redirect(UrlHelper::createUrl('/admin/users/index'));
        }

        if ($this->isPostSet('sendEmail')) {
            $emailText = $this->getPostValue('emailText');
        } else {
            $emailText = null;
        }

        if ($user) {
            $this->userGroupAdminMethods->addSingleDetailedUser($user, $toAssignGroups, $emailText);
            $this->getHttpSession()->setFlash('success', \Yii::t('admin', 'siteacc_user_added_single'));
        } else {
            if ($this->isPostSet('generatePassword')) {
                $password = null;
            } else {
                $password = $this->getPostValue('password');
            }
            $user = $this->userGroupAdminMethods->createSingleDetailedUser(
                $this->getPostValue('authType'),
                $this->getPostValue('authUsername'),
                $password,
                $this->getPostValue('nameGiven'),
                $this->getPostValue('nameFamily'),
                $this->getPostValue('organization'),
                $toAssignGroups,
                $emailText
            );
            $this->getHttpSession()->setFlash('success', \Yii::t('admin', 'siteacc_user_created_single'));
        }

        foreach ($this->consultation->screeningUsers as $screeningUser) {
            if ($screeningUser->userId === $user->id) {
                $screeningUser->delete();
            }
        }

        return $this->redirect(UrlHelper::createUrl('/admin/users/index'));
    }

    public function actionAddMultipleWw(): string
    {
        $this->getConsultationAndCheckAdminPermission();

{'fraction_non_alphanumeric': 0.13762198765186218, 'fraction_numerical': 0.0017924716191993627, 'mean_word_length': 2.6653284671532846, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 129, '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': '2999703', 'n_tokens_mistral': 2798, 'n_tokens_neox': 2709, 'n_words': 444}