From c38219e7b3049a931c09bbe99d264977524111ae Mon Sep 17 00:00:00 2001 From: Mark Rogers Date: Fri, 17 Apr 2009 08:13:54 +0000 Subject: [PATCH] ALFCOM-2018 - invitation user name generator pattern is now cofigurable git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@13989 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../authentication-services-context.xml | 23 +++- .../invitation/InvitationServiceImpl.java | 2 +- .../NameBasedUserNameGenerator.java | 107 ++++++++++++++++++ .../NameBasedUserNameGeneratorTest.java | 47 ++++++++ .../RandomUserNameGenerator.java | 57 ++++++++++ ...java => TenantAwareUserNameGenerator.java} | 45 ++++---- .../authentication/UserNameGenerator.java | 12 +- 7 files changed, 267 insertions(+), 26 deletions(-) create mode 100644 source/java/org/alfresco/repo/security/authentication/NameBasedUserNameGenerator.java create mode 100644 source/java/org/alfresco/repo/security/authentication/NameBasedUserNameGeneratorTest.java create mode 100644 source/java/org/alfresco/repo/security/authentication/RandomUserNameGenerator.java rename source/java/org/alfresco/repo/security/authentication/{BasicUserNameGenerator.java => TenantAwareUserNameGenerator.java} (69%) diff --git a/config/alfresco/authentication-services-context.xml b/config/alfresco/authentication-services-context.xml index ed1fcab05d..b3c844c9c2 100644 --- a/config/alfresco/authentication-services-context.xml +++ b/config/alfresco/authentication-services-context.xml @@ -526,10 +526,27 @@ - - + + + + + %firstName%_%lastName% + + - 6 + 10 + + + + + + + diff --git a/source/java/org/alfresco/repo/invitation/InvitationServiceImpl.java b/source/java/org/alfresco/repo/invitation/InvitationServiceImpl.java index a6440345fe..99be4a0b58 100644 --- a/source/java/org/alfresco/repo/invitation/InvitationServiceImpl.java +++ b/source/java/org/alfresco/repo/invitation/InvitationServiceImpl.java @@ -915,7 +915,7 @@ public class InvitationServiceImpl implements InvitationService String inviteeUserName = null; int i = 0; do { - inviteeUserName = usernameGenerator.generateUserName(); + inviteeUserName = usernameGenerator.generateUserName(inviteeFirstName, inviteeLastName, inviteeEmail, i); i++; } while (this.personService.personExists(inviteeUserName) && (i < getMaxUserNameGenRetries())); diff --git a/source/java/org/alfresco/repo/security/authentication/NameBasedUserNameGenerator.java b/source/java/org/alfresco/repo/security/authentication/NameBasedUserNameGenerator.java new file mode 100644 index 0000000000..695da43406 --- /dev/null +++ b/source/java/org/alfresco/repo/security/authentication/NameBasedUserNameGenerator.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2005-2009 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have received a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.repo.security.authentication; + +import org.apache.commons.lang.RandomStringUtils; + +/** + * Generates a user name based upon firstName and lastName. + * + * The firstNamePattern is used when seed = 0. + * Then a random element is added and randomNamePattern is used. + * + */ +public class NameBasedUserNameGenerator implements UserNameGenerator +{ + // user name length property + private int userNameLength = 10; + + /** + * name generator pattern + */ + private String namePattern = "%lastName%_%firstName%"; + + /** + * The pattern of the user name to generate + * e.g. %lastName%_%firstName% would generate Fred_Bloggs + * + * Patterns available: + * %lastName%, lower case last name + * %firstName%, lower case first name + * %emailAddress% email address + * %i% lower case first name inital + * + * @param userNamePattern + */ + public void setNamePattern(String userNamePattern) + { + this.namePattern = userNamePattern; + } + + /** + * Set the user name length + * + * @param userNameLength the user name length + */ + public void setUserNameLength(int userNameLength) + { + this.userNameLength = userNameLength; + } + + /** + * Returns a generated user name + * + * @return the generated user name + */ + public String generateUserName(String firstName, String lastName, String emailAddress, int seed) + { + String userName; + + String pattern = namePattern; + + String initial = firstName.toLowerCase().substring(0,1); + + userName = pattern + .replace("%i%", initial) + .replace("%firstName%", firstName.toLowerCase()) + .replace("%lastName%", lastName.toLowerCase()) + .replace("%emailAddress%", emailAddress.toLowerCase()); + + if(seed > 0) + { + if (userName.length() < userNameLength + 3) + { + userName = userName + RandomStringUtils.randomNumeric(3); + } + else + { + // truncate the user name and slap on 3 random characters + userName = userName.substring(0, userNameLength -3) + RandomStringUtils.randomNumeric(3); + } + } + + return userName; + } +} diff --git a/source/java/org/alfresco/repo/security/authentication/NameBasedUserNameGeneratorTest.java b/source/java/org/alfresco/repo/security/authentication/NameBasedUserNameGeneratorTest.java new file mode 100644 index 0000000000..4eafae22ec --- /dev/null +++ b/source/java/org/alfresco/repo/security/authentication/NameBasedUserNameGeneratorTest.java @@ -0,0 +1,47 @@ +package org.alfresco.repo.security.authentication; + +import junit.framework.TestCase; + +public class NameBasedUserNameGeneratorTest extends TestCase +{ + public void testGenerate() + { + NameBasedUserNameGenerator generator = new NameBasedUserNameGenerator(); + generator.setUserNameLength(10); + generator.setNamePattern("%firstName%_%lastName%"); + + String firstName = "Buffy"; + String lastName = "Summers"; + String emailAddress = "buffy@sunnydale.com"; + + // should generate buffy_summers + String userName = generator.generateUserName(firstName, lastName, emailAddress, 0); + assertEquals("", (firstName + "_" + lastName).toLowerCase(), userName); + + // should generate something different from above since seed > 0 + userName = generator.generateUserName(firstName, lastName, emailAddress, 1); + assertEquals("", (firstName + "_" + lastName).toLowerCase().substring(0,7), userName.substring(0,7)); + assertTrue("", !(firstName + "_" + lastName).toLowerCase().equals(userName)); + + // should generate buffy_summers@sunnydale.com + generator.setNamePattern("%emailAddress%"); + userName = generator.generateUserName(firstName, lastName, emailAddress, 0); + assertEquals("", (emailAddress).toLowerCase(), userName); + + // should generate buffy_s123 + userName = generator.generateUserName(firstName, lastName, emailAddress, 1); + assertTrue("", !(emailAddress).toLowerCase().equals(userName)); + + // should generate summers.buffy + generator.setNamePattern("%lastName%.%firstName%"); + userName = generator.generateUserName(firstName, lastName, emailAddress, 0); + assertEquals("", (lastName + "." + firstName).toLowerCase(), userName); + + // should generate bsummers + generator.setNamePattern("%i%%lastName%"); + userName = generator.generateUserName(firstName, lastName, emailAddress, 0); + assertEquals("", ("bsummers").toLowerCase(), userName); + + } + +} diff --git a/source/java/org/alfresco/repo/security/authentication/RandomUserNameGenerator.java b/source/java/org/alfresco/repo/security/authentication/RandomUserNameGenerator.java new file mode 100644 index 0000000000..d862469b8a --- /dev/null +++ b/source/java/org/alfresco/repo/security/authentication/RandomUserNameGenerator.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2005-2009 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have received a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.repo.security.authentication; + +import org.alfresco.repo.tenant.TenantService; +import org.apache.commons.lang.RandomStringUtils; + +/** + * Generates a user name based upon a random numeric + * + */ +public class RandomUserNameGenerator implements UserNameGenerator +{ + // user name length property + private int userNameLength; + + /** + * Returns a generated user name + * + * @return the generated user name + */ + public String generateUserName(String firstName, String lastName, String emailAddress, int seed) + { + String userName = RandomStringUtils.randomNumeric(getUserNameLength()); + return userName; + } + + public void setUserNameLength(int userNameLength) { + this.userNameLength = userNameLength; + } + + public int getUserNameLength() { + return userNameLength; + } +} diff --git a/source/java/org/alfresco/repo/security/authentication/BasicUserNameGenerator.java b/source/java/org/alfresco/repo/security/authentication/TenantAwareUserNameGenerator.java similarity index 69% rename from source/java/org/alfresco/repo/security/authentication/BasicUserNameGenerator.java rename to source/java/org/alfresco/repo/security/authentication/TenantAwareUserNameGenerator.java index 6882318c75..d9e4b91500 100644 --- a/source/java/org/alfresco/repo/security/authentication/BasicUserNameGenerator.java +++ b/source/java/org/alfresco/repo/security/authentication/TenantAwareUserNameGenerator.java @@ -25,47 +25,52 @@ package org.alfresco.repo.security.authentication; import org.alfresco.repo.tenant.TenantService; +import org.alfresco.util.PropertyCheck; import org.apache.commons.lang.RandomStringUtils; /** - * Generates a simple numeric user name of specified length + * Tenant Aware user name generator generates user names for each specific tenant. * - * @author glen johnson at Alfresco dot com + * It does this by delegating to other user name generators. + */ -public class BasicUserNameGenerator implements UserNameGenerator -{ - // user name length property - private int userNameLength; - +public class TenantAwareUserNameGenerator implements UserNameGenerator +{ private TenantService tenantService; + private UserNameGenerator generator; + + public void init() + { + PropertyCheck.mandatory(this, "tenantService", tenantService); + PropertyCheck.mandatory(this, "generator", generator); + } + public void setTenantService(TenantService tenantService) { this.tenantService = tenantService; } - - /** - * Set the user name length - * - * @param userNameLength the user name length - */ - public void setUserNameLength(int userNameLength) - { - this.userNameLength = userNameLength; - } - + /** * Returns a generated user name * * @return the generated user name */ - public String generateUserName() + public String generateUserName(String firstName, String lastName, String emailAddress, int seed) { - String userName = RandomStringUtils.randomNumeric(userNameLength); + String userName = generator.generateUserName(firstName, lastName, emailAddress, seed); if (tenantService.isEnabled()) { userName = tenantService.getDomainUser(userName, tenantService.getCurrentUserDomain()); } return userName; } + + public void setGenerator(UserNameGenerator generator) { + this.generator = generator; + } + + public UserNameGenerator getGenerator() { + return generator; + } } diff --git a/source/java/org/alfresco/repo/security/authentication/UserNameGenerator.java b/source/java/org/alfresco/repo/security/authentication/UserNameGenerator.java index c573dc1df1..051b0de659 100644 --- a/source/java/org/alfresco/repo/security/authentication/UserNameGenerator.java +++ b/source/java/org/alfresco/repo/security/authentication/UserNameGenerator.java @@ -32,9 +32,17 @@ package org.alfresco.repo.security.authentication; public interface UserNameGenerator { /** - * Returns a generated user name + * Returns a generated user name. + * + * A seed value of 0 means first attempt. A non zero seed value indicates that the obvious user name is already taken + * and that some random element needs to be added to make a unique user id. + * + * @param firstName the given name of the new user + * @param lastName the family name of the new user + * @param emailAddress the email address of the new user + * @param seed a seed for user name generation, the value 0 means "no seed" * * @return the generated user name */ - public String generateUserName(); + public String generateUserName(String firstName, String lastName, String emailAddress, int seed); }