<?php
namespace Claromentis\Core\Acl;

$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"] = (@$GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"] ?: array()) + array("_58d506edba9" . "ce80af6a061b" . "73e2eb66c" => base64_decode("X" . "w" . "=" . "="), "_c75934a16a05f05c14" . "432ff269640ec4" => base64_decode("SU5TRVJUIElOVE8gcGVybWlzc2lvbnMgKGNsYXNzX2lkLCBvYmplY3RfaWQsIHBlcm1fb3du" . "ZXJfY2xhc3NfaWQsIHBlcm1fb3duZXJfaWQsIHJpZ2h0cyk="), "_190a27304b863980e14c5514d" . "55f6770" => base64_decode("SGFzU" . "GVybW" . "lzc2l" . "vbnM="), "_966b46f1eeef93" . "1d3aab021091170" . "71b" => base64_decode("Q2FuJ3Qgc2F2ZSBwZXJtaXNzaW9uczogb2JqZWN0IG" . "NsYXNzIG5vdCBzcGVjaWZpZWQ="), "_0d42dc2ab069f31f94b6" . "67ad83e04b04" => base64_decode("IFZBTFVFUyAoaW50OmNsYXNzX2lkLCBpbnQ6aWQsIGludDpvY2xhc3MsIGludDpvaWQsIG" . "ludDpwZXJtcyk="), "_f3a6782a0639ec7ac8ea0a44e06d50f" . "a" => base64_decode("REVMRVRFIEZST00gcGVybWlzc2lvbn" . "MgV0hFUkUgY2xhc3NfaWQ9aW50OmNs" . "YXNzX2lkIEFORCBvYmplY3RfaWQ9aW" . "50Omlk"), "_f4ebcb18d6635cf0340" . "b0776aa6c2368" => base64_decode("YW" . "Ns" . "Og" . "=="), "_13753452fad" . "dc2d6579d9ba" . "291d77955" => base64_decode("Q2FuJ3QgbG9hZCBBQ0w6I" . "G9iamVjdCBJRCBub3Qgc3" . "BlY2lmaWVk"), "_5ca1c8340633c3313f886f2a4" . "dccb7e8" => base64_decode("Q2FuJ3QgbG9hZCBBQ0w6IG9iam" . "VjdCBjbGFzcyBub3Qgc3BlY2lm" . "aWVk"), "_b794b4b6e96b498c4b34de9" . "80e60c7c7" => base64_decode("U0VMRUNUIHBlcm1fb3duZXJfY2xhc3NfaWQsIHBlcm1fb3duZXJfaWQsIHJpZ2h0cyBGUk9NIHBlcm1pc3Npb25zIFdIRVJFIG9iamVjdF9pZD1pbnQ6aWQgQU5EIGNsYXNzX2lkPWludDpjbGFzc19p" . "ZA=="), "_3f0a984c475ce692d87e01ddabf630" . "65" => base64_decode("Q2FuJ3Qgc2F2ZSBBQ0w6IG9iamVjdCBJRCBub3Qgc3Bl" . "Y2lmaWVk"), "_c726d95450d9d503c7ef86671a4fc" . "991" => base64_decode("Q2FuJ3QgZGVsZXRlIHBlcm1pc3Npb25zIGZyb20gZGF0YWJhc2U6IG9iamVjdCBJRCB" . "ub3Qgc3BlY2lmaWVk"), "_3b772c19f" . "5d5e7c8d75" . "0ae0a815c7" . "ed7" => base64_decode("Q2FuJ3QgZGVsZXRlIHBlcm1pc3Npb25zIGZyb20gZGF0YWJhc2" . "U6IG9iamVjdCBjbGFzcyBub3Qgc3BlY2lmaWVk"));
use Claromentis\Core\Acl\Exception\InvalidSubjectException;
use Claromentis\Core\Acl\Exception\NoObjectException;
use Claromentis\Core\DAL;
use Claromentis\Core\Security\SecurityContext;
use Claromentis\Core\Services;





class AclRepository
{
    private $db;
    private $cache;
    public function __construct(DAL\Db $db, \ClaCacheHandler $_c38eecfdb2f424d044d37360b72af861)
    {
        $this->db = $db;
        $this->cache = $_c38eecfdb2f424d044d37360b72af861;
    }
    
    
    
    
    public static function I()
    {
        return Services::I()->acl;
    }
    
    
    
    
    
    
    
    
    public function Get(int $_f48acc58bf4e08e63b1173fb1800d0f6, int $object_id)
    {
        $acl = new Acl($_f48acc58bf4e08e63b1173fb1800d0f6, $object_id);
        if ($object_id > 0) {
            $this->Load($acl);
        }
        return $acl;
    }
    
    
    
    
    
    
    
    
    public function Load(Acl $acl)
    {
        if (!$acl->object_id) {
            throw new NoObjectException($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_13753452faddc2d6579d9" . "ba291d77955"]);
        }
        if (!$acl->class_id) {
            throw new NoObjectException($GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_5ca1c8340" . "633c3313f8" . "86f2a4dccb" . "7e8"]);
        }
        $acl->Clear();
        $_4e6a0dcf937145a7f2e335810c4e2d07 = $this->GetCacheKey($acl);
        $_ed121af062079329173bb510b5a82097 = $this->cache->Get($_4e6a0dcf937145a7f2e335810c4e2d07);
        if ($_ed121af062079329173bb510b5a82097 === null) {
            $_1f24efd4735b7a11b391147435e7d35d = $this->db->query($GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_b794b4b6e9" . "6b498c4b34d" . "e980e60c7c7"], $acl->object_id, $acl->class_id);
            $_ed121af062079329173bb510b5a82097 = [];
            while ($_b8cfc2e06d9d1fec1893ea4a054002bf = $_1f24efd4735b7a11b391147435e7d35d->fetchRow()) {
                $_ed121af062079329173bb510b5a82097[] = $_b8cfc2e06d9d1fec1893ea4a054002bf;
            }
            $this->cache->Set($_4e6a0dcf937145a7f2e335810c4e2d07, $_ed121af062079329173bb510b5a82097);
        }
        foreach ($_ed121af062079329173bb510b5a82097 as $_b8cfc2e06d9d1fec1893ea4a054002bf) {
            list($_0f0ee93b89a6a83a69ad4a5909c20cb0, $_1681112f53987f6e29f76d473c53a8f4, $perms) = $_b8cfc2e06d9d1fec1893ea4a054002bf;
            $acl->Add($perms, $_0f0ee93b89a6a83a69ad4a5909c20cb0, $_1681112f53987f6e29f76d473c53a8f4);
        }
    }
    protected function GetCacheKey(Acl $acl)
    {
        return $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_f4ebcb18d6635cf0340b0776aa6c2" . "368"] . $acl->class_id . $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_58d506edba9ce80a" . "f6a061b73e2eb66c"] . $acl->object_id;
    }
    
    
    
    
    public function Save(Acl $acl)
    {
        if (!$acl->object_id) {
            throw new NoObjectException($GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_3f0a984c4" . "75ce692d87" . "e01ddabf63" . "065"]);
        }
        if (!$acl->class_id) {
            throw new NoObjectException($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_966b46f1" . "eeef931d3" . "aab021091" . "17071b"]);
        }
        if ($acl->object_id) {
            $this->Delete($acl);
        }
        foreach ($acl->GetRights() as $_0f0ee93b89a6a83a69ad4a5909c20cb0 => $_3762efdba5c60ca7475e876f0ddacbd8) {
            foreach ($_3762efdba5c60ca7475e876f0ddacbd8 as $_1681112f53987f6e29f76d473c53a8f4 => $perms) {
                if ($perms) {
                    $this->db->query($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_c75934a16a05f05c14432ff269" . "640ec4"] . $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_0d42dc2ab069f31f94b667ad83e" . "04b04"], $acl->class_id, $acl->object_id, $_0f0ee93b89a6a83a69ad4a5909c20cb0, $_1681112f53987f6e29f76d473c53a8f4, $perms);
                }
            }
        }
        $this->cache->Delete($this->GetCacheKey($acl));
    }
    
    
    
    
    
    
    public function Delete(Acl $acl)
    {
        if (!$acl->object_id) {
            throw new NoObjectException($GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_c726d95450d9d5" . "03c7ef86671a4fc" . "991"]);
        }
        if (!$acl->class_id) {
            throw new NoObjectException($GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_3b772c19f5d5e7c8d750ae0a815c7ed" . "7"]);
        }
        $this->db->query($GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_f3a6782a0639ec7ac8ea0a44" . "e06d50fa"], $acl->class_id, $acl->object_id);
        $this->cache->Delete($this->GetCacheKey($acl));
    }
    
    
    
    
    
    
    
    
    
    
    public function Permits(SecurityContext $context, $rights, $_f48acc58bf4e08e63b1173fb1800d0f6, $object_id, $_5e0bec805e42a373f593a284d3fbd303 = 0)
    {
        
        
        if (true || $this->IsCached($_f48acc58bf4e08e63b1173fb1800d0f6, $object_id)) {
            $acl = $this->Get($_f48acc58bf4e08e63b1173fb1800d0f6, $object_id);
            return $acl->Permits($context, $rights, $_5e0bec805e42a373f593a284d3fbd303);
        } else {
            
            
            return $GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_190a27304b863980e14c551" . "4d55f6770"]($_f48acc58bf4e08e63b1173fb1800d0f6, $object_id, $_5e0bec805e42a373f593a284d3fbd303, $rights);
        }
    }
    private function IsCached($_f48acc58bf4e08e63b1173fb1800d0f6, $object_id)
    {
        return $this->cache->Get($this->GetCacheKey(new Acl($_f48acc58bf4e08e63b1173fb1800d0f6, $object_id))) !== null;
    }
}