Skip to content
Extraits de code Groupes Projets
FakeAccess.php 5,09 ko
Newer Older
  • Learn to ignore specific revisions
  • sgiehl's avatar
    sgiehl a validé
    <?php
    /**
     * Piwik - Open source web analytics
     *
     * @link http://piwik.org
     * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
     */
    
    use Piwik\Site;
    
    
    sgiehl's avatar
    sgiehl a validé
    /**
     * FakeAccess for UnitTests
     */
    class FakeAccess
    {
        public static $superUser = false;
        public static $idSitesAdmin = array();
        public static $idSitesView = array();
        public static $identity = 'superUserLogin';
    
        public static $superUserLogin = 'superUserLogin';
    
    mattab's avatar
    mattab a validé
        public function getTokenAuth()
        {
            return false;
        }
    
    
        public function __construct()
        {
    
            self::$idSitesAdmin = array();
    
            self::$idSitesView = array();
            self::$identity = 'superUserLogin';
    
    sgiehl's avatar
    sgiehl a validé
        public static function setIdSitesAdmin($ids)
        {
            self::$superUser = false;
            self::$idSitesAdmin = $ids;
        }
    
    sgiehl's avatar
    sgiehl a validé
        public static function setIdSitesView($ids)
        {
            self::$superUser = false;
            self::$idSitesView = $ids;
        }
    
    mattab's avatar
    mattab a validé
        public static function hasSuperUserAccess()
        {
            return self::$superUser;
        }
    
    
        public static function checkUserHasSuperUserAccess()
    
    sgiehl's avatar
    sgiehl a validé
        {
    
                throw new Exception("checkUserHasSuperUserAccess Fake exception // string not to be tested");
    
    sgiehl's avatar
    sgiehl a validé
            }
        }
    
        /**
         * @see FakeAccess::checkUserHasSuperUserAccess()
         * @deprecated deprecated since version 2.0.4
         */
    
        public function checkUserIsSuperUser()
    
        public static function setSuperUserAccess($bool = true)
    
    sgiehl's avatar
    sgiehl a validé
        {
            self::$superUser = $bool;
        }
    
    
         * @see FakeAccess::setSuperUserAccess()
    
         * @deprecated deprecated since version 2.0.4
         */
        public static function setSuperUser($bool = true)
        {
            self::setSuperUserAccess($bool);
        }
    
    
    sgiehl's avatar
    sgiehl a validé
        public static function reloadAccess()
    
    sgiehl's avatar
    sgiehl a validé
    
    
        public static function checkUserHasAdminAccess($idSites)
    
    sgiehl's avatar
    sgiehl a validé
        {
    
            if (!self::$superUser) {
                $websitesAccess = self::$idSitesAdmin;
            } else {
    
                $websitesAccess = API::getInstance()->getAllSitesId();
    
    sgiehl's avatar
    sgiehl a validé
            }
    
            $idSites = Site::getIdSitesFromIdSitesString($idSites);
    
            foreach ($idSites as $idsite) {
                if (!in_array($idsite, $websitesAccess)) {
    
    sgiehl's avatar
    sgiehl a validé
                    throw new Exception("checkUserHasAdminAccess Fake exception // string not to be tested");
                }
            }
        }
    
    sgiehl's avatar
    sgiehl a validé
        //means at least view access
    
        public static function checkUserHasViewAccess($idSites)
    
    sgiehl's avatar
    sgiehl a validé
        {
    
    Thomas Steur's avatar
    Thomas Steur a validé
            if (self::$superUser) {
                return;
    
    sgiehl's avatar
    sgiehl a validé
            }
    
    Thomas Steur's avatar
    Thomas Steur a validé
            
            $websitesAccess = array_merge(self::$idSitesView, self::$idSitesAdmin);
    
    
            if (!is_array($idSites)) {
                if ($idSites == 'all') {
    
                    $idSites = API::getInstance()->getAllSitesId();
    
                } else {
                    $idSites = explode(',', $idSites);
                }
    
    sgiehl's avatar
    sgiehl a validé
            }
    
    
            if (empty($websitesAccess)) {
                throw new Exception("checkUserHasViewAccess Fake exception // string not to be tested");
    
    
            foreach ($idSites as $idsite) {
                if (!in_array($idsite, $websitesAccess)) {
    
    sgiehl's avatar
    sgiehl a validé
                    throw new Exception("checkUserHasViewAccess Fake exception // string not to be tested");
                }
            }
        }
    
        public static function checkUserHasSomeViewAccess()
        {
    
            if (!self::$superUser) {
                if (count(self::$idSitesView) == 0) {
    
    sgiehl's avatar
    sgiehl a validé
                    throw new Exception("checkUserHasSomeViewAccess Fake exception // string not to be tested");
                }
    
    sgiehl's avatar
    sgiehl a validé
                return;
            }
        }
    
        //means at least view access
        public static function checkUserHasSomeAdminAccess()
        {
    
            if (!self::$superUser) {
                if (count(self::$idSitesAdmin) == 0) {
    
    sgiehl's avatar
    sgiehl a validé
                    throw new Exception("checkUserHasSomeAdminAccess Fake exception // string not to be tested");
                }
    
                return; //Super User has some admin rights
    
    sgiehl's avatar
    sgiehl a validé
            }
        }
    
    sgiehl's avatar
    sgiehl a validé
        public static function getLogin()
        {
            return self::$identity;
        }
    
    sgiehl's avatar
    sgiehl a validé
        public static function getSitesIdWithAdminAccess()
        {
    
                return API::getInstance()->getAllSitesId();
    
    sgiehl's avatar
    sgiehl a validé
            }
    
    sgiehl's avatar
    sgiehl a validé
        }
    
    sgiehl's avatar
    sgiehl a validé
        public static function getSitesIdWithViewAccess()
        {
    
                return API::getInstance()->getAllSitesId();
    
    sgiehl's avatar
    sgiehl a validé
            }
    
    sgiehl's avatar
    sgiehl a validé
        }
    
    sgiehl's avatar
    sgiehl a validé
        public static function getSitesIdWithAtLeastViewAccess()
        {
    
                return API::getInstance()->getAllSitesId();
    
    sgiehl's avatar
    sgiehl a validé
            }
    
            return array_merge(self::$idSitesView, self::$idSitesAdmin);
    
    sgiehl's avatar
    sgiehl a validé
        }
    
        
        public function getRawSitesWithSomeViewAccess($login)
        {
            $result = array();
            foreach (array_merge(self::$idSitesView, self::$idSitesAdmin) as $idSite) {
                $result[] = array('idsite' => $idSite);
            }
            return $result;
        }
    
        /**
         * @deprecated deprecated since version 2.0.4
         */
        public function getSuperUserLogin()
        {
    
    sgiehl's avatar
    sgiehl a validé
    }