Changeset c09e0ade840ad0e35f7951d03b2345b8e458d93d
- Timestamp:
- 05/18/08 14:13:11
(5 months ago)
- Author:
- Christopher Jung <bktheg@web.de>
- git-committer:
- Christopher Jung <bktheg@web.de> 1211112791 +0200
- git-parent:
[aef156152991f65fcb92f36f5cad0fc2c6a704d1]
- git-author:
- Christopher Jung <bktheg@web.de> 1211112791 +0200
- Message:
[ref] Sessionverwaltung auf Java-Sessions umgestellt
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| re65784f |
rc09e0ad |
|
| 35 | 35 | `knownItems` text NOT NULL default '', |
|---|
| 36 | 36 | `version` int(10) unsigned not null default '0', |
|---|
| | 37 | `blocked` tinyint(1) unsigned not null default '0', |
|---|
| 37 | 38 | PRIMARY KEY (`id`), |
|---|
| 38 | 39 | KEY `ally` (`ally`), |
|---|
| r29e34b0 |
rc09e0ad |
|
| 501 | 501 | alter table logging modify type varchar(15) not null; |
|---|
| 502 | 502 | ]]></update> |
|---|
| | 503 | <update type="structure" datum="2008-05-17"><![CDATA[ |
|---|
| | 504 | alter table users add blocked tinyint(1) unsigned not null default '0'; |
|---|
| | 505 | ]]></update> |
|---|
| 503 | 506 | </updates> |
|---|
| r47cd40b |
rc09e0ad |
|
| 1 | 1 | net.driftingsouls.ds2.server.user.authentication.VacationCheckLoginEventListener |
|---|
| | 2 | net.driftingsouls.ds2.server.user.authentication.TickBlockLoginEventListener |
|---|
| r28b2039 |
rc09e0ad |
|
| 27 | 27 | import net.driftingsouls.ds2.server.framework.Context; |
|---|
| 28 | 28 | import net.driftingsouls.ds2.server.framework.ContextInstance; |
|---|
| | 29 | import net.driftingsouls.ds2.server.framework.ContextMap; |
|---|
| 29 | 30 | import net.driftingsouls.ds2.server.scripting.ScriptParserContext; |
|---|
| 30 | 31 | |
|---|
| … | … | |
| 33 | 34 | * @author Christopher Jung |
|---|
| 34 | 35 | */ |
|---|
| 35 | | @ContextInstance(ContextInstance.Type.SINGLETON) |
|---|
| | 36 | @ContextInstance(ContextInstance.Scope.REQUEST) |
|---|
| 36 | 37 | public class ContextCommon { |
|---|
| 37 | 38 | private Context context = null; |
|---|
| … | … | |
| 39 | 40 | /** |
|---|
| 40 | 41 | * Konstruktur - Wird vom Kontext aufgerufen |
|---|
| 41 | | * @param context Der Kontext, an den die Instanz gebunden werden soll |
|---|
| 42 | 42 | */ |
|---|
| 43 | | public ContextCommon(Context context) { |
|---|
| 44 | | this.context = context; |
|---|
| | 43 | public ContextCommon() { |
|---|
| | 44 | this.context = ContextMap.getContext(); |
|---|
| 45 | 45 | } |
|---|
| 46 | 46 | |
|---|
| r65e6cfe |
rc09e0ad |
|
| 193 | 193 | private int lostShips; |
|---|
| 194 | 194 | private String knownItems; |
|---|
| | 195 | private boolean blocked = false; |
|---|
| 195 | 196 | |
|---|
| 196 | 197 | @Transient |
|---|
| … | … | |
| 1006 | 1007 | this.wait4vac = value; |
|---|
| 1007 | 1008 | } |
|---|
| | 1009 | |
|---|
| | 1010 | /** |
|---|
| | 1011 | * Gibt zurueck, ob der User wegen einer Tickberechnung kurzzeitig blockiert wird |
|---|
| | 1012 | * @return <code>true</code>, falls er geblockt wird |
|---|
| | 1013 | */ |
|---|
| | 1014 | public boolean isBlocked() { |
|---|
| | 1015 | return this.blocked; |
|---|
| | 1016 | } |
|---|
| | 1017 | |
|---|
| | 1018 | /** |
|---|
| | 1019 | * Setzt, ob der User wegen einer Tickberechnung kurzzeitig geblockt wird |
|---|
| | 1020 | * @param blocked <code>true</code>, falls er geblockt wird |
|---|
| | 1021 | */ |
|---|
| | 1022 | public void setBlocked(boolean blocked) { |
|---|
| | 1023 | this.blocked = blocked; |
|---|
| | 1024 | } |
|---|
| 1008 | 1025 | } |
|---|
| r5dc487d |
rc09e0ad |
|
| 19 | 19 | package net.driftingsouls.ds2.server.framework; |
|---|
| 20 | 20 | |
|---|
| 21 | | import java.lang.reflect.Constructor; |
|---|
| 22 | 21 | import java.util.ArrayList; |
|---|
| 23 | 22 | import java.util.HashMap; |
|---|
| … | … | |
| 185 | 184 | @SuppressWarnings("unchecked") |
|---|
| 186 | 185 | public <T> T get(Class<T> cls) { |
|---|
| | 186 | if( !cls.isAnnotationPresent(ContextInstance.class) ) { |
|---|
| | 187 | LOG.fatal("ContextInstance Annotation not present: "+cls.getName()); |
|---|
| | 188 | return null; |
|---|
| | 189 | } |
|---|
| | 190 | |
|---|
| | 191 | ContextInstance.Scope scope = cls.getAnnotation(ContextInstance.class).value(); |
|---|
| | 192 | |
|---|
| 187 | 193 | /* TODO: Exceptions? */ |
|---|
| 188 | | if( !contextSingletons.containsKey(cls) ) { |
|---|
| 189 | | if( !cls.isAnnotationPresent(ContextInstance.class) ) { |
|---|
| 190 | | LOG.fatal("ContextInstance Annotation not present: "+cls.getName()); |
|---|
| 191 | | return null; |
|---|
| 192 | | } |
|---|
| 193 | | if( cls.getAnnotation(ContextInstance.class).value() != ContextInstance.Type.SINGLETON ) { |
|---|
| 194 | | LOG.fatal("Context-Class is not a singleton: "+cls.getName()); |
|---|
| 195 | | return null; |
|---|
| 196 | | } |
|---|
| 197 | | try { |
|---|
| 198 | | Constructor<T> cons = cls.getConstructor(Context.class); |
|---|
| 199 | | contextSingletons.put(cls, cons.newInstance(this)); |
|---|
| 200 | | } |
|---|
| 201 | | catch( Exception e ) { |
|---|
| 202 | | LOG.error(e,e); |
|---|
| 203 | | return null; |
|---|
| 204 | | } |
|---|
| 205 | | } |
|---|
| 206 | | |
|---|
| 207 | | return (T)contextSingletons.get(cls); |
|---|
| | 194 | if( scope == ContextInstance.Scope.REQUEST ) { |
|---|
| | 195 | if( !contextSingletons.containsKey(cls) ) { |
|---|
| | 196 | try { |
|---|
| | 197 | contextSingletons.put(cls, cls.newInstance()); |
|---|
| | 198 | } |
|---|
| | 199 | catch( Exception e ) { |
|---|
| | 200 | LOG.error(e,e); |
|---|
| | 201 | return null; |
|---|
| | 202 | } |
|---|
| | 203 | } |
|---|
| | 204 | |
|---|
| | 205 | return (T)contextSingletons.get(cls); |
|---|
| | 206 | } |
|---|
| | 207 | |
|---|
| | 208 | return this.request.getFromSession(cls); |
|---|
| 208 | 209 | } |
|---|
| 209 | 210 | |
|---|
| … | … | |
| 241 | 242 | this.listener.add(listener); |
|---|
| 242 | 243 | } |
|---|
| | 244 | |
|---|
| | 245 | public void remove(Class<?> cls) { |
|---|
| | 246 | if( !cls.isAnnotationPresent(ContextInstance.class) ) { |
|---|
| | 247 | LOG.fatal("ContextInstance Annotation not present: "+cls.getName()); |
|---|
| | 248 | return; |
|---|
| | 249 | } |
|---|
| | 250 | |
|---|
| | 251 | ContextInstance.Scope scope = cls.getAnnotation(ContextInstance.class).value(); |
|---|
| | 252 | if( scope == ContextInstance.Scope.REQUEST ) { |
|---|
| | 253 | this.contextSingletons.remove(cls); |
|---|
| | 254 | } |
|---|
| | 255 | else { |
|---|
| | 256 | this.request.removeFromSession(cls); |
|---|
| | 257 | } |
|---|
| | 258 | } |
|---|
| 243 | 259 | } |
|---|
| r65e6cfe |
rc09e0ad |
|
| 98 | 98 | /** |
|---|
| 99 | 99 | * Fuegt dem Benutzer weitere Sessiondaten hinzu |
|---|
| 100 | | * @param sessiondata Die Sessiondaten |
|---|
| 101 | | */ |
|---|
| 102 | | public void setSessionData(Session sessiondata) { |
|---|
| 103 | | if( (sessiondata != null) && !sessiondata.getUseGfxPak() ) { |
|---|
| | 100 | * @param useGfxPak <code>true</code>, falls ein Grafikpak genutzt werden soll |
|---|
| | 101 | */ |
|---|
| | 102 | public void setSessionData(boolean useGfxPak) { |
|---|
| | 103 | if( !useGfxPak ) { |
|---|
| 104 | 104 | forceDefaultImgPath = true; |
|---|
| 105 | 105 | } |
|---|
| r283e6c7 |
rc09e0ad |
|
| 29 | 29 | |
|---|
| 30 | 30 | import org.apache.commons.fileupload.FileItem; |
|---|
| | 31 | import org.apache.commons.logging.Log; |
|---|
| | 32 | import org.apache.commons.logging.LogFactory; |
|---|
| 31 | 33 | |
|---|
| 32 | 34 | /** |
|---|
| … | … | |
| 36 | 38 | */ |
|---|
| 37 | 39 | public class CmdLineRequest implements Request { |
|---|
| | 40 | private Log log = LogFactory.getLog(CmdLineRequest.class); |
|---|
| | 41 | |
|---|
| 38 | 42 | private Map<String,String> params = new HashMap<String,String>(); |
|---|
| 39 | 43 | |
|---|
| … | … | |
| 131 | 135 | } |
|---|
| 132 | 136 | |
|---|
| | 137 | public <T> T getFromSession(Class<T> cls) { |
|---|
| | 138 | log.error("getFromSession not supported"); |
|---|
| | 139 | |
|---|
| | 140 | return null; |
|---|
| | 141 | } |
|---|
| | 142 | |
|---|
| | 143 | public void removeFromSession(Class<?> cls) { |
|---|
| | 144 | log.error("removeFromSession not supported"); |
|---|
| | 145 | } |
|---|
| | 146 | |
|---|
| 133 | 147 | } |
|---|
| r9ee106f |
rc09e0ad |
|
| 42 | 42 | * @return Eine Database-Instanz |
|---|
| 43 | 43 | */ |
|---|
| 44 | | public abstract Database getDatabase(); |
|---|
| | 44 | public Database getDatabase(); |
|---|
| 45 | 45 | |
|---|
| 46 | 46 | /** |
|---|
| … | … | |
| 48 | 48 | * @return Die DB-Session |
|---|
| 49 | 49 | */ |
|---|
| 50 | | public abstract org.hibernate.Session getDB(); |
|---|
| | 50 | public org.hibernate.Session getDB(); |
|---|
| 51 | 51 | |
|---|
| 52 | 52 | /** |
|---|
| … | … | |
| 72 | 72 | * @return Eine Liste mit dem Ergebnis |
|---|
| 73 | 73 | */ |
|---|
| 74 | | public abstract <T> List<T> query(String query, Class<T> classType); |
|---|
| | 74 | public <T> List<T> query(String query, Class<T> classType); |
|---|
| 75 | 75 | |
|---|
| 76 | 76 | /** |
|---|
| … | … | |
| 79 | 79 | * @return Das zum gerade aktiven User gehoerende User-Objekt |
|---|
| 80 | 80 | */ |
|---|
| 81 | | public abstract BasicUser getActiveUser(); |
|---|
| | 81 | public BasicUser getActiveUser(); |
|---|
| 82 | 82 | |
|---|
| 83 | 83 | /** |
|---|
| … | … | |
| 86 | 86 | * @param user Der neue aktive User |
|---|
| 87 | 87 | */ |
|---|
| 88 | | public abstract void setActiveUser(BasicUser user); |
|---|
| | 88 | public void setActiveUser(BasicUser user); |
|---|
| 89 | 89 | |
|---|
| 90 | 90 | /** |
|---|
| … | … | |
| 93 | 93 | * @param error Die Beschreibung des Fehlers |
|---|
| 94 | 94 | */ |
|---|
| 95 | | public abstract void addError(String error); |
|---|
| | 95 | public void addError(String error); |
|---|
| 96 | 96 | |
|---|
| 97 | 97 | /** |
|---|
| … | … | |
| 101 | 101 | * @param link Die Ausweich-URL |
|---|
| 102 | 102 | */ |
|---|
| 103 | | public abstract void addError(String error, String link); |
|---|
| | 103 | public void addError(String error, String link); |
|---|
| 104 | 104 | |
|---|
| 105 | 105 | /** |
|---|
| … | … | |
| 111 | 111 | * @see #addError(String) |
|---|
| 112 | 112 | */ |
|---|
| 113 | | public abstract Error getLastError(); |
|---|
| | 113 | public Error getLastError(); |
|---|
| 114 | 114 | |
|---|
| 115 | 115 | /** |
|---|
| … | … | |
| 118 | 118 | * @return Eine Liste aller Fehlerbeschreibungen |
|---|
| 119 | 119 | */ |
|---|
| 120 | | public abstract Error[] getErrorList(); |
|---|
| | 120 | public Error[] getErrorList(); |
|---|
| 121 | 121 | |
|---|
| 122 | 122 | /** |
|---|
| … | … | |
| 124 | 124 | * @return Die Request des Aufrufs |
|---|
| 125 | 125 | */ |
|---|
| 126 | | public abstract Request getRequest(); |
|---|
| | 126 | public Request getRequest(); |
|---|
| 127 | 127 | |
|---|
| 128 | 128 | /** |
|---|
| … | … | |
| 130 | 130 | * @return Die Response des Aufrufs |
|---|
| 131 | 131 | */ |
|---|
| 132 | | public abstract Response getResponse(); |
|---|
| | 132 | public Response getResponse(); |
|---|
| 133 | 133 | |
|---|
| 134 | 134 | /** |
|---|
| … | … | |
| 136 | 136 | * @param response Das Response-Objekt |
|---|
| 137 | 137 | */ |
|---|
| 138 | | public abstract void setResponse(Response response); |
|---|
| | 138 | public void setResponse(Response response); |
|---|
| 139 | 139 | |
|---|
| 140 | 140 | /** |
|---|
| 141 | | * Liefert eine pro Kontext einmalige Instanz einer Klasse. |
|---|
| 142 | | * Sollte keine Instanz dieser Klasse im Kontext vorhanden sein, |
|---|
| | 141 | * Liefert eine unter einem bestimmten Scope einmalige Instanz einer Klasse. |
|---|
| | 142 | * Sollte keine Instanz dieser Klasse im Scope vorhanden sein, |
|---|
| 143 | 143 | * wird dieses erstellt. |
|---|
| 144 | | * Hinweis: Die Klasse muss die Annotation ContextInstance besitzen |
|---|
| 145 | | * und auf den Wert SINGLETON gesetzt haben |
|---|
| 146 | 144 | * |
|---|
| 147 | | * @param <T> Eine Klasse, welche Kontextbezogen arbeiten kann |
|---|
| | 145 | * @param <T> Eine Klasse |
|---|
| 148 | 146 | * @param cls Die gewuenschte Klasse |
|---|
| 149 | 147 | * @return Eine Instanz der Klase |
|---|
| 150 | 148 | */ |
|---|
| 151 | | public abstract <T> T get(Class<T> cls); |
|---|
| | 149 | public <T> T get(Class<T> cls); |
|---|
| | 150 | |
|---|
| | 151 | /** |
|---|
| | 152 | * Entfernt die unter einem bestimmten Scope gueltige Instanz dieser Klasse |
|---|
| | 153 | * @param cls Die Klasse |
|---|
| | 154 | */ |
|---|
| | 155 | public void remove(Class<?> cls); |
|---|
| 152 | 156 | |
|---|
| 153 | 157 | /** |
|---|
| … | … | |
| 158 | 162 | * @return Die Session-ID oder ein leerer String |
|---|
| 159 | 163 | */ |
|---|
| 160 | | public abstract String getSession(); |
|---|
| | 164 | public String getSession(); |
|---|
| 161 | 165 | |
|---|
| 162 | 166 | /** |
|---|
| … | … | |
| 166 | 170 | * @param value Der neue Wert der Variablen |
|---|
| 167 | 171 | */ |
|---|
| 168 | | public abstract void putVariable(Class<?> cls, String varname, Object value); |
|---|
| | 172 | public void putVariable(Class<?> cls, String varname, Object value); |
|---|
| 169 | 173 | |
|---|
| 170 | 174 | /** |
|---|
| … | … | |
| 174 | 178 | * @return Die Variable oder <code>null</code>, falls die Variable nicht existiert |
|---|
| 175 | 179 | */ |
|---|
| 176 | | public abstract Object getVariable(Class<?> cls, String varname); |
|---|
| | 180 | public Object getVariable(Class<?> cls, String varname); |
|---|
| 177 | 181 | |
|---|
| 178 | 182 | /** |
|---|
| … | … | |
| 181 | 185 | * |
|---|
| 182 | 186 | */ |
|---|
| 183 | | public abstract void revalidate(); |
|---|
| | 187 | public void revalidate(); |
|---|
| 184 | 188 | |
|---|
| 185 | 189 | /** |
|---|
| … | … | |
| 188 | 192 | * @param listener Der Listener |
|---|
| 189 | 193 | */ |
|---|
| 190 | | public abstract void registerListener(ContextListener listener); |
|---|
| | 194 | public void registerListener(ContextListener listener); |
|---|
| 191 | 195 | } |
|---|
| rae09dfe |
rc09e0ad |
|
| 35 | 35 | public @interface ContextInstance { |
|---|
| 36 | 36 | /** |
|---|
| 37 | | * Der Typ der kontextgebundenen Klasse |
|---|
| | 37 | * Der Scope der kontextgebundenen Klasse |
|---|
| 38 | 38 | * @author Christopher Jung |
|---|
| 39 | 39 | * |
|---|
| 40 | 40 | */ |
|---|
| 41 | | enum Type { |
|---|
| | 41 | enum Scope { |
|---|
| 42 | 42 | /** |
|---|
| 43 | | * Die Klasse darf lediglich einmal pro Kontext vorkommen |
|---|
| | 43 | * Die Klasse darf lediglich pro Request einmal vorkommen. |
|---|
| | 44 | * Verschiedene Requests muessen unterschiedliche Instanzen haben |
|---|
| 44 | 45 | */ |
|---|
| 45 | | SINGLETON |
|---|
| | 46 | REQUEST, |
|---|
| | 47 | /** |
|---|
| | 48 | * Die Klasse darf lediglich einmal pro Session vorkommen. |
|---|
| | 49 | * Verschiedene Sessions muessen unterschiedliche Instanzen haben |
|---|
| | 50 | */ |
|---|
| | 51 | SESSION |
|---|
| 46 | 52 | }; |
|---|
| 47 | 53 | |
|---|
| 48 | 54 | /** |
|---|
| 49 | | * Gibt den Typ der kontextgebundenen Klasse zurueck |
|---|
| 50 | | * @return der Typ |
|---|
| | 55 | * Gibt den Scope der kontextgebundenen Klasse zurueck |
|---|
| | 56 | * @return der Scope |
|---|
| 51 | 57 | */ |
|---|
| 52 | | Type value(); |
|---|
| | 58 | Scope value(); |
|---|
| 53 | 59 | } |
|---|
| r5dc487d |
rc09e0ad |
|
| 20 | 20 | |
|---|
| 21 | 21 | import net.driftingsouls.ds2.server.framework.BasicUser; |
|---|
| 22 | | import net.driftingsouls.ds2.server.framework.Session; |
|---|
| 23 | 22 | |
|---|
| 24 | 23 | /** |
|---|
| … | … | |
| 34 | 33 | * @param password Das Passwort im Klartext |
|---|
| 35 | 34 | * @param useGfxPak <code>true</code>, falls ein evt angegebenes Grafikpak genutzt werden soll |
|---|
| 36 | | * @return Session Die neu erstellte Session |
|---|
| | 35 | * @return Der Account des eingeloggten Benutzers |
|---|
| 37 | 36 | * @throws AuthenticationException Falls der Loginvorgang nicht erfolgreich ist |
|---|
| 38 | 37 | */ |
|---|
| 39 | | public Session login(String username, String password, boolean useGfxPak) |
|---|
| | 38 | public BasicUser login(String username, String password, boolean useGfxPak) |
|---|
| 40 | 39 | throws AuthenticationException; |
|---|
| 41 | 40 | |
|---|
| … | … | |
| 50 | 49 | * @param user Der Account, in den der Admin eingeloggt werden soll |
|---|
| 51 | 50 | * @param attach <code>true</code>, falls die Adminrechte an die neue Session "angeklebt" werden sollen |
|---|
| 52 | | * @return Die neu erstellte Session |
|---|
| | 51 | * @return Der Account des eingeloggten Benutzers |
|---|
| 53 | 52 | * @throws AuthenticationException Falls der Loginvorgang nicht moeglich ist |
|---|
| 54 | 53 | */ |
|---|
| 55 | | public Session adminLogin(BasicUser user, boolean attach) throws AuthenticationException; |
|---|
| | 54 | public BasicUser adminLogin(BasicUser user, boolean attach) throws AuthenticationException; |
|---|
| 56 | 55 | |
|---|
| 57 | 56 | /** |
|---|
| r5dc487d |
rc09e0ad |
|
| 26 | 26 | import net.driftingsouls.ds2.server.framework.Context; |
|---|
| 27 | 27 | import net.driftingsouls.ds2.server.framework.ContextMap; |
|---|
| 28 | | import net.driftingsouls.ds2.server.framework.Session; |
|---|
| 29 | 28 | import net.driftingsouls.ds2.server.framework.db.Database; |
|---|
| 30 | 29 | import net.driftingsouls.ds2.server.framework.pipeline.Request; |
|---|
| 31 | 30 | |
|---|
| 32 | | import org.apache.commons.lang.math.RandomUtils; |
|---|
| 33 | 31 | import org.apache.commons.logging.Log; |
|---|
| 34 | 32 | import org.apache.commons.logging.LogFactory; |
|---|
| … | … | |
| 41 | 39 | public class DefaultAuthenticationManager implements AuthenticationManager { |
|---|
| 42 | 40 | private static final Log log = LogFactory.getLog(DefaultAuthenticationManager.class); |
|---|
| 43 | | private static final ServiceLoader<LoginEventListener> listenerList = ServiceLoader.load(LoginEventListener.class); |
|---|
| | 41 | private static final ServiceLoader<LoginEventListener> loginListenerList = ServiceLoader.load(LoginEventListener.class); |
|---|
| | 42 | private static final ServiceLoader<AuthenticateEventListener> authListenerList = ServiceLoader.load(AuthenticateEventListener.class); |
|---|
| 44 | 43 | |
|---|
| 45 | | private static final String[] actionBlockingPhrases = { |
|---|
| 46 | | "Immer mit der Ruhe!\nDer arme Server ist schon total erschlöpft.\nGönn ihm mal ein oder zwei Minuten Pause :)", |
|---|
| 47 | | "Laaaaangsaaaaam!\nDeine Maus hat ja schon nen Krampf von dem vielen geklicke bekommen!\nMach mal eine kleine Pause und gönn ihr die Entspannung...", |
|---|
| 48 | | "In der Ruhe liegt die Kraft!\nNur der arme Server hat wegen deiner Klickerei keine Ruhe und somit auch keine Kraft mehr. Lass ihm eine kleine Verschnaufpause und mach dann weiter.", |
|---|
| 49 | | "Vorsicht mein junger Padawan!\nBei diesem Klicktempo könntest du aus versehen auf den Selbstzerstörungsknopf drücken! Mach eine kurze Pause und klicke dann langsam und gewissenhaft weiter..." |
|---|
| 50 | | }; |
|---|
| 51 | | |
|---|
| 52 | | public Session login(String username, String password, boolean useGfxPak) throws AuthenticationException { |
|---|
| | 44 | public BasicUser login(String username, String password, boolean useGfxPak) throws AuthenticationException { |
|---|
| 53 | 45 | Context context = ContextMap.getContext(); |
|---|
| 54 | 46 | org.hibernate.Session db = context.getDB(); |
|---|
| … | … | |
| 85 | 77 | } |
|---|
| 86 | 78 | |
|---|
| 87 | | for( LoginEventListener listener : listenerList ) { |
|---|
| | 79 | for( LoginEventListener listener : loginListenerList ) { |
|---|
| 88 | 80 | listener.onLogin(user); |
|---|
| 89 | 81 | } |
|---|
| 90 | | |
|---|
| 91 | | checkTickInProgress(context, user); |
|---|
| 92 | 82 | |
|---|
| | 83 | log.info("Login "+user.getId()); |
|---|
| 93 | 84 | Common.writeLog("login.log",Common.date( "j.m.Y H:i:s")+": <"+request.getRemoteAddress()+"> ("+user.getId()+") <"+user.getUN()+"> Login von Browser <"+request.getUserAgent()+">\n"); |
|---|
| 94 | 85 | |
|---|
| 95 | | return createNewSession(context, user, useGfxPak); |
|---|
| 96 | | } |
|---|
| 97 | | |
|---|
| 98 | | private void checkTickInProgress(Context context, BasicUser user) throws TickInProgressException { |
|---|
| 99 | | org.hibernate.Session db = context.getDB(); |
|---|
| | 86 | JavaSession jsession = context.get(JavaSession.class); |
|---|
| | 87 | jsession.setUser(user); |
|---|
| | 88 | jsession.setUseGfxPak(useGfxPak); |
|---|
| | 89 | jsession.setIP("<"+context.getRequest().getRemoteAddress()+">"); |
|---|
| 100 | 90 | |
|---|
| 101 | | Session session = (Session)db.createQuery("from Session where user=? and tick!=0") |
|---|
| 102 | | .setEntity(0, user) |
|---|
| 103 | | .uniqueResult(); |
|---|
| 104 | | |
|---|
| 105 | | if( session != null ) { |
|---|
| 106 | | throw new TickInProgressException(); |
|---|
| 107 | | } |
|---|
| 108 | | } |
|---|
| 109 | | |
|---|
| 110 | | private Session createNewSession(Context context, BasicUser user, boolean useGfxPak) { |
|---|
| 111 | | org.hibernate.Session db = context.getDB(); |
|---|
| 112 | | |
|---|
| 113 | | db.createQuery("delete from Session where user=? and attach is null") |
|---|
| 114 | | .setEntity(0, user) |
|---|
| 115 | | .executeUpdate(); |
|---|
| 116 | | |
|---|
| 117 | | Session session = new Session(user); |
|---|
| 118 | | session.setIP("<"+context.getRequest().getRemoteAddress()+">"); |
|---|
| 119 | | session.setUseGfxPak(useGfxPak); |
|---|
| 120 | | db.persist(session); |
|---|
| 121 | | |
|---|
| 122 | | context.commit(); |
|---|
| 123 | | return session; |
|---|
| | 91 | return user; |
|---|
| 124 | 92 | } |
|---|
| 125 | 93 | |
|---|
| … | … | |
| 135 | 103 | public void logout() { |
|---|
| 136 | 104 | Context context = ContextMap.getContext(); |
|---|
| 137 | | |
|---|
| 138 | | context.getDB().createQuery("delete from Session where session= :sess or attach= :sess") |
|---|
| 139 | | .setString("sess", context.getSession()) |
|---|
| 140 | | .executeUpdate(); |
|---|
| | 105 | context.remove(JavaSession.class); |
|---|
| 141 | 106 | } |
|---|
| 142 | 107 | |
|---|
| 143 | | public Session adminLogin(BasicUser user, boolean attach) throws AuthenticationException { |
|---|
| | 108 | public BasicUser adminLogin(BasicUser user, boolean attach) throws AuthenticationException { |
|---|
| 144 | 109 | Context context = ContextMap.getContext(); |
|---|
| 145 | 110 | |
|---|
| 146 | | Session session = new Session(user); |
|---|
| 147 | | session.setIP("<"+context.getRequest().getRemoteAddress()+">"); |
|---|
| 148 | | session.setUseGfxPak(false); |
|---|
| | 111 | BasicUser oldUser = context.getActiveUser(); |
|---|
| | 112 | |
|---|
| | 113 | JavaSession jsession = context.get(JavaSession.class); |
|---|
| | 114 | jsession.setUser(user); |
|---|
| | 115 | jsession.setIP("<"+context.getRequest().getRemoteAddress()+">"); |
|---|
| | 116 | jsession.setUseGfxPak(false); |
|---|
| 149 | 117 | if( attach ) { |
|---|
| 150 | | session.setAttach(context.getSession()); |
|---|
| | 118 | jsession.setAttach(oldUser); |
|---|
| 151 | 119 | } |
|---|
| 152 | | context.getDB().save(session); |
|---|
| 153 | 120 | |
|---|
| 154 | | return session; |
|---|
| | 121 | return user; |
|---|
| 155 | 122 | } |
|---|
| 156 | 123 | |
|---|
| 157 | 124 | public void authenticateCurrentSession() { |
|---|
| 158 | 125 | Context context = ContextMap.getContext(); |
|---|
| | 126 | |
|---|
| | 127 | String errorurl = Configuration.getSetting("URL")+"ds?module=portal&action=login"; |
|---|
| 159 | 128 | |
|---|
| 160 | | Request request = context.getRequest(); |
|---|
| 161 | | org.hibernate.Session db = context.getDB(); |
|---|
| | 129 | JavaSession jsession = context.get(JavaSession.class); |
|---|
| 162 | 130 | |
|---|
| 163 | | String sess = request.getParameter("sess"); |
|---|
| 164 | | |
|---|
| 165 | | String errorurl = Configuration.getSetting("URL")+"ds?module=portal&action=login"; |
|---|
| 166 | | |
|---|
| 167 | | if( (sess == null) || sess.equals("") ) { |
|---|
| 168 | | return; |
|---|
| 169 | | } |
|---|
| 170 | | |
|---|
| 171 | | long time = Common.time(); |
|---|
| 172 | | Session sessdata = (Session)db.get(Session.class, sess); |
|---|
| 173 | | |
|---|
| 174 | | if( sessdata == null ) { |
|---|
| 175 | | context.addError( "Sie sind offenbar nicht eingeloggt", errorurl ); |
|---|
| 176 | | |
|---|
| | 131 | if( jsession == null || jsession.getUser() == null ) { |
|---|
| 177 | 132 | return; |
|---|
| 178 | 133 | } |
|---|
| 179 | 134 | |
|---|
| 180 | | if( sessdata.getTick() ) { |
|---|
| 181 | | context.addError( "Im Moment werden einige Tick-Berechnungen für sie durchgeführt. Bitte haben sie daher ein wenig Geduld", |
|---|
| 182 | | request.getRequestURL() + |
|---|
| 183 | | (request.getQueryString() != null ? "?" + request.getQueryString() : "") |
|---|
| 184 | | ); |
|---|
| 185 | | |
|---|
| 186 | | return; |
|---|
| 187 | | } |
|---|
| 188 | | |
|---|
| 189 | | BasicUser user = sessdata.getUser(); |
|---|
| 190 | | user.setSessionData(sessdata); |
|---|
| 191 | | if( !user.hasFlag(BasicUser.FLAG_DISABLE_IP_SESSIONS) && !sessdata.isValidIP(request.getRemoteAddress()) ) { |
|---|
| | 135 | BasicUser user = jsession.getUser(); |
|---|
| | 136 | user.setSessionData(jsession.getUseGfxPak()); |
|---|
| | 137 | if( !user.hasFlag(BasicUser.FLAG_DISABLE_IP_SESSIONS) && !jsession.isValidIP(context.getRequest().getRemoteAddress()) ) { |
|---|
| 192 | 138 | context.addError( "Diese Session ist einer anderen IP zugeordnet", errorurl ); |
|---|
| 193 | 139 | |
|---|
| 194 | 140 | return; |
|---|
| 195 | 141 | } |
|---|
| 196 | | |
|---|
| 197 | | if( !user.hasFlag(BasicUser.FLAG_DISABLE_AUTO_LOGOUT) && (Common.time() - sessdata.getLastAction() > Configuration.getIntSetting("AUTOLOGOUT_TIME")) ) { |
|---|
| 198 | | db.delete(sessdata); |
|---|
| 199 | | context.addError( "Diese Session ist bereits abgelaufen", errorurl ); |
|---|
| 200 | | |
|---|
| | 142 | |
|---|
| | 143 | try { |
|---|
| | 144 | for( AuthenticateEventListener listener : authListenerList ) { |
|---|
| | 145 | listener.onAuthenticate(user); |
|---|
| | 146 | } |
|---|
| | 147 | } |
|---|
| | 148 | catch( AuthenticationException e ) { |
|---|
| 201 | 149 | return; |
|---|
| 202 | | } |
|---|
| 203 | | |
|---|
| 204 | | final long oldtime = sessdata.getLastAction(); |
|---|
| 205 | | |
|---|
| 206 | | sessdata.setLastAction(time); |
|---|
| 207 | | context.commit(); |
|---|
| 208 | | |
|---|
| 209 | | if( !user.hasFlag(BasicUser.FLAG_NO_ACTION_BLOCKING) ) { |
|---|
| 210 | | // Alle 1.5 Sekunden Counter um 1 reduzieren, sofern mindestens 5 Sekunden Pause vorhanden waren |
|---|
| 211 | | int reduce = (int)((time - oldtime)/1.5); |
|---|
| 212 | | if( time < oldtime + 5 ) { |
|---|
| 213 | | reduce = -1; |
|---|
| 214 | | } |
|---|
| 215 | | int actioncounter = sessdata.getActionCounter()-reduce; |
|---|
| 216 | | if( actioncounter < 0 ) { |
|---|
| 217 | | actioncounter = 0; |
|---|
| 218 | | } |
|---|
| 219 | | |
|---|
| 220 | | if( reduce > 0 ) { |
|---|
| 221 | | final int value = sessdata.getActionCounter() - reduce; |
|---|
| 222 | | |
|---|
| 223 | | sessdata.setActionCounter(value > 0 ? value : 0); |
|---|
| 224 | | } |
|---|
| 225 | | else if( reduce < 0 ) { |
|---|
| 226 | | sessdata.setActionCounter(sessdata.getActionCounter()+1); |
|---|
| 227 | | } |
|---|
| 228 | | |
|---|
| 229 | | int sleep = -1; |
|---|
| 230 | | |
|---|
| 231 | | // Bei viel zu hoher Aktivitaet einfach die Ausfuehrung mit einem Fehler beenden |
|---|
| 232 | | if( actioncounter >= 35 ) { |
|---|
| 233 | | context.addError( actionBlockingPhrases[RandomUtils.nextInt(actionBlockingPhrases.length)], |
|---|
| 234 | | request.getRequestURL() + |
|---|
| 235 | | (request.getQueryString() != null ? "?" + request.getQueryString() : "") ); |
|---|
| 236 | | |
|---|
| 237 | | return; |
|---|
| 238 | | } |
|---|
| 239 | | // Bei hoher Aktivitaet stattdessen nur eine Pause einlegen |
|---|
| 240 | | else if( actioncounter > 10 ) { |
|---|
| 241 | | sleep = 100 * actioncounter; |
|---|
| 242 | | } |
|---|
| 243 | | |
|---|
| 244 | | if( sleep > 0 ) { |
|---|
| 245 | | try { |
|---|
| 246 | | Thread.sleep(2500); |
|---|
| 247 | | } |
|---|
| 248 | | catch( InterruptedException e ) { |
|---|
| 249 | | log.error(e,e); |
|---|
| 250 | | } |
|---|
| 251 | | } |
|---|
| 252 | 150 | } |
|---|
| 253 | 151 | |
|---|
| … | … | |
| 255 | 153 | user.setInactivity(0); |
|---|
| 256 | 154 | |
|---|
| 257 | | if( (sessdata.getAttach() != null) && !sessdata.getAttach().equals("-1") ) { |
|---|
| 258 | | Session attachSession = (Session)db.get(Session.class, sessdata.getAttach()); |
|---|
| 259 | | if( attachSession != null) { |
|---|
| 260 | | user.attachToUser(attachSession.getUser()); |
|---|
| 261 | | } |
|---|
| | 155 | if( jsession.getAttach() != null ) { |
|---|
| | 156 | user.attachToUser(jsession.getAttach()); |
|---|
| 262 | 157 | } |
|---|
| 263 | 158 | |
|---|
| r9fc4d87 |
rc09e0ad |
|
| 22 | 22 | import java.io.InputStream; |
|---|
| 23 | 23 | import java.io.UnsupportedEncodingException; |
|---|
| | 24 | import java.lang.reflect.Constructor; |
|---|
| | 25 | import java.lang.reflect.InvocationTargetException; |
|---|
| 24 | 26 | import java.util.ArrayList; |
|---|
| 25 | 27 | import java.util.HashMap; |
|---|
| … | … | |
| 28 | 30 | |
|---|
| 29 | 31 | import javax.servlet.http.HttpServletRequest; |
|---|
| | 32 | import javax.servlet.http.HttpSession; |
|---|
| 30 | 33 | |
|---|
| 31 | 34 | import net.driftingsouls.ds2.server.framework.Loggable; |
|---|
| … | … | |
| 184 | 187 | return result; |
|---|
| 185 | 188 | } |
|---|
| | 189 | |
|---|
| | 190 | @SuppressWarnings("unchecked") |
|---|
| | 191 | public <T> T getFromSession(Class<T> cls) { |
|---|
| | 192 | HttpSession session = this.request.getSession(false); |
|---|
| | 193 | |
|---|
| | 194 | if( session == null ) { |
|---|
| | 195 | return null; |
|---|
| | 196 | } |
|---|
| | 197 | Object obj = session.getAttribute(getClass().getName()+"#"+cls.getName()); |
|---|
| | 198 | if( obj == null ) { |
|---|
| | 199 | try { |
|---|
| | 200 | Constructor constr = cls.getConstructor(); |
|---|
| | 201 | constr.setAccessible(true); |
|---|
| | 202 | obj = constr.newInstance(); |
|---|
| | 203 | } |
|---|
| | 204 | catch( InstantiationException e ) { |
|---|
| | 205 | LOG.error("getFromSession for "+cls.getName()+" failed", e); |
|---|
| | 206 | return null; |
|---|
| | 207 | } |
|---|
| | 208 | catch( IllegalAccessException e ) { |
|---|
| | 209 | LOG.error("getFromSession for "+cls.getName()+" failed", e); |
|---|
| | 210 | return null; |
|---|
| | 211 | } |
|---|
| | 212 | catch( InvocationTargetException e ) { |
|---|
| | 213 | LOG.error("getFromSession for "+cls.getName()+" failed", e); |
|---|
| | 214 | return null; |
|---|
| | 215 | } |
|---|
| | 216 | catch( NoSuchMethodException e ) { |
|---|
| | 217 | LOG.error("getFromSession for "+cls.getName()+" failed", e); |
|---|
| | 218 | return null; |
|---|
| | 219 | } |
|---|
| | 220 | session.setAttribute(getClass().getName()+"#"+cls.getName(), obj); |
|---|
| | 221 | } |
|---|
| | 222 | if( cls.isInstance(obj) ) { |
|---|
| | 223 | return (T)obj; |
|---|
| | 224 | } |
|---|
| | 225 | LOG.error("getFromSession for "+cls.getName()+" failed - invalid type"); |
|---|
| | 226 | |
|---|
| | 227 | return null; |
|---|
| | 228 | } |
|---|
| | 229 | |
|---|
| | 230 | public void removeFromSession(Class<?> cls) { |
|---|
| | 231 | HttpSession session = this.request.getSession(false); |
|---|
| | 232 | |
|---|
| | 233 | if( session == null ) { |
|---|
| | 234 | return; |
|---|
| | 235 | }& |
|---|