Ignore:
Timestamp:
12/07/08 01:22:46 (3 years ago)
Author:
WanWizard
Message:

updated the authentication system to allow new authentication methods to be 'plugged in'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/administration/settings_security.php

    r2033 r2095  
    2929$variables['this_module'] = FUSION_SELF; 
    3030 
     31if (!isset($action)) $action = ""; 
     32 
    3133// check for the proper admin access rights 
    3234if (!checkrights("S4") || !defined("iAUTH") || $aid != iAUTH) fallback(BASEDIR."index.php"); 
     
    4446    } 
    4547    if ($variables['errormessage'] == "") { 
    46         // authentication method check 
    47         $auth_method = $_POST['auth_method']{0}; 
    48         $auth_local = (isset($_POST['auth_method']{1}) && $_POST['auth_method']{1} == "+") ? "1" : "0"; 
    49         switch ($auth_method) { 
    50             case "0":   // Local only 
    51                 $result = dbquery("UPDATE ".$db_prefix."configuration SET cfg_value = 'local' WHERE cfg_name = 'auth_type'"); 
    52                 break; 
    53             case "1":   // LDAP 
    54                 if ($auth_local) { 
    55                     $result = dbquery("UPDATE ".$db_prefix."configuration SET cfg_value = 'ldap,local' WHERE cfg_name = 'auth_type'"); 
    56                 } else { 
    57                     $result = dbquery("UPDATE ".$db_prefix."configuration SET cfg_value = 'ldap' WHERE cfg_name = 'auth_type'"); 
    58                 } 
    59                 break; 
    60             case "2":   // AD 
    61                 if ($auth_local) { 
    62                     $result = dbquery("UPDATE ".$db_prefix."configuration SET cfg_value = 'ad,local' WHERE cfg_name = 'auth_type'"); 
    63                 } else { 
    64                     $result = dbquery("UPDATE ".$db_prefix."configuration SET cfg_value = 'ad' WHERE cfg_name = 'auth_type'"); 
    65                 } 
    66                 break; 
    67             case "3":   // OpenID 
    68                 if ($auth_local) { 
    69                     $result = dbquery("UPDATE ".$db_prefix."configuration SET cfg_value = 'local,openid' WHERE cfg_name = 'auth_type'"); 
    70                 } else { 
    71                     $result = dbquery("UPDATE ".$db_prefix."configuration SET cfg_value = 'openid' WHERE cfg_name = 'auth_type'"); 
    72                 } 
    73                 break; 
    74             default: 
    75                 $variables['errormessage'] = "Invalid authentication method. This may never happen!"; 
    76         } 
    7748        if ($variables['errormessage'] == "") { 
    7849            $result = dbquery("UPDATE ".$db_prefix."configuration SET cfg_value = '".(isNum($_POST['enable_registration']) ? $_POST['enable_registration'] : "1")."' WHERE cfg_name = 'enable_registration'"); 
     
    11485} 
    11586 
    116 // check if the PHP installation supports the OpenID class 
    117 $variables['has_curl'] = function_exists('curl_exec'); 
     87// load the defined authentication methods 
     88$methods = unserialize($settings2['authentication_methods']); 
     89$selected = explode(",", $settings2['authentication_selected']); 
     90foreach ($methods as $name => $method) { 
     91    $methods[$name]['status'] = 'old'; 
     92} 
    11893 
    119 // determine the auth_method defined 
    120 $auth_methods = explode(",",$settings2['auth_type']); 
    121 $auth_method = 0; 
    122 $auth_local = false; 
    123 foreach($auth_methods as $this_method) { 
    124     switch($this_method) { 
    125         case "ldap": 
    126             $auth_method = 1; 
    127             break; 
    128         case "ad": 
    129             $auth_method = 2; 
    130             break; 
    131         case "openid": 
    132             // OpenID requires CURL to be installed 
    133             if ($variables['has_curl']) { 
    134                 $auth_method = 3; 
    135             } 
    136         case "local": 
    137             $auth_local = true; 
    138             break; 
    139         default: 
    140             $auth_method = 0; 
     94// status update request? 
     95if ($action == "setstatus") { 
     96    if ($status == 1 && array_search($authmethod,$selected) == false) { 
     97        // add the method to the selected array and update the configuration 
     98        $selected[] = $authmethod; 
     99    } elseif ($status == 0 && array_search($authmethod,$selected) !== false) { 
     100        // remove the method from the selected array and update the configuration 
     101        unset($selected[array_search($authmethod,$selected)]); 
     102    } 
     103    // write the update back 
     104    $settings2['authentication_selected'] = ""; 
     105    foreach($selected as $sel) { 
     106        $settings2['authentication_selected'] .= ($settings2['authentication_selected'] == "" ? "" : ",").$sel; 
     107    } 
     108    $result = dbquery("UPDATE ".$db_prefix."configuration SET cfg_value = '".$settings2['authentication_selected']."' WHERE cfg_name = 'authentication_selected'"); 
     109} 
     110 
     111// move up requested? 
     112if ($action == "up") { 
     113    // swap the selected method with the previous in the list 
     114    $sel = $selected[$method_id-1]; 
     115    $selected[$method_id-1] = $selected[$method_id]; 
     116    $selected[$method_id] = $sel; 
     117    // write the update back 
     118    $settings2['authentication_selected'] = ""; 
     119    foreach($selected as $sel) { 
     120        $settings2['authentication_selected'] .= ($settings2['authentication_selected'] == "" ? "" : ",").$sel; 
     121    } 
     122    $result = dbquery("UPDATE ".$db_prefix."configuration SET cfg_value = '".$settings2['authentication_selected']."' WHERE cfg_name = 'authentication_selected'"); 
     123} 
     124 
     125// move down requested? 
     126if ($action == "down") { 
     127    // swap the selected method with the previous in the list 
     128    $sel = $selected[$method_id+1]; 
     129    $selected[$method_id+1] = $selected[$method_id]; 
     130    $selected[$method_id] = $sel; 
     131    // write the update back 
     132    $settings2['authentication_selected'] = ""; 
     133    foreach($selected as $sel) { 
     134        $settings2['authentication_selected'] .= ($settings2['authentication_selected'] == "" ? "" : ",").$sel; 
     135    } 
     136    $result = dbquery("UPDATE ".$db_prefix."configuration SET cfg_value = '".$settings2['authentication_selected']."' WHERE cfg_name = 'authentication_selected'"); 
     137} 
     138 
     139// check if new authentication methods have been installed 
     140$files = makefilelist(PATH_INCLUDES."authentication", ".|..", $sort=true, $type="files", $hidden=false); 
     141foreach($files as $file) { 
     142    if (substr($file,0,5) == "auth_" && strrchr($file,".") == ".php") { 
     143        $class = substr($file, 0, strrpos($file, ".")); 
     144        $method = substr($class, strrpos($file, "_")+1); 
     145        if (!isset($methods[$method])) { 
     146            $methods[$method] = array('class' => $class, 'status' => "new"); 
     147        } else { 
     148            $methods[$method]['status'] = "found"; 
     149        } 
    141150    } 
    142151} 
    143152 
    144 // check if a local fallback is defined 
    145 $variables['auth_method'] = $auth_method . ($auth_local ? "+" : " "); 
     153// delete old ones, add the others to the sortlist, and update the config 
     154$sortlist = array(); 
     155foreach ($methods as $name => $method) { 
     156    if ($method['status'] == "old") { 
     157        unset($methods[$name]); 
     158    } else { 
     159        // add to the sortlist 
     160        if (in_array($name, $selected)) { 
     161            $sortlist[] = substr('000'.array_search($name, $selected),-3).".".$name; 
     162        } else { 
     163            $sortlist[] = "zzz.".$name; 
     164        } 
     165    } 
     166} 
     167$result = dbquery("UPDATE ".$db_prefix."configuration SET cfg_value = '".mysql_real_escape_string(serialize($methods))."' WHERE cfg_name = 'authentication_methods'"); 
     168 
     169// create the list of available methods, in the correct order 
     170sort($sortlist); 
     171$variables['methods'] = array(); 
     172$c = count($selected);$i=1; 
     173foreach($sortlist as $entry) { 
     174    $listentry = explode(".", $entry); 
     175    $variables['methods'][] = array('name' => $listentry[1], 'class' => $methods[$listentry[1]]['class'], 'status' => $listentry[0] == 'zzz' ? 0 :1, 'last' => ($i++ == $c ? 1 : 0)); 
     176} 
    146177 
    147178// define the admin body panel 
Note: See TracChangeset for help on using the changeset viewer.