Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-27321

user_can_assign($context, $targetroleid) seems incomplete

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Inactive
    • Icon: Minor Minor
    • None
    • 2.0.2
    • Libraries
    • None
    • MOODLE_20_STABLE

       * Checks if a user can assign users to a particular role in this context
       *
       * @param object $context
       * @param int $targetroleid - the id of the role you want to assign users to
       * @return boolean
       */
      function user_can_assign($context, $targetroleid) {
          global $DB;
       
          // first check if user has override capability
          // if not return false;
          if (!has_capability('moodle/role:assign', $context)) {
              return false;
          }
          // pull out all active roles of this user from this context(or above)
          if ($userroles = get_user_roles($context)) {
              foreach ($userroles as $userrole) {
                  // if any in the role_allow_override table, then it's ok
                  if ($DB->get_record('role_allow_assign', array('roleid'=>$userrole->roleid, 'allowassign'=>$targetroleid))) {
                      return true;
                  }
              }
          }
       
          return false;
      }

      This function will return false for admin when the if condition fails. From reading the phpdoc and the function name I understand this function returns true if a user can assign a role to a context. It's why it seemed to me as admin should always be able to assign.

      Either this function is confusing developers (name/phpdoc), either it needs a fix.

            Unassigned Unassigned
            jerome Jérôme Mouneyrac
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.