Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

how to get admin session using service user

Avatar

Avatar
Validate 25
Level 3
manikanthar1295
Level 3

Likes

7 likes

Total Posts

66 posts

Correct Reply

0 solutions
Top badges earned
Validate 25
Give Back 10
Give Back 5
Give Back 3
Give Back
View profile

Avatar
Validate 25
Level 3
manikanthar1295
Level 3

Likes

7 likes

Total Posts

66 posts

Correct Reply

0 solutions
Top badges earned
Validate 25
Give Back 10
Give Back 5
Give Back 3
Give Back
View profile
manikanthar1295
Level 3

29-04-2021

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 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Boost 25
Employee
markus_bulla_adobe
Employee

Likes

40 likes

Total Posts

46 posts

Correct Reply

18 solutions
Top badges earned
Boost 25
Affirm 10
Applaud 5
Affirm 5
Establish
View profile

Avatar
Boost 25
Employee
markus_bulla_adobe
Employee

Likes

40 likes

Total Posts

46 posts

Correct Reply

18 solutions
Top badges earned
Boost 25
Affirm 10
Applaud 5
Affirm 5
Establish
View profile
markus_bulla_adobe
Employee

29-04-2021

Hi @manikanthar1295!

Please refer to the following documentation:

It points to

What you need to do:

 

SlingRepository.loginService()

or

ResourceResolverFactory.getServiceResourceResolver()

 

Hope that helps!

Answers (2)

Answers (2)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

582 likes

Total Posts

564 posts

Correct Reply

218 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

582 likes

Total Posts

564 posts

Correct Reply

218 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

29-04-2021

@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.

 

Avatar

Avatar
Affirm 100
Level 10
asutosh_jena
Level 10

Likes

373 likes

Total Posts

480 posts

Correct Reply

133 solutions
Top badges earned
Affirm 100
Ignite 1
Establish
Give Back 50
Give Back 5
View profile

Avatar
Affirm 100
Level 10
asutosh_jena
Level 10

Likes

373 likes

Total Posts

480 posts

Correct Reply

133 solutions
Top badges earned
Affirm 100
Ignite 1
Establish
Give Back 50
Give Back 5
View profile
asutosh_jena
Level 10

29-04-2021

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