Ticket #146: 011_validate_username_password_functions.diff

File 011_validate_username_password_functions.diff, 11.6 kB (added by rho, 9 months ago)

updated patch, includes auth/ldap updates

  • a/auth/ldap/lib.php

    old new  
    126126        } 
    127127 
    128128        /** 
    129           * checks if the LDAP username is valid by elgg 
    130           * TODO - this should be a library function somewhere. 
    131           * IMPORTANT - Currently (1Jun07) this differs from the normal elgg  
    132           * username check as LDAP can have long usernames and non-alphanum 
    133           * characters. A clear policy needs to be decided on in this matter 
    134           */ 
    135  
    136         function elgg_valid_username($username){ 
    137         return preg_match("/^[A-Za-z0-9.\-]{3,20}$/",$username); 
    138         } 
    139  
    140         /** 
    141129          * creates an entry in the elgg database for the given username and  
    142130          * password and LDAP entry 
    143131          */ 
    144132 
    145133        function ldap_create_elgg_user($username, $password, $user_info) { 
    146                 if(! elgg_valid_username($username)) { 
     134                if(!validate_username($username)) { 
    147135            $messages[] = __gettext("Error! LDAP Username does not meet Elgg requirements"); 
    148136        } else { 
    149137            // Does the user already exist? 
  • a/lib/elgglib.php

    old new  
    20242024 
    20252025} 
    20262026 
     2027/** 
     2028 * Check if system reached account limit 
     2029 * @return boolean 
     2030 */ 
     2031function maxusers_limit() { 
     2032    global $CFG; 
     2033 
     2034    $limit = isset($CFG->maxusers) ? intval($CFG->maxusers) : 0; 
     2035 
     2036    if ($limit > 0 && count_users('person') >= $limit) { 
     2037        return true; 
     2038    } 
     2039     
     2040    return false; 
     2041} 
     2042 
     2043/** 
     2044 * Validates username  
     2045 * @param string $username The username to validate 
     2046 * @return boolean 
     2047 */ 
     2048function validate_username($username) { 
     2049    global $CFG; 
     2050 
     2051    $minchars = isset($CFG->username_minchars) ? intval($CFG->username_minchars) : 3; 
     2052    $maxchars = isset($CFG->username_maxchars) ? intval($CFG->username_maxchars) : 12; 
     2053 
     2054    // TODO: modifying regex needs update rewrite rules 
     2055    $regex = sprintf('/^[A-Za-z0-9]{%d,%d}$/i', $minchars, $maxchars); // letters and numbers only 
     2056 
     2057    // TODO: should allow plugins to extend validation? 
     2058 
     2059    if (!preg_match($regex, $username)) { 
     2060        return false; 
     2061    } 
     2062     
     2063    return true; 
     2064} 
     2065 
     2066/** 
     2067 * Check if username is available 
     2068 * @param string $username The username to check availability  
     2069 * @return boolean 
     2070 */ 
     2071function username_is_available($username) { 
     2072 
     2073    if (!validate_username($username)) { 
     2074        return false; 
     2075    } 
     2076 
     2077    if (record_exists('users', 'username', $username)) { 
     2078        return false; 
     2079    } 
     2080 
     2081    return true; 
     2082} 
     2083 
     2084/** 
     2085 * Validates password 
     2086 * @param string $password1 The password to validate 
     2087 * @param string $password2 The 2nd password to verify that match 
     2088 * @return boolean 
     2089 */ 
     2090function validate_password($password1, $password2=null) { 
     2091    global $CFG; 
     2092 
     2093    $minchars = isset($CFG->password_minchars) ? intval($CFG->password_minchars) : 6; 
     2094    $maxchars = isset($CFG->password_maxchars) ? intval($CFG->password_maxchars) : 32; 
     2095 
     2096    // TODO: should allow plugins to extend validate? 
     2097    // could help to enforce passwords 
     2098 
     2099    if (!empty($password2) && $password1 != $password2) { 
     2100        return false; 
     2101    } 
     2102 
     2103    // TODO: from units/users/userdetails_actions.php 
     2104    /* 
     2105    if (!preg_match('/^[a-z0-9]*$/i', $password1)) { // only allow letters and numbers 
     2106        return false; 
     2107    } 
     2108     */ 
     2109 
     2110    $len = strlen($password1); 
     2111 
     2112    if ($len < $minchars || $len > $maxchars) { 
     2113        return  false; 
     2114    } 
     2115     
     2116    return true; 
     2117} 
    20272118 
    20282119/** 
    20292120 * Validates an email to make sure it makes sense and adheres 
  • a/lms/join.php

    old new  
    4444    $messages = array(); 
    4545    if ($mode == 'join') { 
    4646        // validate 
    47         if (!preg_match("/^[A-Za-z0-9]{3,12}$/",$u->username)) { 
     47        if (!validate_username($u->username)) { 
    4848            $messages[] = __gettext("Error! Your username must contain letters and numbers only, cannot be blank, and must be between 3 and 12 characters in length."); 
    49         } 
    50  
    51         if (record_exists('users','username',strtolower($u->username))) { 
     49        } elseif (!username_is_available(strtolower($u->username))) { 
    5250            $messages[] = __gettext("The username '$username' is already taken by another user. You will need to pick a different one."); 
    5351        } 
    5452 
    55         if ($u->password1 != $u->password2 || strlen($u->password1) < 6 || strlen($u->password2) > 16) { 
     53        if (!validate_password($u->password1, $u->password2)) { 
    5654            $messages[] = __gettext("Error! Invalid password. Your passwords must match and be between 6 and 16 characters in length."); 
    5755        } 
    5856 
  • a/mod/admin/lib/admin_actions.php

    old new  
    3535            // Manage users 
    3636        case "userdetails:update": 
    3737            $id = optional_param('id',0,PARAM_INT); 
    38             $change_username = optional_param('change_username','',PARAM_CLEAN); 
     38            $change_username = trim(strtolower(optional_param('change_username','',PARAM_CLEAN))); 
    3939            $change_filequota = optional_param('change_filequota',0,PARAM_INT); 
    4040            $change_iconquota = optional_param('change_iconquota',0,PARAM_INT); 
    4141            if (!empty($id)) { 
    4242                if (!empty($change_username)) { 
    43                     if (!preg_match("/^[A-Za-z0-9]{3,12}$/",$change_username)) { 
     43                    if (!validate_username($change_username)) { 
    4444                        $messages[] = __gettext("Error! The new username must contain letters and numbers only, cannot be blank, and must be between 3 and 12 characters in length."); 
    4545                    } else { 
    4646                        $u = new StdClass; 
     
    116116                        continue; 
    117117                    } 
    118118                         
    119                     $new_username[$i] = trim($new_username[$i]); 
     119                    $new_username[$i] = trim(strtolower($new_username[$i])); 
    120120                    $new_name[$i] = trim($new_name[$i]); 
    121121                    $new_email[$i] = trim($new_email[$i]); 
    122122                    if (empty($new_username[$i]) || empty($new_name[$i]) || empty($new_email[$i])) { 
     
    124124                        continue; 
    125125                    } 
    126126                     
    127                     if (!preg_match("/^[A-Za-z0-9]{3,12}$/",$new_username[$i])) { 
     127                    if (!validate_username($new_username[$i])) { 
    128128                        $messages[] = sprintf(__gettext("New username %d (%s) was invalid; usernames must contain letters and numbers only, cannot be blank, and must be between 3 and 12 characters in length.") 
    129129                                              ,($i + 1),$new_username[$i]); 
    130130                        continue; 
    131                     } 
    132                      
    133                     if (record_exists('users','username',$new_username[$i])) { 
     131                    } elseif (!username_is_available($new_username[$i])) { 
    134132                        $messages[] = sprintf(__gettext("User addition %d failed: username %s is already in use."),($i + 1),$new_username[$i]); 
    135133                        continue; 
    136134                    }  
  • a/mod/community/lib/communities_actions.php

    old new  
    2020            $comm_username = optional_param('comm_username'); 
    2121            if (logged_on && !empty($comm_name) && !empty($comm_username) && 
    2222                ($CFG->community_create_flag == "" || user_flag_get($CFG->community_create_flag, $USER->ident))) { 
    23                 if (!preg_match("/^[A-Za-z0-9]{3,12}$/",$comm_username)) { 
     23                if (!validate_username($comm_username)) { 
    2424                    $messages[] = __gettext("Error! The community username must contain letters and numbers only, cannot be blank, and must be between 3 and 12 characters in length."); 
    2525                } else if (trim($comm_name) == "") { 
    2626                    $messages[] = __gettext("Error! The community name cannot be blank."); 
    2727                } else { 
    2828                    $comm_username = strtolower(trim($comm_username)); 
    29                     if (record_exists('users','username',$comm_username)) { 
     29                    if (!username_is_available($comm_username)) { 
    3030                        $messages[] = sprintf(__gettext("The username %s is already taken by another user. You will need to pick a different one."), $comm_username); 
    3131                    } else { 
    3232                        $name = trim($comm_name); 
  • a/mod/invite/lib/invite_actions.php

    old new  
    2121         $invite->email = trim(optional_param('invite_email')); 
    2222         if (!empty($invite->name) && !empty($invite->email)) { 
    2323             if (logged_on || ($CFG->publicinvite == true)) { 
    24                  if (($CFG->maxusers == 0 || (count_users('person') < $CFG->maxusers))) { 
     24                 if (!maxusers_limit()) { 
    2525                     if (validate_email(stripslashes($invite->email))) { 
    2626                         $strippedname = stripslashes($invite->name); // for the message text. 
    2727                         $invitations = count_records('invitations','email',$invite->email); 
     
    7979         $name = trim(optional_param('join_name')); 
    8080         $code = trim(optional_param('invitecode')); 
    8181         $over13 = optional_param('over13'); 
    82          $username = trim(optional_param('join_username')); 
     82         $username = trim(strtolower(optional_param('join_username'))); 
    8383         $password1 = trim(optional_param('join_password1')); 
    8484         $password2 = trim(optional_param('join_password2')); 
    8585 
    8686         if (isset($name) && isset($code)) { 
    87              if (!($CFG->maxusers == 0 || (count_users('person') < $CFG->maxusers))) { 
     87             if (maxusers_limit()) { 
    8888                 $messages[] = __gettext("Unfortunately this community has reached its account limit and you are unable to join at this time."); 
    8989                 break; 
    9090             } 
     
    9696                 $messages[] = __gettext("Error! Invalid invite code."); 
    9797                 break; 
    9898             } 
    99              if ($password1 != $password2 || strlen($password1) < 6 || strlen($password2) > 16) { 
     99             if (!validate_password($password1, $password2)) { 
    100100                 $messages[] = __gettext("Error! Invalid password. Your passwords must match and be between 6 and 16 characters in length."); 
    101101                 break; 
    102102             } 
    103              if (!preg_match("/^[A-Za-z0-9]{3,12}$/",$username)) { 
     103             if (!validate_username($username)) { 
    104104                 $messages[] = __gettext("Error! Your username must contain letters and numbers only, cannot be blank, and must be between 3 and 12 characters in length."); 
    105105                 break; 
    106106             } 
    107              $username = strtolower($username); 
    108              if (record_exists('users','username',$username)) { 
     107             if (!username_is_available($username)) { 
    109108                 $messages[] = __gettext("The username '$username' is already taken by another user. You will need to pick a different one."); 
    110109                 break; 
    111110             }