Search code examples
phppermissionsmoodlecapability

Local Plugin permission issue not apply on course manager specifically - Moodle


I am new in Moodle and i have a task to create local plugin with course manager role. In my system i have bulk of users in specific course and they are categories in different roles. Some of are bind with students and rest of are Managers. For my case i have received request from the client to make a local plugin which generate multiple reports against student records. I have successfully made the plugin but the condition is this plugin only can access those one who has enrolled in course as a Manager. I m trying with following code which i share you in below but no success. right now only admin can access local plugin rest of are received error messages from the moodle state.

["Sorry, but you do not currently have permissions to do that Project view "]

No idea how it will be resolved.

Please advise.

local/project/db/access.php

defined('MOODLE_INTERNAL') || die();

$capabilities = array(

    'local/project:view' => array(
        'riskbitmask' => RISK_PERSONAL,
        'captype' => 'read',
        'contextlevel' => CONTEXT_SYSTEM,
        'archetypes' => array(
            'manager' => CAP_ALLOW
        ),
    'local/project:manage' => array(
        'captype' => 'write',
        'contextlevel' => CONTEXT_SYSTEM,
        'archetypes' => array(
            'manager' => CAP_ALLOW
        )
    )
    )
);

local/project/header.php

require(dirname(__FILE__).'/../../config.php');
global $DB;
//Get the system context
$url = new moodle_url('/local/project/index.php');

require_login();
require_capability('local/project:view', context_system::instance());

Solution

  • I have successfully achieved the target against moodle permission. I have used has_capability method with course context and check authenticity with require_capability. Following procedure only work with admin and manager other would not access until when they get full rights from the site administration.

    local/project/db/access.php

    defined('MOODLE_INTERNAL') || die();
    
    $capabilities = array(
    
        'local/project:view' => array(
            'riskbitmask' => RISK_SPAM,
            'captype' => 'write',
            'contextlevel' => CONTEXT_COURSE,
            'archetypes' => array(
                'manager' => CAP_ALLOW,
            ),
        )
    );
    

    local/project/header.php

    require(dirname(__FILE__).'/../../config.php');
    require_login();
    
    //Get the system context
    $context = context_course::instance($course_id);
    
    if (!has_capability('local/project:view', $context)) {
        require_capability('local/project:view', $context);
    }