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
This commit is contained in:
Mark Rogers
2009-04-17 08:13:54 +00:00
parent b4571a1e6e
commit c38219e7b3
7 changed files with 267 additions and 26 deletions

View File

@@ -526,10 +526,27 @@
</property> </property>
</bean> </bean>
<!-- Used for generating user names --> <!-- -->
<bean id="userNameGenerator" class="org.alfresco.repo.security.authentication.BasicUserNameGenerator"> <bean id="nameBasedUserNameGenerator" class="org.alfresco.repo.security.authentication.NameBasedUserNameGenerator">
<!-- name patterns available:
%lastName%, lower case last name
%firstName%, lower case first name
%emailAddress% email address
%i% lower case first name inital
-->
<property name="namePattern">
<value>%firstName%_%lastName%</value>
</property>
<property name="userNameLength"> <property name="userNameLength">
<value>6</value> <value>10</value>
</property>
</bean>
<!-- Used for generating user names -->
<bean id="userNameGenerator" class="org.alfresco.repo.security.authentication.TenantAwareUserNameGenerator">
<property name="generator">
<ref bean="nameBasedUserNameGenerator"/>
</property> </property>
<property name="tenantService"> <property name="tenantService">
<ref bean="tenantService"/> <ref bean="tenantService"/>

View File

@@ -915,7 +915,7 @@ public class InvitationServiceImpl implements InvitationService
String inviteeUserName = null; String inviteeUserName = null;
int i = 0; int i = 0;
do { do {
inviteeUserName = usernameGenerator.generateUserName(); inviteeUserName = usernameGenerator.generateUserName(inviteeFirstName, inviteeLastName, inviteeEmail, i);
i++; i++;
} while (this.personService.personExists(inviteeUserName) } while (this.personService.personExists(inviteeUserName)
&& (i < getMaxUserNameGenRetries())); && (i < getMaxUserNameGenRetries()));

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -25,47 +25,52 @@
package org.alfresco.repo.security.authentication; package org.alfresco.repo.security.authentication;
import org.alfresco.repo.tenant.TenantService; import org.alfresco.repo.tenant.TenantService;
import org.alfresco.util.PropertyCheck;
import org.apache.commons.lang.RandomStringUtils; 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 public class TenantAwareUserNameGenerator implements UserNameGenerator
{ {
// user name length property
private int userNameLength;
private TenantService tenantService; private TenantService tenantService;
private UserNameGenerator generator;
public void init()
{
PropertyCheck.mandatory(this, "tenantService", tenantService);
PropertyCheck.mandatory(this, "generator", generator);
}
public void setTenantService(TenantService tenantService) public void setTenantService(TenantService tenantService)
{ {
this.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 * Returns a generated user name
* *
* @return the 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()) if (tenantService.isEnabled())
{ {
userName = tenantService.getDomainUser(userName, tenantService.getCurrentUserDomain()); userName = tenantService.getDomainUser(userName, tenantService.getCurrentUserDomain());
} }
return userName; return userName;
} }
public void setGenerator(UserNameGenerator generator) {
this.generator = generator;
}
public UserNameGenerator getGenerator() {
return generator;
}
} }

View File

@@ -32,9 +32,17 @@ package org.alfresco.repo.security.authentication;
public interface UserNameGenerator 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 * @return the generated user name
*/ */
public String generateUserName(); public String generateUserName(String firstName, String lastName, String emailAddress, int seed);
} }