Beispiel #1
0
/**
 * Process a SimpleXML object from the Alfresco web script XML data to make an array
 * structure usable in Moodle.
 *
 * @param SimpleXMLElement $sxml A folder-level SimpleXMLElement object.
 * @param boolean $check_permissions Set to true to require "create" permissions
 *                                   on returned folders
 * @param string $path_prefix A prefix to use for the path, in case the parent
 *                            node is not accessible
 * @param string $parent_uuid The uuid of the parent node relative to the top of
 *                            the XML structure
 * @param int $courseid The id of the course whose area we are in, or the site id
 *                      if not in a particular course's area
 * @param boolean $shared Set to true if we are in the shared space
 * @param int $oid The id of the user set whose area we are in, if applicable
 * @param int $uid The id of the user whose area we are in, if applicable
 * @param object $repo The repository object, or NULL if we need to obtain it
 * @return array An array of folder information.
 */
function elis_files_process_folder_structure($sxml, $check_permissions = false, $path_prefix = '', $parent_uuid = '',
                                             $courseid = SITEID, $shared = false, $oid = 0, $uid = 0, $repo = NULL) {
    global $DB, $USER;

    $return = array();

    // Data needed by the repository object
    $params = array(
        'ajax' => false,
        'name' => '',
        'type' =>'elisfiles'
    );

    // Create the repository object
    if ($repo == NULL) {
        $repo = new repository_elisfiles('elisfiles', SYSCONTEXTID, $params);
    }

    if (!empty($sxml->folder)) {
        foreach ($sxml->folder as $folder) {
            // Track values specifically for this node, i.e. not for other folders
            // at the same level
            $node_courseid = $courseid;
            $node_shared = $shared;
            $node_oid = $oid;
            $node_uid = $uid;

            if ($check_permissions) {
                // The course id if we are at a specific course node, or the site id
                // otherwise
                if ($node_courseid == SITEID && $parent_uuid == $repo->elis_files->cuuid) {
                    $params = array(
                        'uuid' => "$folder->uuid"
                    );
                    $tempcourseid = $DB->get_field('repository_elisfiles_course', 'courseid', $params);
                    if ($tempcourseid) {
                        $node_courseid = $tempcourseid;
                    }
                }

                // Flag indicating whether the node is the main shared folder
                if (!$node_shared) {
                    $node_shared = $folder->uuid == $repo->elis_files->suuid;
                }

                // The organization id if we are at a specific organization node, or
                // an empty value otherwise
                if ($node_oid == 0 && $parent_uuid == $repo->elis_files->ouuid) {
                    $params = array(
                        'uuid' => "$folder->uuid"
                    );
                    $node_oid = $DB->get_field('repository_elisfiles_userset', 'usersetid', $params);
                }

                // The user id if we are at the node for the curent user, or an empty
                // value otherwise
                if ($node_uid == 0) {
                    $node_uid = $folder->uuid == $repo->elis_files->uuuid ? $USER->id : 0;
                }

                // Determine whether the current user has the edit permission on this folder
                $has_permissions = $repo->check_editing_permissions($node_courseid, $node_shared, $node_oid,
                                                                    $folder->uuid, $node_uid);
            } else {
                // Allow access
                $has_permissions = true;
            }

            $cat = array(
                'uuid'     => "$folder->uuid",
                // Append the path prefix
                'name'     => "{$path_prefix}{$folder->name}",
                'children' => array()
            );

            // Calculate the list of children
            $children = array();
            if (!empty($folder->folders)) {
                // Calculate the start of the child path
                $child_path_prefix = $path_prefix;
                if (!$has_permissions) {
                    // Will not be inhereted from this node, so add the prefix
                    $child_path_prefix .= $folder->name.'/';
                } else {
                    // We just want the last piece
                    $child_path_prefix = '';
                }

                // Recurse, passing along information about the area we are in
                $new_parent_uuid = "$folder->uuid";
                $children = elis_files_process_folder_structure($folder->folders, $check_permissions, $child_path_prefix,
                                                                $new_parent_uuid, $node_courseid, $node_shared, $node_oid,
                                                                $node_uid, $repo);
            }

            if ($has_permissions) {
                // Current node will be included, so append the children
                $cat['children'] = $children;
                $return[] = $cat;
            } else {
                // Current node will not be included, so include children at this level
                foreach ($children as $child) {
                    $return[] = $child;
                }
            }
        }
    }

    return $return;
}
Beispiel #2
0
    /**
     * Validate that the "check_editing_permissions" method respects site files
     * capabilities assigned at the system level
     * @uses $USER, $DB
     * @param boolean $course True if checking permissions on course files
     * @param boolean $shared True if checking permissions on shared files
     * @param boolean $userset True if checking permissions on userset files
     * @param boolean $own True if checking permissions on own (personal) files
     * @dataProvider node_flag_provider
     */
    public function test_check_editing_permissions_respects_site_files($course, $shared, $userset, $own) {
        if (!class_exists('elispm')) {
            $this->markTestSkipped('elis_program needed for test');
            return false;
        }

        $this->resetAfterTest(true);
        $this->setup_test_data_xml();

        global $USER, $DB;

        // Make sure the test user is not mistaken for a site admin or guest
        set_config('siteadmins', '');
        set_config('siteguest', '');

        // Setup
        $this->setUser(100);
        $roleid = $this->assign_role_capability('repository/elisfiles:createsitecontent');

        // Assign the test role to the test user
        $context = context_system::instance();
        role_assign($roleid, $USER->id, $context->id);

        // Determine the correct set of parameters
        $id = SITEID;
        // Note: shared left as-is
        $oid = false;
        $userid = 0;
        // Note: UUID is not currently used in the method we are testing
        $uuid = false;

        // RL: ELIS files: Alfresco
        $data = null;
        $listing = null;
        $options = array(
            'ajax' => false,
            'name' => 'elis files phpunit test',
            'type' => 'elisfiles'
        );

        try {
            $repo = new repository_elisfiles('elisfiles', context_system::instance(), $options);
        } catch (Exception $e) {
            $this->markTestSkipped('Exception when creating repository_elisfiles object: '.$e->getMessage());
        }

        if ($course) {
            $id = 99;

            $mapping = new stdClass;
            $mapping->courseid = $id;
            $mapping->uuid = 'testuuid';
            $DB->insert_record('repository_elisfiles_course', $mapping);
        }

        if ($userset) {
            require_once(elispm::lib('data/userset.class.php'));

            $userset = new userset(array(
                'name' => 'testusersetname'
            ));
            $userset->save();

            $mapping = new stdClass;
            $oid = $mapping->usersetid = $userset->id;
            $uuid = $mapping->uuid = 'testuuid';
            $DB->insert_record('repository_elisfiles_userset', $mapping);
        }

        if ($own) {
            $userid = $USER->id;
        }

        // Run the permission check method
        $haspermission = $repo->check_editing_permissions($id, $shared, $oid, $uuid, $userid);

        // Validation
        $this->assertTrue($haspermission);
    }