Feature #306 - Changes (final maybe) about username related logic.

skala
Ivan Tcholakov 16 years ago
parent 6df1c39ca2
commit 1aec623a1f
  1. 19
      main/admin/user_import.php
  2. 3
      main/inc/lib/usermanager.lib.php

@ -38,22 +38,22 @@ function validate_data($users) {
} }
// 2. Check username, first, check whether it is empty. // 2. Check username, first, check whether it is empty.
if (!UserManager::is_username_empty($user['UserName'])) { if (!UserManager::is_username_empty($user['UserName'])) {
// 2.1. Check whether the username was used twice in import file. // 2.1. Check whether username is too long.
if (UserManager::is_username_too_long($user['UserName'])) {
$user['error'] = get_lang('UserNameTooLong');
$errors[] = $user;
}
// 2.2. Check whether the username was used twice in import file.
if (isset($usernames[$user['UserName']])) { if (isset($usernames[$user['UserName']])) {
$user['error'] = get_lang('UserNameUsedTwice'); $user['error'] = get_lang('UserNameUsedTwice');
$errors[] = $user; $errors[] = $user;
} }
$usernames[$user['UserName']] = 1; $usernames[$user['UserName']] = 1;
// 2.2. Check whether username is allready occupied. // 2.3. Check whether username is allready occupied.
if (!UserManager::is_username_available($user['UserName'])) { if (!UserManager::is_username_available($user['UserName'])) {
$user['error'] = get_lang('UserNameNotAvailable'); $user['error'] = get_lang('UserNameNotAvailable');
$errors[] = $user; $errors[] = $user;
} }
// 2.3. Check whether username is too long.
if (UserManager::is_username_too_long($user['UserName'])) {
$user['error'] = get_lang('UserNameTooLong');
$errors[] = $user;
}
} }
// 3. Check status. // 3. Check status.
if (isset($user['Status']) && !api_status_exists($user['Status'])) { if (isset($user['Status']) && !api_status_exists($user['Status'])) {
@ -82,9 +82,12 @@ function validate_data($users) {
* Add missing user-information (which isn't required, like password, username etc). * Add missing user-information (which isn't required, like password, username etc).
*/ */
function complete_missing_data($user) { function complete_missing_data($user) {
global $purification_option_for_usernames;
// 1. Create a username if necessary. // 1. Create a username if necessary.
if (UserManager::is_username_empty($user['UserName'])) { if (UserManager::is_username_empty($user['UserName'])) {
$user['UserName'] = UserManager::create_unique_username($user['FirstName'], $user['LastName']); $user['UserName'] = UserManager::create_unique_username($user['FirstName'], $user['LastName']);
} else {
$user['UserName'] = UserManager::purify_username($user['UserName'], $purification_option_for_usernames);
} }
// 2. Generate a password if necessary. // 2. Generate a password if necessary.
if (empty($user['Password'])) { if (empty($user['Password'])) {
@ -215,10 +218,8 @@ function element_end($parser, $data) {
global $user; global $user;
global $users; global $users;
global $current_value; global $current_value;
global $purification_option_for_usernames;
switch ($data) { switch ($data) {
case 'Contact' : case 'Contact' :
$user['UserName'] = UserManager::purify_username($user['UserName'], $purification_option_for_usernames);
if ($user['Status'] == '5') { if ($user['Status'] == '5') {
$user['Status'] = STUDENT; $user['Status'] = STUDENT;
} }

@ -369,6 +369,7 @@ class UserManager {
* @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
* @param string $encoding (optional) The character encoding for the input names. If it is omitted, the platform character set will be used by default. * @param string $encoding (optional) The character encoding for the input names. If it is omitted, the platform character set will be used by default.
* @return string Returns a username that contains only ASCII-letters and digits, and that is unique within the system. * @return string Returns a username that contains only ASCII-letters and digits, and that is unique within the system.
* Note: When the method is called several times with same parameters, its results look like the following sequence: ivan, ivan2, ivan3, ivan4, ...
* @author Ivan Tcholakov, 2009 * @author Ivan Tcholakov, 2009
*/ */
public static function create_unique_username($firstname, $lastname = null, $language = null, $encoding = null) { public static function create_unique_username($firstname, $lastname = null, $language = null, $encoding = null) {
@ -381,7 +382,7 @@ class UserManager {
$username = self::create_username($firstname, $lastname, $language, $encoding); $username = self::create_username($firstname, $lastname, $language, $encoding);
} }
if (!self::is_username_available($username)) { if (!self::is_username_available($username)) {
$i = 0; $i = 2;
$temp_username = substr($username, 0, USERNAME_MAX_LENGTH - strlen((string)$i)).$i; $temp_username = substr($username, 0, USERNAME_MAX_LENGTH - strlen((string)$i)).$i;
while (!self::is_username_available($temp_username)) { while (!self::is_username_available($temp_username)) {
$i++; $i++;

Loading…
Cancel
Save