Exemple #1
0
/**
 * Get the UUID of the user personal storage area.
 *
 * @param int  $uid       The Moodle user ID.
 * @param bool $nomigrate Set to True to not force user data migration.
 * @return string|bool The user store UUID value or, False on error.
 */
    function get_user_store($uid, $nomigrate = false) {
        global $DB;

        if (ELIS_FILES_DEBUG_TRACE) mtrace('get_user_store(' . $uid . ')');

        // Sanity checking.
        if (empty($uid) || !isset($this->uuuid)) {
            return false;
        }

        // Check if the user already has a personal storage directory.
        if ($nomigrate) {
            if (($uuid = $this->has_old_user_store($uid)) !== false) {
                return $uuid;
            }

            // Create a new personal storage directory for this user.
            $user = $DB->get_record('user', array('id'=> $uid));

            if ($node = elis_files_create_dir($user->id, $this->uuuid, fullname($user) . ' (' . $user->email . ')')) {
                return $node->uuid;
            }
        } else {
            if (!($username = $DB->get_field('user', 'username', array('id'=> $uid)))) {
                //error_log("ELIS_files::get_user_store({$uid}) => NO username => false!");
                return false;
            }

            $uuid = false;

            if (($uuid = $this->has_old_user_store($uid)) !== false) {
                $fixed_username = $this->fix_username($username);
                if (!$this->migrate_user($fixed_username)) {
                    //error_log("ELIS_files::get_user_store({$uid}) => NO migrate_user => false!");
                    return false;
                }
            }

            if (empty($this->uuuid)) {
                $this->uuuid = $this->elis_files_userdir($username);
                //error_log("ELIS_files::get_user_store({$uid}) => WAS empty => {$this->uuuid}");
            }
            //error_log("ELIS_files::get_user_store({$uid}) => {$this->uuuid}");
            return $this->uuuid;
        }

        return false;
    }
Exemple #2
0
    /**
     * Validate that the "permission_check" method method works hierarchically and respects the appropriate
     * capabilities assigned at the system level for course files
     * @uses $USER, $DB, $SESSION, $CFG
     * @param string $depth The folder depth to build for hierachy testing
     * @dataProvider hierarchy_provider
     */
    public function test_permission_check_respects_hierarchy_for_course_file($depth) {
        if (!class_exists('elispm')) {
            $this->markTestSkipped('elis_program needed for test');
            return false;
        }

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

        global $CFG, $DB, $SESSION, $USER;
        require_once(elispm::lib('data/userset.class.php'));

        // Make sure the test user is not mistaken for a site admin or guest
        set_config('siteadmins', '');
        set_config('siteguest', '');
        // Use a fixed capability
        $capability = 'repository/elisfiles:createcoursecontent';

        // unset the repo to avoid the is_siteadmin problem in the factory class
        unset($SESSION->repo);

        $courseid = 99;

        // 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());
        }

        // Explicitly set the file transfer method to web services
        set_config('file_transfer_method', ELIS_FILES_XFER_WS, 'elisfiles');

        // use test course id
        $currentuuid = $repo->elis_files->get_course_store($courseid);
        $this->createduuids[] = $currentuuid;

        $this->setUser(100);
        $roleid = $this->assign_role_capability($capability);

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

        // Now we need to create folders up to the depth from the data provider and upload a file
        for ($i = 1; $i <= $depth; ++$i) {
            // create a sub-folder
            $currentnode = elis_files_create_dir(TEST_PREFIX.'course_'.$i, $currentuuid);
            $this->assertTrue($currentnode && !empty($currentnode->uuid));
            $currentuuid = $currentnode->uuid;
            $this->createduuids[] = $currentuuid;
        }

        $filename = $CFG->dirroot.'/repository/elisfiles/tests/'.TEST_PREFIX.'file.txt';
        // upload a file to this folder
        $response = $this->call_upload_file($repo, '', $filename, $currentuuid);
        // FTP was failing, but this is a good check to keep in
        $this->assertFalse(!$response);

        // Perform the appropriate permission check
        $haspermission = $repo->elis_files->permission_check($response->uuid, 0, true, $repo);

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