Package org.apache.directory.api.ldap.extras.extended

Examples of org.apache.directory.api.ldap.extras.extended.PwdModifyResponseImpl


            if ( !service.getOperationManager().hasEntry( hasEntryContext ) )
            {
                LOG.error( "Cannot find an entry for DN " + userDn );
                // We can't find the entry in the DIT
                requestor.getIoSession().write( new PwdModifyResponseImpl(
                    req.getMessageId(), ResultCodeEnum.NO_SUCH_OBJECT, "Cannot find an entry for DN " + userDn ) );

                return;
            }
        }
        catch ( LdapException le )
        {
            LOG.error( "Cannot find an entry for DN " + userDn + ", exception : " + le.getMessage() );
            // We can't find the entry in the DIT
            requestor.getIoSession().write(
                new PwdModifyResponseImpl(
                    req.getMessageId(), ResultCodeEnum.NO_SUCH_OBJECT, "Cannot find an entry for DN " + userDn
                        + ", exception : " + le.getMessage() ) );

            return;
        }

        // We can try to update the userPassword now
        ModifyOperationContext modifyContext = new ModifyOperationContext( adminSession );
        modifyContext.setDn( userDn );
        List<Modification> modifications = new ArrayList<Modification>();
        Modification modification = null;

        if ( oldPassword != null )
        {
            modification = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE,
                SchemaConstants.USER_PASSWORD_AT, oldPassword );

            modifications.add( modification );
        }

        if ( newPassword != null )
        {
            if ( oldPassword == null )
            {
                modification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE,
                    SchemaConstants.USER_PASSWORD_AT, newPassword );
            }
            else
            {
                modification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE,
                    SchemaConstants.USER_PASSWORD_AT, newPassword );
            }

            modifications.add( modification );
        }
        else
        {
            // In this case, we could either generate a new password, or return an error
            // Atm, we will return an unwillingToPerform error
            LOG.error( "Cannot create a new password for user " + userDn + ", exception : " + userDn );

            // We can't modify the password
            requestor.getIoSession().write( new PwdModifyResponseImpl(
                req.getMessageId(), ResultCodeEnum.UNWILLING_TO_PERFORM, "Cannot generate a new password for user "
                    + userDn ) );

            return;
        }

        modifyContext.setModItems( modifications );

        try
        {
            service.getOperationManager().modify( modifyContext );

            LOG.debug( "Password modified for user " + userDn );

            // Ok, all done
            requestor.getIoSession().write( new PwdModifyResponseImpl(
                req.getMessageId(), ResultCodeEnum.SUCCESS ) );
        }
        catch ( LdapException le )
        {
            LOG.error( "Cannot modify the password for user " + userDn + ", exception : " + le.getMessage() );
            // We can't modify the password
            requestor.getIoSession().write( new PwdModifyResponseImpl(
                req.getMessageId(), ResultCodeEnum.INVALID_CREDENTIALS, "Cannot modify the password for user "
                    + userDn + ", exception : " + le.getMessage() ) );

            return;
        }
View Full Code Here


            service.getOperationManager().modify( modifyContext );

            LOG.debug( "Password modified for user " + principalDn );

            // Ok, all done
            requestor.getIoSession().write( new PwdModifyResponseImpl(
                req.getMessageId(), ResultCodeEnum.SUCCESS ) );
        }
        catch ( LdapException le )
        {
            LOG.error( "Cannot modify the password for user " + principalDn + ", exception : " + le.getMessage() );
            // We can't modify the password
            requestor.getIoSession().write(
                new PwdModifyResponseImpl( req.getMessageId(), ResultCodeEnum.INVALID_CREDENTIALS,
                    "Cannot modify the password for user "
                        + principalDn + ", exception : " + le.getMessage() ) );

            return;
        }
View Full Code Here

            }
            catch ( LdapInvalidDnException lide )
            {
                LOG.error( "The user DN is invalid : " + userDn );
                // The userIdentity is not a DN : return with an error code.
                requestor.getIoSession().write( new PwdModifyResponseImpl(
                    req.getMessageId(), ResultCodeEnum.INVALID_DN_SYNTAX, "The user DN is invalid : " + userDn ) );
                return;
            }
        }

        byte[] oldPassword = req.getOldPassword();
        byte[] newPassword = req.getNewPassword();

        // First check if the user is bound or not
        if ( requestor.isAuthenticated() )
        {
            Dn principalDn = requestor.getCoreSession().getEffectivePrincipal().getDn();

            LOG.debug( "Trying to modify password for user " + principalDn );

            // First, check that the userDn is null : we can't change the password of someone else
            // except if we are admin
            if ( ( userDn != null ) && ( !userDn.equals( principalDn ) ) )
            {
                // Are we admin ?
                if ( !requestor.getCoreSession().isAdministrator() )
                {
                    // No : error
                    LOG.error( "Cannot access to another user's password to modify it" );
                    requestor.getIoSession().write( new PwdModifyResponseImpl(
                        req.getMessageId(), ResultCodeEnum.INSUFFICIENT_ACCESS_RIGHTS,
                        "Cannot access to another user's password to modify it" ) );
                }
                else
                {
                    // We are administrator, we can try to modify the user's credentials
                    modifyUserPassword( requestor, userDn, oldPassword, newPassword, req );
                }
            }
            else
            {
                // We are trying to modify our own password
                modifyOwnPassword( requestor, principalDn, oldPassword, newPassword, req );
            }
        }
        else
        {
            // The user is not authenticated : we have to use the provided userIdentity
            // and the oldPassword to check if the user is present
            BindOperationContext bindContext = new BindOperationContext( adminSession );
            bindContext.setDn( userDn );
            bindContext.setCredentials( oldPassword );

            try
            {
                service.getOperationManager().bind( bindContext );
            }
            catch ( LdapException le )
            {
                // We can't bind with the provided information : we thus can't
                // change the password...
                requestor.getIoSession().write( new PwdModifyResponseImpl(
                    req.getMessageId(), ResultCodeEnum.INVALID_CREDENTIALS ) );

                return;
            }

            // Ok, we were able to bind using the userIdentity and the password. Let's
            // modify the password now
            ModifyOperationContext modifyContext = new ModifyOperationContext( adminSession );
            modifyContext.setDn( userDn );
            List<Modification> modifications = new ArrayList<Modification>();
            Modification modification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE,
                SchemaConstants.USER_PASSWORD_AT, newPassword );
            modifications.add( modification );
            modifyContext.setModItems( modifications );

            try
            {
                service.getOperationManager().modify( modifyContext );

                // Ok, all done
                requestor.getIoSession().write( new PwdModifyResponseImpl(
                    req.getMessageId(), ResultCodeEnum.SUCCESS ) );
            }
            catch ( LdapException le )
            {
                // We can't modify the password
                requestor.getIoSession().write(
                    new PwdModifyResponseImpl(
                        req.getMessageId(), ResultCodeEnum.UNWILLING_TO_PERFORM,
                        "Cannot modify the password, exception : " + le.getMessage() ) );

                return;
            }
View Full Code Here

     * {@inheritDoc}
     */
    public static PwdModifyResponse getPwdModifyResponse()
    {
        // build the PwdModifyResponse message with replicationContexts
        return new PwdModifyResponseImpl();
    }
View Full Code Here

     * {@inheritDoc}
     */
    public PwdModifyResponse newResponse( byte[] encodedValue ) throws DecoderException
    {
        PasswordModifyResponseDecorator response = new PasswordModifyResponseDecorator( codec,
            new PwdModifyResponseImpl() );
        response.setResponseValue( encodedValue );
        return response;
    }
View Full Code Here

            StringWriter sw = new StringWriter();
            de.printStackTrace( new PrintWriter( sw ) );
            String stackTrace = sw.toString();

            // Error while decoding the value.
            PwdModifyResponse pwdModifyResponse = new PwdModifyResponseImpl(
                decoratedResponse.getMessageId(),
                ResultCodeEnum.OPERATIONS_ERROR,
                stackTrace );

            return new PasswordModifyResponseDecorator( codec, pwdModifyResponse );
View Full Code Here

            if ( !service.getOperationManager().hasEntry( hasEntryContext ) )
            {
                LOG.error( "Cannot find an entry for DN " + userDn );
                // We can't find the entry in the DIT
                requestor.getIoSession().write( new PwdModifyResponseImpl(
                    req.getMessageId(), ResultCodeEnum.NO_SUCH_OBJECT ) );

                return;
            }
        }
        catch ( LdapException le )
        {
            LOG.error( "Cannot find an entry for DN " + userDn + ", exception : " + le.getMessage() );
            // We can't find the entry in the DIT
            requestor.getIoSession().write( new PwdModifyResponseImpl(
                req.getMessageId(), ResultCodeEnum.NO_SUCH_OBJECT ) );

            return;
        }

        // We can try to update the userPassword now
        ModifyOperationContext modifyContext = new ModifyOperationContext( adminSession );
        modifyContext.setDn( userDn );
        List<Modification> modifications = new ArrayList<Modification>();
        Modification modification = null;

        if ( oldPassword != null )
        {
            modification = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE,
                SchemaConstants.USER_PASSWORD_AT, oldPassword );

            modifications.add( modification );
        }

        if ( newPassword != null )
        {
            if ( oldPassword == null )
            {
                modification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE,
                    SchemaConstants.USER_PASSWORD_AT, newPassword );
            }
            else
            {
                modification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE,
                    SchemaConstants.USER_PASSWORD_AT, newPassword );
            }

            modifications.add( modification );
        }

        modifyContext.setModItems( modifications );

        try
        {
            service.getOperationManager().modify( modifyContext );

            LOG.debug( "Password modified for user " + userDn );

            // Ok, all done
            requestor.getIoSession().write( new PwdModifyResponseImpl(
                req.getMessageId(), ResultCodeEnum.SUCCESS ) );
        }
        catch ( LdapException le )
        {
            LOG.error( "Cannot modify the password for user " + userDn + ", exception : " + le.getMessage() );
            // We can't modify the password
            requestor.getIoSession().write( new PwdModifyResponseImpl(
                req.getMessageId(), ResultCodeEnum.INVALID_CREDENTIALS ) );

            return;
        }
    }
View Full Code Here

            service.getOperationManager().modify( modifyContext );

            LOG.debug( "Password modified for user " + principalDn );

            // Ok, all done
            requestor.getIoSession().write( new PwdModifyResponseImpl(
                req.getMessageId(), ResultCodeEnum.SUCCESS ) );
        }
        catch ( LdapException le )
        {
            LOG.error( "Cannot modify the password for user " + principalDn + ", exception : " + le.getMessage() );
            // We can't modify the password
            requestor.getIoSession().write( new PwdModifyResponseImpl(
                req.getMessageId(), ResultCodeEnum.INVALID_CREDENTIALS ) );

            return;
        }
    }
View Full Code Here

            }
            catch ( LdapInvalidDnException lide )
            {
                LOG.error( "The user DN is invalid : " + userDn );
                // The userIdentity is not a DN : return with an error code.
                requestor.getIoSession().write( new PwdModifyResponseImpl(
                    req.getMessageId(), ResultCodeEnum.INVALID_DN_SYNTAX ) );

                return;
            }
        }

        byte[] oldPassword = req.getOldPassword();
        byte[] newPassword = req.getNewPassword();

        // First check if the user is bound or not
        if ( requestor.isAuthenticated() )
        {
            Dn principalDn = requestor.getCoreSession().getEffectivePrincipal().getDn();

            LOG.debug( "Trying to modify password for user " + principalDn );

            // First, check that the userDn is null : we can't change the password of someone else
            // except if we are admin
            if ( ( userDn != null ) && ( !userDn.equals( principalDn ) ) )
            {
                // Are we admin ?
                if ( !requestor.getCoreSession().isAdministrator() )
                {
                    // No : error
                    LOG.error( "Cannot access to another user's password to modify it" );
                    requestor.getIoSession().write( new PwdModifyResponseImpl(
                        req.getMessageId(), ResultCodeEnum.INSUFFICIENT_ACCESS_RIGHTS ) );
                }
                else
                {
                    // We are administrator, we can try to modify the user's credentials
                    modifyUserPassword( requestor, userDn, oldPassword, newPassword, req );
                }
            }
            else
            {
                // We are trying to modify our own password
                modifyOwnPassword( requestor, principalDn, oldPassword, newPassword, req );
            }
        }
        else
        {
            // The user is not authenticated : we have to use the provided userIdentity
            // and the oldPassword to check if the user is present
            BindOperationContext bindContext = new BindOperationContext( adminSession );
            bindContext.setDn( userDn );
            bindContext.setCredentials( oldPassword );

            try
            {
                service.getOperationManager().bind( bindContext );
            }
            catch ( LdapException le )
            {
                // We can't bind with the provided information : we thus can't
                // change the password...
                requestor.getIoSession().write( new PwdModifyResponseImpl(
                    req.getMessageId(), ResultCodeEnum.INVALID_CREDENTIALS ) );

                return;
            }

            // Ok, we were able to bind using the userIdentity and the password. Let's
            // modify the password now
            ModifyOperationContext modifyContext = new ModifyOperationContext( adminSession );
            modifyContext.setDn( userDn );
            List<Modification> modifications = new ArrayList<Modification>();
            Modification modification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE,
                SchemaConstants.USER_PASSWORD_AT, newPassword );
            modifications.add( modification );
            modifyContext.setModItems( modifications );

            try
            {
                service.getOperationManager().modify( modifyContext );

                // Ok, all done
                requestor.getIoSession().write( new PwdModifyResponseImpl(
                    req.getMessageId(), ResultCodeEnum.SUCCESS ) );
            }
            catch ( LdapException le )
            {
                // We can't modify the password
                requestor.getIoSession().write( new PwdModifyResponseImpl(
                    req.getMessageId(), ResultCodeEnum.INVALID_CREDENTIALS ) );

                return;
            }
        }
View Full Code Here

     * {@inheritDoc}
     */
    public static PwdModifyResponse getPwdModifyResponse()
    {
        // build the PwdModifyResponse message with replicationContexts
        return new PwdModifyResponseImpl();
    }
View Full Code Here

TOP

Related Classes of org.apache.directory.api.ldap.extras.extended.PwdModifyResponseImpl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.