本文整理汇总了PHP中Core\Session\manager::set_user方法的典型用法代码示例。如果您正苦于以下问题:PHP manager::set_user方法的具体用法?PHP manager::set_user怎么用?PHP manager::set_user使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Core\Session\manager
的用法示例。
在下文中一共展示了manager::set_user方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_set_user
public function test_set_user()
{
global $USER, $DB;
$this->assertEquals(0, $USER->id);
$this->assertSame($_SESSION['USER'], $USER);
$user = $DB->get_record('user', array('id' => 2));
$this->assertNotEmpty($user);
$this->setUser($user);
$this->assertEquals(2, $USER->id);
$this->assertEquals(2, $_SESSION['USER']->id);
$this->assertSame($_SESSION['USER'], $USER);
$USER->id = 3;
$this->assertEquals(3, $USER->id);
$this->assertEquals(3, $_SESSION['USER']->id);
$this->assertSame($_SESSION['USER'], $USER);
\core\session\manager::set_user($user);
$this->assertEquals(2, $USER->id);
$this->assertEquals(2, $_SESSION['USER']->id);
$this->assertSame($_SESSION['USER'], $USER);
$USER = $DB->get_record('user', array('id' => 1));
$this->assertNotEmpty($USER);
$this->assertEquals(1, $USER->id);
$this->assertEquals(1, $_SESSION['USER']->id);
$this->assertSame($_SESSION['USER'], $USER);
$this->setUser(null);
$this->assertEquals(0, $USER->id);
$this->assertSame($_SESSION['USER'], $USER);
}
示例2: test_set_user
public function test_set_user()
{
global $USER;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser(0);
$this->assertEquals(0, $USER->id);
\core\session\manager::set_user($user);
$this->assertEquals($user->id, $USER->id);
}
示例3: execute
public function execute()
{
global $CFG, $DB;
require_once $CFG->dirroot . "/mod/turnitintooltwo/lib.php";
require_once $CFG->dirroot . "/mod/turnitintooltwo/turnitintooltwo_view.class.php";
$data = (array) $this->get_custom_data();
// Make sure we are still wanted.
$submission = $DB->get_record('turnitintooltwo_submissions', array('id' => $data['submissionid']));
if (!$submission) {
return true;
}
cli_writeln("Processing Turnitintooltwo submission: " . $data['submissionid']);
$user = $DB->get_record('user', array('id' => $data['userid']));
\core\session\manager::set_user($user);
$turnitintooltwo = $DB->get_record('turnitintooltwo', array('id' => $data['tiiid']));
list($course, $cm) = get_course_and_cm_from_instance($turnitintooltwo, 'turnitintooltwo');
try {
$turnitintooltwoassignment = new \turnitintooltwo_assignment($turnitintooltwo->id, $turnitintooltwo);
$turnitintooltwosubmission = new \turnitintooltwo_submission($data['submissionid'], "moodle", $turnitintooltwoassignment);
$parts = $turnitintooltwoassignment->get_parts();
$tiisubmission = $turnitintooltwosubmission->do_tii_submission($cm, $turnitintooltwoassignment);
// Update submission.
$DB->update_record('turnitintooltwo_submissions', array('id' => $data['submissionid'], 'submission_modified' => $data['subtime']));
} catch (\Exception $e) {
$tiisubmission = array('success' => false, 'message' => $e->getMessage());
cli_writeln($e->getMessage());
}
$digitalreceipt = $tiisubmission;
$digitalreceipt['is_manual'] = 0;
$digitalreceipt = json_encode($digitalreceipt);
$this->update_sub_status($data['submissionid'], $tiisubmission['success'], $digitalreceipt);
if ($tiisubmission['success'] === true) {
$lockedassignment = new \stdClass();
$lockedassignment->id = $turnitintooltwoassignment->turnitintooltwo->id;
$lockedassignment->submitted = 1;
$DB->update_record('turnitintooltwo', $lockedassignment);
$lockedpart = new \stdClass();
$lockedpart->id = $data['submissionpart'];
$lockedpart->submitted = 1;
// Disable anonymous marking if post date has passed.
if ($parts[$data['submissionpart']]->dtpost <= time()) {
$lockedpart->unanon = 1;
}
$DB->update_record('turnitintooltwo_parts', $lockedpart);
cli_writeln("Finished processing successful submission: " . $data['submissionid']);
} else {
turnitintooltwo_add_to_log($course->id, "errored submission", 'view.php?id=' . $cm->id, "Failed to submit '" . $turnitintooltwosubmission->submission_title . "'", $cm->id, $user->id, array('submissionid' => $data['submissionid']));
cli_writeln("Finished processing unsuccessful submission: " . $data['submissionid']);
}
\core\session\manager::set_user(get_admin());
return $tiisubmission['success'];
}
示例4: test_set_user
public function test_set_user()
{
global $USER, $DB, $SESSION;
$this->resetAfterTest();
$this->assertEquals(0, $USER->id);
$this->assertSame($_SESSION['USER'], $USER);
$this->assertSame($GLOBALS['USER'], $USER);
$user = $DB->get_record('user', array('id' => 2));
$this->assertNotEmpty($user);
$this->setUser($user);
$this->assertEquals(2, $USER->id);
$this->assertEquals(2, $_SESSION['USER']->id);
$this->assertSame($_SESSION['USER'], $USER);
$this->assertSame($GLOBALS['USER'], $USER);
$USER->id = 3;
$this->assertEquals(3, $USER->id);
$this->assertEquals(3, $_SESSION['USER']->id);
$this->assertSame($_SESSION['USER'], $USER);
$this->assertSame($GLOBALS['USER'], $USER);
\core\session\manager::set_user($user);
$this->assertEquals(2, $USER->id);
$this->assertEquals(2, $_SESSION['USER']->id);
$this->assertSame($_SESSION['USER'], $USER);
$this->assertSame($GLOBALS['USER'], $USER);
$USER = $DB->get_record('user', array('id' => 1));
$this->assertNotEmpty($USER);
$this->assertEquals(1, $USER->id);
$this->assertEquals(1, $_SESSION['USER']->id);
$this->assertSame($_SESSION['USER'], $USER);
$this->assertSame($GLOBALS['USER'], $USER);
$this->setUser(null);
$this->assertEquals(0, $USER->id);
$this->assertSame($_SESSION['USER'], $USER);
$this->assertSame($GLOBALS['USER'], $USER);
// Ensure session is reset after setUser, as it may contain extra info.
$SESSION->sometestvalue = true;
$this->setUser($user);
$this->assertObjectNotHasAttribute('sometestvalue', $SESSION);
}
示例5: cron_setup_user
/**
* Sets up current user and course environment (lang, etc.) in cron.
* Do not use outside of cron script!
*
* @param stdClass $user full user object, null means default cron user (admin),
* value 'reset' means reset internal static caches.
* @param stdClass $course full course record, null means $SITE
* @return void
*/
function cron_setup_user($user = NULL, $course = NULL)
{
global $CFG, $SITE, $PAGE;
if (!CLI_SCRIPT) {
throw new coding_exception('Function cron_setup_user() cannot be used in normal requests!');
}
static $cronuser = NULL;
static $cronsession = NULL;
if ($user === 'reset') {
$cronuser = null;
$cronsession = null;
\core\session\manager::init_empty_session();
return;
}
if (empty($cronuser)) {
/// ignore admins timezone, language and locale - use site default instead!
$cronuser = get_admin();
$cronuser->timezone = $CFG->timezone;
$cronuser->lang = '';
$cronuser->theme = '';
unset($cronuser->description);
$cronsession = new stdClass();
}
if (!$user) {
// Cached default cron user (==modified admin for now).
\core\session\manager::init_empty_session();
\core\session\manager::set_user($cronuser);
$GLOBALS['SESSION'] = $cronsession;
} else {
// Emulate real user session - needed for caps in cron.
if ($GLOBALS['USER']->id != $user->id) {
\core\session\manager::init_empty_session();
\core\session\manager::set_user($user);
}
}
// TODO MDL-19774 relying on global $PAGE in cron is a bad idea.
// Temporary hack so that cron does not give fatal errors.
$PAGE = new moodle_page();
if ($course) {
$PAGE->set_course($course);
} else {
$PAGE->set_course($SITE);
}
// TODO: it should be possible to improve perf by caching some limited number of users here ;-)
}
示例6: authenticate_user
//.........这里部分代码省略.........
}
if (!($auth = get_auth_plugin('webservice'))) {
throw new webservice_access_exception('The web service authentication plugin is missing.');
}
$this->restricted_context = context_system::instance();
if (!$this->username) {
throw new moodle_exception('missingusername', 'webservice');
}
if (!$this->password) {
throw new moodle_exception('missingpassword', 'webservice');
}
if (!$auth->user_login_webservice($this->username, $this->password)) {
// Log failed login attempts.
$params = $loginfaileddefaultparams;
$params['other']['reason'] = 'password';
$params['other']['username'] = $this->username;
$event = \core\event\webservice_login_failed::create($params);
$event->set_legacy_logdata(array(SITEID, 'webservice', get_string('simpleauthlog', 'webservice'), '', get_string('failedtolog', 'webservice') . ": " . $this->username . "/" . $this->password . " - " . getremoteaddr(), 0));
$event->trigger();
throw new moodle_exception('wrongusernamepassword', 'webservice');
}
$user = $DB->get_record('user', array('username' => $this->username, 'mnethostid' => $CFG->mnet_localhost_id), '*', MUST_EXIST);
} else {
if ($this->authmethod == WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN) {
$user = $this->authenticate_by_token(EXTERNAL_TOKEN_PERMANENT);
} else {
$user = $this->authenticate_by_token(EXTERNAL_TOKEN_EMBEDDED);
}
}
//Non admin can not authenticate if maintenance mode
$hassiteconfig = has_capability('moodle/site:config', context_system::instance(), $user);
if (!empty($CFG->maintenance_enabled) and !$hassiteconfig) {
throw new moodle_exception('sitemaintenance', 'admin');
}
//only confirmed user should be able to call web service
if (!empty($user->deleted)) {
$params = $loginfaileddefaultparams;
$params['other']['reason'] = 'user_deleted';
$params['other']['username'] = $user->username;
$event = \core\event\webservice_login_failed::create($params);
$event->set_legacy_logdata(array(SITEID, '', '', '', get_string('wsaccessuserdeleted', 'webservice', $user->username) . " - " . getremoteaddr(), 0, $user->id));
$event->trigger();
throw new webservice_access_exception('Refused web service access for deleted username: ' . $user->username);
}
//only confirmed user should be able to call web service
if (empty($user->confirmed)) {
$params = $loginfaileddefaultparams;
$params['other']['reason'] = 'user_unconfirmed';
$params['other']['username'] = $user->username;
$event = \core\event\webservice_login_failed::create($params);
$event->set_legacy_logdata(array(SITEID, '', '', '', get_string('wsaccessuserunconfirmed', 'webservice', $user->username) . " - " . getremoteaddr(), 0, $user->id));
$event->trigger();
throw new moodle_exception('wsaccessuserunconfirmed', 'webservice', '', $user->username);
}
//check the user is suspended
if (!empty($user->suspended)) {
$params = $loginfaileddefaultparams;
$params['other']['reason'] = 'user_unconfirmed';
$params['other']['username'] = $user->username;
$event = \core\event\webservice_login_failed::create($params);
$event->set_legacy_logdata(array(SITEID, '', '', '', get_string('wsaccessusersuspended', 'webservice', $user->username) . " - " . getremoteaddr(), 0, $user->id));
$event->trigger();
throw new webservice_access_exception('Refused web service access for suspended username: ' . $user->username);
}
//retrieve the authentication plugin if no previously done
if (empty($auth)) {
$auth = get_auth_plugin($user->auth);
}
// check if credentials have expired
if (!empty($auth->config->expiration) and $auth->config->expiration == 1) {
$days2expire = $auth->password_expire($user->username);
if (intval($days2expire) < 0) {
$params = $loginfaileddefaultparams;
$params['other']['reason'] = 'password_expired';
$params['other']['username'] = $user->username;
$event = \core\event\webservice_login_failed::create($params);
$event->set_legacy_logdata(array(SITEID, '', '', '', get_string('wsaccessuserexpired', 'webservice', $user->username) . " - " . getremoteaddr(), 0, $user->id));
$event->trigger();
throw new webservice_access_exception('Refused web service access for password expired username: ' . $user->username);
}
}
//check if the auth method is nologin (in this case refuse connection)
if ($user->auth == 'nologin') {
$params = $loginfaileddefaultparams;
$params['other']['reason'] = 'login';
$params['other']['username'] = $user->username;
$event = \core\event\webservice_login_failed::create($params);
$event->set_legacy_logdata(array(SITEID, '', '', '', get_string('wsaccessusernologin', 'webservice', $user->username) . " - " . getremoteaddr(), 0, $user->id));
$event->trigger();
throw new webservice_access_exception('Refused web service access for nologin authentication username: ' . $user->username);
}
// now fake user login, the session is completely empty too
enrol_check_plugins($user);
\core\session\manager::set_user($user);
$this->userid = $user->id;
if ($this->authmethod != WEBSERVICE_AUTHMETHOD_SESSION_TOKEN && !has_capability("webservice/{$this->wsname}:use", $this->restricted_context)) {
throw new webservice_access_exception('You are not allowed to use the {$a} protocol (missing capability: webservice/' . $this->wsname . ':use)');
}
external_api::set_context_restriction($this->restricted_context);
}
示例7: isValid
/**
* See if the request contains a proper username/password for login
*
* @param Zend_Controller_Request_Http $request The request to check
* @return boolean
*/
public function isValid($request)
{
// No cookies !
if (!PHPUNIT_TEST) {
if (!defined('NO_MOODLE_COOKIES') or !NO_MOODLE_COOKIES) {
$this->_error(self::LOGIN_COOKIE);
return false;
}
}
$wsusername = $request->getParam($this->_paramusername, '');
$wsusername = clean_param($wsusername, PARAM_RAW);
$wspassword = $request->getParam($this->_parampassword, '');
$wspassword = clean_param($wspassword, PARAM_RAW);
// Are they empty ?
if (empty($wsusername) or empty($wspassword)) {
$this->_error(self::LOGIN_MISSING);
return false;
}
// Can we login ?
if (!($user = authenticate_user_login($wsusername, $wspassword))) {
$this->_error(self::LOGIN_FAIL);
return false;
}
// Set the user to the session
enrol_check_plugins($user);
\core\session\manager::set_user($user);
return true;
}
示例8: set_calendar_type
/**
* Set the calendar type for this user.
*
* @param string $type the calendar type we want to set
*/
private function set_calendar_type($type)
{
$this->user->calendartype = $type;
\core\session\manager::set_user($this->user);
}
示例9: require_user_key_login
/**
* Require key login. Function terminates with error if key not found or incorrect.
*
* @uses NO_MOODLE_COOKIES
* @uses PARAM_ALPHANUM
* @param string $script unique script identifier
* @param int $instance optional instance id
* @return int Instance ID
*/
function require_user_key_login($script, $instance = null)
{
global $DB;
if (!NO_MOODLE_COOKIES) {
print_error('sessioncookiesdisable');
}
// Extra safety.
\core\session\manager::write_close();
$keyvalue = required_param('key', PARAM_ALPHANUM);
if (!($key = $DB->get_record('user_private_key', array('script' => $script, 'value' => $keyvalue, 'instance' => $instance)))) {
print_error('invalidkey');
}
if (!empty($key->validuntil) and $key->validuntil < time()) {
print_error('expiredkey');
}
if ($key->iprestriction) {
$remoteaddr = getremoteaddr(null);
if (empty($remoteaddr) or !address_in_subnet($remoteaddr, $key->iprestriction)) {
print_error('ipmismatch');
}
}
if (!($user = $DB->get_record('user', array('id' => $key->userid)))) {
print_error('invaliduserid');
}
// Emulate normal session.
enrol_check_plugins($user);
\core\session\manager::set_user($user);
// Note we are not using normal login.
if (!defined('USER_KEY_LOGIN')) {
define('USER_KEY_LOGIN', true);
}
// Return instance id - it might be empty.
return $key->instance;
}
示例10: array
define('NO_OUTPUT_BUFFERING', true);
require __DIR__ . '/../../../../config.php';
require_once $CFG->libdir . '/clilib.php';
// CLI options.
list($options, $unrecognized) = cli_get_params(array('help' => false, 'size' => false, 'fixeddataset' => false, 'filesizelimit' => false, 'bypasscheck' => false, 'quiet' => false), array('h' => 'help'));
$sitesizes = '* ' . implode(PHP_EOL . '* ', tool_generator_site_backend::get_size_choices());
// Display help.
if (!empty($options['help']) || empty($options['size'])) {
echo "\nUtility to generate a standard test site data set.\n\nNot for use on live sites; only normally works if debugging is set to DEVELOPER\nlevel.\n\nConsider that, depending on the size you select, this CLI tool can really generate a lot of data, aproximated sizes:\n\n{$sitesizes}\n\nOptions:\n--size Size of the generated site, this value affects the number of courses and their size. Accepted values: XS, S, M, L, XL, or XXL (required)\n--fixeddataset Use a fixed data set instead of randomly generated data\n--filesizelimit Limits the size of the generated files to the specified bytes\n--bypasscheck Bypasses the developer-mode check (be careful!)\n--quiet Do not show any output\n\n-h, --help Print out this help\n\nExample from Moodle root directory:\n\$ php admin/tool/generator/cli/maketestsite.php --size=S\n";
// Exit with error unless we're showing this because they asked for it.
exit(empty($options['help']) ? 1 : 0);
}
// Check debugging is set to developer level.
if (empty($options['bypasscheck']) && !$CFG->debugdeveloper) {
cli_error(get_string('error_notdebugging', 'tool_generator'));
}
// Get options.
$sizename = $options['size'];
$fixeddataset = $options['fixeddataset'];
$filesizelimit = $options['filesizelimit'];
// Check size.
try {
$size = tool_generator_site_backend::size_for_name($sizename);
} catch (coding_exception $e) {
cli_error("Invalid size ({$sizename}). Use --help for help.");
}
// Switch to admin user account.
\core\session\manager::set_user(get_admin());
// Do backend code to generate site.
$backend = new tool_generator_site_backend($size, $options['bypasscheck'], $fixeddataset, $filesizelimit, empty($options['quiet']));
$backend->make();
示例11: test_set_user
public function test_set_user()
{
global $USER;
$this->resetAfterTest();
$this->assertEquals(0, $USER->id);
$user = $this->getDataGenerator()->create_user();
$this->assertObjectHasAttribute('description', $user);
$this->assertObjectHasAttribute('password', $user);
\core\session\manager::set_user($user);
$this->assertEquals($user->id, $USER->id);
$this->assertObjectNotHasAttribute('description', $user);
$this->assertObjectNotHasAttribute('password', $user);
$this->assertObjectHasAttribute('sesskey', $user);
$this->assertSame($user, $GLOBALS['USER']);
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
$this->assertSame($GLOBALS['USER'], $USER);
}
示例12: cron_setup_user
/**
* Sets up current user and course environment (lang, etc.) in cron.
* Do not use outside of cron script!
*
* @param stdClass $user full user object, null means default cron user (admin)
* @param $course full course record, null means $SITE
* @return void
*/
function cron_setup_user($user = NULL, $course = NULL)
{
global $CFG, $SITE, $PAGE;
static $cronuser = NULL;
static $cronsession = NULL;
if (empty($cronuser)) {
/// ignore admins timezone, language and locale - use site default instead!
$cronuser = get_admin();
$cronuser->timezone = $CFG->timezone;
$cronuser->lang = '';
$cronuser->theme = '';
unset($cronuser->description);
$cronsession = new stdClass();
}
if (!$user) {
// cached default cron user (==modified admin for now)
\core\session\manager::set_user($cronuser);
$_SESSION['SESSION'] = $cronsession;
} else {
// emulate real user session - needed for caps in cron
if ($_SESSION['USER']->id != $user->id) {
\core\session\manager::set_user($user);
$_SESSION['SESSION'] = new stdClass();
}
}
// TODO MDL-19774 relying on global $PAGE in cron is a bad idea.
// Temporary hack so that cron does not give fatal errors.
$PAGE = new moodle_page();
if ($course) {
$PAGE->set_course($course);
} else {
$PAGE->set_course($SITE);
}
// TODO: it should be possible to improve perf by caching some limited number of users here ;-)
}
示例13: setUser
/**
* Set current $USER, reset access cache.
* @static
* @param null|int|stdClass $user user record, null or 0 means non-logged-in, positive integer means userid
* @return void
*/
public static function setUser($user = null)
{
global $CFG, $DB;
if (is_object($user)) {
$user = clone $user;
} else {
if (!$user) {
$user = new stdClass();
$user->id = 0;
$user->mnethostid = $CFG->mnet_localhost_id;
} else {
$user = $DB->get_record('user', array('id' => $user));
}
}
unset($user->description);
unset($user->access);
unset($user->preference);
// Enusre session is empty, as it may contain caches and user specific info.
\core\session\manager::init_empty_session();
\core\session\manager::set_user($user);
}
示例14: lti_set_session_user
/**
* Set the passed user ID to the session user.
*
* @param int $userid
*/
function lti_set_session_user($userid)
{
global $DB;
if ($user = $DB->get_record('user', array('id' => $userid))) {
\core\session\manager::set_user($user);
}
}
示例15: before_scenario
/**
* Resets the test environment.
*
* @param OutlineExampleEvent|ScenarioEvent $event event fired before scenario.
* @throws coding_exception If here we are not using the test database it should be because of a coding error
* @BeforeScenario
*/
public function before_scenario($event)
{
global $DB, $CFG;
// TODO: check config value to ensure site is set for performance data.
$moreinfo = 'More info in ' . behat_command::DOCS_URL . '#Running_tests';
$driverexceptionmsg = 'Selenium server is not running, you need to start it to run tests that involve Javascript. ' . $moreinfo;
try {
$session = $this->getSession();
} catch (CurlExec $e) {
// Exception thrown by WebDriver, so only @javascript tests will be caugth; in
// behat_util::is_server_running() we already checked that the server is running.
throw new Exception($driverexceptionmsg);
} catch (DriverException $e) {
throw new Exception($driverexceptionmsg);
} catch (UnknownError $e) {
// Generic 'I have no idea' Selenium error. Custom exception to provide more feedback about possible solutions.
$this->throw_unknown_exception($e);
}
// We need the Mink session to do it and we do it only before the first scenario.
if (self::is_first_scenario()) {
behat_selectors::register_moodle_selectors($session);
behat_context_helper::set_session($session);
}
// Reset mink session between the scenarios.
$session->reset();
// Assign valid data to admin user (some generator-related code needs a valid user).
$user = $DB->get_record('user', array('username' => 'admin'));
\core\session\manager::set_user($user);
// Start always in the the homepage.
try {
// Let's be conservative as we never know when new upstream issues will affect us.
$session->visit($this->locate_path('/'));
} catch (UnknownError $e) {
$this->throw_unknown_exception($e);
}
}