Expand my Community achievements bar.

SOLVED

how to get admin session using service user

Avatar

Level 6

Hi Team,

 

I have created service user and i have added 

i have given permission to service user

Now question is how to get the session using  service user.

can any one give coding example so that i can understand 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi @manikanthar1295!

Please refer to the following documentation:

It points to

What you need to do:

 

SlingRepository.loginService()

or

ResourceResolverFactory.getServiceResourceResolver()

 

Hope that helps!

View solution in original post

3 Replies

Avatar

Community Advisor

Hi @manikanthar1295 

 

You can create a JCRUtilService class which will read the service user and then get the resource resolver from it.

Once you have the resource resolver available, you can get the session from it.

 

This Util class can be used anywhere you need an admin resource resolver.

 

Please see the below code:

 

@Component(service = JcrUtilService.class, immediate = true, property = {"process.label=Project - JCR Service",
Constants.SERVICE_DESCRIPTION + "=Project - JCR Service", Constants.SERVICE_VENDOR + "=Project",
Constants.SERVICE_RANKING + ":Integer=1001"})

public class JcrUtilService {
private static final Logger LOG = LoggerFactory.getLogger(JcrUtilService.class);

private static final String SUBSERVICE = "dataread";

@Activate
protected void activate(final ComponentContext componentContext) throws RepositoryException, LoginException {
LOG.info("JCR Util Service :: Activate Method");
}

@Deactivate
protected void deactivate(final ComponentContext componentContext) throws RepositoryException, LoginException {
LOG.info("JCR Util Service :: Deactivate Method");
}

public static Object getService(final Class<?> serviceType) {
final Bundle bundle = FrameworkUtil.getBundle(serviceType);
final BundleContext bundleContext = bundle.getBundleContext();
final ServiceReference serviceReference = bundleContext.getServiceReference(serviceType.getName());

return bundleContext.getService(serviceReference);
}

private static ResourceResolverFactory getResourceResolverFactory() {
return (ResourceResolverFactory) getService(ResourceResolverFactory.class);
}

public static ResourceResolver getResourceResolver() {
ResourceResolver resourceResolver = null;
try {
Map<String, Object> param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, SUBSERVICE);
resourceResolver = getResourceResolverFactory().getServiceResourceResolver(param);
} catch (LoginException e) {
LOG.error("Error while getting Resource Resolver", e);
}
return resourceResolver;
}
}

 

try (ResourceResolver resourceResolver = JcrUtilService.getResourceResolver()) {
Session session = resourceResolver.adaptTo(Session.class); // Session Object obtained from System User

} catch (RepositoryException e) {
log.error("Repository Exception", e);
}

Service User permission with dataread. Create an OSGi config and add the below properties.

org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.xml

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:OsgiConfig"
service.ranking="0"
user.default="service-user"
user.mapping="[com.adobe.granite.oauth.server=oauthservice,com.project:dataread=service-user]"/>

 

Hope this helps!

 

Thanks

Avatar

Correct answer by
Employee Advisor

Hi @manikanthar1295!

Please refer to the following documentation:

It points to

What you need to do:

 

SlingRepository.loginService()

or

ResourceResolverFactory.getServiceResourceResolver()

 

Hope that helps!

Avatar

Community Advisor

@manikanthar1295,

If you are a visual learner (by watching videos), here is a great tutorial on how this can be done. End to end configuration + code will be available - https://www.youtube.com/watch?v=H_ljzX9B4Q0

The video goes through step by step, creating a system user, setting up OSGI configuration (service user)), and includes an OSGI service example.