<?php
namespace Claromentis\People\UseCase;

$GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"] ?: array()) + array("_aa01344e9b" . "f0e5480edbb" . "cd1152e335e" => base64_decode("QXNzaWdubWVudC" . "BwZXJtaXNzaW9u" . "IGVycm9ycw=="), "_022910c5627a7e5" . "fbeb666c28497d2e" . "0" => base64_decode("WW91IGRvIG5vdCBoYXZlIHBlcm1pc3Npb24gdG8gdW5hc" . "3NpZ24gdXNlcnMgZnJvbSByb2xlIA=="), "_cecaaa8a2dc5612782" . "98872dff3b9c0c" => base64_decode("IHNldCBmb3IgdXN" . "lciA="), "_5887288fff87e34693d4d275b691" . "03e0" => base64_decode("IGNhbm5vdCBiZSBh" . "c3NpZ25lZCB0byBy" . "b2xlIA=="), "_9d799ff313bcf1dd5c17f004da" . "40f231" => base64_decode("VXNl" . "ciA="), "_0284d38f97" . "f9e8b9c7b03" . "bb8928afae1" => base64_decode("K" . "Q" . "=" . "="), "_76d14612538e52" . "c5e6329ce4fa353" . "f1a" => base64_decode("WW91IGRvIG5vdCB" . "oYXZlIHBlcm1pc3" . "Npb24gdG8gZWRpd" . "CB0aGlzIHVzZXI="), "_7dfc646ea8c9714b80e65f" . "92a5388ed0" => base64_decode("IC" . "g="), "nam" . "e" => base64_decode("bmF" . "tZQ" . "=="), "_fe0b9f5d93812b801fc36403" . "250dfa3e" => base64_decode("Um" . "9s" . "ZX" . "Mg"), "_1307d61617a82e798c53fada52dc01" . "14" => base64_decode("VXNlci" . "Bub3Qg" . "Zm91bm" . "Q="), "_01f117db67aa749d3ec8da" . "7eb478730c" => base64_decode("LC" . "A="), "_b66252d7bbf361012a653" . "1a8e3a73230" => base64_decode("YXJyYXlfZG" . "lmZg=="), "_28fd57f" . "750dbb5f" . "fb3ffad3" . "a888358a" . "9" => base64_decode("ZGVzY3Jp" . "cHRpb24="), "_01f0f050eb785ee" . "7b934fa2a8651bba" . "c" => base64_decode("WW91IGRvIG5vdCBoYXZlIHBlcm1pc3" . "Npb24gdG8gYXNzaWduIHVzZXJzIHRv" . "IGFueSByb2xlcw=="), "_23ab4dd81b6c619f47099ef7c2485a" . "12" => base64_decode("cm9sZX" . "M="), "_2c42463" . "35d948e3" . "ea311e79" . "a5b06eec" . "f" => base64_decode("WW91IGRvIG5vdCBoYXZlIHBlcm1" . "pc3Npb24gdG8gYXNzaWduIHVzZX" . "JzIHRvIHJvbGUg"));
use Analogue\ORM\EntityCollection;
use Analogue\ORM\Exceptions\MappingException;
use Claromentis\Core\Event\GenericEvent;
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\RoleRepository;
use Claromentis\People\Repository\UserRepository;
use Claromentis\People\Validator\RoleValidator;
use Claromentis\People\Validator\Util;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class SetUserRoles
{
    
    
    
    
    protected $acl;
    
    
    
    
    protected $validator;
    
    
    
    
    protected $user_repository;
    
    
    
    
    protected $role_repository;
    
    
    
    
    protected $dispatcher;
    public function __construct(PeopleAcl $acl, RoleValidator $_24c8d051c033d19c2c97cdc3d7098b1b, UserRepository $_eeeeb0d673b7b0fd0bdca58b5541c3e0, RoleRepository $_90f6cb30a269a622b4c838bfd5a0a5db, EventDispatcherInterface $dispatcher)
    {
        $this->acl = $acl;
        $this->validator = $_24c8d051c033d19c2c97cdc3d7098b1b;
        $this->user_repository = $_eeeeb0d673b7b0fd0bdca58b5541c3e0;
        $this->role_repository = $_90f6cb30a269a622b4c838bfd5a0a5db;
        $this->dispatcher = $dispatcher;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    public function SetUserRoles(SecurityContext $context, int $user_id, array $_25fb31f00b26bde87effd4759e283d9f)
    {
        if (!$this->acl->CanUpdateUser($context, $user_id)) {
            throw new PermissionException($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_76d1461253" . "8e52c5e6329" . "ce4fa353f1a"]);
        }
        if (!$this->acl->CanAssignRoles($context)) {
            throw new PermissionException($GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_01f0f050eb" . "785ee7b934f" . "a2a8651bbac"]);
        }
        Util::ValidateIds($_25fb31f00b26bde87effd4759e283d9f);
        
        
        
        
        $_24e0e36600d6af77406a44bb618a6253 = $this->user_repository->findWith($user_id, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_23ab4dd81b6c619f47" . "099ef7c2485a12"]);
        if (!$_24e0e36600d6af77406a44bb618a6253) {
            throw new NotFoundException($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_1307d61617a82e798c" . "53fada52dc0114"]);
        }
        
        
        
        
        $_23ab4dd81b6c619f47099ef7c2485a12 = $this->role_repository->find($_25fb31f00b26bde87effd4759e283d9f);
        $this->validator->ValidateExistence($_25fb31f00b26bde87effd4759e283d9f, $_23ab4dd81b6c619f47099ef7c2485a12);
        $this->CheckAssignmentPermissions($context, $_24e0e36600d6af77406a44bb618a6253, $_23ab4dd81b6c619f47099ef7c2485a12);
        $_24e0e36600d6af77406a44bb618a6253->roles = $_23ab4dd81b6c619f47099ef7c2485a12;
        $this->user_repository->store($_24e0e36600d6af77406a44bb618a6253);
        $_7992522feabc5d00d00c8cfcccc9abc3 = $_23ab4dd81b6c619f47099ef7c2485a12->implode($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["na" . "me"], $GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_01f117db67a" . "a749d3ec8da7" . "eb478730c"]);
        $event = new GenericEvent($_24e0e36600d6af77406a44bb618a6253, [$GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_28fd57f7" . "50dbb5ffb" . "3ffad3a88" . "8358a9"] => $GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_fe0b9f5d93812b801fc36403250d" . "fa3e"] . $_7992522feabc5d00d00c8cfcccc9abc3 . $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_cecaaa8a" . "2dc561278" . "298872dff" . "3b9c0c"] . $_24e0e36600d6af77406a44bb618a6253->fullname . $GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_7dfc646ea8c9714" . "b80e65f92a5388ed" . "0"] . $_24e0e36600d6af77406a44bb618a6253->username . $GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_0284d38f97f9e8b9c7b03b" . "b8928afae1"]]);
        $this->dispatcher->dispatch(PeopleEventType::SET_USER_ROLES, $event);
        return $_23ab4dd81b6c619f47099ef7c2485a12;
    }
    
    
    
    
    
    
    
    
    
    
    protected function CheckAssignmentPermissions(SecurityContext $context, User $_24e0e36600d6af77406a44bb618a6253, EntityCollection $_23ab4dd81b6c619f47099ef7c2485a12)
    {
        $_7a7ab6950f68791550e6968747fac73a = $_24e0e36600d6af77406a44bb618a6253->roles ? $_24e0e36600d6af77406a44bb618a6253->roles->getEntityKeys() : [];
        $_79dc0ef7bfcc9878eb5810f0364165ce = $_23ab4dd81b6c619f47099ef7c2485a12->getEntityKeys();
        $_ac28a0119009e23ea788d6c83912cde1 = SecurityContext::CreateForUser($_24e0e36600d6af77406a44bb618a6253->id);
        $_c78f29e7b113c7a4d29fe3c4468181e9 = [];
        
        
        $_17de5423c41547e316b56e61af19b8ed = $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_b66252d7bbf3" . "61012a6531a8e" . "3a73230"]($_79dc0ef7bfcc9878eb5810f0364165ce, $_7a7ab6950f68791550e6968747fac73a);
        $_60f6461b7495744358309060fa68449b = $this->role_repository->find($_17de5423c41547e316b56e61af19b8ed);
        foreach ($_60f6461b7495744358309060fa68449b as $_d1071a032215f0eb5c4a8ef175f7049d) {
            if (!$this->acl->CanAssignRoleUsers($context, $_d1071a032215f0eb5c4a8ef175f7049d)) {
                $_c78f29e7b113c7a4d29fe3c4468181e9[] = $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_2c4246335d948e" . "3ea311e79a5b06e" . "ecf"] . $_d1071a032215f0eb5c4a8ef175f7049d->id;
            } else {
                if (!$this->acl->CanBelongToRole($_ac28a0119009e23ea788d6c83912cde1, $_d1071a032215f0eb5c4a8ef175f7049d)) {
                    $_c78f29e7b113c7a4d29fe3c4468181e9[] = $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_9d799ff313bcf1dd5c17f004da40" . "f231"] . $_24e0e36600d6af77406a44bb618a6253->id . $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_5887288fff87" . "e34693d4d275b" . "69103e0"] . $_d1071a032215f0eb5c4a8ef175f7049d->id;
                }
            }
        }
        
        
        $_73a72ea270169a9b8fde664abbc86475 = $GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_b66252d7bbf361012a65" . "31a8e3a73230"]($_7a7ab6950f68791550e6968747fac73a, $_79dc0ef7bfcc9878eb5810f0364165ce);
        $_2a1bc49bc5884d5d2fc7f7e921a96e70 = $this->role_repository->find($_73a72ea270169a9b8fde664abbc86475);
        foreach ($_2a1bc49bc5884d5d2fc7f7e921a96e70 as $_d1071a032215f0eb5c4a8ef175f7049d) {
            if (!$this->acl->CanAssignRoleUsers($context, $_d1071a032215f0eb5c4a8ef175f7049d)) {
                $_c78f29e7b113c7a4d29fe3c4468181e9[] = $GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_022910c5627a7e5fbeb666c2849" . "7d2e0"] . $_d1071a032215f0eb5c4a8ef175f7049d->id;
            }
        }
        if (!empty($_c78f29e7b113c7a4d29fe3c4468181e9)) {
            throw (new PermissionException($GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_aa01344e9bf0e5480edbbcd1152e335" . "e"]))->setErrors($_c78f29e7b113c7a4d29fe3c4468181e9);
        }
    }
}