Ticket #146: 011_validate_username_password_functions.diff
| File 011_validate_username_password_functions.diff, 11.6 kB (added by rho, 9 months ago) |
|---|
-
a/auth/ldap/lib.php
old new 126 126 } 127 127 128 128 /** 129 * checks if the LDAP username is valid by elgg130 * TODO - this should be a library function somewhere.131 * IMPORTANT - Currently (1Jun07) this differs from the normal elgg132 * username check as LDAP can have long usernames and non-alphanum133 * characters. A clear policy needs to be decided on in this matter134 */135 136 function elgg_valid_username($username){137 return preg_match("/^[A-Za-z0-9.\-]{3,20}$/",$username);138 }139 140 /**141 129 * creates an entry in the elgg database for the given username and 142 130 * password and LDAP entry 143 131 */ 144 132 145 133 function ldap_create_elgg_user($username, $password, $user_info) { 146 if(! elgg_valid_username($username)) {134 if(!validate_username($username)) { 147 135 $messages[] = __gettext("Error! LDAP Username does not meet Elgg requirements"); 148 136 } else { 149 137 // Does the user already exist? -
a/lib/elgglib.php
old new 2024 2024 2025 2025 } 2026 2026 2027 /** 2028 * Check if system reached account limit 2029 * @return boolean 2030 */ 2031 function 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 */ 2048 function 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 */ 2071 function 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 */ 2090 function 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 } 2027 2118 2028 2119 /** 2029 2120 * Validates an email to make sure it makes sense and adheres -
a/lms/join.php
old new 44 44 $messages = array(); 45 45 if ($mode == 'join') { 46 46 // validate 47 if (! preg_match("/^[A-Za-z0-9]{3,12}$/",$u->username)) {47 if (!validate_username($u->username)) { 48 48 $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))) { 52 50 $messages[] = __gettext("The username '$username' is already taken by another user. You will need to pick a different one."); 53 51 } 54 52 55 if ( $u->password1 != $u->password2 || strlen($u->password1) < 6 || strlen($u->password2) > 16) {53 if (!validate_password($u->password1, $u->password2)) { 56 54 $messages[] = __gettext("Error! Invalid password. Your passwords must match and be between 6 and 16 characters in length."); 57 55 } 58 56 -
a/mod/admin/lib/admin_actions.php
old new 35 35 // Manage users 36 36 case "userdetails:update": 37 37 $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))); 39 39 $change_filequota = optional_param('change_filequota',0,PARAM_INT); 40 40 $change_iconquota = optional_param('change_iconquota',0,PARAM_INT); 41 41 if (!empty($id)) { 42 42 if (!empty($change_username)) { 43 if (! preg_match("/^[A-Za-z0-9]{3,12}$/",$change_username)) {43 if (!validate_username($change_username)) { 44 44 $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."); 45 45 } else { 46 46 $u = new StdClass; … … 116 116 continue; 117 117 } 118 118 119 $new_username[$i] = trim( $new_username[$i]);119 $new_username[$i] = trim(strtolower($new_username[$i])); 120 120 $new_name[$i] = trim($new_name[$i]); 121 121 $new_email[$i] = trim($new_email[$i]); 122 122 if (empty($new_username[$i]) || empty($new_name[$i]) || empty($new_email[$i])) { … … 124 124 continue; 125 125 } 126 126 127 if (! preg_match("/^[A-Za-z0-9]{3,12}$/",$new_username[$i])) {127 if (!validate_username($new_username[$i])) { 128 128 $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.") 129 129 ,($i + 1),$new_username[$i]); 130 130 continue; 131 } 132 133 if (record_exists('users','username',$new_username[$i])) { 131 } elseif (!username_is_available($new_username[$i])) { 134 132 $messages[] = sprintf(__gettext("User addition %d failed: username %s is already in use."),($i + 1),$new_username[$i]); 135 133 continue; 136 134 } -
a/mod/community/lib/communities_actions.php
old new 20 20 $comm_username = optional_param('comm_username'); 21 21 if (logged_on && !empty($comm_name) && !empty($comm_username) && 22 22 ($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)) { 24 24 $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."); 25 25 } else if (trim($comm_name) == "") { 26 26 $messages[] = __gettext("Error! The community name cannot be blank."); 27 27 } else { 28 28 $comm_username = strtolower(trim($comm_username)); 29 if ( record_exists('users','username',$comm_username)) {29 if (!username_is_available($comm_username)) { 30 30 $messages[] = sprintf(__gettext("The username %s is already taken by another user. You will need to pick a different one."), $comm_username); 31 31 } else { 32 32 $name = trim($comm_name); -
a/mod/invite/lib/invite_actions.php
old new 21 21 $invite->email = trim(optional_param('invite_email')); 22 22 if (!empty($invite->name) && !empty($invite->email)) { 23 23 if (logged_on || ($CFG->publicinvite == true)) { 24 if ( ($CFG->maxusers == 0 || (count_users('person') < $CFG->maxusers))) {24 if (!maxusers_limit()) { 25 25 if (validate_email(stripslashes($invite->email))) { 26 26 $strippedname = stripslashes($invite->name); // for the message text. 27 27 $invitations = count_records('invitations','email',$invite->email); … … 79 79 $name = trim(optional_param('join_name')); 80 80 $code = trim(optional_param('invitecode')); 81 81 $over13 = optional_param('over13'); 82 $username = trim( optional_param('join_username'));82 $username = trim(strtolower(optional_param('join_username'))); 83 83 $password1 = trim(optional_param('join_password1')); 84 84 $password2 = trim(optional_param('join_password2')); 85 85 86 86 if (isset($name) && isset($code)) { 87 if ( !($CFG->maxusers == 0 || (count_users('person') < $CFG->maxusers))) {87 if (maxusers_limit()) { 88 88 $messages[] = __gettext("Unfortunately this community has reached its account limit and you are unable to join at this time."); 89 89 break; 90 90 } … … 96 96 $messages[] = __gettext("Error! Invalid invite code."); 97 97 break; 98 98 } 99 if ( $password1 != $password2 || strlen($password1) < 6 || strlen($password2) > 16) {99 if (!validate_password($password1, $password2)) { 100 100 $messages[] = __gettext("Error! Invalid password. Your passwords must match and be between 6 and 16 characters in length."); 101 101 break; 102 102 } 103 if (! preg_match("/^[A-Za-z0-9]{3,12}$/",$username)) {103 if (!validate_username($username)) { 104 104 $messages[] = __gettext("Error! Your username must contain letters and numbers only, cannot be blank, and must be between 3 and 12 characters in length."); 105 105 break; 106 106 } 107 $username = strtolower($username); 108 if (record_exists('users','username',$username)) { 107 if (!username_is_available($username)) { 109 108 $messages[] = __gettext("The username '$username' is already taken by another user. You will need to pick a different one."); 110 109 break; 111 110 }
