Skip to content

Instantly share code, notes, and snippets.

@EGreg
Created June 18, 2014 18:05
Show Gist options
  • Save EGreg/9342f52c53c5c714b6f3 to your computer and use it in GitHub Desktop.
Save EGreg/9342f52c53c5c714b6f3 to your computer and use it in GitHub Desktop.
Streams/access patch to fix some things for now
From a93d584bfd081e352e06ea094cb07d07e1e8c15c Mon Sep 17 00:00:00 2001
From: Greg Magarshak <[email protected]>
Date: Wed, 18 Jun 2014 12:38:03 -0400
Subject: [PATCH] Started working on the modern Streams/access tool
---
.../plugins/Streams/classes/Streams/Avatar.php | 5 ++-
.../plugins/Streams/classes/Streams/Stream.php | 6 +--
.../Streams/handlers/Streams/access/put.php | 6 +--
.../Streams/handlers/Streams/access/tool.php | 45 ++++++++++++----------
4 files changed, 34 insertions(+), 28 deletions(-)
diff --git a/platform/plugins/Streams/classes/Streams/Avatar.php b/platform/plugins/Streams/classes/Streams/Avatar.php
index 4d6db08..3823b54 100644
--- a/platform/plugins/Streams/classes/Streams/Avatar.php
+++ b/platform/plugins/Streams/classes/Streams/Avatar.php
@@ -30,9 +30,10 @@ class Streams_Avatar extends Base_Streams_Avatar
* @static
* @param $toUserId {User_User|string} The id of the user to which this would be displayed
* @param $publisherIds {string|array} Array of various users whose avatars are being fetched
+ * @param $indexField {string} Optional name of the field by which to index the resulting array
* @return {Streams_Avatar|array}
*/
- static function fetch($toUserId, $publisherId) {
+ static function fetch($toUserId, $publisherId, $indexField = null) {
if (!isset($toUserIs)) {
$toUserId = Users::loggedInUser();
if (!$toUserId) $toUserId = "";
@@ -48,7 +49,7 @@ class Streams_Avatar extends Base_Streams_Avatar
$rows = Streams_Avatar::select('*')->where(array(
'toUserId' => array($toUserId, ''),
'publisherId' => $publisherId
- ))->fetchDbRows();
+ ))->fetchDbRows(null, '', $indexField);
$avatars = array();
foreach ($rows as $r) {
if (!isset($avatars[$r->publisherId])
diff --git a/platform/plugins/Streams/classes/Streams/Stream.php b/platform/plugins/Streams/classes/Streams/Stream.php
index 22467b3..e2c9af5 100644
--- a/platform/plugins/Streams/classes/Streams/Stream.php
+++ b/platform/plugins/Streams/classes/Streams/Stream.php
@@ -987,7 +987,7 @@ class Streams_Stream extends Base_Streams_Stream
if ($this->publishedByFetcher) {
return true;
}
- if ($this->closedTime and !$this->testWriteLevel('close')) {
+ if (!empty($this->closedTime) and !$this->testWriteLevel('close')) {
return false;
}
if (!is_numeric($level)) {
@@ -1036,7 +1036,7 @@ class Streams_Stream extends Base_Streams_Stream
if ($this->publishedByFetcher) {
return true;
}
- if ($this->closedTime and $level !== 'close' and !$this->testWriteLevel('close')) {
+ if (!empty($this->closedTime) and $level !== 'close' and !$this->testWriteLevel('close')) {
return false;
}
if (!is_numeric($level)) {
@@ -1085,7 +1085,7 @@ class Streams_Stream extends Base_Streams_Stream
if ($this->publishedByFetcher) {
return true;
}
- if ($this->closedTime and !$this->testWriteLevel('close')) {
+ if (!empty($this->closedTime) and !$this->testWriteLevel('close')) {
return false;
}
if (!is_numeric($level)) {
diff --git a/platform/plugins/Streams/handlers/Streams/access/put.php b/platform/plugins/Streams/handlers/Streams/access/put.php
index a4e1239..0d8e93b 100644
--- a/platform/plugins/Streams/handlers/Streams/access/put.php
+++ b/platform/plugins/Streams/handlers/Streams/access/put.php
@@ -8,13 +8,13 @@ function Streams_access_put($params)
$stream = new Streams_Stream();
$stream->publisherId = Streams::requestedPublisherId(true);
- if ($stream->publisherId != $user->id) {
- throw new Users_Exception_NotAuthorized();
- }
$stream->name = Streams::requestedName(true);
if (!$stream->retrieve()) {
throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => 'that name'));
}
+ if (!$stream->testAdminLevel('own')) {
+ throw new Users_Exception_NotAuthorized();
+ }
$p = array_merge($_REQUEST, $params);
$access = new Streams_Access();
diff --git a/platform/plugins/Streams/handlers/Streams/access/tool.php b/platform/plugins/Streams/handlers/Streams/access/tool.php
index 02ffc76..6b4835c 100644
--- a/platform/plugins/Streams/handlers/Streams/access/tool.php
+++ b/platform/plugins/Streams/handlers/Streams/access/tool.php
@@ -3,6 +3,7 @@
/**
* Access tool
* @param array $options
+ * "publisherId" => the id of the user who is publishing the stream
* "streamName" => the name of the stream for which to edit access levels
* "tabs" => optional array of tab name => title. Defaults to read, write, admin tabs.
* "ranges" => optional. Associative array with keys "read", "write", "admin"
@@ -16,20 +17,29 @@ function Streams_access_tool($options)
'admin' => 'members'
);
extract($options);
+ $user = Users::loggedInUser(true);
/**
* @var string $streamName
*/
if (empty($streamName)) {
- throw new Q_Exception("no streamName", "streamName");
+ $streamName = Streams::requestedName(true);
+ }
+ if (empty($publisherId)) {
+ $publisherId = Streams::requestedPublisherId();
+ if (empty($publisherId)) {
+ $publisherId = $user->id;
+ }
}
- $user = Users::loggedInUser(true);
- $streamName = Streams::requestedName(true);
- $stream = new Streams_Stream();
- $stream->publisherId = $user->id;
- $stream->name = $streamName;
- if (!$stream->retrieve()) {
- throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => 'that name'));
+ $stream = Streams::fetchOne($user->id, $publisherId, $streamName);
+ if (!$stream) {
+ throw new Q_Exception_MissingRow(array(
+ 'table' => 'stream',
+ 'criteria' => 'that name'
+ ));
+ }
+ if (!$stream->testAdminLevel('own')) {
+ throw new Users_Exception_NotAuthorized();
}
$access_array = Streams_Access::select('*')
@@ -55,16 +65,10 @@ function Streams_access_tool($options)
$userId_list[] = $a->ofUserId;
}
}
- if (empty($userId_list)) {
- $avatar_array = array();
- } else {
- $avatar_array = Streams_Avatar::select('*')
- ->where(array(
- 'publisherId' => $userId_list,
- 'toUserId' => $user->id
- ))->fetchDbRows(null, '', 'publisherId');
- }
-
+ $avatar_array = empty($userId_list)
+ ? array()
+ : Streams_Avatar::fetch($user->id, $userId_list);
+
$labels = array();
$icons = array();
foreach ($contact_array as $contact) {
@@ -77,8 +81,9 @@ function Streams_access_tool($options)
$icons[$label->label] = 'label_'.$user->id.'_'.$label->label;
}
}
-
- $tab = Q::ifset($_REQUEST, 'tab', reset($tabs));
+
+ reset($tabs);
+ $tab = Q::ifset($_REQUEST, 'tab', key($tabs));
switch ($tab) {
case 'read':
--
1.8.5.2 (Apple Git-48)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment