<?php
namespace Claromentis\People\UseCase;

$GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"] = (@$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"] ?: array()) + array("na" . "me" => base64_decode("bm" . "Ft" . "ZQ" . "=="), "_02dea17dca55bde2031ba" . "6c3bfdb4ba7" => base64_decode("R3Jvd" . "XBzIA" . "=="), "_7331abbad24fb" . "e11818f3e4e936" . "744ce" => base64_decode("WW91IGRvIG5vdCBoYXZlIHBlcm1pc3Npb24" . "gdG8gYXNzaWduIHVzZXJzIHRvIGdyb3VwIA" . "=="), "_26cdbc145b3a6473db7fdcee042146" . "4d" => base64_decode("IGNhbm5vdCBiZSBhc3NpZ25" . "lZCB0byBncm91cCA="), "_b66252d7bbf3" . "61012a6531a8e" . "3a73230" => base64_decode("YXJyY" . "XlfZG" . "lmZg=" . "="), "_28fd57f75" . "0dbb5ffb3f" . "fad3a88835" . "8a9" => base64_decode("ZGVzY3Jp" . "cHRpb24="), "_0284d38f97f9e8b9c7b0" . "3bb8928afae1" => base64_decode("KQ=" . "="), "_dc6e3dec340f6e7cce1" . "ba21f2d47b621" => base64_decode("WW91IGRvIG5vdCBoYXZlIH" . "Blcm1pc3Npb24gdG8gYXNz" . "aWduIHVzZXJzIHRvIGFueS" . "Bncm91cHM="), "_9d799ff" . "313bcf1d" . "d5c17f00" . "4da40f23" . "1" => base64_decode("VX" . "Nl" . "ci" . "A="), "_14e7d5dee412ccf5169" . "39d1e99c87733" => base64_decode("Z3Jv" . "dXBz"), "_7dfc646ea" . "8c9714b80e" . "65f92a5388" . "ed0" => base64_decode("IC" . "g="), "_cecaaa8a" . "2dc561278" . "298872dff" . "3b9c0c" => base64_decode("IHNldCBmb3" . "IgdXNlciA="), "_01f117db67aa749d3ec8da7eb478730" . "c" => base64_decode("L" . "C" . "A" . "="), "_aa01344e9bf0e5480edbbcd115" . "2e335e" => base64_decode("QXNzaWdubWVudCBwZXJtaXNzaW9uIGVycm9y" . "cw=="), "_4d8791444e2fdc353fc4c9" . "a5e213fcbb" => base64_decode("WW91IGRvIG5vdCBoYXZlIHBlcm1pc3Npb24gdG8gdW5hc3NpZ24gdXNl" . "cnMgZnJvbSBncm91cCA="), "_1307d61617a" . "82e798c53fad" . "a52dc0114" => base64_decode("VXNlci" . "Bub3Qg" . "Zm91bm" . "Q="));
use Analogue\ORM\EntityCollection;
use Analogue\ORM\Exceptions\MappingException;
use Claromentis\Core\Event\GenericEvent;
use Claromentis\Core\Localization\Lmsg;
use Claromentis\Core\Security\SecurityContext;
use Claromentis\People\Entity\User;
use Claromentis\People\Event\PeopleEventType;
use Claromentis\People\Exception\NotFoundException;
use Claromentis\People\Exception\NotUniqueException;
use Claromentis\People\Exception\PermissionException;
use Claromentis\People\Exception\ValidationException;
use Claromentis\People\PeopleAcl;
use Claromentis\People\Repository\GroupRepository;
use Claromentis\People\Repository\UserRepository;
use Claromentis\People\Validator\GroupValidator;
use Claromentis\People\Validator\Util;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class SetUserGroups
{
    const E_NO_PERMS = 'people.error.cannot_update_user';
    
    
    
    
    protected $acl;
    
    
    
    
    protected $lmsg;
    
    
    
    
    protected $user_repository;
    
    
    
    
    protected $group_repository;
    
    
    
    
    protected $dispatcher;
    
    
    
    
    protected $validator;
    public function __construct(PeopleAcl $acl, Lmsg $lmsg, GroupValidator $_24c8d051c033d19c2c97cdc3d7098b1b, UserRepository $_eeeeb0d673b7b0fd0bdca58b5541c3e0, GroupRepository $_efe123fd19d21a3a78376b566dad7284, EventDispatcherInterface $dispatcher)
    {
        $this->acl = $acl;
        $this->lmsg = $lmsg;
        $this->validator = $_24c8d051c033d19c2c97cdc3d7098b1b;
        $this->user_repository = $_eeeeb0d673b7b0fd0bdca58b5541c3e0;
        $this->group_repository = $_efe123fd19d21a3a78376b566dad7284;
        $this->dispatcher = $dispatcher;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    public function SetUserGroups(SecurityContext $context, int $user_id, array $_bc39899fd8a79cbcb9abb4f5d7085899) : EntityCollection
    {
        if (!$this->acl->CanUpdateUser($context, $user_id)) {
            throw new PermissionException(($this->lmsg)(self::E_NO_PERMS));
        }
        if (!$this->acl->CanAssignGroups($context)) {
            throw new PermissionException($GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_dc6e3dec340f6e7cce" . "1ba21f2d47b621"]);
        }
        Util::ValidateIds($_bc39899fd8a79cbcb9abb4f5d7085899);
        
        
        
        
        $_24e0e36600d6af77406a44bb618a6253 = $this->user_repository->findWith($user_id, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_14e7d5de" . "e412ccf51" . "6939d1e99" . "c87733"]);
        if (!$_24e0e36600d6af77406a44bb618a6253) {
            throw new NotFoundException($GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_1307d61617a82e798c53" . "fada52dc0114"]);
        }
        
        
        
        
        $_14e7d5dee412ccf516939d1e99c87733 = $this->group_repository->find($_bc39899fd8a79cbcb9abb4f5d7085899);
        $this->validator->ValidateExistence($_bc39899fd8a79cbcb9abb4f5d7085899, $_14e7d5dee412ccf516939d1e99c87733);
        $this->CheckAssignmentPermissions($context, $_24e0e36600d6af77406a44bb618a6253, $_14e7d5dee412ccf516939d1e99c87733);
        $_24e0e36600d6af77406a44bb618a6253->groups = $_14e7d5dee412ccf516939d1e99c87733;
        $this->user_repository->store($_24e0e36600d6af77406a44bb618a6253);
        $_215b92aa345d1aad980ddcf7b628af09 = $_14e7d5dee412ccf516939d1e99c87733->implode($GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["na" . "me"], $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_01f117db67aa749d3ec8da7eb47" . "8730c"]);
        $event = new GenericEvent($_24e0e36600d6af77406a44bb618a6253, [$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_28fd57f750dbb5ff" . "b3ffad3a888358a9"] => $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_02dea17dca55" . "bde2031ba6c3b" . "fdb4ba7"] . $_215b92aa345d1aad980ddcf7b628af09 . $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_cecaaa8" . "a2dc5612" . "78298872" . "dff3b9c0" . "c"] . $_24e0e36600d6af77406a44bb618a6253->fullname . $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_7dfc646ea8c9714b80e" . "65f92a5388ed0"] . $_24e0e36600d6af77406a44bb618a6253->username . $GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_0284d38f97f9e8" . "b9c7b03bb8928af" . "ae1"]]);
        $this->dispatcher->dispatch($event, PeopleEventType::SET_USER_GROUPS);
        return $_14e7d5dee412ccf516939d1e99c87733;
    }
    
    
    
    
    
    
    
    
    
    
    protected function CheckAssignmentPermissions(SecurityContext $context, User $_24e0e36600d6af77406a44bb618a6253, EntityCollection $_14e7d5dee412ccf516939d1e99c87733)
    {
        $_f218e28342e4f68c9adb9a5dc073f13e = $_24e0e36600d6af77406a44bb618a6253->groups ? $_24e0e36600d6af77406a44bb618a6253->groups->getEntityKeys() : [];
        $_88892404f737faacca94f274f8d1c03d = $_14e7d5dee412ccf516939d1e99c87733->getEntityKeys();
        $_ac28a0119009e23ea788d6c83912cde1 = SecurityContext::CreateForUser($_24e0e36600d6af77406a44bb618a6253->id);
        $_c78f29e7b113c7a4d29fe3c4468181e9 = [];
        
        
        $_39efe907a21c3ec1c8b65f851cb3f851 = $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_b66252d7bbf361012a6531a8" . "e3a73230"]($_88892404f737faacca94f274f8d1c03d, $_f218e28342e4f68c9adb9a5dc073f13e);
        $_12afcb9a388b662661d86c5191f71707 = $this->group_repository->find($_39efe907a21c3ec1c8b65f851cb3f851);
        foreach ($_12afcb9a388b662661d86c5191f71707 as $_e9208e4fb555ac989f7519e19c1da810) {
            if (!$this->acl->CanAssignGroupUsers($context, $_e9208e4fb555ac989f7519e19c1da810)) {
                $_c78f29e7b113c7a4d29fe3c4468181e9[] = $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_7331abbad24fbe11818f3e4" . "e936744ce"] . $_e9208e4fb555ac989f7519e19c1da810->id;
            } else {
                if (!$this->acl->CanBelongToGroup($_ac28a0119009e23ea788d6c83912cde1, $_e9208e4fb555ac989f7519e19c1da810)) {
                    $_c78f29e7b113c7a4d29fe3c4468181e9[] = $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_9d799ff" . "313bcf1d" . "d5c17f00" . "4da40f23" . "1"] . $_24e0e36600d6af77406a44bb618a6253->id . $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_26cdbc145b3a64" . "73db7fdcee04214" . "64d"] . $_e9208e4fb555ac989f7519e19c1da810->id;
                }
            }
        }
        
        
        $_5f6ffab7100cfe7b9bfbafbee623c90f = $GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_b66252d7bbf361" . "012a6531a8e3a73" . "230"]($_f218e28342e4f68c9adb9a5dc073f13e, $_88892404f737faacca94f274f8d1c03d);
        $_01e2a126442f523fa0de338d629854fe = $this->group_repository->find($_5f6ffab7100cfe7b9bfbafbee623c90f);
        foreach ($_01e2a126442f523fa0de338d629854fe as $_e9208e4fb555ac989f7519e19c1da810) {
            if (!$this->acl->CanAssignGroupUsers($context, $_e9208e4fb555ac989f7519e19c1da810)) {
                $_c78f29e7b113c7a4d29fe3c4468181e9[] = $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_4d8791444e2fdc353fc4c9a" . "5e213fcbb"] . $_e9208e4fb555ac989f7519e19c1da810->id;
            }
        }
        if (!empty($_c78f29e7b113c7a4d29fe3c4468181e9)) {
            throw (new PermissionException($GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_aa01344e9bf0e5480" . "edbbcd1152e335e"]))->setErrors($_c78f29e7b113c7a4d29fe3c4468181e9);
        }
    }
}