Skip to content
Extraits de code Groupes Projets
Valider 1238ea35 rédigé par Thomas Steur's avatar Thomas Steur
Parcourir les fichiers

refs #4679 splitted this method into 3 methods and extracted them into a model...

refs #4679 splitted this method into 3 methods and extracted them into a model so we do not have to check for permission when archiving
parent 1dcc0c24
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -261,11 +261,10 @@ class API extends \Piwik\Plugin\API ...@@ -261,11 +261,10 @@ class API extends \Piwik\Plugin\API
/** /**
* Returns all stored segments. * Returns all stored segments.
* *
* @param bool $idSite Whether to return stored segments that are only auto-archived for a specific idSite, or all of them. If supplied, must be a valid site ID. * @param bool|int $idSite Whether to return stored segments for a specific idSite, or all of them. If supplied, must be a valid site ID.
* @param bool $returnOnlyAutoArchived Whether to only return stored segments that are auto-archived or not.
* @return array * @return array
*/ */
public function getAll($idSite = false, $returnOnlyAutoArchived = false) public function getAll($idSite = false)
{ {
if (!empty($idSite)) { if (!empty($idSite)) {
Piwik::checkUserHasViewAccess($idSite); Piwik::checkUserHasViewAccess($idSite);
...@@ -273,35 +272,15 @@ class API extends \Piwik\Plugin\API ...@@ -273,35 +272,15 @@ class API extends \Piwik\Plugin\API
Piwik::checkUserHasSomeViewAccess(); Piwik::checkUserHasSomeViewAccess();
} }
if ($returnOnlyAutoArchived) { $userLogin = Piwik::getCurrentUserLogin();
Piwik::checkUserHasSuperUserAccess();
}
$bind = array();
// Build basic segment filtering
$whereIdSite = '';
if (!empty($idSite)) {
$whereIdSite = 'enable_only_idsite = ? OR ';
$bind[] = $idSite;
}
if ($returnOnlyAutoArchived) { $model = new Model();
$extraWhere = ' AND auto_archive = 1'; if (empty($idSite)) {
$segments = $model->getAllSegments($userLogin);
} else { } else {
$extraWhere = ' AND (enable_all_users = 1 OR login = ?)'; $segments = $model->getAllSegmentsForSite($idSite, $userLogin);
$bind[] = Piwik::getCurrentUserLogin();
} }
// Query
$sql = "SELECT * " .
" FROM " . Common::prefixTable("segment") .
" WHERE ($whereIdSite enable_only_idsite = 0)
AND deleted = 0
$extraWhere
ORDER BY name ASC";
$segments = Db::get()->fetchAll($sql, $bind);
return $segments; return $segments;
} }
......
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\SegmentEditor;
use Exception;
use Piwik\Common;
use Piwik\Date;
use Piwik\Db;
use Piwik\Piwik;
use Piwik\Segment;
/**
* The SegmentEditor Model lets you persist and read custom Segments from the backend without handling any logic.
*/
class Model
{
/**
* Returns all stored segments.
*
* @param bool|int $idSite Whether to return stored segments for a specific idSite, or segments that are available
* for all sites. If supplied, must be a valid site ID.
* @return array
*/
public function getSegmentsToAutoArchive($idSite = false)
{
$bind = array();
$whereIdSite = '';
if (!empty($idSite)) {
$whereIdSite = 'enable_only_idsite = ? OR ';
$bind[] = $idSite;
}
$sql = $this->buildQuerySortedByName("($whereIdSite enable_only_idsite = 0)
AND deleted = 0 AND auto_archive = 1");
$segments = Db::get()->fetchAll($sql, $bind);
return $segments;
}
/**
* Returns all stored segments that are available to the given login.
*
* @param string $userLogin
* @return array
*/
public function getAllSegments($userLogin)
{
$bind = array($userLogin);
$sql = $this->buildQuerySortedByName('deleted = 0 AND (enable_all_users = 1 OR login = ?)');
$segments = Db::get()->fetchAll($sql, $bind);
return $segments;
}
/**
* Returns all stored segments that are available for the given site and login.
*
* @param string $userLogin
* @param int $idSite Whether to return stored segments for a specific idSite, or all of them. If supplied, must be a valid site ID.
* @return array
*/
public function getAllSegmentsForSite($idSite, $userLogin)
{
$bind = array($idSite, $userLogin);
$sql = $this->buildQuerySortedByName('(enable_only_idsite = ? OR enable_only_idsite = 0)
AND deleted = 0
AND (enable_all_users = 1 OR login = ?)');
$segments = Db::get()->fetchAll($sql, $bind);
return $segments;
}
private function buildQuerySortedByName($where)
{
$sql = "SELECT * FROM " . Common::prefixTable("segment") .
" WHERE $where ORDER BY name ASC";
return $sql;
}
}
...@@ -54,7 +54,9 @@ class SegmentEditor extends \Piwik\Plugin ...@@ -54,7 +54,9 @@ class SegmentEditor extends \Piwik\Plugin
public function getKnownSegmentsToArchiveAllSites(&$segments) public function getKnownSegmentsToArchiveAllSites(&$segments)
{ {
$segmentsToAutoArchive = API::getInstance()->getAll($idSite = false, $returnOnlyAutoArchived = true); $model = new Model();
$segmentsToAutoArchive = $model->getSegmentsToAutoArchive($idSite = false);
foreach ($segmentsToAutoArchive as $segment) { foreach ($segmentsToAutoArchive as $segment) {
$segments[] = $segment['definition']; $segments[] = $segment['definition'];
} }
...@@ -62,11 +64,13 @@ class SegmentEditor extends \Piwik\Plugin ...@@ -62,11 +64,13 @@ class SegmentEditor extends \Piwik\Plugin
public function getKnownSegmentsToArchiveForSite(&$segments, $idSite) public function getKnownSegmentsToArchiveForSite(&$segments, $idSite)
{ {
$segmentToAutoArchive = API::getInstance()->getAll($idSite, $returnOnlyAutoArchived = true); $model = new Model();
$segmentToAutoArchive = $model->getSegmentsToAutoArchive($idSite);
foreach ($segmentToAutoArchive as $segmentInfo) { foreach ($segmentToAutoArchive as $segmentInfo) {
$segments[] = $segmentInfo['definition']; $segments[] = $segmentInfo['definition'];
} }
$segments = array_unique($segments); $segments = array_unique($segments);
} }
......
...@@ -10,6 +10,7 @@ use Piwik\Date; ...@@ -10,6 +10,7 @@ use Piwik\Date;
use Piwik\Piwik; use Piwik\Piwik;
use Piwik\Plugins\SegmentEditor\API; use Piwik\Plugins\SegmentEditor\API;
use Piwik\Plugins\SitesManager\API as APISitesManager; use Piwik\Plugins\SitesManager\API as APISitesManager;
use Piwik\Plugins\SegmentEditor\Model;
/** /**
* Class Plugins_SegmentEditorTest * Class Plugins_SegmentEditorTest
...@@ -108,20 +109,21 @@ class Plugins_SegmentEditorTest extends DatabaseTestCase ...@@ -108,20 +109,21 @@ class Plugins_SegmentEditorTest extends DatabaseTestCase
$this->assertEquals($segment, $expected); $this->assertEquals($segment, $expected);
// There is a segment to process for this particular site // There is a segment to process for this particular site
$segments = API::getInstance()->getAll($idSite, $autoArchived = true); $model = new Model();
$segments = $model->getSegmentsToAutoArchive($idSite);
unset($segments[0]['ts_created']); unset($segments[0]['ts_created']);
$this->assertEquals($segments, array($expected)); $this->assertEquals($segments, array($expected));
// There is no segment to process for a non existing site // There is no segment to process for a non existing site
try { try {
$segments = API::getInstance()->getAll(33, $autoArchived = true); $segments = $model->getSegmentsToAutoArchive(33);
$this->fail(); $this->fail();
} catch(Exception $e) { } catch(Exception $e) {
// expected // expected
} }
// There is no segment to process across all sites // There is no segment to process across all sites
$segments = API::getInstance()->getAll($idSite = false, $autoArchived = true); $segments = $model->getSegmentsToAutoArchive($idSite = false);
$this->assertEquals($segments, array()); $this->assertEquals($segments, array());
} }
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter