Example #1
0
/**
 * Utility function for getting alfresco ticket.
 *
 * @uses $CFG
 * @param string $op       Option for refreshing ticket or not.
 * @param string $username The username to use for user authentication.
 * @return string|bool The ticket value or, False on error.
 */
function elis_files_utils_get_ticket($op = 'norefresh', $username = '') {
    global $CFG;

    if (ELIS_FILES_DEBUG_TRACE) print_object('elis_files_utils_get_ticket(' . $op . ', ' . $username . ')');

    $alf_username = !empty($_SESSION['elis_files_username']) ? $_SESSION['elis_files_username'] : NULL;
    $alf_ticket   = !empty($_SESSION['elis_files_ticket']) ? $_SESSION['elis_files_ticket'] : NULL;

    if (!empty($username)) {
        $username = elis_files_transform_username($username);
        $user = $username;
        $pass = '******';
    } else {
        if (!isset(elis::$config->elisfiles->server_username) || !isset(elis::$config->elisfiles->server_password)) {
            return false;
        }

        $user = elis::$config->elisfiles->server_username;
        $pass = elis::$config->elisfiles->server_password;
    }

    // We must include the tenant portion of the username here if it is not already included.
    if ($user != elis::$config->elisfiles->server_username) {
        if (($tenantpos = strpos(elis::$config->elisfiles->server_username, '@')) > 0) {
            $tenantname = substr(elis::$config->elisfiles->server_username, $tenantpos);

            if (strpos($user, $tenantname) === false) {
                $user .= $tenantname;
            }
        }
    }

    // Make sure that we refresh the ticket if we're authenticating with a different user than what
    // the current ticket was generated for.
    if (empty($alf_username) || (!empty($alf_username) && $alf_username !== $user)) {
        $op = 'refresh';
    }

    if ($alf_ticket == NULL || $op == 'refresh') {
        // Authenticate and store the ticket
        $url = elis_files_utils_get_url('/api/login?u=' . urlencode($user) . '&pw=' . urlencode($pass));

        // Prepare curl session
        $session = curl_init($url);

        if (ELIS_FILES_DEBUG_TRACE) {
            curl_setopt($session, CURLOPT_VERBOSE, true);
        }

        // Don't return HTTP headers. Do return the contents of the call
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($session, CURLOPT_USERPWD, "$user:$pass");

        elis_files_set_curl_timeouts($session);

        // Make the call
        $return_data = curl_exec($session);

        // Get return http status code
        $httpcode = curl_getinfo($session, CURLINFO_HTTP_CODE);

        // Close HTTP session
        curl_close($session);

        if ($httpcode == 200 || $httpcode == 201) {
            $sxml = RLsimpleXMLelement($return_data);
            if (empty($sxml)) {
                return false;
            }
            $alf_ticket = current($sxml);
        } else {
            return false;

            debugging(get_string('errorreceivedfromendpoint', 'repository_elisfiles') .
                      (!empty($response->code) ? $response->code . ' ' : ' ') .
                      $response->error, DEBUG_DEVELOPER);
        }

        if ($alf_ticket == '') {
            debug(get_string('unabletoauthenticatewithendpoint', 'repository_elisfiles'), DEBUG_DEVELOPER);
        }

        $_SESSION['elis_files_ticket']   = $alf_ticket;
        $_SESSION['elis_files_username'] = $user;
    }

    return !empty($alf_ticket) ? $alf_ticket : false;
}
Example #2
0
    /**
     * Test broken method: get_parent_path_from_tree()
     */
    public function test_get_parent_path_from_tree() {
        global $USER;

        $this->resetAfterTest(true);

        $dataset = $this->get_parent_data_provider();
        $i = 0;

        foreach ($dataset as $data) {
            $childuuid = $data[0];
            $parentname = $data[1];

            // This condition was added to test if the uuid was false, assert for a fals value and then report on the error
            // The idea is to mimic a data provider method where by the test will continue if there is one false assertion
            if (empty($childuuid)) {
                $this->assertFalse($childuuid, 'Data set#'.$i.': '.$data[2].' in parent '.$data[1].' is false ');
                $i++;
                continue;
            }

            $foldertree = elis_files_folder_structure();
            $resultpath = array();
            $this->repo->get_parent_path_from_tree($childuuid, $foldertree, $resultpath, 0, 0, false, 0);

            if ($parentname != 'Company Home') {
                $this->assertTrue(!empty($resultpath), 'Data set#'.$i.' childuuid = '.$childuuid);
                if ($parentname == USERS_HOME) {
                    $parentname = elis_files_transform_username($USER->username);
                }
                $this->assertEquals($parentname, $resultpath[count($resultpath) -1]['name'], 'Data set#'.$i);
            } else {
                $this->assertTrue(empty($resultpath), 'Data set#'.$i);
            }
            $i++;
        }
    }
Example #3
0
/**
 * Update a Moodle user's Alfresco account with a new password value.
 *
 * @param $CFG
 * @param object $user     The Moodle DB record object or username.
 * @param string $password The new password for the Alfresco account.
 * @return bool True on success, False otherwise.
 */
    function update_user_password($user, $password) {
        global $CFG;

        if (ELIS_FILES_DEBUG_TRACE) mtrace('update_user_password(' . $user->username . ', ' . $password . ')');

        $username = elis_files_transform_username($user->username);

        // We need to create a new account now.
        $userdata = array(
            'username'     => $username,
            'password'     => $password,
            'firstname'    => $user->firstname,
            'lastname'     => $user->lastname,
            'email'        => $user->email,
            'organization' => $user->institution
        );

        if (!empty($this->config->user_quota)) {
            $userdata['quota'] = $this->config->user_quota;
        }

        $response = elis_files_send('/moodle/createuser', $userdata, 'POST');

        try {
            $sxml = new SimpleXMLElement($response);
        } catch (Exception $e) {
            debugging(get_string('badxmlreturn', 'repository_elisfiles') . "\n\n$response", DEBUG_DEVELOPER);
            return false;
        }

        // Verify the correct return results.
        return (!empty($sxml->username) && !empty($sxml->firstname) && !empty($sxml->lastname) && !empty($sxml->email));
    }