<?php
namespace Claromentis\Core\Acl\Simple;

$GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"] = (@$GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"] ?: array()) + array("_c95cbc316178c97697a403f4d9124" . "106" => base64_decode("U0VMRUNUIHVzZXJfaWQsIHJpZ2h0cyBGUk9NIHNpbXBsZV9hY2wgV0hFUkUg" . "b2JqZWN0X2NvZGUgZXE6c3RyOmNvZGU="), "_84718d8" . "28d2acc3" . "c366f96b" . "07e841f6" . "a" => base64_decode("YWNsL" . "nNpbX" . "BsZQ=" . "="), "_80fc2a45" . "acd0150df" . "87d1fb728" . "940a60" => base64_decode("U0VMRUNUIHJpZ2h0cyBGUk9NIHNpbXBsZV9hY2wgV0hFUkUgb2JqZWN0X2NvZGUgZXE" . "6c3RyOmNvZGUgQU5EIHVzZXJfaWQ9aW50OnVpZA=="), "_3cda3f34ada928e4aa39d4629c5" . "504aa" => base64_decode("aW50O" . "nJpZ2" . "h0cw=" . "="), "_a7ed4d7408bb99b87a09d89d" . "4212d5de" => base64_decode("c2ltcGx" . "lX2FjbA" . "=="), "_a8b22fedb27a" . "11e07f5bb9a7c" . "73608ea" => base64_decode("c3RyKDEw" . "MCk6b2Jq" . "ZWN0X2Nv" . "ZGU="), "_ef2948c87ef38aab9226e41189b" . "29ca7" => base64_decode("REVMRVRFIEZST00gc2ltcGxlX2FjbCBXSE" . "VSRSBvYmplY3RfY29kZSBlcTpzdHI6Y29k" . "ZQ=="), "_0877a0b48cf" . "424a0de57a45" . "3ce95e30c" => base64_decode("aW50On" . "VzZXJf" . "aWQ="), "_b06968b9268deaf3b380bd0" . "fe922b453" => base64_decode("REVMRVRFIEZST00gc2ltcGxlX2FjbCBXSEVSRSBv" . "YmplY3RfY29kZSBlcTpzdHI6Y29kZSBBTkQgdXNl" . "cl9pZD1pbnQ6dWlk"), "_938704c4c0" . "169c3804d4b" . "d6aa9941a76" => base64_decode("Q2FuJ3QgbG9hZCBBQ0w6IG9iamVj" . "dCBjb2RlIG5vdCBzcGVjaWZpZWQ="), "_dbbe14f567693a631f8a43b65af9" . "17ab" => base64_decode("Q2FuJ3QgZGVsZXRlIEFDTCBmcm9tIGRhdGFiYXNlOiBvYmplY3QgY29" . "kZSBub3Qgc3BlY2lmaWVk"), "_27de5a45db71bf24058769" . "b61b3975a3" => base64_decode("Q2FuJ3Qgc2F2ZSBBQ0w6IG9iamVjdCBjb2RlIG5vdCBzcGVja" . "WZpZWQ="));
use Claromentis\Core\Acl\Exception\NoObjectException;
use Claromentis\Core\DAL;





class SimpleAclRepository
{
    private $db;
    public function __construct(DAL\Db $db)
    {
        $this->db = $db;
    }
    
    
    
    
    public static function I()
    {
        global $g_application;
        return $g_application[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_84718d828d2acc3c36" . "6f96b07e841f6a"]];
    }
    public function IsGranted(string $_65c616d6ef3c21352d2d44595fdf776b, int $user_id, int $perms = SimpleAcl::STANDARD) : bool
    {
        if (!$user_id) {
            return false;
        }
        
        
        
        $_1f24efd4735b7a11b391147435e7d35d = $this->db->query($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_80fc2a45acd0150d" . "f87d1fb728940a60"], $_65c616d6ef3c21352d2d44595fdf776b, $user_id);
        if (!$_1f24efd4735b7a11b391147435e7d35d->hasData()) {
            return false;
        }
        list($p) = $_1f24efd4735b7a11b391147435e7d35d->fetchRow();
        return ($p & $perms) == $perms;
    }
    public function Grant(string $_65c616d6ef3c21352d2d44595fdf776b, int $user_id, int $perms = SimpleAcl::STANDARD)
    {
        $this->db->query(new DAL\QueryInsert($GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_a7ed4d7408" . "bb99b87a09d" . "89d4212d5de"], [$GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_a8b22fedb27a11e07f5bb9a7c7360" . "8ea"] => $_65c616d6ef3c21352d2d44595fdf776b, $GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_0877a0b48cf424a0de57a453ce95" . "e30c"] => $user_id, $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_3cda3f34ada928e4aa39d46" . "29c5504aa"] => $perms]));
    }
    public function Revoke(string $_65c616d6ef3c21352d2d44595fdf776b, int $user_id)
    {
        $this->db->query($GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_b06968b9268" . "deaf3b380bd0" . "fe922b453"], $_65c616d6ef3c21352d2d44595fdf776b, $user_id);
    }
    
    
    
    
    
    
    
    
    public function Get(string $_65c616d6ef3c21352d2d44595fdf776b) : SimpleAcl
    {
        $acl = new SimpleAcl($_65c616d6ef3c21352d2d44595fdf776b);
        $this->Load($acl);
        return $acl;
    }
    
    
    
    
    
    
    public function Load(SimpleAcl $acl)
    {
        if (!$acl->GetObjectCode()) {
            throw new NoObjectException($GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_938704c4c" . "0169c3804d" . "4bd6aa9941" . "a76"]);
        }
        $acl->Clear();
        $_1f24efd4735b7a11b391147435e7d35d = $this->db->query($GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_c95cbc3161" . "78c97697a40" . "3f4d9124106"], $acl->GetObjectCode());
        while (list($user_id, $perms) = $_1f24efd4735b7a11b391147435e7d35d->fetchRow()) {
            $acl->Set($user_id, $perms);
        }
    }
    
    
    
    
    public function Save(SimpleAcl $acl)
    {
        if (!$acl->GetObjectCode()) {
            throw new NoObjectException($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_27de5a45db71bf24" . "058769b61b3975a3"]);
        }
        $this->Delete($acl);
        $_65c616d6ef3c21352d2d44595fdf776b = $acl->GetObjectCode();
        foreach ($acl->GetRights() as $user_id => $perms) {
            if ($perms) {
                $this->Grant($_65c616d6ef3c21352d2d44595fdf776b, $user_id, $perms);
            }
        }
    }
    
    
    
    
    
    
    public function Delete(SimpleAcl $acl)
    {
        if (!$acl->GetObjectCode()) {
            throw new NoObjectException($GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_dbbe14f567693a631f8a43b65a" . "f917ab"]);
        }
        $this->db->query($GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_ef2948c87ef38aab9226e41189b" . "29ca7"], $acl->GetObjectCode());
    }
}