Change Disqus plugin to allow restricting to users with "richedit" role

This commit is contained in:
Zach Copley 2010-09-30 16:39:56 -07:00
parent 8a28d9cc4d
commit 0d5dadc81d

View File

@ -52,16 +52,19 @@ if (!defined('STATUSNET')) {
* ); * );
* *
* If you only want to allow commenting on a specific user's notices or * If you only want to allow commenting on a specific user's notices or
* a specific set of user's notices, use the "nicknames" array, e.g.: * a specific set of users' notices initialize the plugin with the "restricted"
* parameter and grant the "richedit" role to those users. E.g.:
* *
* addPlugin( * addPlugin(
* 'Disqus', array( * 'Disqus', array(
* 'shortname' => 'YOURSHORTNAME', * 'shortname' => 'YOURSHORTNAME',
* 'divStyle' => 'width:675px; padding-top:10px; position:relative; float:left;', * 'divStyle' => 'width:675px; padding-top:10px; position:relative; float:left;',
* 'nicknames' => array('spock', 'kirk', 'bones') * 'restricted' => true
* ) * )
* ); * );
* *
* $ php userrole.php -s#### -nusername -rrichedit
*
* *
* NOTE: the 'divStyle' in an optional parameter that passes in some * NOTE: the 'divStyle' in an optional parameter that passes in some
* inline CSS when creating the Disqus widget. It's a shortcut to make * inline CSS when creating the Disqus widget. It's a shortcut to make
@ -85,7 +88,11 @@ class DisqusPlugin extends Plugin
{ {
public $shortname; // Required 'shortname' for actually triggering Disqus public $shortname; // Required 'shortname' for actually triggering Disqus
public $divStyle; // Optional CSS chunk for the main <div> public $divStyle; // Optional CSS chunk for the main <div>
public $nicknames; // Optional array of nicks to restrict commenting to (default on for all users)
// By default, Disqus commenting will be available to all users.
// With restricted on, only users who have been granted the
// "richedit" role get it.
public $restricted = false;
/** /**
* Add a Disqus commenting section to the end of an individual * Add a Disqus commenting section to the end of an individual
@ -185,9 +192,9 @@ ENDOFSCRIPT;
$profile = Profile::staticGet('id', $noticeListItem->notice->profile_id); $profile = Profile::staticGet('id', $noticeListItem->notice->profile_id);
if ($this->hasCommenting($profile)) { if ($this->isAllowedRichEdit($profile)) {
// @todo Refactor individual notice display to have it's own event hooks // @todo Refactor individual notice display to have its own event hooks
$noticeListItem->showNotice(); $noticeListItem->showNotice();
$noticeListItem->showNoticeInfo(); $noticeListItem->showNoticeInfo();
@ -196,7 +203,9 @@ ENDOFSCRIPT;
$noticeUrl .= '#disqus_thread'; $noticeUrl .= '#disqus_thread';
$noticeListItem->out->element( $noticeListItem->out->element(
'a', array('href' => $noticeUrl, 'class' => 'disqus_count'), 'Comments' 'a',
array('href' => $noticeUrl, 'class' => 'disqus_count'),
_m('Comments')
); );
$noticeListItem->showNoticeOptions(); $noticeListItem->showNoticeOptions();
@ -209,28 +218,24 @@ ENDOFSCRIPT;
} }
/** /**
* Helper to check whether commenting should be enabled * Does the current user have permission to use the Disqus plugin?
* for a given notice * Always true unless the plugin's "restricted" setting is on, in which
* case it's limited to users with the "richedit" role.
* *
* Assumes commenting should be enabled, unless the * @fixme make that more sanely configurable :)
* nicknames array is populated
* *
* @param Profile $profile the profile to check * @param Profile $profile the profile to check
* *
* @return boolean true if yes * @return boolean
*/ */
private function hasCommenting($profile) private function isAllowedRichEdit($profile)
{ {
if (!empty($this->nicknames)) { if ($this->restricted) {
foreach ($this->nicknames as $nickname) { $user = User::staticGet($profile->id);
if ($profile->nickname == $nickname) { return !empty($user) && $user->hasRole('richedit');
return true; } else {
} return true;
}
return false;
} }
return true;
} }
/** /**