<?php
namespace Claromentis\Quiz\Permissions;

$GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"] = (@$GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"] ?: array()) + array("i" . "d" => base64_decode("a" . "W" . "Q" . "="), "_320a678c" . "369b6cf01" . "dd2bb3bb2" . "66b339" => base64_decode("cXVpei5zdXJ2ZXkucGVybWlzc2lvbnMubWFuYWdlX2FsbA=" . "="), "_3af7126ad354" . "f14d615a9662d" . "83ea489" => base64_decode("c3VydmV5LnB" . "lcm1pc3Npb2" . "5z"), "_62df08b6ed8667f8174a" . "96d58970883d" => base64_decode("aW50d" . "mFs"), "_9561a795cc" . "75e5361adee" . "a71c940883c" => base64_decode("cXVpei5zdXJ2ZXkucGVybWlzc2lvbnMudmlld19hbGxfcmVzdWx0" . "cw=="), "_29e1a79fbc23ef1ab54dba" . "1c57bce0e8" => base64_decode("L3BvbGxzLXN1cnZle" . "XM="), "_5295c1a" . "47b6794d" . "eef19a2f" . "ccae5d11" . "e" => base64_decode("cXVpei5zdXJ2ZXkuYWRtaW4u" . "bm9fcGVybXM="), "poll_" . "id" => base64_decode("cG9sbF9" . "pZA=="), "_d538314" . "4f621499" . "a79954d6" . "04a5d17b" . "1" => base64_decode("cXVpei5zdXJ2ZXkucGVybWlzc2lvbnMubWFuYWdlX293b" . "g=="), "_137953344d10e6c5c73346" . "9bfd88c770" => base64_decode("cG9sbC5wZXJta" . "XNzaW9ucw=="), "_9743f1ba2bdffb4964f4e5" . "a4b1039fc7" => base64_decode("cXVpei5yZXBvc2l0b3" . "J5LnN1cnZleQ=="), "_9b76ea02294bb77bd2335" . "796f8478c76" => base64_decode("cXVpei5yZXBvc2l0b3J5LnB" . "vbGw="), "_647aa6e0ba68d8b414997085ce1a308" . "2" => base64_decode("cXVpei5zdXJ2ZXkucGVybWlzc2lvbi5jYW50X2NyZWF0Z" . "Q=="), "_54b269a" . "12af880b" . "045f6712" . "e6f86546" . "2" => base64_decode("c3R" . "hdH" . "Vz"), "_38f12d8b8c719a8ce0" . "fc2faefc225f35" => base64_decode("YXJ" . "yYX" . "lfb" . "WFw"), "su" . "rv" . "ey" . "_i" . "d" => base64_decode("c3VydmV5X2l" . "k"), "_48ae50419f92f5" . "1e0a9e1c23382fb" . "f27" => base64_decode("cXVpei5hZG1pbi" . "5ub19wZXJtc19z" . "dXJ2ZXk="));
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"]["_9561a795cc" . "75e5361adee" . "a71c940883c"]), 1], self::PERM_MANAGE_OWN => [($this->lmsg)($GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_d5383144f621499a79954d604" . "a5d17b1"]), 1], self::PERM_MANAGE_ALL => [($this->lmsg)($GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_320a678c369b6cf01dd2bb" . "3bb266b339"]), 1]];
    }
    
    
    
    
    
    
    
    public function GetUsersWhoCan($_a48eb1db4f6ab2c82fb9db687c5f4a32)
    {
        $acl = $this->acl->Get(self::CLASS_SURVEY_ADMIN, 1);
        return $GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_38f12d8b8c719" . "a8ce0fc2faefc2" . "25f35"]($GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_62df08b6ed8667f8174a96d5" . "8970883d"], $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["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["survey" . "_id"])) {
            $survey_id = $request->attributes->get($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["survey_i" . "d"]);
        }
        if ($request->attributes->has($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["poll_i" . "d"])) {
            $poll_id = $request->attributes->get($GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["po" . "ll" . "_i" . "d"]);
        }
        
        
        if (!$survey_id && !$poll_id) {
            
            
            if ($this->CanCreate($_6eb6fcb92c942851e079f8ec5fb80324->security)) {
                return null;
            } else {
                return RedirectResponse::httpRedirect($GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_29e1a79f" . "bc23ef1ab" . "54dba1c57" . "bce0e8"], ($this->lmsg)($GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_647aa6e0ba68d8b414997085ce" . "1a3082"]), 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["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_29e1a79fbc23ef1ab54dba1c57b" . "ce0e8"], ($this->lmsg)($GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_48ae50419f92f51e0a9e1c2338" . "2fbf27"]), true);
    }
    protected function CanManageSurvey(Application $_6eb6fcb92c942851e079f8ec5fb80324, int $survey_id)
    {
        
        
        
        
        
        
        $_864ec8f1fa467c206b6bb2b9c0f308bf = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_9743f1ba2bdffb496" . "4f4e5a4b1039fc7"]];
        $survey_permissions = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_3af7126a" . "d354f14d6" . "15a9662d8" . "3ea489"]];
        $survey = $_864ec8f1fa467c206b6bb2b9c0f308bf->GetOrNew($survey_id);
        return $survey_permissions->CanManage($_6eb6fcb92c942851e079f8ec5fb80324->security, $survey);
    }
    protected function CanManagePoll(Application $_6eb6fcb92c942851e079f8ec5fb80324, int $poll_id)
    {
        
        
        
        
        
        
        $_864ec8f1fa467c206b6bb2b9c0f308bf = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_9b76ea02294bb77bd" . "2335796f8478c76"]];
        $poll_permissions = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_137953344d10e6" . "c5c733469bfd88c" . "770"]];
        $_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["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["sur" . "vey" . "_id"])) {
            $survey_id = $request->attributes->get($GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["survey" . "_id"]);
        }
        
        
        if (!$survey_id) {
            
            
            if ($this->CanCreate($_6eb6fcb92c942851e079f8ec5fb80324->security)) {
                return null;
            }
            return new JsonResponse([$GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_54b269a12af880b045f671" . "2e6f865462"] => ($this->lmsg)($GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_5295c1a47b6794deef19a" . "2fccae5d11e"])], 403);
        }
        $survey_repo = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_9743f1ba2bd" . "ffb4964f4e5a" . "4b1039fc7"]];
        $survey_permissions = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_3af7126ad35" . "4f14d615a966" . "2d83ea489"]];
        
        
        
        
        $survey = $survey_repo->GetOrNew((int) $survey_id);
        if (!isset($survey->id)) {
            $survey->id = (int) $survey_id;
            $survey->__set($GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["i" . "d"], (int) $survey_id);
        }
        $_b86962d657d34c804fd37edbef8af5e4 = $survey_permissions->CanManage($_6eb6fcb92c942851e079f8ec5fb80324->security, $survey);
        
        
        if ($_b86962d657d34c804fd37edbef8af5e4) {
            return null;
        }
        return new JsonResponse([$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_54b269a12af880b045f6712e6f8" . "65462"] => ($this->lmsg)($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_5295c1a47b6794deef19a2fcc" . "ae5d11e"])], 403);
    }
}