From: tjhunt Date: Wed, 6 May 2009 09:01:42 +0000 (+0000) Subject: moodle_page: MDL-12212 new implementation of user_allowed_editing X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=934524d70bdcb25a1a12186d2d5107feabce59a8;p=moodle.git moodle_page: MDL-12212 new implementation of user_allowed_editing --- diff --git a/lib/pagelib.php b/lib/pagelib.php index f1159dc87a..a0b185d401 100644 --- a/lib/pagelib.php +++ b/lib/pagelib.php @@ -79,6 +79,10 @@ class moodle_page { protected $_blocks = null; + protected $_blockseditingcap = 'moodle/site:manageblocks'; + + protected $_othereditingcaps = array(); + /// Getter methods ============================================================= /// Due to the __get magic below, you normally do not call these as $PAGE->get_x /// methods, but instead use the $PAGE->x syntax. @@ -227,7 +231,7 @@ class moodle_page { * @return boolean does the user have permission to see this page in editing mode. */ public function user_allowed_editing() { - return true; // TODO + return has_any_capability($this->all_editing_caps(), $this->_context); } /// Setter methods ============================================================= @@ -382,6 +386,30 @@ class moodle_page { } } + /** + * Set the capability that allows users to edit blocks on this page. Normally + * the default of 'moodle/site:manageblocks' is used, but a few pages like + * the My Moodle page need to use a different capability like 'moodle/my:manageblocks'. + * @param string $capability a capability. + */ + public function set_blocks_editing_capability($capability) { + $this->_blockseditingcap = $capability; + } + + /** + * Some pages let you turn editing on for reasons other than editing blocks. + * If that is the case, you can pass other capabilitise that let the user + * edit this page here. + * @param string|array $capability either a capability, or an array of capabilities. + */ + public function set_other_editing_capability($capability) { + if (is_array($capability)) { + $this->_othereditingcaps = array_unique($this->_othereditingcaps + $capability); + } else { + $this->_othereditingcaps[] = $capability; + } + } + /// Initialisation methods ===================================================== /// These set various things up in a default way. @@ -531,6 +559,12 @@ class moodle_page { return $class; } + protected function all_editing_caps() { + $caps = $this->_othereditingcaps; + $caps[] = $this->_blockseditingcap; + return $caps; + } + /// Deprecated fields and methods for backwards compatibility ================== /** diff --git a/lib/simpletest/testpagelib_moodlepage.php b/lib/simpletest/testpagelib_moodlepage.php index 0240d0a4d9..60f42cc371 100644 --- a/lib/simpletest/testpagelib_moodlepage.php +++ b/lib/simpletest/testpagelib_moodlepage.php @@ -44,6 +44,9 @@ class testable_moodle_page extends moodle_page { public function url_to_class_name($url) { return parent::url_to_class_name($url); } + public function all_editing_caps() { + return parent::all_editing_caps(); + } } /** @@ -464,6 +467,7 @@ class moodle_page_editing_test extends UnitTestCase { global $USER; $this->originaluserediting = !empty($USER->editing); $this->testpage = new testable_moodle_page(); + $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); } public function tearDown() { @@ -472,6 +476,8 @@ class moodle_page_editing_test extends UnitTestCase { $USER->editing = $this->originaluserediting; } + // We are relying on the fact that unit tests are alwyas run by admin, to + // ensure the user_allows_editing call returns true. public function test_user_is_editing_on() { // Setup fixture global $USER; @@ -480,6 +486,8 @@ class moodle_page_editing_test extends UnitTestCase { $this->assertTrue($this->testpage->user_is_editing()); } + // We are relying on the fact that unit tests are alwyas run by admin, to + // ensure the user_allows_editing call returns true. public function test_user_is_editing_off() { // Setup fixture global $USER; @@ -487,5 +495,35 @@ class moodle_page_editing_test extends UnitTestCase { // Validate $this->assertFalse($this->testpage->user_is_editing()); } + + public function test_default_editing_capabilities() { + // Validate + $this->assertEqual(array('moodle/site:manageblocks'), $this->testpage->all_editing_caps()); + } + + public function test_other_block_editing_cap() { + // Exercise SUT + $this->testpage->set_blocks_editing_capability('moodle/my:manageblocks'); + // Validate + $this->assertEqual(array('moodle/my:manageblocks'), $this->testpage->all_editing_caps()); + } + + public function test_other_editing_cap() { + // Exercise SUT + $this->testpage->set_other_editing_capability('moodle/course:manageactivities'); + // Validate + $actualcaps = $this->testpage->all_editing_caps(); + $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:manageblocks'); + $this->assert(new ArraysHaveSameValuesExpectation($expectedcaps), $actualcaps); + } + + public function test_other_editing_caps() { + // Exercise SUT + $this->testpage->set_other_editing_capability(array('moodle/course:manageactivities', 'moodle/site:other')); + // Validate + $actualcaps = $this->testpage->all_editing_caps(); + $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:other', 'moodle/site:manageblocks'); + $this->assert(new ArraysHaveSameValuesExpectation($expectedcaps), $actualcaps); + } } ?>