blob: 7ce9b20b15d6ff579ca07479fbeb6a1fe679dc29 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>DefaultUserImpl.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Turbine</a> &gt; <a href="index.source.html" class="el_package">org.apache.turbine.om.security</a> &gt; <span class="el_source">DefaultUserImpl.java</span></div><h1>DefaultUserImpl.java</h1><pre class="source lang-java linenums">package org.apache.turbine.om.security;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import jakarta.servlet.http.HttpSessionBindingEvent;
import org.apache.fulcrum.security.model.turbine.entity.TurbineUser;
import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole;
import org.apache.fulcrum.security.util.DataBackendException;
import org.apache.turbine.services.TurbineServices;
import org.apache.turbine.services.security.SecurityService;
import org.apache.turbine.util.ObjectUtils;
/**
* This is the Default user implementation. It is a wrapper around
* a TurbineUser object
*
* @author &lt;a href=&quot;mailto:tv@apache.org&quot;&gt;Thomas Vandahl&lt;/a&gt;
* @version $Id: TorqueUser.java 1199856 2011-11-09 17:06:04Z tv $
*/
public class DefaultUserImpl implements User
{
/** Serial version */
private static final long serialVersionUID = -1866504873085624111L;
/** The date on which the user last accessed the application. */
<span class="fc" id="L50"> private Date lastAccessDate = null;</span>
/** This is data that will survive a servlet engine restart. */
<span class="fc" id="L53"> private Map&lt;String, Object&gt; permStorage = null;</span>
/** This is data that will not survive a servlet engine restart. */
<span class="fc" id="L56"> private Map&lt;String, Object&gt; tempStorage = null;</span>
/** The Fulcrum user instance to delegate to */
<span class="fc" id="L59"> private TurbineUser userDelegate = null;</span>
/**
* Constructor
*
* @param user the user object to wrap
*/
public DefaultUserImpl(TurbineUser user)
{
<span class="fc" id="L68"> super();</span>
<span class="fc" id="L69"> setUserDelegate( user );</span>
<span class="fc" id="L70"> setCreateDate(new Date());</span>
<span class="fc" id="L71"> tempStorage = new HashMap&lt;&gt;(10);</span>
<span class="fc" id="L72"> setHasLoggedIn(Boolean.FALSE);</span>
<span class="fc" id="L73"> }</span>
/**
* Implement this method if you wish to be notified when the User
* has been Bound to the session.
*
* @param hsbe Indication of value/session binding.
*/
@Override
public void valueBound(HttpSessionBindingEvent hsbe)
{
// Currently we have no need for this method.
<span class="nc" id="L85"> }</span>
/**
* Implement this method if you wish to be notified when the User
* has been Unbound from the session.
*
* @param hsbe Indication of value/session unbinding.
*/
@Override
public void valueUnbound(HttpSessionBindingEvent hsbe)
{
try
{
<span class="nc bnc" id="L98" title="All 2 branches missed."> if (hasLoggedIn())</span>
{
SecurityService securityService =
<span class="nc" id="L101"> (SecurityService)TurbineServices.getInstance()</span>
<span class="nc" id="L102"> .getService(SecurityService.SERVICE_NAME);</span>
<span class="nc" id="L103"> securityService.saveOnSessionUnbind(this);</span>
}
}
<span class="nc" id="L106"> catch (Exception e)</span>
{
//Log.error(&quot;TorqueUser.valueUnbound(): &quot; + e.getMessage(), e);
// To prevent messages being lost in case the logging system
// goes away before sessions get unbound on servlet container
// shutdown, print the stacktrace to the container's console.
<span class="nc" id="L113"> e.printStackTrace(System.out);</span>
<span class="nc" id="L114"> }</span>
<span class="nc" id="L115"> }</span>
/**
* Get the Name of the SecurityEntity.
*
* @return The Name of the SecurityEntity.
*/
@Override
public String getName()
{
<span class="nc" id="L125"> return userDelegate.getName();</span>
}
/**
* Sets the Name of the SecurityEntity.
*
* @param name
* Name of the SecurityEntity.
*/
@Override
public void setName(String name)
{
<span class="nc" id="L137"> userDelegate.setName(name);</span>
<span class="nc" id="L138"> }</span>
/**
* Get the Id of the SecurityEntity.
*
* @return The Id of the SecurityEntity.
*/
@Override
public Object getId()
{
<span class="nc" id="L148"> return userDelegate.getId();</span>
}
/**
* Sets the Id of the SecurityEntity.
*
* @param id
* The new Id of the SecurityEntity
*/
@Override
public void setId(Object id)
{
<span class="nc" id="L160"> userDelegate.setId(id);</span>
<span class="nc" id="L161"> }</span>
/**
* Returns the user's password. This method should not be used by
* the application directly, because it's meaning depends upon
* the implementation of UserManager that manages this particular
* user object. Some implementations will use this attribute for
* storing a password encrypted in some way, other will not use
* it at all, when user entered password is presented to some external
* authority (like NT domain controller) to validate it.
* See also {@link org.apache.turbine.services.security.UserManager#authenticate(User,String)}.
*
* @return A String with the password for the user.
*/
@Override
public String getPassword()
{
<span class="nc" id="L178"> return userDelegate.getPassword();</span>
}
/**
* Set password. Application should not use this method
* directly, see {@link #getPassword()}.
* See also {@link org.apache.turbine.services.security.UserManager#changePassword(User,String,String)}.
*
* @param password The new password.
*/
@Override
public void setPassword(String password)
{
<span class="nc" id="L191"> userDelegate.setPassword(password);</span>
<span class="nc" id="L192"> }</span>
/**
* Returns the first name for this user.
*
* @return A String with the user's first name.
*/
@Override
public String getFirstName()
{
<span class="nc" id="L202"> return userDelegate.getFirstName();</span>
}
/**
* Sets the first name for this user.
*
* @param firstName User's first name.
*/
@Override
public void setFirstName(String firstName)
{
<span class="nc" id="L213"> userDelegate.setFirstName(firstName);</span>
<span class="nc" id="L214"> }</span>
/**
* Returns the last name for this user.
*
* @return A String with the user's last name.
*/
@Override
public String getLastName()
{
<span class="nc" id="L224"> return userDelegate.getLastName();</span>
}
/**
* Sets the last name for this user.
*
* @param lastName User's last name.
*/
@Override
public void setLastName(String lastName)
{
<span class="nc" id="L235"> userDelegate.setLastName(lastName);</span>
<span class="nc" id="L236"> }</span>
/**
* Returns the email address for this user.
*
* @return A String with the user's email address.
*/
@Override
public String getEmail()
{
<span class="nc" id="L246"> return userDelegate.getEmail();</span>
}
/**
* Sets the email address.
*
* @param address The email address.
*/
@Override
public void setEmail(String address)
{
<span class="nc" id="L257"> userDelegate.setEmail(address);</span>
<span class="nc" id="L258"> }</span>
/**
* Returns the value of the objectdata for this user.
* Objectdata is a storage area used
* to store the permanent storage table from the User
* object.
*
* @return The bytes in the objectdata for this user
*/
@Override
public byte[] getObjectdata()
{
<span class="fc" id="L271"> return userDelegate.getObjectdata();</span>
}
/**
* Sets the value of the objectdata for the user
*
* @param objectdata The new permanent storage for the user
*/
@Override
public void setObjectdata(byte[] objectdata)
{
<span class="nc" id="L282"> userDelegate.setObjectdata(objectdata);</span>
<span class="nc" id="L283"> }</span>
/**
* Get the User/Group/Role set associated with this entity
*
* @return a set of User/Group/Role relations
* @throws DataBackendException if there was an error accessing the data
* backend.
*/
@Override
public &lt;T extends TurbineUserGroupRole&gt; Set&lt;T&gt; getUserGroupRoleSet() throws DataBackendException
{
<span class="nc" id="L295"> return userDelegate.getUserGroupRoleSet();</span>
}
/**
* Set the User/Group/Role set associated with this entity
*
* @param userGroupRoleSet
* a set of User/Group/Role relations
*/
@Override
public &lt;T extends TurbineUserGroupRole&gt; void setUserGroupRoleSet(Set&lt;T&gt; userGroupRoleSet)
{
<span class="nc" id="L307"> userDelegate.setUserGroupRoleSet(userGroupRoleSet);</span>
<span class="nc" id="L308"> }</span>
/**
* Add a User/Group/Role relation to this entity
*
* @param userGroupRole
* a User/Group/Role relation to add
* @throws DataBackendException if there was an error accessing the data
* backend.
*/
@Override
public void addUserGroupRole(TurbineUserGroupRole userGroupRole) throws DataBackendException
{
<span class="nc" id="L321"> userDelegate.addUserGroupRole(userGroupRole);</span>
<span class="nc" id="L322"> }</span>
/**
* Remove a User/Group/Role relation from this entity
*
* @param userGroupRole
* a User/Group/Role relation to remove
* @throws DataBackendException if there was an error accessing the data
* backend.
*/
@Override
public void removeUserGroupRole(TurbineUserGroupRole userGroupRole) throws DataBackendException
{
<span class="nc" id="L335"> userDelegate.removeUserGroupRole(userGroupRole);</span>
<span class="nc" id="L336"> }</span>
/**
* Gets the access counter for a user from perm storage.
*
* @return The access counter for the user.
*/
@Override
public int getAccessCounter()
{
try
{
<span class="nc" id="L348"> return ((Integer) getPerm(User.ACCESS_COUNTER)).intValue();</span>
}
<span class="nc" id="L350"> catch (Exception e)</span>
{
<span class="nc" id="L352"> return 0;</span>
}
}
/**
* Gets the access counter for a user during a session.
*
* @return The access counter for the user for the session.
*/
@Override
public int getAccessCounterForSession()
{
try
{
<span class="nc" id="L366"> return ((Integer) getTemp(User.SESSION_ACCESS_COUNTER)).intValue();</span>
}
<span class="nc" id="L368"> catch (Exception e)</span>
{
<span class="nc" id="L370"> return 0;</span>
}
}
/**
* Increments the permanent hit counter for the user.
*/
@Override
public void incrementAccessCounter()
{
// Ugh. Race city, here I come...
<span class="nc" id="L381"> setAccessCounter(getAccessCounter() + 1);</span>
<span class="nc" id="L382"> }</span>
/**
* Increments the session hit counter for the user.
*/
@Override
public void incrementAccessCounterForSession()
{
<span class="nc" id="L390"> setAccessCounterForSession(getAccessCounterForSession() + 1);</span>
<span class="nc" id="L391"> }</span>
/**
* Sets the access counter for a user, saved in perm storage.
*
* @param cnt The new count.
*/
@Override
public void setAccessCounter(int cnt)
{
<span class="nc" id="L401"> setPerm(User.ACCESS_COUNTER, Integer.valueOf(cnt));</span>
<span class="nc" id="L402"> }</span>
/**
* Sets the session access counter for a user, saved in temp
* storage.
*
* @param cnt The new count.
*/
@Override
public void setAccessCounterForSession(int cnt)
{
<span class="nc" id="L413"> setTemp(User.SESSION_ACCESS_COUNTER, Integer.valueOf(cnt));</span>
<span class="nc" id="L414"> }</span>
/**
* Gets the last access date for this User. This is the last time
* that the user object was referenced.
*
* @return A Java Date with the last access date for the user.
*/
@Override
public Date getLastAccessDate()
{
<span class="nc bnc" id="L425" title="All 2 branches missed."> if (lastAccessDate == null)</span>
{
<span class="nc" id="L427"> setLastAccessDate();</span>
}
<span class="nc" id="L429"> return Date.from( lastAccessDate.toInstant()); //immutable</span>
}
/**
* Sets the last access date for this User. This is the last time
* that the user object was referenced.
*/
@Override
public void setLastAccessDate()
{
<span class="nc" id="L439"> lastAccessDate = new Date();</span>
<span class="nc" id="L440"> }</span>
/**
* Returns the permanent storage. This is implemented
* as a Map
*
* @return A Map.
*/
@Override
public synchronized Map&lt;String, Object&gt; getPermStorage()
{
<span class="pc bpc" id="L451" title="1 of 2 branches missed."> if (permStorage == null)</span>
{
<span class="fc" id="L453"> byte [] objectdata = getObjectdata();</span>
<span class="pc bpc" id="L455" title="1 of 2 branches missed."> if (objectdata != null)</span>
{
<span class="nc" id="L457"> permStorage = ObjectUtils.deserialize(objectdata);</span>
}
<span class="pc bpc" id="L460" title="1 of 2 branches missed."> if (permStorage == null)</span>
{
<span class="fc" id="L462"> permStorage = new HashMap&lt;&gt;();</span>
}
}
<span class="fc" id="L466"> return permStorage;</span>
}
/**
* This should only be used in the case where we want to make the
* data persistent.
*
* @param permStorage A Map.
*/
@Override
public void setPermStorage(Map&lt;String, Object&gt; permStorage)
{
<span class="nc bnc" id="L478" title="All 2 branches missed."> if (permStorage != null)</span>
{
<span class="nc" id="L480"> this.permStorage = permStorage;</span>
}
<span class="nc" id="L482"> }</span>
/**
* Returns the temporary storage. This is implemented
* as a Map
*
* @return A Map.
*/
@Override
public Map&lt;String, Object&gt; getTempStorage()
{
<span class="pc bpc" id="L493" title="1 of 2 branches missed."> if (tempStorage == null)</span>
{
<span class="nc" id="L495"> tempStorage = new HashMap&lt;&gt;();</span>
}
<span class="fc" id="L497"> return tempStorage;</span>
}
/**
* This should only be used in the case where we want to save the
* data to the database.
*
* @param tempStorage A Map.
*/
@Override
public void setTempStorage(Map&lt;String, Object&gt; tempStorage)
{
<span class="nc bnc" id="L509" title="All 2 branches missed."> if (tempStorage != null)</span>
{
<span class="nc" id="L511"> this.tempStorage = tempStorage;</span>
}
<span class="nc" id="L513"> }</span>
/**
* Get an object from permanent storage.
*
* @param name The object's name.
* @return An Object with the given name.
*/
@Override
public Object getPerm(String name)
{
<span class="nc" id="L524"> return getPermStorage().get(name);</span>
}
/**
* Get an object from permanent storage; return default if value
* is null.
*
* @param name The object's name.
* @param def A default value to return.
* @return An Object with the given name.
*/
@Override
public Object getPerm(String name, Object def)
{
try
{
<span class="nc" id="L540"> Object val = getPermStorage().get(name);</span>
<span class="nc bnc" id="L541" title="All 2 branches missed."> return (val == null ? def : val);</span>
}
<span class="nc" id="L543"> catch (Exception e)</span>
{
<span class="nc" id="L545"> return def;</span>
}
}
/**
* Put an object into permanent storage.
*
* @param name The object's name.
* @param value The object.
*/
@Override
public void setPerm(String name, Object value)
{
<span class="fc" id="L558"> getPermStorage().put(name, value);</span>
<span class="fc" id="L559"> }</span>
/**
* Get an object from temporary storage.
*
* @param name The object's name.
* @return An Object with the given name.
*/
@Override
public Object getTemp(String name)
{
<span class="nc" id="L570"> return getTempStorage().get(name);</span>
}
/**
* Get an object from temporary storage; return default if value
* is null.
*
* @param name The object's name.
* @param def A default value to return.
* @return An Object with the given name.
*/
@Override
public Object getTemp(String name, Object def)
{
Object val;
try
{
<span class="nc" id="L587"> val = getTempStorage().get(name);</span>
<span class="nc bnc" id="L588" title="All 2 branches missed."> if (val == null)</span>
{
<span class="nc" id="L590"> val = def;</span>
}
}
<span class="nc" id="L593"> catch (Exception e)</span>
{
<span class="nc" id="L595"> val = def;</span>
<span class="nc" id="L596"> }</span>
<span class="nc" id="L597"> return val;</span>
}
/**
* Put an object into temporary storage.
*
* @param name The object's name.
* @param value The object.
*/
@Override
public void setTemp(String name, Object value)
{
<span class="fc" id="L609"> getTempStorage().put(name, value);</span>
<span class="fc" id="L610"> }</span>
/**
* Remove an object from temporary storage and return the object.
*
* @param name The name of the object to remove.
* @return An Object.
*/
@Override
public Object removeTemp(String name)
{
<span class="nc" id="L621"> return getTempStorage().remove(name);</span>
}
/**
* Returns the confirm value of the user
*
* @return The confirm value of the user
*/
@Override
public String getConfirmed()
{
<span class="nc" id="L632"> return (String) getPerm(User.CONFIRM_VALUE);</span>
}
/**
* Sets the new confirm value of the user
*
* @param confirm The new confirm value of the user
*/
@Override
public void setConfirmed(String confirm)
{
<span class="nc" id="L643"> setPerm(User.CONFIRM_VALUE, confirm);</span>
<span class="nc" id="L644"> }</span>
/**
* Returns the creation date of the user
*
* @return The creation date of the user
*/
@Override
public Date getCreateDate()
{
<span class="nc" id="L654"> return (Date)getPerm(CREATE_DATE, new Date());</span>
}
/**
* Sets the new creation date of the user
*
* @param createDate The new creation date of the user
*/
@Override
public void setCreateDate(Date createDate)
{
<span class="fc" id="L665"> setPerm(CREATE_DATE, createDate);</span>
<span class="fc" id="L666"> }</span>
/**
* Returns the date of the last login of the user
*
* @return The date of the last login of the user
*/
@Override
public Date getLastLogin()
{
<span class="nc" id="L676"> return (Date) getPerm(User.LAST_LOGIN);</span>
}
/**
* Sets the new date of the last login of the user
*
* @param lastLogin The new the date of the last login of the user
*/
@Override
public void setLastLogin(Date lastLogin)
{
<span class="nc" id="L687"> setPerm(User.LAST_LOGIN, lastLogin);</span>
<span class="nc" id="L688"> }</span>
/**
* The user is considered logged in if they have not timed out.
*
* @return Whether the user has logged in.
*/
@Override
public boolean hasLoggedIn()
{
<span class="nc" id="L698"> Boolean loggedIn = (Boolean) getTemp(User.HAS_LOGGED_IN);</span>
<span class="nc bnc" id="L699" title="All 4 branches missed."> return loggedIn != null &amp;&amp; loggedIn.booleanValue();</span>
}
/**
* This sets whether or not someone has logged in. hasLoggedIn()
* returns this value.
*
* @param value Whether someone has logged in or not.
*/
@Override
public void setHasLoggedIn(Boolean value)
{
<span class="fc" id="L711"> setTemp(User.HAS_LOGGED_IN, value);</span>
<span class="fc" id="L712"> }</span>
/**
* This method reports whether or not the user has been confirmed
* in the system by checking the User.CONFIRM_VALUE
* column in the users record to see if it is equal to
* User.CONFIRM_DATA.
*
* @return True if the user has been confirmed.
*/
@Override
public boolean isConfirmed()
{
<span class="nc" id="L725"> String value = getConfirmed();</span>
<span class="nc bnc" id="L726" title="All 4 branches missed."> return value != null &amp;&amp; value.equals(User.CONFIRM_DATA);</span>
}
/**
* Updates the last login date in the database.
*
* @throws Exception A generic exception.
*/
@Override
public void updateLastLogin()
throws Exception
{
<span class="nc" id="L738"> setLastLogin(new Date());</span>
<span class="nc" id="L739"> }</span>
/* (non-Javadoc)
* @see org.apache.turbine.om.security.UserDelegate#getUserDelegate()
*/
@Override
public TurbineUser getUserDelegate()
{
<span class="nc" id="L747"> return userDelegate;</span>
}
/* (non-Javadoc)
* @see org.apache.turbine.om.security.UserDelegate#setUserDelegate(org.apache.fulcrum.security.model.turbine.entity.TurbineUser)
*/
@Override
public final void setUserDelegate(TurbineUser userDelegate)
{
<span class="fc" id="L756"> this.userDelegate = userDelegate;</span>
<span class="fc" id="L757"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html>