View Javadoc

    1   package org.apache.turbine.om.security;
    2
    3   /*
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *   http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21
    22  import java.io.ByteArrayOutputStream;
    23  import java.io.PrintWriter;
    24  import java.util.Date;
    25  import java.util.HashMap;
    26  import java.util.Map;
    27  import java.util.Set;
    28
    29  import javax.servlet.http.HttpSessionBindingEvent;
    30
    31  import org.apache.fulcrum.security.model.turbine.entity.TurbineUser;
    32  import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole;
    33  import org.apache.turbine.services.security.TurbineSecurity;
    34  import org.apache.turbine.util.ObjectUtils;
    35
    36  /**
    37   * This is the Default user implementation. It is a wrapper around
    38   * a TurbineUser object
    39   *
    40   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
    41   * @version $Id: TorqueUser.java 1199856 2011-11-09 17:06:04Z tv $
    42   */
    43
    44  public class DefaultUserImpl implements User
    45  {
    46      /** Serial version */
    47      private static final long serialVersionUID = -1866504873085624111L;
    48
    49      /** The date on which the user last accessed the application. */
    50      private Date lastAccessDate = null;
    51
    52      /** This is data that will survive a servlet engine restart. */
    53      private Map<String, Object> permStorage = null;
    54
    55      /** This is data that will not survive a servlet engine restart. */
    56      private Map<String, Object> tempStorage = null;
    57
    58      /** The Fulcrum user instance to delegate to */
    59      private TurbineUser userDelegate = null;
    60
    61      /**
    62       * Constructor
    63       *
    64       * @param user the user object to wrap
    65       */
    66      public DefaultUserImpl(TurbineUser user)
    67      {
    68          super();
    69          this.userDelegate = user;
    70          setCreateDate(new Date());
    71          tempStorage = new HashMap<String, Object>(10);
    72          setHasLoggedIn(Boolean.FALSE);
    73      }
    74
    75      /**
    76       * Implement this method if you wish to be notified when the User
    77       * has been Bound to the session.
    78       *
    79       * @param hsbe Indication of value/session binding.
    80       */
    81      @Override
    82      public void valueBound(HttpSessionBindingEvent hsbe)
    83      {
    84          // Currently we have no need for this method.
    85      }
    86
    87      /**
    88       * Implement this method if you wish to be notified when the User
    89       * has been Unbound from the session.
    90       *
    91       * @param hsbe Indication of value/session unbinding.
    92       */
    93      @Override
    94      public void valueUnbound(HttpSessionBindingEvent hsbe)
    95      {
    96          try
    97          {
    98              if (hasLoggedIn())
    99              {
    100                 TurbineSecurity.saveOnSessionUnbind(this);
    101             }
    102         }
    103         catch (Exception e)
    104         {
    105             //Log.error("TorqueUser.valueUnbound(): " + e.getMessage(), e);
    106
    107             // To prevent messages being lost in case the logging system
    108             // goes away before sessions get unbound on servlet container
    109             // shutdown, print the stacktrace to the container's console.
    110             ByteArrayOutputStream ostr = new ByteArrayOutputStream();
    111             e.printStackTrace(new PrintWriter(ostr, true));
    112             String stackTrace = ostr.toString();
    113             System.out.println(stackTrace);
    114         }
    115     }
    116
    117     /**
    118      * Get the Name of the SecurityEntity.
    119      *
    120      * @return The Name of the SecurityEntity.
    121      */
    122     @Override
    123     public String getName()
    124     {
    125         return userDelegate.getName();
    126     }
    127
    128     /**
    129      * Sets the Name of the SecurityEntity.
    130      *
    131      * @param name
    132      *            Name of the SecurityEntity.
    133      */
    134     @Override
    135     public void setName(String name)
    136     {
    137         userDelegate.setName(name);
    138     }
    139
    140     /**
    141      * Get the Id of the SecurityEntity.
    142      *
    143      * @return The Id of the SecurityEntity.
    144      */
    145     @Override
    146     public Object getId()
    147     {
    148         return userDelegate.getId();
    149     }
    150
    151     /**
    152      * Sets the Id of the SecurityEntity.
    153      *
    154      * @param id
    155      *            The new Id of the SecurityEntity
    156      */
    157     @Override
    158     public void setId(Object id)
    159     {
    160         userDelegate.setId(id);
    161     }
    162
    163     /**
    164      * Returns the user's password. This method should not be used by
    165      * the application directly, because it's meaning depends upon
    166      * the implementation of UserManager that manages this particular
    167      * user object. Some implementations will use this attribute for
    168      * storing a password encrypted in some way, other will not use
    169      * it at all, when user entered password is presented to some external
    170      * authority (like NT domain controller) to validate it.
    171      * See also {@link org.apache.turbine.services.security.UserManager#authenticate(User,String)}.
    172      *
    173      * @return A String with the password for the user.
    174      */
    175     @Override
    176     public String getPassword()
    177     {
    178         return userDelegate.getPassword();
    179     }
    180
    181     /**
    182      * Set password. Application should not use this method
    183      * directly, see {@link #getPassword()}.
    184      * See also {@link org.apache.turbine.services.security.UserManager#changePassword(User,String,String)}.
    185      *
    186      * @param password The new password.
    187      */
    188     @Override
    189     public void setPassword(String password)
    190     {
    191         userDelegate.setPassword(password);
    192     }
    193
    194     /**
    195      * Returns the first name for this user.
    196      *
    197      * @return A String with the user's first name.
    198      */
    199
    200     @Override
    201     public String getFirstName()
    202     {
    203         return userDelegate.getFirstName();
    204     }
    205
    206     /**
    207      * Sets the first name for this user.
    208      *
    209      * @param firstName User's first name.
    210      */
    211     @Override
    212     public void setFirstName(String firstName)
    213     {
    214         userDelegate.setFirstName(firstName);
    215     }
    216
    217     /**
    218      * Returns the last name for this user.
    219      *
    220      * @return A String with the user's last name.
    221      */
    222     @Override
    223     public String getLastName()
    224     {
    225         return userDelegate.getLastName();
    226     }
    227
    228     /**
    229      * Sets the last name for this user.
    230      *
    231      * @param lastName User's last name.
    232      */
    233     @Override
    234     public void setLastName(String lastName)
    235     {
    236         userDelegate.setLastName(lastName);
    237     }
    238
    239     /**
    240      * Returns the email address for this user.
    241      *
    242      * @return A String with the user's email address.
    243      */
    244     @Override
    245     public String getEmail()
    246     {
    247         return userDelegate.getEmail();
    248     }
    249
    250     /**
    251      * Sets the email address.
    252      *
    253      * @param address The email address.
    254      */
    255     @Override
    256     public void setEmail(String address)
    257     {
    258         userDelegate.setEmail(address);
    259     }
    260
    261     /**
    262      * Returns the value of the objectdata for this user.
    263      * Objectdata is a storage area used
    264      * to store the permanent storage table from the User
    265      * object.
    266      *
    267      * @return The bytes in the objectdata for this user
    268      */
    269     @Override
    270     public byte[] getObjectdata()
    271     {
    272         return userDelegate.getObjectdata();
    273     }
    274
    275     /**
    276      * Sets the value of the objectdata for the user
    277      *
    278      * @param objectdata The new permanent storage for the user
    279      */
    280     @Override
    281     public void setObjectdata(byte[] objectdata)
    282     {
    283         userDelegate.setObjectdata(objectdata);
    284     }
    285
    286     /**
    287      * Get the User/Group/Role set associated with this entity
    288      *
    289      * @return a set of User/Group/Role relations
    290      */
    291     @Override
    292     public <T extends TurbineUserGroupRole> Set<T> getUserGroupRoleSet()
    293     {
    294         return userDelegate.getUserGroupRoleSet();
    295     }
    296
    297     /**
    298      * Set the User/Group/Role set associated with this entity
    299      *
    300      * @param userGroupRoleSet
    301      *            a set of User/Group/Role relations
    302      */
    303     @Override
    304     public <T extends TurbineUserGroupRole> void setUserGroupRoleSet(Set<T> userGroupRoleSet)
    305     {
    306         userDelegate.setUserGroupRoleSet(userGroupRoleSet);
    307     }
    308
    309     /**
    310      * Add a User/Group/Role relation to this entity
    311      *
    312      * @param userGroupRole
    313      *            a User/Group/Role relation to add
    314      */
    315     @Override
    316     public void addUserGroupRole(TurbineUserGroupRole userGroupRole)
    317     {
    318         userDelegate.addUserGroupRole(userGroupRole);
    319     }
    320
    321     /**
    322      * Remove a User/Group/Role relation from this entity
    323      *
    324      * @param userGroupRole
    325      *            a User/Group/Role relation to remove
    326      */
    327     @Override
    328     public void removeUserGroupRole(TurbineUserGroupRole userGroupRole)
    329     {
    330         userDelegate.removeUserGroupRole(userGroupRole);
    331     }
    332
    333     /**
    334      * Gets the access counter for a user from perm storage.
    335      *
    336      * @return The access counter for the user.
    337      */
    338     @Override
    339     public int getAccessCounter()
    340     {
    341         try
    342         {
    343             return ((Integer) getPerm(User.ACCESS_COUNTER)).intValue();
    344         }
    345         catch (Exception e)
    346         {
    347             return 0;
    348         }
    349     }
    350
    351     /**
    352      * Gets the access counter for a user during a session.
    353      *
    354      * @return The access counter for the user for the session.
    355      */
    356     @Override
    357     public int getAccessCounterForSession()
    358     {
    359         try
    360         {
    361             return ((Integer) getTemp(User.SESSION_ACCESS_COUNTER)).intValue();
    362         }
    363         catch (Exception e)
    364         {
    365             return 0;
    366         }
    367     }
    368
    369     /**
    370      * Increments the permanent hit counter for the user.
    371      */
    372     @Override
    373     public void incrementAccessCounter()
    374     {
    375         // Ugh. Race city, here I come...
    376         setAccessCounter(getAccessCounter() + 1);
    377     }
    378
    379     /**
    380      * Increments the session hit counter for the user.
    381      */
    382     @Override
    383     public void incrementAccessCounterForSession()
    384     {
    385         setAccessCounterForSession(getAccessCounterForSession() + 1);
    386     }
    387
    388     /**
    389      * Sets the access counter for a user, saved in perm storage.
    390      *
    391      * @param cnt The new count.
    392      */
    393     @Override
    394     public void setAccessCounter(int cnt)
    395     {
    396         setPerm(User.ACCESS_COUNTER, Integer.valueOf(cnt));
    397     }
    398
    399     /**
    400      * Sets the session access counter for a user, saved in temp
    401      * storage.
    402      *
    403      * @param cnt The new count.
    404      */
    405     @Override
    406     public void setAccessCounterForSession(int cnt)
    407     {
    408         setTemp(User.SESSION_ACCESS_COUNTER, Integer.valueOf(cnt));
    409     }
    410
    411     /**
    412      * Gets the last access date for this User.  This is the last time
    413      * that the user object was referenced.
    414      *
    415      * @return A Java Date with the last access date for the user.
    416      */
    417     @Override
    418     public java.util.Date getLastAccessDate()
    419     {
    420         if (lastAccessDate == null)
    421         {
    422             setLastAccessDate();
    423         }
    424         return lastAccessDate;
    425     }
    426
    427     /**
    428      * Sets the last access date for this User. This is the last time
    429      * that the user object was referenced.
    430      */
    431     @Override
    432     public void setLastAccessDate()
    433     {
    434         lastAccessDate = new java.util.Date();
    435     }
    436
    437     /**
    438      * Returns the permanent storage. This is implemented
    439      * as a Map
    440      *
    441      * @return A Map.
    442      */
    443     @Override
    444     public Map<String, Object> getPermStorage()
    445     {
    446         if (permStorage == null)
    447         {
    448             byte [] objectdata = getObjectdata();
    449
    450             if (objectdata != null)
    451             {
    452                 permStorage = ObjectUtils.deserialize(objectdata);
    453             }
    454
    455             if (permStorage == null)
    456             {
    457                 permStorage = new HashMap<String, Object>();
    458             }
    459         }
    460
    461         return permStorage;
    462     }
    463
    464     /**
    465      * This should only be used in the case where we want to make the
    466      * data persistent.
    467      *
    468      * @param permStorage A Map.
    469      */
    470     @Override
    471     public void setPermStorage(Map<String, Object> permStorage)
    472     {
    473         if (permStorage != null)
    474         {
    475             this.permStorage = permStorage;
    476         }
    477     }
    478
    479     /**
    480      * Returns the temporary storage. This is implemented
    481      * as a Map
    482      *
    483      * @return A Map.
    484      */
    485     @Override
    486     public Map<String, Object> getTempStorage()
    487     {
    488         if (tempStorage == null)
    489         {
    490             tempStorage = new HashMap<String, Object>();
    491         }
    492         return tempStorage;
    493     }
    494
    495     /**
    496      * This should only be used in the case where we want to save the
    497      * data to the database.
    498      *
    499      * @param tempStorage A Map.
    500      */
    501     @Override
    502     public void setTempStorage(Map<String, Object> tempStorage)
    503     {
    504         if (tempStorage != null)
    505         {
    506             this.tempStorage = tempStorage;
    507         }
    508     }
    509
    510     /**
    511      * Get an object from permanent storage.
    512      *
    513      * @param name The object's name.
    514      * @return An Object with the given name.
    515      */
    516     @Override
    517     public Object getPerm(String name)
    518     {
    519         return getPermStorage().get(name);
    520     }
    521
    522     /**
    523      * Get an object from permanent storage; return default if value
    524      * is null.
    525      *
    526      * @param name The object's name.
    527      * @param def A default value to return.
    528      * @return An Object with the given name.
    529      */
    530     @Override
    531     public Object getPerm(String name, Object def)
    532     {
    533         try
    534         {
    535             Object val = getPermStorage().get(name);
    536             return (val == null ? def : val);
    537         }
    538         catch (Exception e)
    539         {
    540             return def;
    541         }
    542     }
    543
    544     /**
    545      * Put an object into permanent storage.
    546      *
    547      * @param name The object's name.
    548      * @param value The object.
    549      */
    550     @Override
    551     public void setPerm(String name, Object value)
    552     {
    553         getPermStorage().put(name, value);
    554     }
    555
    556     /**
    557      * Get an object from temporary storage.
    558      *
    559      * @param name The object's name.
    560      * @return An Object with the given name.
    561      */
    562     @Override
    563     public Object getTemp(String name)
    564     {
    565         return getTempStorage().get(name);
    566     }
    567
    568     /**
    569      * Get an object from temporary storage; return default if value
    570      * is null.
    571      *
    572      * @param name The object's name.
    573      * @param def A default value to return.
    574      * @return An Object with the given name.
    575      */
    576     @Override
    577     public Object getTemp(String name, Object def)
    578     {
    579         Object val;
    580         try
    581         {
    582             val = getTempStorage().get(name);
    583             if (val == null)
    584             {
    585                 val = def;
    586             }
    587         }
    588         catch (Exception e)
    589         {
    590             val = def;
    591         }
    592         return val;
    593     }
    594
    595     /**
    596      * Put an object into temporary storage.
    597      *
    598      * @param name The object's name.
    599      * @param value The object.
    600      */
    601     @Override
    602     public void setTemp(String name, Object value)
    603     {
    604         getTempStorage().put(name, value);
    605     }
    606
    607     /**
    608      * Remove an object from temporary storage and return the object.
    609      *
    610      * @param name The name of the object to remove.
    611      * @return An Object.
    612      */
    613     @Override
    614     public Object removeTemp(String name)
    615     {
    616         return getTempStorage().remove(name);
    617     }
    618
    619     /**
    620      * Returns the confirm value of the user
    621      *
    622      * @return The confirm value of the user
    623      */
    624     @Override
    625     public String getConfirmed()
    626     {
    627         return (String) getPerm(User.CONFIRM_VALUE);
    628     }
    629
    630     /**
    631      * Sets the new confirm value of the user
    632      *
    633      * @param confirm The new confirm value of the user
    634      */
    635     @Override
    636     public void setConfirmed(String confirm)
    637     {
    638         setPerm(User.CONFIRM_VALUE, confirm);
    639     }
    640
    641     /**
    642      * Returns the creation date of the user
    643      *
    644      * @return The creation date of the user
    645      */
    646     @Override
    647     public java.util.Date getCreateDate()
    648     {
    649         return (java.util.Date)getPerm(CREATE_DATE, new java.util.Date());
    650     }
    651
    652     /**
    653      * Sets the new creation date of the user
    654      *
    655      * @param createDate The new creation date of the user
    656      */
    657     @Override
    658     public void setCreateDate(java.util.Date createDate)
    659     {
    660         setPerm(CREATE_DATE, createDate);
    661     }
    662
    663     /**
    664      * Returns the date of the last login of the user
    665      *
    666      * @return The date of the last login of the user
    667      */
    668     @Override
    669     public java.util.Date getLastLogin()
    670     {
    671         return (java.util.Date) getPerm(User.LAST_LOGIN);
    672     }
    673
    674     /**
    675      * Sets the new date of the last login of the user
    676      *
    677      * @param lastLogin The new the date of the last login of the user
    678      */
    679     @Override
    680     public void setLastLogin(java.util.Date lastLogin)
    681     {
    682         setPerm(User.LAST_LOGIN, lastLogin);
    683     }
    684
    685     /**
    686      * The user is considered logged in if they have not timed out.
    687      *
    688      * @return Whether the user has logged in.
    689      */
    690     @Override
    691     public boolean hasLoggedIn()
    692     {
    693         Boolean loggedIn = (Boolean) getTemp(User.HAS_LOGGED_IN);
    694         return (loggedIn != null && loggedIn.booleanValue());
    695     }
    696
    697     /**
    698      * This sets whether or not someone has logged in.  hasLoggedIn()
    699      * returns this value.
    700      *
    701      * @param value Whether someone has logged in or not.
    702      */
    703     @Override
    704     public void setHasLoggedIn(Boolean value)
    705     {
    706         setTemp(User.HAS_LOGGED_IN, value);
    707     }
    708
    709     /**
    710      * This method reports whether or not the user has been confirmed
    711      * in the system by checking the User.CONFIRM_VALUE
    712      * column in the users record to see if it is equal to
    713      * User.CONFIRM_DATA.
    714      *
    715      * @return True if the user has been confirmed.
    716      */
    717     @Override
    718     public boolean isConfirmed()
    719     {
    720         String value = getConfirmed();
    721         return (value != null && value.equals(User.CONFIRM_DATA));
    722     }
    723
    724     /**
    725      * Updates the last login date in the database.
    726      *
    727      * @exception Exception A generic exception.
    728      */
    729     @Override
    730     public void updateLastLogin()
    731         throws Exception
    732     {
    733         setLastLogin(new java.util.Date());
    734     }
    735
    736     /**
    737      * Get the delegated user
    738      *
    739      * @return the userDelegate
    740      */
    741     public TurbineUser getUserDelegate()
    742     {
    743         return userDelegate;
    744     }
    745
    746     /**
    747      * Set the delegated user
    748      *
    749      * @param userDelegate the userDelegate to set
    750      */
    751     public void setUserDelegate(TurbineUser userDelegate)
    752     {
    753         this.userDelegate = userDelegate;
    754     }
    755 }