<?php
namespace Claromentis\Quiz\Permissions;

$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"] ?: array()) + array("_3af7126ad354f14d" . "615a9662d83ea489" => base64_decode("c3VydmV5" . "LnBlcm1p" . "c3Npb25z"), "_62df08b6ed8667f8174a" . "96d58970883d" => base64_decode("aW50dmF" . "s"), "_320a678c369b6cf01dd2bb3bb2" . "66b339" => base64_decode("cXVpei5zdXJ2ZXkucGVybWlzc2lvbnMubWFuYWdlX2FsbA=" . "="), "_54b269a12a" . "f880b045f67" . "12e6f865462" => base64_decode("c3RhdHV" . "z"), "_48ae50419f92f51e0a9e1c23382" . "fbf27" => base64_decode("cXVpei5hZG1pbi5ub19wZX" . "Jtc19zdXJ2ZXk="), "_d5383144f621499a79954d604a5d1" . "7b1" => base64_decode("cXVpei5zdXJ2ZXkucGVybWlzc2lvbnMubWFuYWd" . "lX293bg=="), "survey" . "_id" => base64_decode("c3Vyd" . "mV5X2" . "lk"), "_137953344d10e6c5c733469" . "bfd88c770" => base64_decode("cG9sbC5wZXJtaXNzaW9" . "ucw=="), "_9561a795cc75e5361" . "adeea71c940883c" => base64_decode("cXVpei5zdXJ2ZXkucGVybWlzc2lvbnMudmlld19hbGx" . "fcmVzdWx0cw=="), "i" . "d" => base64_decode("aW" . "Q="), "_9b76ea0" . "2294bb77" . "bd233579" . "6f8478c7" . "6" => base64_decode("cXVpei5yZXBv" . "c2l0b3J5LnBv" . "bGw="), "_29e1a79fbc23ef1ab54dba1c57bc" . "e0e8" => base64_decode("L3BvbGxzLXN1cnZl" . "eXM="), "_5295c1a47b6794deef19a2" . "fccae5d11e" => base64_decode("cXVpei5zdXJ2ZXkuYWRtaW4ubm9fcGVybX" . "M="), "_9743f1ba2b" . "dffb4964f4e" . "5a4b1039fc7" => base64_decode("cXVpei5yZXBvc2l0b3J5Ln" . "N1cnZleQ=="), "_647aa6e0ba68d8b414997085ce1a3" . "082" => base64_decode("cXVpei5zdXJ2ZXkucGVybWlzc2lvbi5jYW50" . "X2NyZWF0ZQ=="), "_38f12d8b8c" . "719a8ce0fc2" . "faefc225f35" => base64_decode("YXJyYXlfb" . "WFw"), "po" . "ll" . "_i" . "d" => base64_decode("cG9sbF9p" . "ZA=="));
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["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_9561a795cc75e5361adeea71c" . "940883c"]), 1], self::PERM_MANAGE_OWN => [($this->lmsg)($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_d5383144f" . "621499a799" . "54d604a5d1" . "7b1"]), 1], self::PERM_MANAGE_ALL => [($this->lmsg)($GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_320a678c369b6cf01dd2b" . "b3bb266b339"]), 1]];
    }
    
    
    
    
    
    
    
    public function GetUsersWhoCan($_a48eb1db4f6ab2c82fb9db687c5f4a32)
    {
        $acl = $this->acl->Get(self::CLASS_SURVEY_ADMIN, 1);
        return $GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_38f12d8b8c719a8ce0f" . "c2faefc225f35"]($GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_62df08b6ed8667f8174a96d58" . "970883d"], $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["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["surve" . "y_id"])) {
            $survey_id = $request->attributes->get($GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["su" . "rv" . "ey" . "_i" . "d"]);
        }
        if ($request->attributes->has($GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["poll_i" . "d"])) {
            $poll_id = $request->attributes->get($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["pol" . "l_i" . "d"]);
        }
        
        
        if (!$survey_id && !$poll_id) {
            
            
            if ($this->CanCreate($_6eb6fcb92c942851e079f8ec5fb80324->security)) {
                return null;
            } else {
                return RedirectResponse::httpRedirect($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_29e1a79fbc23ef1ab54dba" . "1c57bce0e8"], ($this->lmsg)($GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_647aa6e0ba" . "68d8b414997" . "085ce1a3082"]), 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["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_29e1a79fbc23ef1ab54dba1c5" . "7bce0e8"], ($this->lmsg)($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_48ae50419f92f51e0a9e" . "1c23382fbf27"]), true);
    }
    protected function CanManageSurvey(Application $_6eb6fcb92c942851e079f8ec5fb80324, int $survey_id)
    {
        
        
        
        
        
        
        $_864ec8f1fa467c206b6bb2b9c0f308bf = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_9743f1ba2bdffb4964f4e5a4b" . "1039fc7"]];
        $survey_permissions = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_3af7126ad354f" . "14d615a9662d83" . "ea489"]];
        $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"]["_9b76ea02294bb77b" . "d2335796f8478c76"]];
        $poll_permissions = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_137953344d10e6c5c733469bfd88" . "c770"]];
        $_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["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["surv" . "ey_i" . "d"])) {
            $survey_id = $request->attributes->get($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["surv" . "ey_i" . "d"]);
        }
        
        
        if (!$survey_id) {
            
            
            if ($this->CanCreate($_6eb6fcb92c942851e079f8ec5fb80324->security)) {
                return null;
            }
            return new JsonResponse([$GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_54b269a12af880b045f6712e6f8" . "65462"] => ($this->lmsg)($GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_5295c1a47b67" . "94deef19a2fcc" . "ae5d11e"])], 403);
        }
        $survey_repo = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_9743f1ba2bdffb4964f" . "4e5a4b1039fc7"]];
        $survey_permissions = $_6eb6fcb92c942851e079f8ec5fb80324[$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_3af7126" . "ad354f14" . "d615a966" . "2d83ea48" . "9"]];
        
        
        
        
        $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["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_54b269a12" . "af880b045f" . "6712e6f865" . "462"] => ($this->lmsg)($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_5295c1a47b6794deef19a2fccae5d1" . "1e"])], 403);
    }
}