Added server config values to check user name minimal and maximal length and allowed chars for user name. Set to 3-14 length and [^a-z0-9_] chars (ignoring case) .

This commit is contained in:
LevelX2 2013-04-13 14:21:42 +02:00
parent 6dd99350a3
commit 6e4b06fc5b
6 changed files with 34 additions and 2 deletions

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../Config.xsd">
<server serverAddress="0.0.0.0" serverName="mage-server" port="17171" maxGameThreads="10" maxSecondsIdle="600"/>
<server serverAddress="0.0.0.0" serverName="mage-server" port="17171" maxGameThreads="10" maxSecondsIdle="600" minUserNameLength="3" maxUserNameLength="14" userNamePattern="[^a-z0-9_]"/>
<playerTypes>
<playerType name="Human" jar="mage-player-human.jar" className="mage.player.human.HumanPlayer"/>
<!--<playerType name="Computer - minimax" jar="mage-player-aiminimax.jar" className="mage.player.ai.ComputerPlayer3"/>-->

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../Config.xsd">
<server serverAddress="0.0.0.0" serverName="mage-server" port="17171" maxGameThreads="10" maxSecondsIdle="600"/>
<server serverAddress="0.0.0.0" serverName="mage-server" port="17171" maxGameThreads="10" maxSecondsIdle="600" minUserNameLength="3" maxUserNameLength="14" userNamePattern="[^a-z0-9_]"/>
<playerTypes>
<playerType name="Human" jar="mage-player-human-${project.version}.jar" className="mage.player.human.HumanPlayer"/>
<playerType name="Computer - mad" jar="mage-player-ai-ma-${project.version}.jar" className="mage.player.ai.ComputerPlayer7"/>

View file

@ -41,6 +41,9 @@ import org.jboss.remoting.callback.InvokerCallbackHandler;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import mage.server.util.ConfigSettings;
/**
*
@ -69,6 +72,17 @@ public class Session {
this.isAdmin = false;
if (userName.equals("Admin"))
throw new MageException("User name already in use");
if (userName.length() > ConfigSettings.getInstance().getMaxUserNameLength()) {
throw new MageException(new StringBuilder("User name may not be longer than ").append(ConfigSettings.getInstance().getMaxUserNameLength()).append(" characters").toString());
}
if (userName.length() < ConfigSettings.getInstance().getMinUserNameLength()) {
throw new MageException(new StringBuilder("User name may not be shorter than ").append(ConfigSettings.getInstance().getMinUserNameLength()).append(" characters").toString());
}
Pattern p = Pattern.compile(ConfigSettings.getInstance().getUserNamePattern(), Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(userName);
if (m.find()) {
throw new MageException("User name includes not allowed characters: use a-z, A-Z and 0-9");
}
User user = UserManager.getInstance().createUser(userName, host);
if (user == null) { // user already exists
user = UserManager.getInstance().findUser(userName);

View file

@ -21,6 +21,9 @@
<xs:attribute name="port" type="xs:positiveInteger" use="required"/>
<xs:attribute name="maxGameThreads" type="xs:positiveInteger" use="required"/>
<xs:attribute name="maxSecondsIdle" type="xs:positiveInteger" use="required"/>
<xs:attribute name="minUserNameLength" type="xs:positiveInteger" use="required"/>
<xs:attribute name="maxUserNameLength" type="xs:positiveInteger" use="required"/>
<xs:attribute name="userNamePattern" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>

View file

@ -83,6 +83,18 @@ public class ConfigSettings {
return config.getServer().getMaxSecondsIdle().intValue();
}
public int getMinUserNameLength() {
return config.getServer().getMinUserNameLength().intValue();
}
public int getMaxUserNameLength() {
return config.getServer().getMaxUserNameLength().intValue();
}
public String getUserNamePattern() {
return config.getServer().getUserNamePattern();
}
public List<Plugin> getPlayerTypes() {
return config.getPlayerTypes().getPlayerType();
}

View file

@ -21,6 +21,9 @@
<xs:attribute name="port" type="xs:positiveInteger" use="required"/>
<xs:attribute name="maxGameThreads" type="xs:positiveInteger" use="required"/>
<xs:attribute name="maxSecondsIdle" type="xs:positiveInteger" use="required"/>
<xs:attribute name="minUserNameLength" type="xs:positiveInteger" use="required"/>
<xs:attribute name="maxUserNameLength" type="xs:positiveInteger" use="required"/>
<xs:attribute name="userNamePattern" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>