本文整理汇总了PHP中Core\Session\manager::loginas方法的典型用法代码示例。如果您正苦于以下问题:PHP manager::loginas方法的具体用法?PHP manager::loginas怎么用?PHP manager::loginas使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Core\Session\manager
的用法示例。
在下文中一共展示了manager::loginas方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
/**
* Run the deletion task.
*
* @throws \coding_exception if the module could not be removed.
*/
public function execute()
{
global $CFG;
require_once $CFG->dirroot . '/course/lib.php';
// Set the proper user.
if ($this->get_custom_data()->userid !== $this->get_custom_data()->realuserid) {
$realuser = \core_user::get_user($this->get_custom_data()->realuserid, '*', MUST_EXIST);
cron_setup_user($realuser);
\core\session\manager::loginas($this->get_custom_data()->userid, \context_system::instance(), false);
} else {
$user = \core_user::get_user($this->get_custom_data()->userid, '*', MUST_EXIST);
cron_setup_user($user);
}
$cms = $this->get_custom_data()->cms;
foreach ($cms as $cm) {
try {
course_delete_module($cm->id);
} catch (\Exception $e) {
throw new \coding_exception("The course module {$cm->id} could not be deleted. {$e->getTraceAsString()}");
}
}
}
示例2: session_loginas
/**
* Login as another user - no security checks here.
* @deprecated since 2.6
* @param int $userid
* @param stdClass $context
* @return void
*/
function session_loginas($userid, $context)
{
debugging('session_loginas() is deprecated, use \\core\\session\\manager::loginas() instead', DEBUG_DEVELOPER);
\core\session\manager::loginas($userid, $context);
}
示例3: test_get_realuser
public function test_get_realuser()
{
$this->resetAfterTest();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$this->setUser($user1);
$normal = \core\session\manager::get_realuser();
$this->assertSame($GLOBALS['USER'], $normal);
\core\session\manager::loginas($user2->id, context_system::instance());
$real = \core\session\manager::get_realuser();
unset($real->password);
unset($real->description);
unset($real->sesskey);
unset($user1->password);
unset($user1->description);
unset($user1->sesskey);
$this->assertEquals($real, $user1);
$this->assertSame($_SESSION['REALUSER'], $real);
}
示例4: test_log_writing
public function test_log_writing()
{
global $DB;
$this->resetAfterTest();
$this->preventResetByRollback();
// Logging waits till the transaction gets committed.
$this->setAdminUser();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$module1 = $this->getDataGenerator()->create_module('resource', array('course' => $course1));
$course2 = $this->getDataGenerator()->create_course();
$module2 = $this->getDataGenerator()->create_module('resource', array('course' => $course2));
// Test all plugins are disabled by this command.
set_config('enabled_stores', '', 'tool_log');
$manager = get_log_manager(true);
$stores = $manager->get_readers();
$this->assertCount(0, $stores);
// Enable logging plugin.
set_config('enabled_stores', 'logstore_standard', 'tool_log');
set_config('buffersize', 0, 'logstore_standard');
set_config('logguests', 1, 'logstore_standard');
$manager = get_log_manager(true);
$stores = $manager->get_readers();
$this->assertCount(1, $stores);
$this->assertEquals(array('logstore_standard'), array_keys($stores));
/** @var \logstore_standard\log\store $store */
$store = $stores['logstore_standard'];
$this->assertInstanceOf('logstore_standard\\log\\store', $store);
$this->assertInstanceOf('tool_log\\log\\writer', $store);
$this->assertTrue($store->is_logging());
$logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
$this->assertCount(0, $logs);
$this->setCurrentTimeStart();
$this->setUser(0);
$event1 = \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)));
$event1->trigger();
$logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
$this->assertCount(1, $logs);
$log1 = reset($logs);
unset($log1->id);
$log1->other = unserialize($log1->other);
$log1 = (array) $log1;
$data = $event1->get_data();
$data['origin'] = 'cli';
$data['ip'] = null;
$data['realuserid'] = null;
$this->assertEquals($data, $log1);
$this->setAdminUser();
\core\session\manager::loginas($user1->id, context_system::instance());
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
logstore_standard_restore::hack_executing(1);
$event2 = \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module2->cmid), 'other' => array('sample' => 6, 'xx' => 9)));
$event2->trigger();
logstore_standard_restore::hack_executing(0);
\core\session\manager::init_empty_session();
$this->assertFalse(\core\session\manager::is_loggedinas());
$logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
$this->assertCount(3, $logs);
array_shift($logs);
$log2 = array_shift($logs);
$this->assertSame('\\core\\event\\user_loggedinas', $log2->eventname);
$this->assertSame('cli', $log2->origin);
$log3 = array_shift($logs);
unset($log3->id);
$log3->other = unserialize($log3->other);
$log3 = (array) $log3;
$data = $event2->get_data();
$data['origin'] = 'restore';
$data['ip'] = null;
$data['realuserid'] = 2;
$this->assertEquals($data, $log3);
// Test table exists.
$tablename = $store->get_internal_log_table_name();
$this->assertTrue($DB->get_manager()->table_exists($tablename));
// Test reading.
$this->assertSame(3, $store->get_events_select_count('', array()));
$events = $store->get_events_select('', array(), 'timecreated ASC', 0, 0);
// Is actually sorted by "timecreated ASC, id ASC".
$this->assertCount(3, $events);
$resev1 = array_shift($events);
array_shift($events);
$resev2 = array_shift($events);
$this->assertEquals($event1->get_data(), $resev1->get_data());
$this->assertEquals($event2->get_data(), $resev2->get_data());
// Test buffering.
set_config('buffersize', 3, 'logstore_standard');
$manager = get_log_manager(true);
$stores = $manager->get_readers();
/** @var \logstore_standard\log\store $store */
$store = $stores['logstore_standard'];
$DB->delete_records('logstore_standard_log');
\logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(0, $DB->count_records('logstore_standard_log'));
\logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(0, $DB->count_records('logstore_standard_log'));
$store->flush();
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
\logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
//.........这里部分代码省略.........
示例5: test_check_capability
//.........这里部分代码省略.........
$this->setUser($user1);
$caughtexception = false;
try {
$courserepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// From a block within the course, I can view the repository.
$courserepo = repository::get_repository_by_id($courserepoid, $blockc1context);
$this->setUser($user1);
$this->assertTrue($courserepo->check_capability());
// But not without the capability.
$this->setUser($user2);
$caughtexception = false;
try {
$courserepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// From a block in the wrong course, I cannot view the repository.
$courserepo = repository::get_repository_by_id($courserepoid, $blockc2context);
$this->setUser($user1);
$caughtexception = false;
try {
$courserepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// Instance on a user level.
// Instance on a course level.
$record = new stdClass();
$record->contextid = $user1context->id;
$user1repoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
$record->contextid = $user2context->id;
$user2repoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
// Check that a user can see its own repository.
$userrepo = repository::get_repository_by_id($user1repoid, $syscontext);
$this->setUser($user1);
$this->assertTrue($userrepo->check_capability());
// But not without the capability.
$userrepo = repository::get_repository_by_id($user2repoid, $syscontext);
$this->setUser($user2);
$caughtexception = false;
try {
$userrepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// Check that a user cannot see someone's repository.
$userrepo = repository::get_repository_by_id($user2repoid, $syscontext);
$this->setUser($user1);
$caughtexception = false;
try {
$userrepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// Make sure the repo from user 2 was accessible.
role_unassign($roleid, $user2->id, $syscontext->id);
accesslib_clear_all_caches_for_unit_testing();
$this->setUser($user2);
$this->assertTrue($userrepo->check_capability());
role_assign($roleid, $user2->id, $syscontext->id);
accesslib_clear_all_caches_for_unit_testing();
// Check that a user can view SOME repositories when logged in as someone else.
$params = new stdClass();
$params->name = 'Dropbox';
$params->dropbox_key = 'key';
$params->dropbox_secret = 'secret';
$privaterepoid = $this->getDataGenerator()->create_repository('dropbox')->id;
$notprivaterepoid = $this->getDataGenerator()->create_repository('upload')->id;
$privaterepo = repository::get_repository_by_id($privaterepoid, $syscontext);
$notprivaterepo = repository::get_repository_by_id($notprivaterepoid, $syscontext);
$userrepo = repository::get_repository_by_id($user1repoid, $syscontext);
$this->setAdminUser();
\core\session\manager::loginas($user1->id, $syscontext);
// Logged in as, I cannot view a user instance.
$caughtexception = false;
try {
$userrepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// Logged in as, I cannot view a private instance.
$caughtexception = false;
try {
$privaterepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// Logged in as, I can view a non-private instance.
$this->assertTrue($notprivaterepo->check_capability());
}
示例6: test_log_writing
public function test_log_writing()
{
global $DB, $CFG;
$this->resetAfterTest();
$this->preventResetByRollback();
// Logging waits till the transaction gets committed.
$dbman = $DB->get_manager();
$this->assertTrue($dbman->table_exists('logstore_standard_log'));
$DB->delete_records('logstore_standard_log');
$this->setAdminUser();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$module1 = $this->getDataGenerator()->create_module('resource', array('course' => $course1));
$course2 = $this->getDataGenerator()->create_course();
$module2 = $this->getDataGenerator()->create_module('resource', array('course' => $course2));
// Test all plugins are disabled by this command.
set_config('enabled_stores', '', 'tool_log');
$manager = get_log_manager(true);
$stores = $manager->get_readers();
$this->assertCount(0, $stores);
// Fake the settings, we will abuse the standard plugin table here...
$parts = explode('_', get_class($DB));
set_config('dbdriver', $parts[1] . '/' . $parts[0], 'logstore_database');
set_config('dbhost', $CFG->dbhost, 'logstore_database');
set_config('dbuser', $CFG->dbuser, 'logstore_database');
set_config('dbpass', $CFG->dbpass, 'logstore_database');
set_config('dbname', $CFG->dbname, 'logstore_database');
set_config('dbtable', $CFG->prefix . 'logstore_standard_log', 'logstore_database');
if (!empty($CFG->dboptions['dbpersist'])) {
set_config('dbpersist', 1, 'logstore_database');
} else {
set_config('dbpersist', 0, 'logstore_database');
}
if (!empty($CFG->dboptions['dbsocket'])) {
set_config('dbsocket', $CFG->dboptions['dbsocket'], 'logstore_database');
} else {
set_config('dbsocket', '', 'logstore_database');
}
if (!empty($CFG->dboptions['dbport'])) {
set_config('dbport', $CFG->dboptions['dbport'], 'logstore_database');
} else {
set_config('dbport', '', 'logstore_database');
}
if (!empty($CFG->dboptions['dbschema'])) {
set_config('dbschema', $CFG->dboptions['dbschema'], 'logstore_database');
} else {
set_config('dbschema', '', 'logstore_database');
}
if (!empty($CFG->dboptions['dbcollation'])) {
set_config('dbcollation', $CFG->dboptions['dbcollation'], 'logstore_database');
} else {
set_config('dbcollation', '', 'logstore_database');
}
// Enable logging plugin.
set_config('enabled_stores', 'logstore_database', 'tool_log');
set_config('buffersize', 0, 'logstore_database');
set_config('logguests', 1, 'logstore_database');
$manager = get_log_manager(true);
$stores = $manager->get_readers();
$this->assertCount(1, $stores);
$this->assertEquals(array('logstore_database'), array_keys($stores));
$store = $stores['logstore_database'];
$this->assertInstanceOf('logstore_database\\log\\store', $store);
$this->assertInstanceOf('tool_log\\log\\writer', $store);
$this->assertTrue($store->is_logging());
$logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
$this->assertCount(0, $logs);
$this->setCurrentTimeStart();
$this->setUser(0);
$event1 = \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)));
$event1->trigger();
$logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
$this->assertCount(1, $logs);
$log1 = reset($logs);
unset($log1->id);
$log1->other = unserialize($log1->other);
$log1 = (array) $log1;
$data = $event1->get_data();
$data['origin'] = 'cli';
$data['ip'] = null;
$data['realuserid'] = null;
$this->assertEquals($data, $log1);
$this->setAdminUser();
\core\session\manager::loginas($user1->id, context_system::instance());
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
$event2 = \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module2->cmid), 'other' => array('sample' => 6, 'xx' => 9)));
$event2->trigger();
\core\session\manager::init_empty_session();
$this->assertFalse(\core\session\manager::is_loggedinas());
$logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
$this->assertCount(3, $logs);
array_shift($logs);
$log2 = array_shift($logs);
$this->assertSame('\\core\\event\\user_loggedinas', $log2->eventname);
$log3 = array_shift($logs);
unset($log3->id);
$log3->other = unserialize($log3->other);
$log3 = (array) $log3;
$data = $event2->get_data();
//.........这里部分代码省略.........
示例7: array
$course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
// User must be logged in.
$systemcontext = context_system::instance();
$coursecontext = context_course::instance($course->id);
require_login();
if (has_capability('moodle/user:loginas', $systemcontext)) {
if (is_siteadmin($userid)) {
print_error('nologinas');
}
$context = $systemcontext;
$PAGE->set_context($context);
} else {
require_login($course);
require_capability('moodle/user:loginas', $coursecontext);
if (is_siteadmin($userid)) {
print_error('nologinas');
}
if (!is_enrolled($coursecontext, $userid)) {
print_error('usernotincourse');
}
$context = $coursecontext;
}
// Login as this user and return to course home page.
\core\session\manager::loginas($userid, $context);
$newfullname = fullname($USER, true);
$strloginas = get_string('loginas');
$strloggedinas = get_string('loggedinas', '', $newfullname);
$PAGE->set_title($strloggedinas);
$PAGE->set_heading($course->fullname);
$PAGE->navbar->add($strloggedinas);
notice($strloggedinas, "{$CFG->wwwroot}/course/view.php?id={$course->id}");
示例8: redirect
// If the user is not enrolled, a later check will take care of things.
}
}
// Aditionally, if we are switching from 'instructor' view (i.e., not in 'firstuse' mode), switch user.
if ($viewrole == 'instructor') {
if (is_siteadmin($demostudentid)) {
// This should never happen. Haven't seen it happen yet.
print_error('nologinas');
}
if (!is_enrolled($coursecontext, $demostudentid)) {
// This should never happen. Haven't seen it happen yet.
print_error('usernotincourse');
}
// Switch role using builtin loginas functionality.
$syscontext = context_system::instance();
\core\session\manager::loginas($demostudentid, $syscontext);
}
} else {
// You were on the DemoStudent view. You're switching back to instructor.
if (\core\session\manager::is_loggedinas()) {
// Later, we would like to return to the session before we switched roles without requiring login,
// iff we can ensure that it's not a security hole.
require_logout();
redirect(new moodle_url($url, array('redirect' => 1)));
} else {
// If the demouser somehow manages to login without using the DemoStudent block, make them log out.
// This should not typically happen. DemoStudent should always be 'loginas'ed.
// You can test this by:
// - disabling pasword clobbering of the demostudent user
// - logging in manually
// - viewing course