package votorola.a.web; // Copyright 2008, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.sql.SQLException; import org.apache.wicket.*; import org.apache.wicket.protocol.http.*; import votorola._.*; import votorola.a.*; import votorola.a.election.district.*; import votorola.a.register.*; import votorola.a.voter.*; import votorola.a.web.authen.*; import votorola.g.lang.*; import votorola.g.logging.*; /** A user session in the Web-based voter interface. */ public final @ThreadSafe class VSession extends WebSession implements ElectoralSubserver.UserSession { /** Constructs a VSession. */ VSession( Request request, VApplication application ) { super( request ); if( VApplication.TEST_USER_EMAIL != null ) { try { final Register register = application.subserverRun().register(); setUser( new RegistrationC( VApplication.TEST_USER_EMAIL, register.voterInputTable() ), register.getListNode( /*list ref*/null, VApplication.TEST_USER_EMAIL )); } catch( Exception x ) { throw VotorolaRuntimeException.castOrWrapped( x ); } // not much expected } } // ------------------------------------------------------------------------------------ /** Returns the voter's name; or, failing that, her email address. */ public static String displayName( Registration voter ) { String name = voter.getName(); if( name == null ) name = voter.voterEmail(); return name; } /** The registration identifying the logged in user; or null, * if the user is not logged in. * * @see #userEmail() * @see #setUser(Registration,ListNode) * @see votorola.a.web.authen.WP_Login */ public Registration getUser() { return user; } private volatile Registration user; /** Sets a registration and list node to identify the logged in user. * * @param user the user's registration, or null if the user is logged out * @param userListNode a read-only copy of the user's list node (do not write * to it); or null, if the user either is logged out, or has no list node * * @see #getUser() */ public void setUser( Registration user, ListNode userListNode ) // potential BUG, need atomic setter/getter of all user fields (registration, list node), so they are mutually consistent for getters { if( user == null ) assert userListNode == null; else { assert userListNode != null; if( !user.voterEmail().equals( userEmail() )) LoggerX.i(getClass()).fine( "user login: " + user.voterEmail() ); } this.user = user; this.userListNode = userListNode; dirty(); // per Session API } /** The list node of the logged in user; or null, if the user is not logged in. * * @see #getUser() * @see #setUser(Registration,ListNode) */ public ListNode getUserListNode() { return userListNode; } private volatile ListNode userListNode; /** A placeholder registration for nobody. Its values are at default. */ public static final Registration NOBODY = new Registration1( "nobody-votorola@zelea.com" ); /** Returns the session scope for instances of WP_List. */ public WP_List.SessionScope scopeList() { return scopeList; } private final WP_List.SessionScope scopeList = new WP_List.SessionScope( VSession.this ); /** Returns the session scope for instances of NavBar. */ NavBar.SessionScope scopeNavBar() { return scopeNavBar; } private final NavBar.SessionScope scopeNavBar = new NavBar.SessionScope( VSession.this ); /** Returns the session scope for instances of WP_OpenIDReturn. */ public WP_OpenIDReturn.SessionScope scopeOpenIDReturn() { return scopeOpenIDReturn; } private final WP_OpenIDReturn.SessionScope scopeOpenIDReturn = new WP_OpenIDReturn.SessionScope( VSession.this ); /** Returns the session scope for instances of RegionalPathMP. */ public RegionalPathMP.SessionScope scopeRegionalPathMP() { return scopeRegionalPathMP; } private final RegionalPathMP.SessionScope scopeRegionalPathMP = new RegionalPathMP.SessionScope( VSession.this ); /** Returns the session scope for instances of WC_VoterNavigator. */ public WC_VoterNavigator.SessionScope scopeVoterNavigator() { return scopeVoterNavigator; } private final WC_VoterNavigator.SessionScope scopeVoterNavigator = new WC_VoterNavigator.SessionScope( VSession.this ); /** Returns the {@linkplain #getUser() user} registration, if it is non-null; * otherwise {@linkplain #NOBODY NOBODY}. This is just a convenience, shorthand * method. */ public Registration userOrNobody() { Registration r = user; // snapshot copy, for atomic test/return if( r == null ) r = NOBODY; return r; } /** @see #getApplication() */ public VApplication vApplication() { return (VApplication)getApplication(); } // rather than override getApplication(), per VRequestCycle.vApplication() // - E l e c t o r a l - S u b s e r v e r . U s e r - S e s s i o n ------------------ /** @see #getUser() */ public String userEmail() { Registration u = user; // snapshot copy, for atomic test/call return u == null? null: u.voterEmail(); } /** @see #getUserListNode() */ public int userTrustLevel() { return userListNode.trustLevel(); } }