<?php
namespace Claromentis\Quiz\Permissions;

$GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"] ?: array()) + array("i" . "d" => base64_decode("aWQ" . "="), "_d5383144f6214" . "99a79954d604a5" . "d17b1" => base64_decode("cXVpei5zdXJ2ZXkucGVybWlzc2lvbnMub" . "WFuYWdlX293bg=="), "_9b76ea02294bb77bd" . "2335796f8478c76" => base64_decode("cXVpei5yZXBvc" . "2l0b3J5LnBvbG" . "w="), "_5295c1a47b6794de" . "ef19a2fccae5d11e" => base64_decode("cXVpei5zdXJ2ZXkuYWRta" . "W4ubm9fcGVybXM="), "_9743f1ba2bdffb4964f4e5a4b103" . "9fc7" => base64_decode("cXVpei5yZXBvc" . "2l0b3J5LnN1cn" . "ZleQ=="), "_320a678c369b6c" . "f01dd2bb3bb266b" . "339" => base64_decode("cXVpei5zdXJ2ZXkucGVybWlzc2lvbnMubWFuYWdl" . "X2FsbA=="), "_48ae50419f92f51e0a9" . "e1c23382fbf27" => base64_decode("cXVpei5hZG1pb" . "i5ub19wZXJtc1" . "9zdXJ2ZXk="), "_54b269a12af880b045f671" . "2e6f865462" => base64_decode("c3Rh" . "dHVz"), "_137953344d10e6c5c733469b" . "fd88c770" => base64_decode("cG9sbC5wZXJtaXNzaW" . "9ucw=="), "_62df08b6ed8" . "667f8174a96d" . "58970883d" => base64_decode("aW50dmF" . "s"), "poll_" . "id" => base64_decode("cG9sbF9pZA" . "=="), "_9561a795cc75e5361adeea7" . "1c940883c" => base64_decode("cXVpei5zdXJ2ZXkucGVybWlzc2lvbnMudmlld19hbGxfcmVzdWx0c" . "w=="), "sur" . "vey" . "_id" => base64_decode("c3Vyd" . "mV5X2" . "lk"), "_3af7126ad354f14d615a966" . "2d83ea489" => base64_decode("c3VydmV5LnBlcm1pc3Npb25" . "z"), "_38f12d8b8c719a8ce0fc2faefc225" . "f35" => base64_decode("YXJyY" . "XlfbW" . "Fw"), "_29e1a79fbc23e" . "f1ab54dba1c57b" . "ce0e8" => base64_decode("L3BvbGxzL" . "XN1cnZleX" . "M="), "_647aa6e0ba68d8b4" . "14997085ce1a3082" => base64_decode("cXVpei5zdXJ2ZXkucGVybWlzc2lv" . "bi5jYW50X2NyZWF0ZQ=="));
use Claromentis\Core\Acl;
use Claromentis\Core\Application;
use Claromentis\Core\Http\RedirectResponse;
use Claromentis\Core\Http\SymfonyRequest;
use Claromentis\Core\Localization\Lmsg;
use Claromentis\Core\Security\SecurityContext;
use Symfony\Component\HttpFoundation\JsonResponse;




class SurveyAdminPermissions
{
    
    
    const CLASS_SURVEY_ADMIN = 147;
    
    
    const PERM_VIEW_RESULTS = 0x100;
    const PERM_MANAGE_OWN = 0x10;
    const PERM_MANAGE_ALL = 0x111;
    protected $acl = null;
    protected $lmsg = null;
    public function __construct(Acl\AclRepository $acl, Lmsg $lmsg)
    {
        $this->acl = $acl;
        $this->lmsg = $lmsg;
    }
    
    
    
    
    
    
    public function GetDialog()
    {
        $acl = $this->acl->Get(self::CLASS_SURVEY_ADMIN, 1);
        $_199355a18b7f1825de748a3bdea73bdb = new Acl\CommonPermsDialog($acl);
        
        
        $_199355a18b7f1825de748a3bdea73bdb->max_guest_perms = 0;
        $_199355a18b7f1825de748a3bdea73bdb->SetAllPermValues($this->GetDialogValues());
        return $_199355a18b7f1825de748a3bdea73bdb;
    }
    
    
    
    
    
    
    public function GetDialogValues()
    {
        return [self::PERM_VIEW_RESULTS => [($this->lmsg)($GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_9561a795c" . "c75e5361ad" . "eea71c9408" . "83c"]), 1], self::PERM_MANAGE_OWN => [($this->lmsg)($GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_d5383144f621499a79954d604" . "a5d17b1"]), 1], self::PERM_MANAGE_ALL => [($this->lmsg)($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_320a678c" . "369b6cf01" . "dd2bb3bb2" . "66b339"]), 1]];
    }
    
    
    
    
    
    
    
    public function GetUsersWhoCan($_a48eb1db4f6ab2c82fb9db687c5f4a32)
    {
        $acl = $this->acl->Get(self::CLASS_SURVEY_ADMIN, 1);
        return $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_38f12d8b8c719a8ce0fc" . "2faefc225f35"]($GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_62df08b6ed8667" . "f8174a96d589708" . "83d"], $acl->GetIndividualsList($_a48eb1db4f6ab2c82fb9db687c5f4a32));
    }
    
    
    
    
    
    
    
    public function CanCreate(SecurityContext $context)
    {
        $acl = $this->acl->Get(self::CLASS_SURVEY_ADMIN, 1);
        
        
        return $acl->Permits($context, self::PERM_MANAGE_OWN) || $acl->Permits($context, self::PERM_MANAGE_ALL);
    }
    
    
    
    
    
    
    
    public function CanManageOwn(SecurityContext $context)
    {
        $acl = $this->acl->Get(self::CLASS_SURVEY_ADMIN, 1);
        return $acl->Permits($context, self::PERM_MANAGE_OWN);
    }
    
    
    
    
    
    
    
    public function CanManageAll(SecurityContext $context)
    {
        $acl = $this->acl->Get(self::CLASS_SURVEY_ADMIN, 1);
        return $acl->Permits($context, self::PERM_MANAGE_ALL);
    }
    
    
    
    
    
    
    
    public function CanViewResults(SecurityContext $context)
    {
        
        
        if ($this->CanManageAll($context)) {
            return true;
        }
        $acl = $this->acl->Get(self::CLASS_SURVEY_ADMIN, 1);
        return $acl->Permits($context, self::PERM_VIEW_RESULTS);
    }
    
    
    
    
    
    
    
    public function CanViewAll(SecurityContext $context)
    {
        return $this->CanManageAll($context) || $this->CanViewResults($context);
    }
    
    
    
    
    
    
    
    public function CanAttemptAll(SecurityContext $context)
    {
        return $this->CanManageAll($context);
    }
    public function BeforeHook(SymfonyRequest $request, Application $_6eb6fcb92c942851e079f8ec5fb80324)
    {
        
        
        if ($this->CanManageAll($_6eb6fcb92c942851e079f8ec5fb80324->security)) {
            return null;
        }
        $survey_id = 0;
        $poll_id = 0;
        
        
        if ($request->attributes->has($GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["survey_i" . "d"])) {
            $survey_id = $request->attributes->get($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["surve" . "y_id"]);
        }
        if ($request->attributes->has($GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["poll_i" . "d"])) {
            $poll_id = $request->attributes->get($GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["pol" . "l_i" . "d"]);
        }
        
        
        if (!$survey_id && !$poll_id) {
            
            
            if ($this->CanCreate($_6eb6fcb92c942851e079f8ec5fb80324->security)) {
                return null;
            } else {
                return RedirectResponse::httpRedirect($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_29e1a79fbc23ef" . "1ab54dba1c57bce" . "0e8"], ($this->lmsg)($GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_647aa6e0ba68d8b41499" . "7085ce1a3082"]), true);
            }
        }
        
        
        if ($survey_id && $this->CanManageSurvey($_6eb6fcb92c942851e079f8ec5fb80324, $survey_id)) {
            return null;
        } elseif ($poll_id && $this->CanManagePoll($_6eb6fcb92c942851e079f8ec5fb80324, $poll_id)) {
            return null;
        }
        return RedirectResponse::httpRedirect($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_29e1a79fb" . "c23ef1ab54" . "dba1c57bce" . "0e8"], ($this->lmsg)($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_48ae50419f92f51e0a9e1c23382f" . "bf27"]), true);
    }
    protected function CanManageSurvey(Application $_6eb6fcb92c942851e079f8ec5fb80324, int $survey_id)
    {
        
        
        
        
        
        
        $_864ec8f1fa467c206b6bb2b9c0f308bf = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_9743f1ba2bdffb4964f4e" . "5a4b1039fc7"]];
        $survey_permissions = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_3af7126ad354f14d615a" . "9662d83ea489"]];
        $survey = $_864ec8f1fa467c206b6bb2b9c0f308bf->GetOrNew($survey_id);
        return $survey_permissions->CanManage($_6eb6fcb92c942851e079f8ec5fb80324->security, $survey);
    }
    protected function CanManagePoll(Application $_6eb6fcb92c942851e079f8ec5fb80324, int $poll_id)
    {
        
        
        
        
        
        
        $_864ec8f1fa467c206b6bb2b9c0f308bf = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_9b76ea02294b" . "b77bd2335796f" . "8478c76"]];
        $poll_permissions = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_1379533" . "44d10e6c" . "5c733469" . "bfd88c77" . "0"]];
        $_b3ee1ca1d7e4bff63ae777968483325d = $_864ec8f1fa467c206b6bb2b9c0f308bf->GetOrNew($poll_id);
        return $poll_permissions->CanManage($_6eb6fcb92c942851e079f8ec5fb80324->security, $_b3ee1ca1d7e4bff63ae777968483325d);
    }
    public function BeforeHookApi(SymfonyRequest $request, Application $_6eb6fcb92c942851e079f8ec5fb80324)
    {
        if ($this->CanManageAll($_6eb6fcb92c942851e079f8ec5fb80324->security)) {
            return null;
        }
        $survey_id = 0;
        
        
        if ($request->attributes->has($GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["survey" . "_id"])) {
            $survey_id = $request->attributes->get($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["su" . "rv" . "ey" . "_i" . "d"]);
        }
        
        
        if (!$survey_id) {
            
            
            if ($this->CanCreate($_6eb6fcb92c942851e079f8ec5fb80324->security)) {
                return null;
            }
            return new JsonResponse([$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_54b269a12af880b045f67" . "12e6f865462"] => ($this->lmsg)($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_5295c1a47b" . "6794deef19a" . "2fccae5d11e"])], 403);
        }
        $survey_repo = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_9743f1ba2bdffb4964f4e5" . "a4b1039fc7"]];
        $survey_permissions = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_3af7126a" . "d354f14d6" . "15a9662d8" . "3ea489"]];
        
        
        
        
        $survey = $survey_repo->GetOrNew((int) $survey_id);
        if (!isset($survey->id)) {
            $survey->id = (int) $survey_id;
            $survey->__set($GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["i" . "d"], (int) $survey_id);
        }
        $_b86962d657d34c804fd37edbef8af5e4 = $survey_permissions->CanManage($_6eb6fcb92c942851e079f8ec5fb80324->security, $survey);
        
        
        if ($_b86962d657d34c804fd37edbef8af5e4) {
            return null;
        }
        return new JsonResponse([$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_54b269a12af880b045f6712" . "e6f865462"] => ($this->lmsg)($GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_5295c1a47b6794deef19" . "a2fccae5d11e"])], 403);
    }
}