From d7f03dab9e563d4bb712920ef668a4c282a12e46 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 30 Sep 2010 13:22:25 -0700 Subject: [PATCH 1/4] Added an option to TinyMCE plugin to restrict the rich-text editor to users who have the 'richedit' role. This allows enabling it for a subset of accounts on a site while leaving other users using the regular posting system, which is more stable. --- plugins/TinyMCE/TinyMCEPlugin.php | 34 +++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/plugins/TinyMCE/TinyMCEPlugin.php b/plugins/TinyMCE/TinyMCEPlugin.php index 2ec4b71608..e0640ebdf3 100644 --- a/plugins/TinyMCE/TinyMCEPlugin.php +++ b/plugins/TinyMCE/TinyMCEPlugin.php @@ -50,9 +50,14 @@ class TinyMCEPlugin extends Plugin { var $html; + // By default, TinyMCE editor will be available to all users. + // With restricted on, only users who have been granted the + // "richedit" role get it. + public $restricted = false; + function onEndShowScripts($action) { - if (common_logged_in ()) { + if (common_logged_in() && $this->isAllowedRichEdit()) { $action->script(common_path('plugins/TinyMCE/js/jquery.tinymce.js')); $action->inlineScript($this->_inlineScript()); } @@ -62,7 +67,9 @@ class TinyMCEPlugin extends Plugin function onEndShowStyles($action) { - $action->style('span#notice_data-text_container, span#notice_data-text_parent { float: left }'); + if ($this->isAllowedRichEdit()) { + $action->style('span#notice_data-text_container, span#notice_data-text_parent { float: left }'); + } return true; } @@ -116,7 +123,7 @@ class TinyMCEPlugin extends Plugin */ function onStartSaveNewNoticeWeb($action, $user, &$content, &$options) { - if ($action->arg('richedit')) { + if ($action->arg('richedit') && $this->isAllowedRichEdit()) { $html = $this->sanitizeHtml($content); $options['rendered'] = $html; $content = $this->stripHtml($html); @@ -135,7 +142,7 @@ class TinyMCEPlugin extends Plugin */ function onStartSaveNewNoticeAppendAttachment($action, $media, &$content, &$options) { - if ($action->arg('richedit')) { + if ($action->arg('richedit') && $this->isAllowedRichEdit()) { // See if we've got a placeholder inline image; if so, fill it! $dom = new DOMDocument(); @@ -320,4 +327,23 @@ END_OF_SCRIPT; return $scr; } + + /** + * Does the current user have permission to use the rich-text editor? + * Always true unless the plugin's "restricted" setting is on, in which + * case it's limited to users with the "richedit" role. + * + * @fixme make that more sanely configurable :) + * + * @return boolean + */ + private function isAllowedRichEdit() + { + if ($this->restricted) { + $user = common_current_user(); + return !empty($user) && $user->hasRole('richedit'); + } else { + return true; + } + } } From 8ad933c86f8593e61eea9692f863f9adaa6820b7 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 30 Sep 2010 13:30:39 -0700 Subject: [PATCH 2/4] Add 'restricted' option to NoticeTitle; if set, only users with 'richedit' role get the fancy extra title field. --- plugins/NoticeTitle/NoticeTitlePlugin.php | 48 ++++++++++++++++++----- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/plugins/NoticeTitle/NoticeTitlePlugin.php b/plugins/NoticeTitle/NoticeTitlePlugin.php index dea0417f5f..269f061893 100644 --- a/plugins/NoticeTitle/NoticeTitlePlugin.php +++ b/plugins/NoticeTitle/NoticeTitlePlugin.php @@ -51,6 +51,12 @@ define('NOTICE_TITLE_PLUGIN_VERSION', '0.1'); class NoticeTitlePlugin extends Plugin { + + // By default, notice-title widget will be available to all users. + // With restricted on, only users who have been granted the + // "richedit" role get it. + public $restricted = false; + /** * Database schema setup * @@ -137,14 +143,16 @@ class NoticeTitlePlugin extends Plugin function onStartShowNoticeFormData($form) { - $form->out->element('style', - null, - 'label#notice_data-text-label { display: none }'); - $form->out->element('input', array('type' => 'text', - 'id' => 'notice_title', - 'name' => 'notice_title', - 'size' => 40, - 'maxlength' => Notice_title::MAXCHARS)); + if ($this->isAllowedRichEdit()) { + $form->out->element('style', + null, + 'label#notice_data-text-label { display: none }'); + $form->out->element('input', array('type' => 'text', + 'id' => 'notice_title', + 'name' => 'notice_title', + 'size' => 40, + 'maxlength' => Notice_title::MAXCHARS)); + } return true; } @@ -162,7 +170,7 @@ class NoticeTitlePlugin extends Plugin function onStartNoticeSaveWeb($action, &$authorId, &$text, &$options) { $title = $action->trimmed('notice_title'); - if (!empty($title)) { + if (!empty($title) && $this->isAllowedRichEdit()) { if (mb_strlen($title) > Notice_title::MAXCHARS) { throw new Exception(sprintf(_m("The notice title is too long (max %d characters).", Notice_title::MAXCHARS))); @@ -186,7 +194,7 @@ class NoticeTitlePlugin extends Plugin $title = $action->trimmed('notice_title'); - if (!empty($title)) { + if (!empty($title) && $this->isAllowedRichEdit()) { $nt = new Notice_title(); @@ -327,4 +335,24 @@ class NoticeTitlePlugin extends Plugin return true; } + + /** + * Does the current user have permission to use the notice-title widget? + * Always true unless the plugin's "restricted" setting is on, in which + * case it's limited to users with the "richedit" role. + * + * @fixme make that more sanely configurable :) + * + * @return boolean + */ + private function isAllowedRichEdit() + { + if ($this->restricted) { + $user = common_current_user(); + return !empty($user) && $user->hasRole('richedit'); + } else { + return true; + } + } + } From 0d5dadc81d8477dad885b52c89bf69cdd2ad0cc5 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Thu, 30 Sep 2010 16:39:56 -0700 Subject: [PATCH 3/4] Change Disqus plugin to allow restricting to users with "richedit" role --- plugins/Disqus/DisqusPlugin.php | 51 ++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/plugins/Disqus/DisqusPlugin.php b/plugins/Disqus/DisqusPlugin.php index ec5857dd74..10155c3381 100644 --- a/plugins/Disqus/DisqusPlugin.php +++ b/plugins/Disqus/DisqusPlugin.php @@ -52,16 +52,19 @@ if (!defined('STATUSNET')) { * ); * * 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( * 'Disqus', array( - * 'shortname' => 'YOURSHORTNAME', - * 'divStyle' => 'width:675px; padding-top:10px; position:relative; float:left;', - * 'nicknames' => array('spock', 'kirk', 'bones') + * 'shortname' => 'YOURSHORTNAME', + * 'divStyle' => 'width:675px; padding-top:10px; position:relative; float:left;', + * 'restricted' => true * ) * ); * + * $ php userrole.php -s#### -nusername -rrichedit + * * * NOTE: the 'divStyle' in an optional parameter that passes in some * 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 $divStyle; // Optional CSS chunk for the main
- 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 @@ -185,9 +192,9 @@ ENDOFSCRIPT; $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->showNoticeInfo(); @@ -196,7 +203,9 @@ ENDOFSCRIPT; $noticeUrl .= '#disqus_thread'; $noticeListItem->out->element( - 'a', array('href' => $noticeUrl, 'class' => 'disqus_count'), 'Comments' + 'a', + array('href' => $noticeUrl, 'class' => 'disqus_count'), + _m('Comments') ); $noticeListItem->showNoticeOptions(); @@ -209,28 +218,24 @@ ENDOFSCRIPT; } /** - * Helper to check whether commenting should be enabled - * for a given notice + * Does the current user have permission to use the Disqus plugin? + * 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 - * nicknames array is populated + * @fixme make that more sanely configurable :) * * @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)) { - foreach ($this->nicknames as $nickname) { - if ($profile->nickname == $nickname) { - return true; - } - } - return false; + if ($this->restricted) { + $user = User::staticGet($profile->id); + return !empty($user) && $user->hasRole('richedit'); + } else { + return true; } - - return true; } /** From cdd43d8da97e4188327c7f9f5fab29a20d1868c3 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Thu, 30 Sep 2010 16:51:19 -0700 Subject: [PATCH 4/4] Less intrusive insertion of Disqus notice count into notice lists --- plugins/Disqus/DisqusPlugin.php | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/plugins/Disqus/DisqusPlugin.php b/plugins/Disqus/DisqusPlugin.php index 10155c3381..3024d81a62 100644 --- a/plugins/Disqus/DisqusPlugin.php +++ b/plugins/Disqus/DisqusPlugin.php @@ -176,29 +176,22 @@ ENDOFSCRIPT; } /** - * Override the default Notice display to add Disqus comments link + * Tack on a Disqus comments link to the notice options stanza * (the link displays the total number of comments for each notice) * * @param NoticeListItem $noticeListItem * - * @return boolean override */ - function onStartShowNoticeItem($noticeListItem) + function onEndShowNoticeInfo($noticeListItem) { // Don't enable commenting for remote notices if (empty($noticeListItem->notice->is_local)) { - return true; + return; } $profile = Profile::staticGet('id', $noticeListItem->notice->profile_id); if ($this->isAllowedRichEdit($profile)) { - - // @todo Refactor individual notice display to have its own event hooks - - $noticeListItem->showNotice(); - $noticeListItem->showNoticeInfo(); - $noticeUrl = $noticeListItem->notice->bestUrl(); $noticeUrl .= '#disqus_thread'; @@ -207,13 +200,6 @@ ENDOFSCRIPT; array('href' => $noticeUrl, 'class' => 'disqus_count'), _m('Comments') ); - - $noticeListItem->showNoticeOptions(); - Event::handle('EndShowNoticeItem', array($noticeListItem)); - - return false; - } else { - return true; } }