/** * Tests environment info. * * @return void */ public function test_environment_info() { $this->resetAfterTest(); $envinfo = MHUtil::get_environment_info(); $this->assertEquals(true, !empty($envinfo->phpversion)); }
public function get_params() { if (!($data = $this->get_data())) { return null; } $serviceparams = (object) $data->serviceparams; $secret = get_config('core', 'block_mhaairs_shared_secret'); // Create the token. $token = MHUtil::create_token($serviceparams->userid); $encodedtoken = MHUtil::encode_token2($token, $secret); $serviceparams->token = $encodedtoken; unset($serviceparams->userid); return $this->format_params($serviceparams); }
/** * Returns the main part of the block display content. * In this version this contains service links. * * @return string HTML fragment. */ protected function get_service_links() { $services = $this->get_service_data(); if ($services === false) { return null; } $blocklinks = ''; $imagealt = get_string('imagealt'); $targetw = array('target' => '__mhaairs_service_window'); $course = $this->page->course; foreach ($services as $aserv) { // Icon. $iconparams = array('src' => $aserv['ServiceIconUrl'], 'class' => 'serviceicon', 'alt' => $imagealt); $icon = html_writer::tag('img', '', $iconparams); // Url. $urlparams = array('serviceurl' => MHUtil::hex_encode($aserv['ServiceUrl']), 'serviceid' => MHUtil::hex_encode($aserv['ServiceID']), 'courseid' => $course->id); $url = new moodle_url('/blocks/mhaairs/redirect.php', $urlparams); // Link. $link = html_writer::link($url, $aserv['ServiceName'], $targetw); $blocklinks .= html_writer::tag('div', $icon . $link, array('class' => 'servicelink')); } return $blocklinks; }
} $token = optional_param('token', 'test', PARAM_ALPHANUM); $action = optional_param('action', null, PARAM_ALPHA); $username = optional_param('username', null, PARAM_USERNAME); $userid = optional_param('userid', null, PARAM_INT); $password = optional_param('password', null, PARAM_TEXT); $identitytype = optional_param('identitytype', null, PARAM_TEXT); if (empty($secure) and !empty($CFG->block_mhaairs_sslonly)) { echo 'Connection must be secured with SSL'; return; } $secret = !empty($CFG->block_mhaairs_shared_secret) ? $CFG->block_mhaairs_shared_secret : ''; $result = null; switch ($action) { case "test": $result = "OK"; break; case "ValidateLogin": $result = MHUtil::validate_login($token, $secret, $username, $password); break; case "GetUserInfo": $result = MHUtil::get_user_info($token, $secret, $identitytype); break; case "GetServerTime": $result = MHUtil::get_time_stamp(); break; default: break; } header('Content-Type: application/json; charset=utf-8'); echo json_encode($result);
/** * Asserts successful calls to get user info. * The userid and secret parameters are used for creating an encoded token * of the target user. * The method checks the result status, message, user->username and number of courses. * * @param string $userid * @param string $secret * @param string $identitytype * @return void */ protected function assert_user_found($case, $users, $secret) { global $CFG; // The service function. $callback = 'block_mhaairs_utilservice_external::get_user_info'; $uservar = MHUtil::get_user_var($case->identitytype); // Get the user id. $internal = $case->identitytype == 'internal'; $userid = $internal ? $users[$case->username]->id : $case->username; // Create the token. $token = MHUtil::create_token($userid); $encodedtoken = MHUtil::encode_token2($token, $secret); // Service params. $serviceparams = array('token' => $encodedtoken, 'identitytype' => $case->identitytype); $result = call_user_func_array($callback, $serviceparams); $this->assertEquals(MHUserInfo::SUCCESS, $result->status); // Verify user info. $user = $users[$case->username]; $this->assertEquals('', $result->message); $this->assertEquals($user->username, $result->user->username); $this->assertEquals($user->email, $result->user->email); // Verify course info. $coursecount = 0; foreach (array('tc2', 'tc3', 'tc4', 'tc5') as $tc) { if (!empty($case->{$tc})) { $coursecount += count(explode(',', $case->{$tc})); } } $this->assertEquals($coursecount, count($result->courses)); }
/** * Allows external services to push grades into the course gradebook. * Alias for {@link block_mhaairs_gradebookservice_external::update_grade()}. * * @return mixed * @throws invalid_parameter_exception * @throws moodle_exception */ public static function validate_login($token, $username, $password) { // Require secured connection. if ($error = self::require_ssl()) { $authresult = new MHAuthenticationResult(MHAuthenticationResult::FAILURE, '', $error); return $authresult; } // Get the configured secret. $secret = self::get_secret(); // Validate login. $result = MHUtil::validate_login($token, $secret, $username, $password); return $result; }
// (at your option) any later version. // // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. require_once '../../config.php'; require_once $CFG->dirroot . '/blocks/mhaairs/block_mhaairs_util.php'; /* * We can not use PARAM_INT for filtering since it filters values up to PHP integer maximum, * which can be less than database coulmn maximum * for example on Linux complied PHP 32-bit signed maxint is 2147483647 * on Linux x64 compiled PHP 64-bit signed maxint is 9223372036854775807 * on Windows both offical PHP 32-bit and unofficial 64-bit have maxint of 2147483647 * in case of mysql Moodle uses by default signed BIGINT for all id columns which has maxint of 9223372036854775807 * 64bit version * */ $courseid = required_param('courseid', PARAM_ALPHANUM); $serviceurl = required_param('serviceurl', PARAM_ALPHANUM); $serviceid = required_param('serviceid', PARAM_ALPHANUM); if (!is_numeric($courseid)) { print_error('invalidaccessparameter'); } require_login($courseid); // Set the url and redirect. $url = MHUtil::get_sso_url($serviceurl, $serviceid); redirect($url);
// // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. require_once '../../config.php'; global $CFG; require_once $CFG->libdir . '/accesslib.php'; require_once $CFG->libdir . '/datalib.php'; require_once $CFG->libdir . '/moodlelib.php'; require_once $CFG->dirroot . '/blocks/mhaairs/block_mhaairs_util.php'; global $CFG, $COURSE, $USER, $_SERVER; $blockrequestbase = "/blocks/mhaairs/"; $testuserid = "moodleinstructor"; $testcourseid = "testcourse123"; $testtimestamp = MHUtil::get_time_stamp(); echo "<p>time stamp:<b>" . $testtimestamp . "</b></p>"; echo "<p>test user id:<b>" . $testuserid . "</b></p>"; echo "<p>test course id:<b>" . $testcourseid . "</b></p>"; $customer = $CFG->block_mhaairs_customer_number; $sharedsecret = $CFG->block_mhaairs_sharedsecret; $base = $CFG->block_mhaairs_base_address; $requesttoken = MHUtil::create_token($testuserid); $encodedrequesttoken = MHUtil::encode_token2($requesttoken, $sharedsecret); echo "<p>the token is valid:<b>" . (MHUtil::is_token_valid($encodedrequesttoken, $sharedsecret) ? "true" : "false") . "</b></p>"; $getuserinfourl = $blockrequestbase . "block_mhaairs_action.php?action=GetUserInfo&token=" . $encodedrequesttoken; "<p>encoded request token:<b>" . $encodedrequesttoken . "</b></p>"; echo "<a href='" . $getuserinfourl . "' target='blank'>get user info</a>";