<?php
namespace Claromentis\Pages;

$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"] = (@$GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"] ?: array()) + array("permiss" . "ions" => base64_decode("cGVybWlzc2" . "lvbnM="), "_3c4ddc372fc520e813cf6ddef60431" . "3c" => base64_decode("cA=" . "="), "_98b8bed" . "5ac84335" . "016322c4" . "01873c3d" . "4" => base64_decode("UGVybUNoZWNrQX" . "NUd29TUUw="), "_dd63e9d94ef9f220" . "789ba00287ea881a" => base64_decode("cHA" . "="), "_f768b53ccdcd6d1af3142" . "d2183e4f183" => base64_decode("cHA" . "uaW" . "Q="), "_3a8ca761206e525598c65fbe81f34" . "fde" => base64_decode("cGFnZX" . "Nfc2l0" . "ZQ=="), "_4d707fab93bebb00bbe69" . "e8159697c90" => base64_decode(""), "_c0deb9b2df" . "b1f05143463" . "96b78458530" => base64_decode("cGFnZX" . "Nfc2l0" . "ZS5pZA" . "=="));
use Claromentis\Core\Acl\Acl;
use Claromentis\Core\Acl\AclRepository;
use Claromentis\Core\Acl\Exception\AclException;
use Claromentis\Core\DAL;
use Claromentis\Core\Repository\Exception\NotFoundException;
use Claromentis\Core\Security\SecurityContext;
use Claromentis\Pages\Model\MenuItem;
use Claromentis\Pages\Model\Page;
use Claromentis\Pages\Model\PageUrl;
use Claromentis\Pages\Model\Site;
use Claromentis\Pages\Model\Tile;
use Claromentis\Pages\Repository\MenuItemRepository;
use Claromentis\Pages\Repository\PagesRepository;
use Claromentis\Pages\Repository\UrlRepository;




class PagesAcl
{
    protected $acl_repository;
    protected $url_repo;
    protected $pages_repo;
    public function __construct(AclRepository $acl, UrlRepository $_4fd544a0039d23957d8ced73ba74acb9, PagesRepository $pages_repo)
    {
        $this->acl_repository = $acl;
        $this->url_repo = $_4fd544a0039d23957d8ced73ba74acb9;
        $this->pages_repo = $pages_repo;
    }
    public function CanCreateLink(SecurityContext $context, $_a1053c2fc8090400c07497f8420a3587)
    {
        $_720c0c64bd5b02debfb65448083be0bf = $this->pages_repo->GetLatestPageIdForMenuItem($_a1053c2fc8090400c07497f8420a3587);
        return $this->CanEditPageProperties($context, $_720c0c64bd5b02debfb65448083be0bf);
    }
    public function CanCreateSite(SecurityContext $context)
    {
        return $this->acl_repository->Permits($context, Perm::ADD_SITES, Perm::CLASS_SITES_ADMIN, 1);
    }
    public function CanEditSite(SecurityContext $context, $site_id)
    {
        return $this->IsSiteAdmin($context, $site_id);
    }
    public function CanDeleteSite(SecurityContext $context, $site_id)
    {
        return $this->IsSiteAdmin($context, $site_id);
    }
    public function CanViewUrl(SecurityContext $context, $url_id)
    {
        $_720c0c64bd5b02debfb65448083be0bf = $this->pages_repo->GetLatestPageIdForUrl($url_id);
        if ($this->IsSiteAdmin($context, null, $_720c0c64bd5b02debfb65448083be0bf)) {
            return true;
        }
        return $this->acl_repository->Permits($context, Perm::PAGE_VIEW, Perm::CLASS_PAGE, $_720c0c64bd5b02debfb65448083be0bf);
    }
    public function CanViewPage(SecurityContext $context, $_720c0c64bd5b02debfb65448083be0bf)
    {
        if ($this->IsSiteAdmin($context, null, $_720c0c64bd5b02debfb65448083be0bf)) {
            return true;
        }
        return $this->acl_repository->Permits($context, Perm::PAGE_VIEW, Perm::CLASS_PAGE, $_720c0c64bd5b02debfb65448083be0bf);
    }
    public function CanDeleteMenuItem(SecurityContext $context, MenuItem $_d43c6db6d25c2749f65bc7bc28bd8df3)
    {
        $url_id = $_d43c6db6d25c2749f65bc7bc28bd8df3->url_id;
        if ($url_id == 0) {
            $url_id = $this->pages_repo->GetNonLinkParentUrl($_d43c6db6d25c2749f65bc7bc28bd8df3->id);
        }
        $_720c0c64bd5b02debfb65448083be0bf = $this->pages_repo->GetLatestPageIdForUrl($url_id);
        return $this->CanEditPageProperties($context, $_720c0c64bd5b02debfb65448083be0bf);
    }
    public function CanMoveChildMenuItems(SecurityContext $context, $_720c0c64bd5b02debfb65448083be0bf)
    {
        return $this->CanEditPageProperties($context, $_720c0c64bd5b02debfb65448083be0bf);
    }
    public function CanShowOrHideMenuItem(SecurityContext $context, MenuItem $_d43c6db6d25c2749f65bc7bc28bd8df3)
    {
        $url_id = $_d43c6db6d25c2749f65bc7bc28bd8df3->url_id;
        if ($url_id == 0) {
            $url_id = $this->pages_repo->GetNonLinkParentUrl($_d43c6db6d25c2749f65bc7bc28bd8df3->id);
        }
        $_720c0c64bd5b02debfb65448083be0bf = $this->pages_repo->GetLatestPageIdForUrl($url_id);
        return $this->CanEditPageProperties($context, $_720c0c64bd5b02debfb65448083be0bf);
    }
    
    
    
    
    
    
    
    
    
    public function CanEditDraft(SecurityContext $security, PageUrl $page_url)
    {
        return $this->CanEditPageProperties($security, $page_url->latest_page_id);
    }
    
    
    
    
    
    
    
    
    
    public function CanEditPage(SecurityContext $security, Page $_221da17e3055fae8d3ff516b832a5f3b)
    {
        if (!$_221da17e3055fae8d3ff516b832a5f3b->is_draft || $_221da17e3055fae8d3ff516b832a5f3b->editor_id != $security->GetUserId()) {
            return false;
        }
        if ($this->IsSiteAdmin($security, null, $_221da17e3055fae8d3ff516b832a5f3b->id)) {
            return true;
        }
        $_5fdf10084135f372540117b2d9fb79fb = $this->pages_repo->GetLatestPageIdForUrl($_221da17e3055fae8d3ff516b832a5f3b->url_id);
        return $this->CanEditPageProperties($security, $_5fdf10084135f372540117b2d9fb79fb);
    }
    
    
    
    
    
    
    
    public function CanEditPageProperties(SecurityContext $context, $_720c0c64bd5b02debfb65448083be0bf)
    {
        if ($this->IsSiteAdmin($context, null, $_720c0c64bd5b02debfb65448083be0bf)) {
            return true;
        }
        return $this->acl_repository->Permits($context, Perm::PAGE_EDIT, Perm::CLASS_PAGE, $_720c0c64bd5b02debfb65448083be0bf);
    }
    
    
    
    
    
    
    
    
    
    public function HasTilePermissions(SecurityContext $security, Tile $tile, $permissions)
    {
        if ($permissions & Perm::PAGE_EDIT) {
            $_221da17e3055fae8d3ff516b832a5f3b = $this->pages_repo->GetById($tile->GetPageId());
            return $this->CanEditPage($security, $_221da17e3055fae8d3ff516b832a5f3b);
        }
        if ($permissions == Perm::PAGE_VIEW) {
            $_221da17e3055fae8d3ff516b832a5f3b = $this->pages_repo->GetById($tile->GetPageId());
            return $this->CanViewPage($security, $_221da17e3055fae8d3ff516b832a5f3b->id);
        }
        return false;
    }
    
    
    
    
    
    
    
    
    public function CanListComponents(SecurityContext $context)
    {
        return $context->GetUserId() > 0;
    }
    
    
    
    
    
    
    
    public function CanViewCategories(SecurityContext $context)
    {
        return $this->CanListComponents($context);
    }
    public function ApplySitesPermissions(SecurityContext $context, DAL\QueryBuilder $_4c8189237e0d0b5cd82c06d69613c88e, $_a48eb1db4f6ab2c82fb9db687c5f4a32 = Perm::SITE_MANAGE)
    {
        list($_bdfba04e3e7f4c28fa5e00cda2d66752, $_c7598a9614976977e540b17c67707384) = $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_98b8bed5ac84335016322c401873c" . "3d4"](Perm::CLASS_SITE, $GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_c0deb9b" . "2dfb1f05" . "14346396" . "b7845853" . "0"], $_a48eb1db4f6ab2c82fb9db687c5f4a32, $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_4d707fab93bebb00bbe69e815" . "9697c90"], $GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_3c4ddc372fc520e8" . "13cf6ddef604313c"]);
        
        
        $_4c8189237e0d0b5cd82c06d69613c88e->AddJoin($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_3a8ca761206e525598c" . "65fbe81f34fde"], $GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["perm" . "issi" . "ons"], $GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_3c4ddc372fc520e813cf6ddef6" . "04313c"], $_bdfba04e3e7f4c28fa5e00cda2d66752);
        $_4c8189237e0d0b5cd82c06d69613c88e->AddWhereAndClause($_c7598a9614976977e540b17c67707384);
    }
    public function ApplyPagesPermissions(SecurityContext $context, DAL\QueryBuilder $_4c8189237e0d0b5cd82c06d69613c88e, $_a48eb1db4f6ab2c82fb9db687c5f4a32 = Perm::PAGE_VIEW)
    {
        list($_bdfba04e3e7f4c28fa5e00cda2d66752, $_c7598a9614976977e540b17c67707384) = $GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_98b8bed5ac8" . "4335016322c4" . "01873c3d4"](Perm::CLASS_PAGE, $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_f768b53ccdcd" . "6d1af3142d218" . "3e4f183"], $_a48eb1db4f6ab2c82fb9db687c5f4a32, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_4d707fab93be" . "bb00bbe69e815" . "9697c90"], $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_3c4ddc372fc" . "520e813cf6dd" . "ef604313c"]);
        
        
        $_4c8189237e0d0b5cd82c06d69613c88e->AddJoin($GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_dd63e9d94e" . "f9f220789ba" . "00287ea881a"], $GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["permission" . "s"], $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_3c4ddc372fc520e813cf6ddef604313" . "c"], $_bdfba04e3e7f4c28fa5e00cda2d66752);
        $_4c8189237e0d0b5cd82c06d69613c88e->AddWhereAndClause($_c7598a9614976977e540b17c67707384);
    }
    
    
    
    
    
    
    
    
    
    public function SetSiteAcl($site_id, Acl $acl)
    {
        $_00467751a7f02e8265821fee2c132f6e = $acl->CreateCopy(Perm::CLASS_SITE, $site_id);
        $this->acl_repository->Save($_00467751a7f02e8265821fee2c132f6e);
        return $_00467751a7f02e8265821fee2c132f6e;
    }
    public function SetPageAcl($_720c0c64bd5b02debfb65448083be0bf, Acl $acl)
    {
        $_00467751a7f02e8265821fee2c132f6e = $acl->CreateCopy(Perm::CLASS_PAGE, $_720c0c64bd5b02debfb65448083be0bf);
        $this->acl_repository->Save($_00467751a7f02e8265821fee2c132f6e);
        return $_00467751a7f02e8265821fee2c132f6e;
    }
    public function GetPageAcl(int $_720c0c64bd5b02debfb65448083be0bf) : Acl
    {
        return $this->acl_repository->Get(Perm::CLASS_PAGE, $_720c0c64bd5b02debfb65448083be0bf);
    }
    public function GetSiteAcl(int $site_id) : Acl
    {
        return $this->acl_repository->Get(Perm::CLASS_SITE, $site_id);
    }
    public function CanViewSiteContent(SecurityContext $context, Site $_ad6d1395fad94fb16ed12d8007727518)
    {
        $_720c0c64bd5b02debfb65448083be0bf = $this->pages_repo->GetLatestPageIdForUrl($_ad6d1395fad94fb16ed12d8007727518->url_id);
        return $this->CanViewPage($context, $_720c0c64bd5b02debfb65448083be0bf);
    }
    public function CanEditSiteContent(SecurityContext $security, $site_id)
    {
        return $this->IsSiteAdmin($security, $site_id);
    }
    public function CanDeleteSiteContent(SecurityContext $security, $site_id)
    {
        return $this->IsSiteAdmin($security, $site_id);
    }
    public function InheritFromParent($_720c0c64bd5b02debfb65448083be0bf, $_6e38cb9e043a07cd69a1711a6704c62a)
    {
        $acl = $this->GetPageAcl($_6e38cb9e043a07cd69a1711a6704c62a);
        $acl->SetObjectId($_720c0c64bd5b02debfb65448083be0bf);
        $this->SetPageAcl($_720c0c64bd5b02debfb65448083be0bf, $acl);
        return $acl;
    }
    public function IsSiteAdmin(SecurityContext $security, $site_id = null, $_720c0c64bd5b02debfb65448083be0bf = null)
    {
        if ($_720c0c64bd5b02debfb65448083be0bf !== null) {
            $site_id = $this->pages_repo->GetSiteIdForPage($_720c0c64bd5b02debfb65448083be0bf);
        }
        if ($site_id === null) {
            return false;
        }
        return $this->acl_repository->Permits($security, Perm::SITE_MANAGE, Perm::CLASS_SITE, $site_id);
    }
    public function CanAccessSiteTree(SecurityContext $security, $site_id)
    {
        if ($this->IsSiteAdmin($security, $site_id)) {
            return true;
        }
        $_2372fff6e6df76f2cb649f85ba60dd81 = $this->pages_repo->GetPagesForSiteId($site_id);
        foreach ($_2372fff6e6df76f2cb649f85ba60dd81 as $_720c0c64bd5b02debfb65448083be0bf => $_e8b500aea78ceaa49741651ee1a43de2) {
            if ($this->acl_repository->Permits($security, Perm::PAGE_EDIT, Perm::CLASS_PAGE, $_720c0c64bd5b02debfb65448083be0bf)) {
                return true;
            }
        }
    }
}