public CmsUser loginUser(CmsDbContext dbc, String userName, String password, String remoteAddress)
    throws CmsAuthentificationException, CmsDataAccessException, CmsPasswordEncryptionException {

        if (CmsStringUtil.isEmptyOrWhitespaceOnly(password)) {
            throw new CmsDbEntryNotFoundException(Messages.get().container(Messages.ERR_UNKNOWN_USER_1, userName));
        }
        CmsUser newUser;
        try {
            // read the user from the driver to avoid the cache
            newUser = getUserDriver(dbc).readUser(dbc, userName, password, remoteAddress);
        } catch (CmsDbEntryNotFoundException e) {
            // this indicates that the username / password combination does not exist
            // any other exception indicates database issues, these are not catched here

            // check if a user with this name exists at all
            CmsUser user = null;
            try {
                user = readUser(dbc, userName);
            } catch (CmsDataAccessException e2) {
                // apparently this user does not exist in the database
            }

            if (user != null) {
                if (dbc.currentUser().isGuestUser()) {
                    // add an invalid login attempt for this user to the storage
                    OpenCms.getLoginManager().addInvalidLogin(userName, remoteAddress);
                }
                OpenCms.getLoginManager().checkInvalidLogins(userName, remoteAddress);
                throw new CmsAuthentificationException(
                    org.opencms.security.Messages.get().container(
                        org.opencms.security.Messages.ERR_LOGIN_FAILED_2,
                        userName,
                        remoteAddress),
                    e);
            } else {
                String userOu = CmsOrganizationalUnit.getParentFqn(userName);
                if (userOu != null) {
                    String parentOu = CmsOrganizationalUnit.getParentFqn(userOu);
                    if (parentOu != null) {
                        // try a higher level ou
                        String uName = CmsOrganizationalUnit.getSimpleName(userName);
                        return loginUser(dbc, parentOu + uName, password, remoteAddress);
                    }
                }
                throw new CmsAuthentificationException(
                    org.opencms.security.Messages.get().container(
                        org.opencms.security.Messages.ERR_LOGIN_FAILED_NO_USER_2,
                        userName,
                        remoteAddress),
                    e);
            }
        }
        // check if the "enabled" flag is set for the user
        if (!newUser.isEnabled()) {
            // user is disabled, throw a securiy exception
            throw new CmsAuthentificationException(
                org.opencms.security.Messages.get().container(
                    org.opencms.security.Messages.ERR_LOGIN_FAILED_DISABLED_2,
                    userName,
                    remoteAddress));
        }

        if (dbc.currentUser().isGuestUser()) {
            // check if this account is temporarily disabled because of too many invalid login attempts
            // this will throw an exception if the test fails
            OpenCms.getLoginManager().checkInvalidLogins(userName, remoteAddress);
            // test successful, remove all previous invalid login attempts for this user from the storage
            OpenCms.getLoginManager().removeInvalidLogins(userName, remoteAddress);
        }

        if (!m_securityManager.hasRole(
            dbc,
            newUser,
            CmsRole.ADMINISTRATOR.forOrgUnit(dbc.getRequestContext().getOuFqn()))) {
            // new user is not Administrator, check if login is currently allowed
            OpenCms.getLoginManager().checkLoginAllowed();
        }
        m_monitor.clearUserCache(newUser);
        // set the last login time to the current time
        newUser.setLastlogin(System.currentTimeMillis());

        // write the changed user object back to the user driver
        Map<String, Object> additionalInfosForRepositories = OpenCms.getRepositoryManager().getAdditionalInfoForLogin(
            newUser.getName(),
            password);
        boolean requiresAddInfoUpdate = false;

        // check for changes
        for (Entry<String, Object> entry : additionalInfosForRepositories.entrySet()) {
            Object value = entry.getValue();
            Object current = newUser.getAdditionalInfo(entry.getKey());
            if (((value == null) && (current != null)) || ((value != null) && !value.equals(current))) {
                requiresAddInfoUpdate = true;
                break;
            }
        }
        if (requiresAddInfoUpdate) {
            newUser.getAdditionalInfo().putAll(additionalInfosForRepositories);
        }
        String lastPasswordChange = (String)newUser.getAdditionalInfo(
            CmsUserSettings.ADDITIONAL_INFO_LAST_PASSWORD_CHANGE);
        if (lastPasswordChange == null) {
            requiresAddInfoUpdate = true;
            newUser.getAdditionalInfo().put(
                CmsUserSettings.ADDITIONAL_INFO_LAST_PASSWORD_CHANGE,
                "" + System.currentTimeMillis());
        }
        if (!requiresAddInfoUpdate) {
            dbc.setAttribute(ATTRIBUTE_LOGIN, newUser.getName());
        }
        getUserDriver(dbc).writeUser(dbc, newUser);
        int changes = CmsUser.FLAG_LAST_LOGIN;

        // check if we need to update the password
        if (!OpenCms.getPasswordHandler().checkPassword(password, newUser.getPassword(), false)
            && OpenCms.getPasswordHandler().checkPassword(password, newUser.getPassword(), true)) {
            // the password does not check with the current hash algorithm but with the fall back, update the password
            getUserDriver(dbc).writePassword(dbc, userName, password, password);
            changes = changes | CmsUser.FLAG_CORE_DATA;
        }

        // update cache
        m_monitor.cacheUser(newUser);

        // invalidate all user dependent caches
        m_monitor.flushCache(
            CmsMemoryMonitor.CacheType.ACL,
            CmsMemoryMonitor.CacheType.GROUP,
            CmsMemoryMonitor.CacheType.ORG_UNIT,
            CmsMemoryMonitor.CacheType.USERGROUPS,
            CmsMemoryMonitor.CacheType.USER_LIST,
            CmsMemoryMonitor.CacheType.PERMISSION,
            CmsMemoryMonitor.CacheType.RESOURCE_LIST);

        // fire user modified event
        Map<String, Object> eventData = new HashMap<String, Object>();
        eventData.put(I_CmsEventListener.KEY_USER_ID, newUser.getId().toString());
        eventData.put(I_CmsEventListener.KEY_USER_NAME, newUser.getName());
        eventData.put(I_CmsEventListener.KEY_USER_ACTION, I_CmsEventListener.VALUE_USER_MODIFIED_ACTION_WRITE_USER);
        eventData.put(I_CmsEventListener.KEY_USER_CHANGES, Integer.valueOf(changes));
        OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_USER_MODIFIED, eventData));

        // return the user object read from the driver
        return newUser.clone();
    }
public CmsUser loginUser ( CmsDbContext dbc , String userName , String password , String remoteAddress ) throws CmsAuthentificationException , CmsDataAccessException , CmsPasswordEncryptionException { if ( CmsStringUtil . isEmptyOrWhitespaceOnly ( password ) ) { throw new CmsDbEntryNotFoundException ( Messages . get ( ) . container ( Messages . ERR_UNKNOWN_USER_1 , userName ) ) ; } CmsUser newUser ; try { newUser = getUserDriver ( dbc ) . readUser ( dbc , userName , password , remoteAddress ) ; } catch ( CmsDbEntryNotFoundException e ) { CmsUser user = null ; try { user = readUser ( dbc , userName ) ; } catch ( CmsDataAccessException e2 ) { } if ( user != null ) { if ( dbc . currentUser ( ) . isGuestUser ( ) ) { OpenCms . getLoginManager ( ) . addInvalidLogin ( userName , remoteAddress ) ; } OpenCms . getLoginManager ( ) . checkInvalidLogins ( userName , remoteAddress ) ; throw new CmsAuthentificationException ( org . opencms . security . Messages . get ( ) . container ( org . opencms . security . Messages . ERR_LOGIN_FAILED_2 , userName , remoteAddress ) , e ) ; } else { String userOu = CmsOrganizationalUnit . getParentFqn ( userName ) ; if ( userOu != null ) { String parentOu = CmsOrganizationalUnit . getParentFqn ( userOu ) ; if ( parentOu != null ) { String uName = CmsOrganizationalUnit . getSimpleName ( userName ) ; return loginUser ( dbc , parentOu + uName , password , remoteAddress ) ; } } throw new CmsAuthentificationException ( org . opencms . security . Messages . get ( ) . container ( org . opencms . security . Messages . ERR_LOGIN_FAILED_NO_USER_2 , userName , remoteAddress ) , e ) ; } } if ( ! newUser . isEnabled ( ) ) { throw new CmsAuthentificationException ( org . opencms . security . Messages . get ( ) . container ( org . opencms . security . Messages . ERR_LOGIN_FAILED_DISABLED_2 , userName , remoteAddress ) ) ; } if ( dbc . currentUser ( ) . isGuestUser ( ) ) { OpenCms . getLoginManager ( ) . checkInvalidLogins ( userName , remoteAddress ) ; OpenCms . getLoginManager ( ) . removeInvalidLogins ( userName , remoteAddress ) ; } if ( ! m_securityManager . hasRole ( dbc , newUser , CmsRole . ADMINISTRATOR . forOrgUnit ( dbc . getRequestContext ( ) . getOuFqn ( ) ) ) ) { OpenCms . getLoginManager ( ) . checkLoginAllowed ( ) ; } m_monitor . clearUserCache ( newUser ) ; newUser . setLastlogin ( System . currentTimeMillis ( ) ) ; Map < String , Object > additionalInfosForRepositories = OpenCms . getRepositoryManager ( ) . getAdditionalInfoForLogin ( newUser . getName ( ) , password ) ; boolean requiresAddInfoUpdate = false ; for ( Entry < String , Object > entry : additionalInfosForRepositories . entrySet ( ) ) { Object value = entry . getValue ( ) ; Object current = newUser . getAdditionalInfo ( entry . getKey ( ) ) ; if ( ( ( value == null ) && ( current != null ) ) || ( ( value != null ) && ! value . equals ( current ) ) ) { requiresAddInfoUpdate = true ; break ; } } if ( requiresAddInfoUpdate ) { newUser . getAdditionalInfo ( ) . putAll ( additionalInfosForRepositories ) ; } String lastPasswordChange = ( String ) newUser . getAdditionalInfo ( CmsUserSettings . ADDITIONAL_INFO_LAST_PASSWORD_CHANGE ) ; if ( lastPasswordChange == null ) { requiresAddInfoUpdate = true ; newUser . getAdditionalInfo ( ) . put ( CmsUserSettings . ADDITIONAL_INFO_LAST_PASSWORD_CHANGE , "" + System . currentTimeMillis ( ) ) ; } if ( ! requiresAddInfoUpdate ) { dbc . setAttribute ( ATTRIBUTE_LOGIN , newUser . getName ( ) ) ; } getUserDriver ( dbc ) . writeUser ( dbc , newUser ) ; int changes = CmsUser . FLAG_LAST_LOGIN ; if ( ! OpenCms . getPasswordHandler ( ) . checkPassword ( password , newUser . getPassword ( ) , false ) && OpenCms . getPasswordHandler ( ) . checkPassword ( password , newUser . getPassword ( ) , true ) ) { getUserDriver ( dbc ) . writePassword ( dbc , userName , password , password ) ; changes = changes | CmsUser . FLAG_CORE_DATA ; } m_monitor . cacheUser ( newUser ) ; m_monitor . flushCache ( CmsMemoryMonitor . CacheType . ACL , CmsMemoryMonitor . CacheType . GROUP , CmsMemoryMonitor . CacheType . ORG_UNIT , CmsMemoryMonitor . CacheType . USERGROUPS , CmsMemoryMonitor . CacheType . USER_LIST , CmsMemoryMonitor . CacheType . PERMISSION , CmsMemoryMonitor . CacheType . RESOURCE_LIST ) ; Map < String , Object > eventData = new HashMap < String , Object > ( ) ; eventData . put ( I_CmsEventListener . KEY_USER_ID , newUser . getId ( ) . toString ( ) ) ; eventData . put ( I_CmsEventListener . KEY_USER_NAME , newUser . getName ( ) ) ; eventData . put ( I_CmsEventListener . KEY_USER_ACTION , I_CmsEventListener . VALUE_USER_MODIFIED_ACTION_WRITE_USER ) ; eventData . put ( I_CmsEventListener . KEY_USER_CHANGES , Integer . valueOf ( changes ) ) ; OpenCms . fireCmsEvent ( new CmsEvent ( I_CmsEventListener . EVENT_USER_MODIFIED , eventData ) ) ; return newUser . clone ( ) ; }
Attempts to authenticate a user into OpenCms with the given password.<p>

@param dbc the current database context
@param userName the name of the user to be logged in
@param password the password of the user
@param remoteAddress the ip address of the request

@return the logged in user

@throws CmsAuthentificationException if the login was not successful
@throws CmsDataAccessException in case of errors accessing the database
@throws CmsPasswordEncryptionException in case of errors encrypting the users password