Beispiel #1
0
/**
 * Set the last time a token was sent and trigger the \core\event\webservice_token_sent event.
 *
 * This function is used when a token is generated by the user via login/token.php or admin/tool/mobile/launch.php.
 * In order to protect the privatetoken, we remove it from the event params.
 *
 * @param  stdClass $token token object
 * @since  Moodle 3.2
 */
function external_log_token_request($token)
{
    global $DB;
    $token->privatetoken = null;
    // Log token access.
    $DB->set_field('external_tokens', 'lastaccess', time(), array('id' => $token->id));
    $params = array('objectid' => $token->id);
    $event = \core\event\webservice_token_sent::create($params);
    $event->add_record_snapshot('external_tokens', $token);
    $event->trigger();
}
            $token = new stdClass();
            $token->token = md5(uniqid(rand(), 1));
            $token->userid = $user->id;
            $token->tokentype = EXTERNAL_TOKEN_PERMANENT;
            $token->contextid = context_system::instance()->id;
            $token->creatorid = $user->id;
            $token->timecreated = time();
            $token->externalserviceid = $service_record->id;
            // MDL-43119 Token valid for 3 months (12 weeks).
            $token->validuntil = $token->timecreated + 12 * WEEKSECS;
            $token->id = $DB->insert_record('external_tokens', $token);
            $params = array('objectid' => $token->id, 'relateduserid' => $user->id, 'other' => array('auto' => true));
            $event = \core\event\webservice_token_created::create($params);
            $event->add_record_snapshot('external_tokens', $token);
            $event->trigger();
        } else {
            throw new moodle_exception('cannotcreatetoken', 'webservice', '', $serviceshortname);
        }
    }
    // log token access
    $DB->set_field('external_tokens', 'lastaccess', time(), array('id' => $token->id));
    $params = array('objectid' => $token->id);
    $event = \core\event\webservice_token_sent::create($params);
    $event->add_record_snapshot('external_tokens', $token);
    $event->trigger();
    $usertoken = new stdClass();
    $usertoken->token = $token->token;
    echo json_encode($usertoken);
} else {
    throw new moodle_exception('usernamenotfound', 'moodle');
}
 public function test_token_sent()
 {
     $user = $this->getDataGenerator()->create_user();
     $this->setUser($user);
     // The Web service API doesn't allow the testing of the events directly by
     // calling some functions which trigger the events, so what we are going here
     // is just checking that the event returns the expected information.
     $sink = $this->redirectEvents();
     $params = array('objectid' => 1, 'other' => array('auto' => true));
     $event = \core\event\webservice_token_sent::create($params);
     $event->trigger();
     $events = $sink->get_events();
     $this->assertCount(1, $events);
     $event = reset($events);
     $this->assertEquals(context_system::instance(), $event->get_context());
     $this->assertEquals(1, $event->objectid);
     $expected = array(SITEID, 'webservice', 'sending requested user token', '', 'User ID: ' . $user->id);
     $this->assertEventLegacyLogData($expected, $event);
 }