示例#1
0
 /**
  * Processes and stores configuration data for this authentication plugin.
  */
 public function process_config($config)
 {
     // Set to defaults if undefined.
     $providers = provider_list();
     foreach ($providers as $providername) {
         $clientidname = $providername . 'clientid';
         $clientsecretname = $providername . 'clientsecret';
         // Set to defaults if undefined.
         if (!isset($config->{$clientidname})) {
             $config->{$clientidname} = '';
         }
         if (!isset($config->{$clientsecretname})) {
             $config->{$clientsecretname} = '';
         }
         // Save settings.
         set_config($clientidname, $config->{$clientidname}, 'auth/googleoauth2');
         set_config($clientsecretname, $config->{$clientsecretname}, 'auth/googleoauth2');
     }
     if (!isset($config->googleuserprefix)) {
         $config->googleuserprefix = 'social_user_';
     }
     if (!isset($config->oauth2displaybuttons)) {
         $config->oauth2displaybuttons = 0;
     }
     set_config('googleipinfodbkey', $config->googleipinfodbkey, 'auth/googleoauth2');
     set_config('googleuserprefix', core_text::strtolower($config->googleuserprefix), 'auth/googleoauth2');
     set_config('oauth2displaybuttons', $config->oauth2displaybuttons, 'auth/googleoauth2');
     return true;
 }
/**
 * The very ugly code to render the html buttons.
 * TODO remove ugly html like center-tag and inline styles, implement a moodle renderer
 * @return string: returns the html for buttons and some JavaScript 
 */
function auth_googleoauth2_render_buttons()
{
    global $CFG;
    $html = '';
    if (!is_enabled_auth('googleoauth2')) {
        return $html;
    }
    // Get previous auth provider.
    $allauthproviders = optional_param('allauthproviders', false, PARAM_BOOL);
    $cookiename = 'MOODLEGOOGLEOAUTH2_' . $CFG->sessioncookie;
    $authprovider = '';
    if (!empty($_COOKIE[$cookiename])) {
        $authprovider = $_COOKIE[$cookiename];
    }
    $html .= "<div>";
    $providerscount = 0;
    // TODO get the list from the provider folder instead to hard code it here.
    $providers = provider_list();
    foreach ($providers as $providername) {
        require_once $CFG->dirroot . '/auth/googleoauth2/classes/provider/' . $providername . '.php';
        // Load the provider plugin.
        $providerclassname = 'provideroauth2' . $providername;
        $provider = new $providerclassname();
        $authurl = $provider->getAuthorizationUrl();
        set_state_token($providername, $provider->state);
        // Check if we should display the button.
        $providerisenabled = $provider->isenabled();
        $providerscount = $providerisenabled ? $providerscount + 1 : $providerscount;
        $displayprovider = (empty($authprovider) || $authprovider == $providername || $allauthproviders) && $providerisenabled;
        $providerdisplaystyle = $displayprovider ? 'display:inline-block;padding:10px;' : 'display:none;';
        // The button html code.
        $html .= $provider->html_button($authurl, $providerdisplaystyle);
    }
    if (!$allauthproviders and !empty($authprovider) and $providerscount > 1) {
        $html .= '<br /><br />
           <div class="moreproviderlink">
                <a href="' . $CFG->wwwroot . (!empty($CFG->alternateloginurl) ? $CFG->alternateloginurl : '/login/index.php') . '?allauthproviders=true' . '" onclick="changecss(\\\'singinprovider\\\',\\\'display\\\',\\\'inline-block\\\');">
                    ' . get_string('moreproviderlink', 'auth_googleoauth2') . '
                </a>
            </div>';
    }
    return $html;
}