示例#1
0
文件: service.php 项目: JP-Git/moodle
//define nav bar
$node = $PAGE->settingsnav->find('externalservice', navigation_node::TYPE_SETTING);
$newnode = $PAGE->settingsnav->find('externalservices', navigation_node::TYPE_SETTING);
if ($node && $newnode) {
    $node->display = false;
    $newnode->make_active();
}
$PAGE->navbar->add(get_string('externalservice', 'webservice'));
//Retrieve few general parameters
$id = required_param('id', PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHANUMEXT);
$confirm = optional_param('confirm', 0, PARAM_BOOL);
$webservicemanager = new webservice();
$renderer = $PAGE->get_renderer('core', 'webservice');
$returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
$service = $id ? $webservicemanager->get_external_service_by_id($id, MUST_EXIST) : null;
/// DELETE operation
if ($action == 'delete' and confirm_sesskey() and $service and empty($service->component)) {
    //Display confirmation Page
    if (!$confirm) {
        echo $OUTPUT->header();
        echo $renderer->admin_remove_service_confirmation($service);
        echo $OUTPUT->footer();
        die;
    }
    //The user has confirmed the deletion, delete and redirect
    $webservicemanager->delete_service($service->id);
    add_to_log(SITEID, 'webservice', 'delete', $returnurl, get_string('deleteservice', 'webservice', $service));
    redirect($returnurl);
}
/// EDIT/CREATE/CANCEL operations => at the end redirect to add function page / main service page
}
require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
$tokenlisturl = new moodle_url("/" . $CFG->admin . "/settings.php", array('section' => 'webservicetokens'));
require_once $CFG->dirroot . "/webservice/lib.php";
$webservicemanager = new webservice();
switch ($action) {
    case 'create':
        $mform = new web_service_token_form(null, array('action' => 'create'));
        $data = $mform->get_data();
        if ($mform->is_cancelled()) {
            redirect($tokenlisturl);
        } else {
            if ($data and confirm_sesskey()) {
                ignore_user_abort(true);
                //check the the user is allowed for the service
                $selectedservice = $webservicemanager->get_external_service_by_id($data->service);
                if ($selectedservice->restrictedusers) {
                    $restricteduser = $webservicemanager->get_ws_authorised_user($data->service, $data->user);
                    if (empty($restricteduser)) {
                        $allowuserurl = new moodle_url('/' . $CFG->admin . '/webservice/service_users.php', array('id' => $selectedservice->id));
                        $allowuserlink = html_writer::tag('a', $selectedservice->name, array('href' => $allowuserurl));
                        $errormsg = $OUTPUT->notification(get_string('usernotallowed', 'webservice', $allowuserlink));
                    }
                }
                //process the creation
                if (empty($errormsg)) {
                    //TODO improvement: either move this function from externallib.php to webservice/lib.php
                    // either move most of webservicelib.php functions into externallib.php
                    // (create externalmanager class) MDL-23523
                    external_generate_token(EXTERNAL_TOKEN_PERMANENT, $data->service, $data->user, get_context_instance(CONTEXT_SYSTEM), $data->validuntil, $data->iprestriction);
                    redirect($tokenlisturl);
 /**
  * Creates tokens.
  *
  * @Given /^the following tokens exist:$/
  * @param TableNode $data
  */
 public function the_following_tokens_exist(TableNode $data)
 {
     global $DB, $CFG;
     foreach ($data->getHash() as $datahash) {
         $service = $this->get_service_id($datahash['service']);
         $userid = $this->get_user_id($datahash['user']);
         $validuntil = !empty($datahash['validuntil']) ? $datahash['validuntil'] : '';
         $iprestriction = !empty($datahash['iprestriction']) ? $datahash['iprestriction'] : '';
         require_once "{$CFG->dirroot}/webservice/lib.php";
         $webservicemanager = new webservice();
         // Check the the user is allowed for the service.
         $selectedservice = $webservicemanager->get_external_service_by_id($service);
         if ($selectedservice->restrictedusers) {
             $restricteduser = $webservicemanager->get_ws_authorised_user($service, $userid);
             if (empty($restricteduser)) {
                 throw new moodle_exception('usernotallowed', 'webservice');
             }
         }
         // Check if the user is deleted. unconfirmed, suspended or guest.
         $user = $DB->get_record('user', array('id' => $userid));
         if ($user->id == $CFG->siteguest or $user->deleted or !$user->confirmed or $user->suspended) {
             throw new moodle_exception('forbiddenwsuser', 'webservice');
         }
         external_generate_token(EXTERNAL_TOKEN_PERMANENT, $service, $userid, context_system::instance(), $validuntil, $iprestriction);
     }
 }