jcrhippocms

Connection to repository Hippo cms


I'm still trying to get to the repository to add new users. My component is connected to the side and I have all needed value in FormMap.

Problem is that i don't know how to do this. In my last question Registration users in Hippo cms I got answer that I need connect component to /hippo:configuration/hippo:users.

How to do?

This is my actual component:

package org.example.components;
import javax.jcr.Session;

import org.hippoecm.hst.component.support.bean.BaseHstComponent;
import org.hippoecm.hst.core.component.HstComponentException;
import org.hippoecm.hst.core.component.HstRequest;
import org.hippoecm.hst.core.component.HstResponse;
import org.hippoecm.hst.component.support.forms.FormMap;
import org.hippoecm.hst.component.support.forms.FormUtils;
import org.hippoecm.hst.component.support.forms.FormField;
import org.hippoecm.hst.content.annotations.Persistable;
import org.hippoecm.hst.content.beans.Node;
import org.hippoecm.hst.content.beans.standard.HippoFolderBean;


public class SignUpComponent extends BaseHstComponent {

@Override
public void doBeforeRender(HstRequest request, HstResponse response) {
    super.doBeforeRender(request, response);
}

@Persistable
@Override
public void doAction(HstRequest request, HstResponse response) throws HstComponentException {
    FormMap map = new FormMap(request, new String[]{"username","email","password"});
    FormField username = map.getField("username");
    FormField password = map.getField("password");
    FormField email = map.getField("email");

    try {
        // NOTE: This session will be logged out automatically in the normal HST request processing thread.
        Session persistableSession = request.getRequestContext().getSession();
    } catch (Exception e) {

    }
    Node users = persistableSession.getNode("/hippo:configuration/hippo:users");

}

Although imports Node does not work

error: cannot find symbol

I also tried

Node users = getSiteContentBaseBean(request).getNode().getSession().getRootNode().getNode("/hippo:configuration/hippo:users"); 

Solution

  • Well for persisting changes to a document the following code works. I've kinda modified it towards your example.

    import javax.jcr.Node;
    import javax.jcr.RepositoryException;
    import javax.jcr.Session;
    
    import org.hippoecm.hst.component.support.bean.BaseHstComponent;
    import org.hippoecm.hst.core.component.HstComponentException;
    import org.hippoecm.hst.core.component.HstRequest;
    import org.hippoecm.hst.core.component.HstResponse;
    import org.hippoecm.repository.api.HippoNodeType;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class PersistenceExampleComponent extends BaseHstComponent {
    
        public static final Logger log = LoggerFactory.getLogger(PersistenceExampleComponent.class);
        public static final String USERS_PATH = "/hippo:configuration/hippo:users";
    
        @Override
        public void doAction(final HstRequest request, final HstResponse response) throws HstComponentException {
            Session writableSession = null;
            try {
                writableSession = this.getPersistableSession(request);
                Node usersNode = writableSession.getRootNode().getNode(USERS_PATH);
                final Node someusername = usersNode.addNode("someusername", HippoNodeType.NT_USER);
                writableSession.save();
            } catch (RepositoryException e) {
                log.error(e.getMessage());
            } finally {
                if(writableSession != null) {
                    writableSession.logout();
                }
            }
        }
    }
    

    Now you need to be aware that by default the user with which the site connects to the repository may not have the proper rights to write into this folder. You might want to read the Access rights page for HST based writes and if that's not enough you will need to learn the concept of repository security and it's domains to alter the existing domains to meet your needs.

    You also might want to take a look at the follow code snippet which is an example of how to store information from a component into the repository.