Skip to content

Commit

Permalink
Fixes #6766 Let Super User view and edit segments created by other users
Browse files Browse the repository at this point in the history
As a Super User:

 * I can now see all segments that were created for this website by any other user
 * When a segment was created by another user who is not Super User, the segment appears below a new section "Visible to you because you have Super User access:"
 * Such segments are editable by the Super User
 * The only difference when editing someone else's segment, as a Super User, is that "This segment is visible to [ME]" now says "This segment is visible to [SEGMENT_AUTHOR_USERNAME]"
 * One can now search in the  search bar for a username and see all segments created by this user

For all users:

 * New section "Shared with you:" now lists segments created by a Super User, and marked as "Visible to [All Users]"
 * Before segments  shared with me, looked the same as segments I created, which was confusing
  • Loading branch information
mattab committed Jan 22, 2016
1 parent d7211cd commit a38c0c3
Show file tree
Hide file tree
Showing 12 changed files with 465 additions and 86 deletions.
1 change: 1 addition & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"Continue": "Continue",
"ContinueToPiwik": "Continue to Piwik",
"CurrentlyUsingUnsecureHttp": "You are currently using Piwik over unsecure HTTP, which can be risky. We recommend you set up Piwik to use SSL (HTTPS) for improved security.",
"CreatedByUser": "created by %s",
"CurrentMonth": "Current Month",
"CurrentWeek": "Current Week",
"CurrentYear": "Current Year",
Expand Down
1 change: 1 addition & 0 deletions plugins/CoreHome/CoreHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,5 +278,6 @@ public function getClientSideTranslationKeys(&$translationKeys)
$translationKeys[] = 'CoreHome_MenuEntries';
$translationKeys[] = 'SitesManager_Sites';
$translationKeys[] = 'CoreHome_ChangeCurrentWebsite';
$translationKeys[] = 'General_CreatedByUser';
}
}
4 changes: 2 additions & 2 deletions plugins/Dashboard/stylesheets/dashboard.less
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@

ul.widgetpreview-widgetlist,
ul.widgetpreview-categorylist {
color: #5d5342;
color: @theme-color-text-light;
list-style: none;
font-size: 11px;
line-height: 20px;
Expand Down Expand Up @@ -318,7 +318,7 @@ div.widgetpreview-preview {
font-weight: normal;
padding-top: 10px;
margin-left: 10px;
color: #5D5342;
color: @theme-color-text-light;
list-style: none;
font-size: 11px;
line-height: 20px;
Expand Down
43 changes: 40 additions & 3 deletions plugins/SegmentEditor/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,52 @@ public function getAll($idSite = false)
$userLogin = Piwik::getCurrentUserLogin();

$model = $this->getModel();
if (empty($idSite)) {
$segments = $model->getAllSegments($userLogin);
if(Piwik::hasUserSuperUserAccess()) {
$segments = $model->getAllSegmentsForAllUsers($idSite);
} else {
$segments = $model->getAllSegmentsForSite($idSite, $userLogin);
if (empty($idSite)) {
$segments = $model->getAllSegments($userLogin);
} else {
$segments = $model->getAllSegmentsForSite($idSite, $userLogin);
}
}

$segments = $this->sortSegmentsCreatedByUserFirst($segments);

return $segments;
}

/**
* Sorts segment in a particular order:
*
* 1) my segments
* 2) segments created by the super user that were shared with all users
* 3) segments created by other users (which are visible to all super users)
*
* @param $segments
* @return array
*/
private function sortSegmentsCreatedByUserFirst($segments)
{
$orderedSegments = array();
foreach($segments as $id => &$segment) {
if($segment['login'] == Piwik::getCurrentUserLogin()) {
$orderedSegments[] = $segment;
unset($segments[$id]);
}
}
foreach($segments as $id => &$segment) {
if($segment['enable_all_users'] == 1) {
$orderedSegments[] = $segment;
unset($segments[$id]);
}
}
foreach($segments as $id => &$segment) {
$orderedSegments[] = $segment;
}
return $orderedSegments;
}

/**
* @return string
*/
Expand Down
21 changes: 21 additions & 0 deletions plugins/SegmentEditor/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ public function getAllSegmentsForSite($idSite, $userLogin)
return $segments;
}

/**
* This should be used _only_ by Super Users
* @param $idSite
* @return array
*/
public function getAllSegmentsForAllUsers($idSite = false)
{
$bind = array();
$sqlWhereCondition = '';

if(!empty($idSite)) {
$bind = array($idSite);
$sqlWhereCondition = '(enable_only_idsite = ? OR enable_only_idsite = 0) AND';
}

$sqlWhereCondition = $this->buildQuerySortedByName($sqlWhereCondition . ' deleted = 0');
$segments = $this->getDb()->fetchAll($sqlWhereCondition, $bind);

return $segments;
}

public function deleteSegment($idSegment)
{
$db = $this->getDb();
Expand Down
2 changes: 2 additions & 0 deletions plugins/SegmentEditor/SegmentEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,7 @@ public static function isAddingSegmentsForAllWebsitesEnabled()
public function getClientSideTranslationKeys(&$translationKeys)
{
$translationKeys[] = 'SegmentEditor_CustomSegment';
$translationKeys[] = 'SegmentEditor_VisibleToSuperUser';
$translationKeys[] = 'SegmentEditor_SharedWithYou';
}
}
Loading

0 comments on commit a38c0c3

Please sign in to comment.