User create()

in src/main/java/org/apache/directory/fortress/core/impl/UserDAO.java [236:371]


    User create( User entity ) throws CreateException
    {
        LdapConnection ld = null;
        boolean setRelaxControl = false;
        try
        {
            entity.setInternalId();

            String dn = getDn( entity.getUserId(), entity.getContextId() );

            Entry myEntry = new DefaultEntry( dn );

            myEntry.add( SchemaConstants.OBJECT_CLASS_AT, getUserObjectClass() );
            myEntry.add( GlobalIds.FT_IID, entity.getInternalId() );
            myEntry.add( SchemaConstants.UID_AT, entity.getUserId() );

            // CN is required on inetOrgPerson object class, if caller did not set, use the userId:
            if ( StringUtils.isEmpty( entity.getCn() ) )
            {
                entity.setCn( entity.getUserId() );
            }

            myEntry.add( SchemaConstants.CN_AT, entity.getCn() );

            // SN is required on inetOrgPerson object class, if caller did not set, use the userId:
            if ( StringUtils.isEmpty( entity.getSn() ) )
            {
                entity.setSn( entity.getUserId() );
            }

            myEntry.add( SchemaConstants.SN_AT, entity.getSn() );

            if( StringUtils.isNotEmpty( entity.getPassword() ))
            {
                myEntry.add( SchemaConstants.USER_PASSWORD_AT, entity.getPassword() );
            }
            else if( !Config.getInstance().getBoolean( GlobalIds.USER_CREATION_PASSWORD_FIELD, false ) )
            {
                myEntry.add( SchemaConstants.USER_PASSWORD_AT, new String() );
            }

            myEntry.add( SchemaConstants.DISPLAY_NAME_AT, StringUtils.isNotEmpty( entity.getDisplayName() ) ? entity.getDisplayName() : entity.getCn() );

            if ( StringUtils.isNotEmpty( entity.getTitle() ) )
            {
                myEntry.add( SchemaConstants.TITLE_AT, entity.getTitle() );
            }

            if ( StringUtils.isNotEmpty( entity.getEmployeeType() ) )
            {
                myEntry.add( EMPLOYEE_TYPE, entity.getEmployeeType() );
            }

            // These are multi-valued attributes, use the util function to load.
            // These items are optional.  The utility function will return quietly if item list is empty:
            loadAttrs( entity.getPhones(), myEntry, SchemaConstants.TELEPHONE_NUMBER_AT );
            loadAttrs( entity.getMobiles(), myEntry, MOBILE );
            loadAttrs( entity.getEmails(), myEntry, SchemaConstants.MAIL_AT );

            // The following attributes are optional:
            if ( entity.isSystem() != null )
            {
                myEntry.add( SYSTEM_USER, entity.isSystem().toString().toUpperCase() );
            }

            // If password policy is set and either openldap or apacheds in use:
            if ( ( Config.getInstance().isOpenldap() || Config.getInstance().isApacheds() ) && StringUtils.isNotEmpty( entity.getPwPolicy() ) )
            {
                myEntry.add( OPENLDAP_POLICY_SUBENTRY, PolicyDAO.getPolicyDn( entity ) );
                setRelaxControl = true;
            }

            if ( StringUtils.isNotEmpty( entity.getOu() ) )
            {
                myEntry.add( SchemaConstants.OU_AT, entity.getOu() );
            }

            if ( StringUtils.isNotEmpty( entity.getDescription() ) )
            {
                myEntry.add( SchemaConstants.DESCRIPTION_AT, entity.getDescription() );
            }

            // props are optional as well:
            // Add "initial" property here.
            entity.addProperty( "init", "" );
            loadProperties( entity.getProperties(), myEntry, GlobalIds.PROPS );
            // map the userid to the name field in constraint:
            entity.setName( entity.getUserId() );
            myEntry.add( GlobalIds.CONSTRAINT, ConstraintUtil.setConstraint( entity ) );
            loadAddress( entity.getAddress(), myEntry );

            if ( ArrayUtils.isNotEmpty( entity.getJpegPhoto() ) )
            {
                myEntry.add( JPEGPHOTO, entity.getJpegPhoto() );
            }

            // Load the posixAccount attributes required by the RFC2307bis (proposed) IETF standard:
            if ( IS_RFC2307 )
            {
                loadPosixIds( entity );

                // required on PosixAccount:
                myEntry.add( GlobalIds.UID_NUMBER, entity.getUidNumber() );
                myEntry.add( GlobalIds.GID_NUMBER, entity.getGidNumber() );

                // if not set, generate a sensible default:
                if ( StringUtils.isEmpty( entity.getHomeDirectory() ) )
                {
                    entity.setHomeDirectory( "/home/" + entity.getUserId() );
                }

                // Also required on PosixAccount:
                myEntry.add( HOME_DIRECTORY, entity.getHomeDirectory() );
            }

            ld = getAdminConnection();
            add( ld, myEntry, entity, setRelaxControl );
            entity.setDn( dn );
        }
        catch ( LdapEntryAlreadyExistsException e )
        {
            String error = "create userId [" + entity.getUserId() + "] failed, already exists in directory";
            throw new CreateException( GlobalErrIds.USER_ADD_FAILED_ALREADY_EXISTS, error, e );
        }
        catch ( LdapException e )
        {
            String error = "create userId [" + entity.getUserId() + "] caught LDAPException=" + e;
            throw new CreateException( GlobalErrIds.USER_ADD_FAILED, error, e );
        }
        finally
        {
            closeAdminConnection( ld );
        }

        return entity;
    }