Piwik\Plugins\UsersManager\API::setUserAccess PHP Метод

setUserAccess() публичный Метод

If access = 'noaccess' the current access (if any) will be deleted. If access = 'view' or 'admin' the current access level is deleted and updated with the new value.
public setUserAccess ( string $userLogin, string $access, integer | array $idSites ) : boolean
$userLogin string The user login
$access string Access to grant. Must have one of the following value : noaccess, view, admin
$idSites integer | array The array of idSites on which to apply the access level for the user. If the value is "all" then we apply the access level to all the websites ID for which the current authentificated user has an 'admin' access.
Результат boolean true on success
    public function setUserAccess($userLogin, $access, $idSites)
    {
        $this->checkAccessType($access);
        $this->checkUserExists($userLogin);
        $this->checkUserHasNotSuperUserAccess($userLogin);
        if ($userLogin == 'anonymous' && $access == 'admin') {
            throw new Exception(Piwik::translate("UsersManager_ExceptionAdminAnonymous"));
        }
        // in case idSites is all we grant access to all the websites on which the current connected user has an 'admin' access
        if ($idSites === 'all') {
            $idSites = \Piwik\Plugins\SitesManager\API::getInstance()->getSitesIdWithAdminAccess();
        } else {
            $idSites = Site::getIdSitesFromIdSitesString($idSites);
        }
        if (empty($idSites)) {
            throw new Exception('Specify at least one website ID in &idSites=');
        }
        // it is possible to set user access on websites only for the websites admin
        // basically an admin can give the view or the admin access to any user for the websites he manages
        Piwik::checkUserHasAdminAccess($idSites);
        $this->model->deleteUserAccess($userLogin, $idSites);
        // if the access is noaccess then we don't save it as this is the default value
        // when no access are specified
        if ($access != 'noaccess') {
            $this->model->addUserAccess($userLogin, $access, $idSites);
        } else {
            if (!empty($idSites) && !is_array($idSites)) {
                $idSites = array($idSites);
            }
            Piwik::postEvent('UsersManager.removeSiteAccess', array($userLogin, $idSites));
        }
        // we reload the access list which doesn't yet take in consideration this new user access
        Access::getInstance()->reloadAccess();
        Cache::deleteTrackerCache();
    }

Usage Example

Пример #1
0
 public function test_setUserAccess_ShouldNotTriggerRemoveSiteAccessEvent_IfAccessIsAdded()
 {
     $eventTriggered = false;
     Piwik::addAction('UsersManager.removeSiteAccess', function () use(&$eventTriggered) {
         $eventTriggered = true;
     });
     $this->api->setUserAccess($this->login, 'admin', array(1, 2));
     $this->assertFalse($eventTriggered, 'UsersManager.removeSiteAccess event was triggered but should not');
 }
All Usage Examples Of Piwik\Plugins\UsersManager\API::setUserAccess