<?php
namespace Claromentis\People\UseCase;

$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"] = (@$GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"] ?: array()) + array("_605915e" . "3d257166" . "7989b992" . "452e5f5e" . "3" => base64_decode("Y2ZnX2RlZmF1bH" . "Rfbm90aWZpY2F0" . "aW9uX21ldGhvZA" . "=="), "_baec81a782bb3d538149a9" . "225688246e" => base64_decode("ZXZlbnRfc291cm" . "Nl"), "_4bce3a08782" . "de108b3737e9" . "4bd8e2956" => base64_decode("cGFzc3dvcmRfcmVzZ" . "XQ="), "_78d8d35f535c" . "de564c5fdcfdb" . "222cd99" => base64_decode("bm90aWZpY2F0aW9uX21ldGhvZ" . "A=="), "_e0637eee905ebe39" . "38c5da50e3db10d7" => base64_decode("cGFzc3dvcmQ" . "="), "_53a46d04502e93273" . "d0204d8ee6cbdb4" => base64_decode("Y2ZnX2R" . "lZmF1bH" . "RfbGFuZ" . "3VhZ2U="), "_d57e4d133" . "47ab90ce20" . "7860c82cc3" . "a75" => base64_decode("WW91IGRvIG5vdC" . "BoYXZlIHBlcm1p" . "c3Npb24gdG8gY3" . "JlYXRlIHVzZXJz"), "_f73dab606630b388645c4f7d3" . "fbb13bb" => base64_decode("YXJyYX" . "lfbWVy" . "Z2U="), "user" . "_id" => base64_decode("dXNlc" . "l9pZA" . "=="), "_4d707fab93beb" . "b00bbe69e81596" . "97c90" => base64_decode(""), "_8cca4dd34e20c5344bb2" . "a30f2533c4e5" => base64_decode("ZnVsbF9uYW" . "1l"), "_32a1dabf70f04f7f9a01a6504439" . "ab2b" => base64_decode("d2hhd" . "HNfbm" . "V3X2x" . "pc3Q="), "_2002b5c1" . "88aff6be5" . "f091cf0f9" . "f0042e" => base64_decode("ZHVtbXlfa" . "GFzaA=="), "lang" . "uage" => base64_decode("bGFuZ" . "3VhZ2" . "U="), "extran" . "et_id" => base64_decode("ZXh0cmF" . "uZXRfaW" . "Q="), "_28c85ce81fee65" . "32980262893b6f2" . "425" => base64_decode("dXNlcm5hb" . "WU="), "_2bd21b4130c6e2c699275c" . "9c77e03b0e" => base64_decode("Y2ZnX2RlZmF1bHRfd2hhdHNfbmV3X2xpc3" . "Q="), "_539cf4259a149" . "ca9dbb739b2cdb" . "7c608" => base64_decode("ZW1haWxfYWRkcmVz" . "cw=="), "_3429a564f3319b" . "cce4277c92b5663" . "01a" => base64_decode("aW50cmFuZXR1c2" . "Vy"));
use Claromentis\Core\Config\Config;
use Claromentis\Core\Event\EventDispatcher;
use Claromentis\Core\Event\GenericEvent;
use Claromentis\Core\Localization\Lmsg;
use Claromentis\Core\Security\SecurityContext;
use Claromentis\Core\Services;
use Claromentis\People\Entity\User;
use Claromentis\People\Event\AddUserEvent;
use Claromentis\People\Event\PeopleEventType;
use Claromentis\People\Exception\NotUniqueException;
use Claromentis\People\Exception\PermissionException;
use Claromentis\People\Exception\ValidationException;
use Claromentis\People\PeopleAcl;
use Claromentis\People\Repository\ExtranetRepository;
use Claromentis\People\Repository\UserRepository;
use Claromentis\People\Service\UserLicenseCheck;
use Claromentis\People\Validator\UserValidator;
use RestExError;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class CreateUser
{
    const E_CREATION_BLOCKED = "people.error.user_create_blocked";
    const E_NO_PERMISSION = "people.error.cant_create_user_in_extranet";
    
    
    
    
    protected $repo;
    
    
    
    
    protected $primary_extranet_id;
    
    
    
    
    protected $validator;
    
    
    
    
    protected $acl;
    
    
    
    
    protected $dispatcher;
    
    
    
    
    protected $extranet_repository;
    
    
    
    
    protected $lmsg;
    
    
    
    
    protected $config;
    
    
    
    
    protected $license;
    public function __construct(UserRepository $repo, UserValidator $_24c8d051c033d19c2c97cdc3d7098b1b, ExtranetRepository $_4af3095f20a1f91b94a8762557924437, UserLicenseCheck $_28d2607075b352d52b45daf2a256f773, PeopleAcl $acl, EventDispatcherInterface $dispatcher, Lmsg $lmsg, Config $config)
    {
        $this->repo = $repo;
        $this->validator = $_24c8d051c033d19c2c97cdc3d7098b1b;
        $this->acl = $acl;
        $this->dispatcher = $dispatcher;
        $this->extranet_repository = $_4af3095f20a1f91b94a8762557924437;
        $this->lmsg = $lmsg;
        $this->license = $_28d2607075b352d52b45daf2a256f773;
        $this->config = $config;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    public function Create(SecurityContext $context, array $data)
    {
        if (!$this->acl->CanCreateUser($context)) {
            throw new PermissionException($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_d57e4d13347ab90ce20786" . "0c82cc3a75"]);
        }
        
        
        $_24e0e36600d6af77406a44bb618a6253 = $this->repo->newInstance();
        $this->validator->ValidateCreate($data);
        $data = $this->FillDefaults($data);
        $_e0637eee905ebe3938c5da50e3db10d7 = null;
        if (!isset($data[$GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_e0637eee9" . "05ebe3938c" . "5da50e3db1" . "0d7"]])) {
            $data[$GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_e0637eee905ebe3938c5da50" . "e3db10d7"]] = $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_2002b5c18" . "8aff6be5f0" . "91cf0f9f00" . "42e"];
        } else {
            $_e0637eee905ebe3938c5da50e3db10d7 = $data[$GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_e0637eee905ebe3938c5da50e3" . "db10d7"]];
            unset($data[$GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_e0637eee905ebe3938c5da50e3d" . "b10d7"]]);
        }
        $_24e0e36600d6af77406a44bb618a6253->fill($data);
        $this->license->CheckLicense($_24e0e36600d6af77406a44bb618a6253, $data);
        if (!$this->acl->CanCreateExtranetUser($context, $_24e0e36600d6af77406a44bb618a6253->extranet_id)) {
            throw new PermissionException(($this->lmsg)(self::E_NO_PERMISSION, $_24e0e36600d6af77406a44bb618a6253->extranet_id));
        }
        $_24e0e36600d6af77406a44bb618a6253->fill($data);
        $event = new AddUserEvent($_24e0e36600d6af77406a44bb618a6253->toLegacy(), $this->GetEventArguments($_24e0e36600d6af77406a44bb618a6253));
        $this->dispatcher->dispatch($event, PeopleEventType::PRE_ADD_USER);
        if ($event->isPropagationStopped()) {
            throw new ValidationException(($this->lmsg)(self::E_CREATION_BLOCKED));
        }
        $_60a6e2636e44ad25e5c6207e7695ec1a = $event->getSubject();
        
        
        
        
        $_24e0e36600d6af77406a44bb618a6253 = $this->repo->store($_24e0e36600d6af77406a44bb618a6253);
        
        
        
        
        
        
        if ($_e0637eee905ebe3938c5da50e3db10d7) {
            
            
            $set_user_password = Services::I()->{SetUserPassword::class};
            
            
            $_5442a35c29ec35b11beeb6f997e3cf51 = new \User($_24e0e36600d6af77406a44bb618a6253->id);
            $_5442a35c29ec35b11beeb6f997e3cf51->Load();
            $set_user_password->SetPassword($_5442a35c29ec35b11beeb6f997e3cf51, $_e0637eee905ebe3938c5da50e3db10d7, null, null, $context, isset($data[$GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_4bce3a08782de10" . "8b3737e94bd8e295" . "6"]]) && (bool) $data[$GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_4bce3a08782de108b3737e94bd8e295" . "6"]] === true);
            if (!$_5442a35c29ec35b11beeb6f997e3cf51->Save()) {
                throw new RestExError($_5442a35c29ec35b11beeb6f997e3cf51->GetErrMsg());
            }
        }
        $event = new AddUserEvent($_24e0e36600d6af77406a44bb618a6253->toLegacy(), $this->GetEventArguments($_24e0e36600d6af77406a44bb618a6253));
        $this->dispatcher->dispatch($event, PeopleEventType::ADD_USER);
        return $_24e0e36600d6af77406a44bb618a6253;
    }
    
    
    
    
    
    
    
    protected function FillDefaults(array $attributes) : array
    {
        $_a50b89f7a699cedab7cc721b2e6008e5 = $this->extranet_repository->getPrimaryExtranet()->id;
        $_7c7a0774c7447eed1a9b0b8aba163b8f = [$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["extrane" . "t_id"] => $_a50b89f7a699cedab7cc721b2e6008e5, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_3429a564f33" . "19bcce4277c9" . "2b566301a"] => User::STATUS_ACTIVE, $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_78d8d35f535cde564c5fdcfdb" . "222cd99"] => $this->config->Get($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_605915e3d2571667989" . "b992452e5f5e3"]), $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["lang" . "uage"] => $this->config->Get($GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_53a46d04502e932" . "73d0204d8ee6cbdb" . "4"]), $GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_32a1dabf7" . "0f04f7f9a0" . "1a6504439a" . "b2b"] => $this->config->Get($GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_2bd21b4130c6" . "e2c699275c9c7" . "7e03b0e"])];
        return $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_f73dab60663" . "0b388645c4f7" . "d3fbb13bb"]($_7c7a0774c7447eed1a9b0b8aba163b8f, $attributes);
    }
    
    
    
    
    
    
    
    protected function GetEventArguments(User $_24e0e36600d6af77406a44bb618a6253)
    {
        return [$GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["user_i" . "d"] => $_24e0e36600d6af77406a44bb618a6253->id, $GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_8cca4dd" . "34e20c53" . "44bb2a30" . "f2533c4e" . "5"] => $_24e0e36600d6af77406a44bb618a6253->fullname, $GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_28c85ce81fee65329802" . "62893b6f2425"] => $_24e0e36600d6af77406a44bb618a6253->username, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_539cf4259a149ca9dbb739b2cdb7c60" . "8"] => $_24e0e36600d6af77406a44bb618a6253->email, $GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_baec81a782bb3d5" . "38149a9225688246" . "e"] => $GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_4d707fab93bebb00bbe" . "69e8159697c90"]];
    }
}