Commit e570cce6 authored by Robert Lyon's avatar Robert Lyon
Browse files

Bug 1852644: Allowing admin to set external app owner to be another user



behatnotneeded

Change-Id: I6224fbc3f449d0e205bf99f4b0f46fa2e76323bf
Signed-off-by: default avatarRobert Lyon <robertl@catalyst.net.nz>
parent 2d14b440
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -278,6 +278,7 @@ $string['authorise'] = 'Authorise application access';
$string['oauth_access'] = 'This application will have access to your users\' details and resources';
$string['oauth_instructions'] = 'If you wish to grant access to this application, then click "Authorise application access". If you do not want to grant access, press "Cancel".';
$string['setauthinstancefailed'] = 'Setting the "Web services" authentication for institution "%s" failed. Please try adding it via Administration -> Institutions -> Settings page.';
$string['needtosetowner'] = 'Need to set an owner for this service';

// running webservices messages
$string['accesstofunctionnotallowed'] = 'Access to the function %s() is not allowed. Please check if a service containing the function is enabled. In the service settings: If the service is restricted, check that the user is listed. Still in the service settings check for IP restriction or if the service requires a capability.';
+19 −0
Original line number Diff line number Diff line
@@ -1338,6 +1338,25 @@ function display_username($user=null) {
    }
}

/**
 * Translate the supplied user id to it's display name
 *
 * @param array $ids  User id number
 * @return object $results containing id and text values
 */
function translate_user_ids_to_names($ids) {
    // for an empty list, the element '' is transmitted
    $ids = array_diff($ids, array(''));
    $results = array();
    foreach ($ids as $id) {
        $deleted = get_field('usr', 'deleted', 'id', $id);
        if (($deleted === '0') && is_numeric($id)) {
            $results[] = (object) array('id' => $id, 'text' => display_name($id));
        }
    }
    return $results;
}

/**
 * helper function to default to currently
 * logged in user if there isn't an id specified
+6 −9
Original line number Diff line number Diff line
@@ -270,16 +270,13 @@ function sendmessage_validate(Pieform $form, $values) {
    }
}

function translate_ids_to_names(array $ids) {
function translate_ids_to_names(array $unfilteredids) {
    global $USER;
    // for an empty list, the element '' is transmitted
    $ids = array_diff($ids, array(''));
    $results = array();
    foreach ($ids as $id) {
        $deleted = get_field('usr', 'deleted', 'id', $id);
        if (($deleted === '0') && is_numeric($id) && can_send_message($USER->to_stdclass(), $id)) {
            $results[] = (object) array('id' => $id, 'text' => hsc(display_name($id, null, true)));
    $ids = array();
    foreach ($unfilteredids as $id) {
        if (is_numeric($id) && can_send_message($USER->to_stdclass(), $id)) {
            $ids[] = $id;
        }
    }
    return $results;
    return translate_user_ids_to_names($ids);
}
+52 −5
Original line number Diff line number Diff line
@@ -134,6 +134,13 @@ function webservices_server_submit(Pieform $form, $values) {
    redirect('/webservice/admin/oauthv1sregister.php');
}

function webservice_oauth_server_validate(Pieform $form, $values) {
    $owner = array_diff($values['user'], array(''));
    if (empty($owner)) {
        $form->set_error('user', get_string('needtosetowner', 'auth.webservice'));
    }
}

function webservice_oauth_server_submit(Pieform $form, $values) {
    global $USER, $SESSION;

@@ -153,6 +160,13 @@ function webservice_oauth_server_submit(Pieform $form, $values) {
                    'consumer_secret'   => $dbserver->consumer_secret,
                    'id'                => $values['id'],
       );
        if ($USER->get('admin') && isset($values['user'])) {
            $useridchange = !empty($values['user'][0]) ? $values['user'][0] : false;
            if ($useridchange) {
                $app['userid'] = $useridchange;
            }
        }

        $key = $store->updateConsumer($app, $USER->get('id'), true);
        $c = (object) $store->getConsumer($key, $USER->get('id'), true);
        if (empty($c)) {
@@ -182,10 +196,12 @@ function webservice_main_submit(Pieform $form, $values) {
}

function webservice_server_edit_form($dbserver, $sopts, $iopts, $disabled = array()) {
    global $USER;

    $server_details =
        array(
            'name'             => 'webservice_oauth_server',
            'validatecallback' => 'webservice_oauth_server_validate',
            'successcallback'  => 'webservice_oauth_server_submit',
            'jsform'           => false,
            'elements'   => array(
@@ -220,11 +236,32 @@ function webservice_server_edit_form($dbserver, $sopts, $iopts, $disabled = arra
        'type'         => 'text',
    );

    if ($USER->get('admin')) {
        // we can set another user as service owner
        $server_details['elements']['user'] = array(
            'title'        => get_string('serviceuser', 'auth.webservice'),
            'defaultvalue'        => array($dbserver->userid),
            'type' => 'autocomplete',
            'ajaxurl' => get_config('wwwroot') . 'webservice/admin/users.json.php',
            'initfunction' => 'translate_ids_to_names',
            'multiple' => true,
            'ajaxextraparams' => array(),
            'extraparams' => array(
                'maximumSelectionLength' => 1
            ),
            'width' => '280px',
            'rules' => array(
                'required' => true,
            ),
        );
    }
    else {
        $server_details['elements']['user'] = array(
            'title'        => get_string('serviceuser', 'auth.webservice'),
            'value'        => get_field('usr', 'username', 'id', $dbserver->userid),
            'type'         => 'html',
        );
    }

    $server_details['elements']['application_uri'] = array(
        'title'        => get_string('application_uri', 'auth.webservice'),
@@ -687,3 +724,13 @@ function get_module_from_serverid($serverid) {
    }
    return array('auth', 'webservice');
}

/**
 * Translate the supplied user id to it's display name
 *
 * @param array $ids  User id number
 * @return object $results containing id and text values
 */
function translate_ids_to_names(array $ids) {
    return translate_user_ids_to_names($ids);
}
+1 −11
Original line number Diff line number Diff line
@@ -191,17 +191,7 @@ function logsearchform_submit(Pieform $form, $values) {
 * @return object $results containing id and text values
 */
function translate_ids_to_names(array $ids) {

    // for an empty list, the element '' is transmitted
    $ids = array_diff($ids, array(''));
    $results = array();
    foreach ($ids as $id) {
        $deleted = get_field('usr', 'deleted', 'id', $id);
        if (($deleted === '0') && is_numeric($id)) {
            $results[] = (object) array('id' => $id, 'text' => display_name($id));
        }
    }
    return $results;
    return translate_user_ids_to_names($ids);
}

/**
Loading