1 Reply Latest reply on Jun 13, 2013 6:12 AM by sivaprasadreddys

    How to lock the page by user?

    sivaprasadreddys Level 1

      Hi,

       

         I've a requirement where i need to lock the page by group/user. Here is my work around.

       

      1. I'm trying to lock the page using  page.lock();   When I use page.lock(), it is locked by admin.

       

      2. Later, I tried to get the user session using session impersonate and then locked the page using page.lock(). but still it is locked by admin.

         

       

      Quick responses would be more helpful.

       

       

       

      Thanks in Advance

       

      Siva

        • 1. Re: How to lock the page by user?
          sivaprasadreddys Level 1

          Hi Team,

           

          Here is my work around:

           

          import javax.jcr.Node;

          import javax.jcr.NodeIterator;

          import javax.jcr.Session;

           

           

          import org.apache.felix.scr.annotations.Component;

          import org.apache.felix.scr.annotations.Properties;

          import org.apache.felix.scr.annotations.Property;

          import org.apache.felix.scr.annotations.Reference;

          import org.apache.felix.scr.annotations.Service;

          import org.apache.jackrabbit.api.JackrabbitSession;

          import org.apache.jackrabbit.api.security.user.UserManager;

          import org.osgi.framework.Constants;

          import org.slf4j.Logger;

          import org.slf4j.LoggerFactory;

           

          import com.adobe.granite.workflow.WorkflowException;

          import com.adobe.granite.workflow.WorkflowSession;

          import com.adobe.granite.workflow.exec.WorkflowProcess;

          import com.adobe.granite.workflow.exec.WorkItem;

          import com.adobe.granite.workflow.metadata.MetaDataMap;

          import com.adobe.granite.workflow.model.WorkflowNode;

          import org.apache.sling.jcr.resource.JcrResourceUtil;

          import com.day.cq.wcm.api.Page;

          import org.apache.sling.api.resource.Resource;

          import org.apache.sling.api.resource.ResourceResolver;

          import org.apache.sling.api.resource.ResourceResolverFactory;

           

          import com.day.cq.wcm.api.Page;

          import com.day.cq.wcm.api.PageManager;

          import com.day.cq.wcm.api.PageManagerFactory;

           

          import org.apache.jackrabbit.api.security.user.Authorizable;

          import org.apache.jackrabbit.api.security.user.AuthorizableExistsException;

          import org.apache.jackrabbit.api.security.user.Group;

          import org.apache.jackrabbit.api.security.user.UserManager;

           

          import org.apache.sling.jcr.api.SlingRepository;

          import javax.jcr.SimpleCredentials;

           

          @Component

          @Service

          @Properties( {

                  @Property(name = Constants.SERVICE_DESCRIPTION, value = "Lock "),

                  @Property(name = Constants.SERVICE_VENDOR, value = "Adobe"),

                  @Property(name = "process.label", value = "Custom Lock Test") })

           

           

          public class CustomLock implements WorkflowProcess

          {

              private static final Logger log = LoggerFactory

              .getLogger(CustomLock.class);

           

           

              @Reference

              private ResourceResolverFactory resourceResolverFactory;

                    @Reference

              private PageManagerFactory pageManagerFactory;

           

           

              @Reference

              SlingRepository repo;

           

           

                    public void execute(WorkItem item, WorkflowSession wfsession, MetaDataMap metaData)

                      throws WorkflowException {

           

           

                  log.error("Lock process started");

           

           

                  String payload = (String) item.getWorkflowData().getPayload();

                  log.error(" >> Payload Path:{}", payload);

                  Node contentNode;

                  Session session;

                  ResourceResolver resolver=null;

                  try{

           

           

                                    Session adminSession = repo.loginAdministrative(null);

                                    session = adminSession.impersonate (new SimpleCredentials("usco1",new char[0]));

                                    adminSession.logout();

           

           

                                   log.error("Session.getUserId:{}",session.getUserID());

           

           

                                   contentNode = JcrResourceUtil.createPath(payload, "nt:resource","nt:resource",session, true);

                                   log.error("Node Path:{}",contentNode.getPath());

                                  resolver = resourceResolverFactory.getAdministrativeResourceResolver(null);

                                  Resource r = resolver.getResource(contentNode.getPath());

           

           

                                   if(r !=null){

                                        Page page = r.adaptTo(Page.class);

           

           

                                      log.error("Paage title:{}",page.getTitle());

           

           

                                     log.error("Before Lock - Session.get User ID:{}",session.getUserID());

                                     page.lock();

           

                                    log.error("Now Page is locked? :{}",page.isLocked());

           

                                     String lockOwner = page.getLockOwner();

           

                                     log.error("Lock owner:{}",lockOwner);      // output is : Lock owner : admin

           

           

                           }

                          else

                                    log.error("Resource is null");

           

           

                  }catch(Exception e)

                  {

                      log.error("Exception:{}",e.getMessage());

                  }

           

           

           

           

              }

          }