本文整理汇总了PHP中assign_capability函数的典型用法代码示例。如果您正苦于以下问题:PHP assign_capability函数的具体用法?PHP assign_capability怎么用?PHP assign_capability使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了assign_capability函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_coursepermissions
/**
* Test for correct assignment of course permissions
*/
public function test_coursepermissions()
{
global $DB, $CFG, $USER;
accesslib_clear_all_caches(true);
set_config('siteguest', '');
set_config('siteadmins', '');
$this->assertFalse($this->has_program_create_capability());
$this->assertFalse($this->has_associate_and_manage_capability());
$this->assertFalse($this->has_program_view_capability());
$syscontext = context_system::instance();
$assigninguser = new user(array('idnumber' => 'testuserid', 'username' => 'testuser', 'firstname' => 'testuser', 'lastname' => 'testuser', 'email' => 'testuser@testuserdomain.com', 'country' => 'CA'));
$assigninguser->save();
$roleid = create_role('userrole', 'userrole', 'userrole');
$usr = $DB->get_record('user', array('username' => 'testuser'));
$this->setUser($usr);
$USER = $usr;
assign_capability('local/elisprogram:associate', CAP_ALLOW, $roleid, $syscontext->id);
assign_capability('local/elisprogram:manage', CAP_ALLOW, $roleid, $syscontext->id);
assign_capability('local/elisprogram:program_create', CAP_ALLOW, $roleid, $syscontext->id);
assign_capability('local/elisprogram:program_enrol', CAP_ALLOW, $roleid, $syscontext->id);
assign_capability('local/elisprogram:program_view', CAP_ALLOW, $roleid, $syscontext->id);
role_assign($roleid, $usr->id, $syscontext->id);
$this->assertTrue($this->has_program_create_capability());
$this->assertTrue($this->has_associate_and_manage_capability());
$this->assertTrue($this->has_program_view_capability());
$this->setUser(null);
}
示例2: load_data
/**
* Loads some data to be used by the different tests
* @param int $s1grade Student 1 grade
* @param int $s2grade Student 2 grade
* @return array Course and users instances
*/
private function load_data($s1grade, $s2grade)
{
global $DB;
$course = $this->getDataGenerator()->create_course(array('groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1));
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$student1 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id);
$student2 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id);
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
$teacher = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
$context = context_course::instance($course->id);
assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $context);
accesslib_clear_all_caches_for_unit_testing();
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
groups_add_member($group1->id, $student1->id);
groups_add_member($group1->id, $teacher->id);
groups_add_member($group2->id, $student2->id);
$assignment = $this->getDataGenerator()->create_module('assign', array('name' => "Test assign", 'course' => $course->id));
$modcontext = get_coursemodule_from_instance('assign', $assignment->id, $course->id);
$assignment->cmidnumber = $modcontext->id;
$student1grade = array('userid' => $student1->id, 'rawgrade' => $s1grade);
$student2grade = array('userid' => $student2->id, 'rawgrade' => $s2grade);
$studentgrades = array($student1->id => $student1grade, $student2->id => $student2grade);
assign_grade_item_update($assignment, $studentgrades);
return array($course, $teacher, $student1, $student2, $assignment);
}
示例3: qcreate_student_q_access_sync
/**
* Called from cron and update_instance. Not called from add_instance as the contexts are not set up yet.
*/
function qcreate_student_q_access_sync($qcreate, $cmcontext = null, $course = null, $forcesync = false)
{
//check if a check is needed
$timenow = time();
$activityopen = ($qcreate->timeopen == 0 || $qcreate->timeopen < $timenow) && ($qcreate->timeclose == 0 || $qcreate->timeclose > $timenow);
$activitywasopen = ($qcreate->timeopen == 0 || $qcreate->timeopen < $qcreate->timesync) && ($qcreate->timeclose == 0 || $qcreate->timeclose > $qcreate->timesync);
$needsync = empty($qcreate->timesync) || $activitywasopen != $activityopen;
if ($forcesync || $needsync) {
if ($cmcontext == null) {
$cm = get_coursemodule_from_instance('qcreate', $qcreate->id);
$cmcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
}
if ($course == null) {
$course = get_record('course', 'id', $qcreate->course);
}
$studentrole = get_default_course_role($course);
if ($activityopen) {
$capabilitiestoassign = array(0 => array('moodle/question:add' => 1, 'moodle/question:usemine' => -1, 'moodle/question:viewmine' => -1, 'moodle/question:editmine' => -1), 1 => array('moodle/question:add' => 1, 'moodle/question:usemine' => 1, 'moodle/question:viewmine' => -1, 'moodle/question:editmine' => -1), 2 => array('moodle/question:add' => 1, 'moodle/question:usemine' => 1, 'moodle/question:viewmine' => 1, 'moodle/question:editmine' => -1), 3 => array('moodle/question:add' => 1, 'moodle/question:usemine' => 1, 'moodle/question:viewmine' => 1, 'moodle/question:editmine' => 1));
foreach ($capabilitiestoassign[$qcreate->studentqaccess] as $capability => $permission) {
assign_capability($capability, $permission, $studentrole->id, $cmcontext->id, true);
}
} else {
$capabilitiestounassign = array('moodle/question:add', 'moodle/question:usemine', 'moodle/question:viewmine', 'moodle/question:editmine');
foreach ($capabilitiestounassign as $capability) {
unassign_capability($capability, $studentrole->id, $cmcontext->id);
}
}
set_field('qcreate', 'timesync', $timenow, 'id', $qcreate->id);
}
}
示例4: test_mymoodleredirectreturnsfalseforadmin
/**
* Validate that redirection from My Moodle does not happen for admins
*/
public function test_mymoodleredirectreturnsfalseforadmin()
{
global $CFG, $USER, $DB;
require_once $CFG->dirroot . '/user/lib.php';
// Make sure we're not a guest.
set_config('siteguest', '');
// Obtain the system context.
$syscontext = context_system::instance();
// Set up the current user global.
$user = new stdClass();
$user->username = "testuser";
$userid = user_create_user($user);
$USER = $DB->get_record('user', array('id' => $userid));
// Enable functionaltiy.
pm_set_config('mymoodle_redirect', 1);
elis::$config = new elis_config();
// Give the admin sufficient permissions.
$roleid = create_role('adminrole', 'adminrole', 'adminrole');
assign_capability('moodle/site:config', CAP_ALLOW, $roleid, $syscontext->id);
role_assign($roleid, $USER->id, $syscontext->id);
// Validate that redirection does not happen for admins.
$result = pm_mymoodle_redirect();
// Clear out cached permissions data so we don't affect other tests.
accesslib_clear_all_caches(true);
$this->assertFalse($result);
}
示例5: xmldb_block_demostudent_install
/**
* @package block
* @subpackage demostudent
* @author Dominik Royko royko@ualberta.ca
*/
function xmldb_block_demostudent_install()
{
global $DB;
$result = true;
$systemcontext = context_system::instance();
// Create DemoStudent role.
$contextlevels = array(CONTEXT_COURSE, CONTEXT_MODULE);
if (!($demostudentrole = $DB->get_record('role', array('shortname' => 'demostudent')))) {
if ($roleid = create_role(get_string('roledemostudentname', 'block_demostudent'), 'demostudent', get_string('roledemostudentdescription', 'block_demostudent'), 'student')) {
$newrole = new stdClass();
$newrole->id = $roleid;
// Set the capabilities to the archetype (student).
// Caution: new capabilities un/set here can get clobbered by 'clonepermissionsfrom',
// defined in access.php.
reset_role_capabilities($roleid);
// DemoStudent needs to see the DemoStudent block.
$result = $result && assign_capability('block/demostudent:seedemostudentblock', CAP_ALLOW, $newrole->id, $systemcontext->id);
// DemoStudent should be able to see hidden courses to facilitate testing.
$result = $result && assign_capability('moodle/course:viewhiddencourses', CAP_ALLOW, $newrole->id, $systemcontext->id);
// DemoStudent should NOT be able to add more demostudents!
$result = $result && unassign_capability('block/demostudent:addinstance', $newrole->id);
$systemcontext->mark_dirty();
set_role_contextlevels($newrole->id, $contextlevels);
} else {
$result = false;
}
}
return $result;
}
示例6: xmldb_block_roomscheduler_upgrade
/**
*
* @since 2.0
* @package blocks
* @copyright 2011 Dustin Durand
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
function xmldb_block_roomscheduler_upgrade($oldversion)
{
global $DB;
$dbman = $DB->get_manager();
if ($oldversion < 2011150301) {
$timenow = time();
$sysctx = get_context_instance(CONTEXT_SYSTEM);
/// Fully setup the Elluminate Moderator role.
if (!($mrole = $DB->get_record('role', array('shortname' => 'roomschedulermanager')))) {
if ($rid = create_role(get_string('roomschedulermanager', 'block_roomscheduler'), 'roomschedulemanager', get_string('roomschedulermanagerdescription', 'block_roomscheduler'))) {
$mrole = $DB->get_record('role', array('id' => $rid));
assign_capability('block/roomscheduler:manage', CAP_ALLOW, $mrole->id, $sysctx->id);
set_role_contextlevels($mrole->id, array(CONTEXT_SYSTEM));
} else {
$mrole = $DB->get_record('role', array('shortname' => 'roomschedulermanager'));
set_role_contextlevels($mrole->id, array(CONTEXT_SYSTEM));
}
}
upgrade_block_savepoint(true, 2011150301, 'roomscheduler');
}
if ($oldversion < 2011140315) {
// Define field reservable to be added to roomscheduler_rooms
$table = new xmldb_table('roomscheduler_rooms');
$field = new xmldb_field('reservable', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'active');
// Conditionally launch add field reservable
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
upgrade_block_savepoint(true, 2011140315, 'roomscheduler');
}
return true;
}
示例7: assign
/**
* Assign a new permission.
* @param int $permission One of CAP_ALLOW, CAP_PROHIBIT, CAP_PREVENT
*/
public function assign($permission)
{
foreach ($this->capability as $capability) {
assign_capability($capability, $permission, $this->roleid, $this->contextid, true);
}
accesslib_clear_all_caches_for_unit_testing();
}
示例8: assign_capability
protected function assign_capability($capability, $permission = CAP_ALLOW, $contextid = null) {
if ($contextid === null) {
$contextid = context_system::instance();
}
if (is_object($contextid)) {
$contextid = $contextid->id;
}
assign_capability($capability, $permission, $this->get_roleid($contextid), $contextid, true);
accesslib_clear_all_caches_for_unit_testing();
}
示例9: create_roles
/**
* Creates the two roles required to test default role association. The first role contains the 'view' and
* 'create' permissions for the entity and the second contains the 'view' and 'edit' capabilities for the entity.
*
* @param string $entity The name of the entity being created.
* @return array An array containg the role for the creator / editor role
*/
private function create_roles($entity)
{
$syscontext = context_system::instance();
$role1 = create_role($entity . '_creator', $entity . '_creator', $entity . '_creator');
assign_capability('local/elisprogram:' . $entity . '_view', CAP_ALLOW, $role1, $syscontext->id);
assign_capability('local/elisprogram:' . $entity . '_create', CAP_ALLOW, $role1, $syscontext->id);
$role2 = create_role($entity . '_editor', $entity . '_editor', $entity . '_editor');
assign_capability('local/elisprogram:' . $entity . '_view', CAP_ALLOW, $role2, $syscontext->id);
assign_capability('local/elisprogram:' . $entity . '_create', CAP_ALLOW, $role2, $syscontext->id);
return array($role1, $role2);
}
示例10: createRoleWithCapabilities
/**
* Cria um papel no MOODLE conforme os padrões.
*
* É sugerido que com esta função seja usado um dos papeis como Arquétipo:
* <lu>
* <li>manager
* <li>coursecreator
* <li>editingteacher
* <li>teacher
* <li>student
* </lu>
*
*
* O Array de habilidades (capabilities) deve ser formado da seguinte forma
* $caps = array(
* array('name'=>'moodle/site:sendmessage', 'perm'=CAP_PREVENT),
* array('name'=>'moodle/restore:rolldates', 'perm'=CAP_INHERIT),
* array('name'=>'moodle/site:manageblocks', 'perm'=CAP_PROHIBIT),
* array('name'=>'mod/data:viewallratings', 'perm'=CAP_ALLOW),
*
* );
* Como pode ser visto 'name'aponta para o nome da habilidade e
* 'perm' indica a permissão que será data da seguinte forma:
* <lu>
* <li>CAP_PREVENT<br/>
* Evita esta habilidade
* <li>CAP_INHERIT<br/>
* Erda esta habilidade do Arquétipo informado.
* <li>CAP_PROHIBIT<br/>
* Proibe esta Habilidade
* <li>CAP_ALLOW<br/>
* Permite ter esta habilidade.
* </lu>
*
* @param string $name
* @param string $shortname
* @param String $description
* @param string $archtype
* @param array $capabilities
*/
public static function createRoleWithCapabilities($name, $shortname, $description, $archtype, $capabilities = NULL)
{
global $DB, $CFG;
require_once "{$CFG->dirlib}/accesslib.php";
$roleid = create_role($name, $shortname, $description, $archetype = '');
if (empty($capabilities)) {
foreach ($capabilities as $cap) {
assign_capability($cap[name], $cap[perm], $roleid, 1);
}
}
}
示例11: create_role_with_caps
/**
* Create a role with capabilities and permissions.
*
* @param string|array $caps Capability names.
* @param int $perm Constant CAP_* to apply to the capabilities.
* @return int The new role ID.
*/
protected function create_role_with_caps($caps, $perm)
{
$caps = (array) $caps;
$dg = $this->getDataGenerator();
$roleid = $dg->create_role();
foreach ($caps as $cap) {
assign_capability($cap, $perm, $roleid, context_system::instance()->id, true);
}
accesslib_clear_all_caches_for_unit_testing();
return $roleid;
}
示例12: test_block_elisadmin_load_menu_children_course
/**
* Test block_elisadmin_load_menu_children_course function.
*/
public function test_block_elisadmin_load_menu_children_course()
{
global $DB, $USER;
accesslib_clear_all_caches(true);
// Create test user - ensure the returned user is NOT a site admin. if they are, our capability restrictions won't work.
$testuser = new user();
$testuser->username = 'testELIS4093';
$testuser->idnumber = 'testELIS4093';
$testuser->firstname = 'testELIS4093';
$testuser->lastname = 'testELIS4093';
$testuser->email = 'testELIS4093@example.com';
$testuser->country = 'CA';
$testuser->save();
$testmuser = $testuser->get_moodleuser();
// Create role with cap: 'local/elisprogram:class_view'.
$testrole = new stdClass();
$testrole->name = 'ELIS Class View';
$testrole->shortname = '_test_ELIS_4093';
$testrole->description = 'ELIS Class View';
$testrole->archetype = '';
$testrole->id = create_role($testrole->name, $testrole->shortname, $testrole->description, $testrole->archetype);
// Ensure our new role is assignable to ELIS class contexts.
set_role_contextlevels($testrole->id, array(CONTEXT_ELIS_CLASS));
// Ensure the role has our required capability assigned.
$sitecontext = context_system::instance();
assign_capability('local/elisprogram:class_view', CAP_ALLOW, $testrole->id, $sitecontext->id, true);
$sitecontext->mark_dirty();
// Create ELIS Course Description.
$testcrs = new course(array('name' => 'CD-ELIS-4093', 'idnumber' => 'CDELIS4093', 'syllabus' => ''));
$testcrs->save();
$testcrs->load();
// Create three(3) Class Instances for Course Descrption.
$testcls1 = new pmclass(array('courseid' => $testcrs->id, 'idnumber' => 'CI_ELIS_4093.1'));
$testcls1->save();
$testcls1->load();
$testcls2 = new pmclass(array('courseid' => $testcrs->id, 'idnumber' => 'CI_ELIS_4093.2'));
$testcls2->save();
$testcls2->load();
$testcls3 = new pmclass(array('courseid' => $testcrs->id, 'idnumber' => 'CI_ELIS_4093.3'));
$testcls3->save();
$testcls3->load();
// Assign testuser new role in one Class Instance.
$context = \local_elisprogram\context\pmclass::instance($testcls2->id);
role_assign($testrole->id, $testmuser->id, $context->id);
// Switch to testuser.
$USER = $testmuser;
$this->setUser($testmuser);
$items = block_elisadmin_load_menu_children_course($testcrs->id, 0, 0, 5, '');
$this->assertEquals(1, count($items));
$this->assertTrue($items[0]->name == 'pmclass_2');
}
示例13: xmldb_block_demostudent_upgrade
function xmldb_block_demostudent_upgrade($oldversion = 0)
{
global $CFG;
$result = true;
// Assign moodle/course:viewhiddencourses capability to demostudent role.
if ($oldversion < 2013100201) {
if (get_capability_info('moodle/course:viewhiddencourses')) {
$demostudentroleid = get_roleid_by_roleshortname('demostudent');
assign_capability('moodle/course:viewhiddencourses', CAP_ALLOW, $demostudentroleid, 1);
}
upgrade_plugin_savepoint(true, 2013100201, 'block', 'demostudent');
}
return $result;
}
示例14: assignUserCapability
/**
* Assign a capability to $USER
* The function creates a student $USER if $USER->id is empty
*
* @param string $capability capability name
* @param int $contextid
* @param int $roleid
* @return int the role id - mainly returned for creation, so calling function can reuse it
*/
public static function assignUserCapability($capability, $contextid, $roleid = null)
{
global $USER;
// Create a new student $USER if $USER doesn't exist
if (empty($USER->id)) {
$user = self::getDataGenerator()->create_user();
self::setUser($user);
}
if (empty($roleid)) {
$roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
}
assign_capability($capability, CAP_ALLOW, $roleid, $contextid);
role_assign($roleid, $USER->id, $contextid);
accesslib_clear_all_caches_for_unit_testing();
return $roleid;
}
示例15: test_load_users
/**
* Test load_users method.
*/
public function test_load_users()
{
global $DB;
$this->setAdminUser();
$this->resetAfterTest(true);
$roleteacher = $DB->get_record('role', array('shortname' => 'teacher'), '*', MUST_EXIST);
// Create a course, users and groups.
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$teacher = $this->getDataGenerator()->create_user();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $roleteacher->id);
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id);
$this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $teacher->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user1->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user2->id));
// Perform a regrade before creating the report.
grade_regrade_final_grades($course->id);
$screentest = new gradereport_singleview_screen_testable($course->id, 0, $group->id);
$groupusers = $screentest->test_load_users();
$this->assertCount(2, $groupusers);
// Now, let's suspend the enrolment of a user. Should return only one user.
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleteacher->id, 'manual', 0, 0, ENROL_USER_SUSPENDED);
$users = $screentest->test_load_users();
$this->assertCount(1, $users);
// Change the viewsuspendedusers capabilities and set the user preference to display suspended users.
assign_capability('moodle/course:viewsuspendedusers', CAP_ALLOW, $roleteacher->id, $coursecontext, true);
set_user_preference('grade_report_showonlyactiveenrol', false, $teacher);
accesslib_clear_all_caches_for_unit_testing();
$this->setUser($teacher);
$screentest = new gradereport_singleview_screen_testable($course->id, 0, $group->id);
$users = $screentest->test_load_users();
$this->assertCount(2, $users);
// Change the capability again, now the user can't see the suspended enrolments.
assign_capability('moodle/course:viewsuspendedusers', CAP_PROHIBIT, $roleteacher->id, $coursecontext, true);
set_user_preference('grade_report_showonlyactiveenrol', false, $teacher);
accesslib_clear_all_caches_for_unit_testing();
$users = $screentest->test_load_users();
$this->assertCount(1, $users);
// Now, activate the user enrolment again. We shall get 2 users now.
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleteacher->id, 'manual', 0, 0, ENROL_USER_ACTIVE);
$users = $screentest->test_load_users();
$this->assertCount(2, $users);
}