Role.php 2.57 KB
Newer Older
1
<?php
2 3
namespace T3o\TerFe2\Security;

4 5
/*
 * This file is part of the TYPO3 CMS project.
6
 *
7 8 9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11 12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14 15
 * The TYPO3 project - inspiring people to share!
 */
16 17

/**
18
 * Service to handle security roles
19
 */
20
class Role implements \TYPO3\CMS\Core\SingletonInterface
21
{
22

23
    /**
24
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
25 26
     */
    protected $configurationManager;
27

28 29 30 31
    /**
     * @var array
     */
    protected $settings;
32

33
    /**
34
     * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
35
     */
36
    public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager)
37 38
    {
        $this->configurationManager = $configurationManager;
39
        $this->settings = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS);
40
    }
41

42 43 44
    /**
     * Checks whether FE User is reviewer or not
     *
45
     * @return bool
46 47 48 49
     */
    public function isReviewer()
    {
        if (empty($this->settings['reviewerGroupUid'])) {
Markus Sommer's avatar
Markus Sommer committed
50
            return false;
51 52 53
        }
        return $this->hasRole($this->settings['reviewerGroupUid']);
    }
54

55 56 57
    /**
     * Checks whether FE User is reviewer or not
     *
58
     * @return bool
59 60 61 62
     */
    public function isAdmin()
    {
        if (empty($this->settings['terAdminGroupUid'])) {
63
            return false;
64
        }
65

66 67
        return $this->hasRole($this->settings['terAdminGroupUid']);
    }
68

69 70 71 72
    /**
     * Determines whether the currently logged in FE user belongs to the specified usergroup
     *
     * @param string $role The usergroup (either the usergroup uid or its title)
73
     * @return bool TRUE if the currently logged in FE user belongs to $role
74 75 76 77
     */
    public function hasRole($role)
    {
        if (empty($role) || !isset($GLOBALS['TSFE']) || !$GLOBALS['TSFE']->loginUser) {
Markus Sommer's avatar
Markus Sommer committed
78
            return false;
79
        }
80
        if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($role)) {
81
            return is_array($GLOBALS['TSFE']->fe_user->groupData['uid']) && in_array($role, $GLOBALS['TSFE']->fe_user->groupData['uid']);
82
        }
83
        return is_array($GLOBALS['TSFE']->fe_user->groupData['title']) && in_array($role, $GLOBALS['TSFE']->fe_user->groupData['title']);
84
    }
85
}