system user

sathyaprakasha1

28-03-2019

Hi Team,

I have created a systemUser and add the same into user mapper configuration like below

com.adobe.aem.guides.aem-guides-wknd.core:systemUser=systemUser

and getting the same via param.put(ResourceResolverFactory.SUBSERVICE,"systemUser");

Accepted Solutions (1)

Accepted Solutions (1)

Answers (14)

Answers (14)

sathya_prakasha

29-03-2019

package com.adobe.aem.guides.wknd.core.impl;

import java.util.HashMap;

import java.util.Map;

import javax.jcr.Node;

import javax.jcr.RepositoryException;

import javax.jcr.Session;

import javax.jcr.ValueFormatException;

import javax.jcr.lock.LockException;

import javax.jcr.nodetype.ConstraintViolationException;

import javax.jcr.version.VersionException;

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

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

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

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

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.adobe.aem.guides.wknd.core.SearchService;

@Component

public class SearchServiceImpl implements SearchService {

   

@Reference

ResourceResolverFactory resourceResolverFactory;

protected final Logger log = LoggerFactory.getLogger(this.getClass());

    @Override

public void addProperty() {

// TODO Auto-generated method stub

Map<String, Object> param = new HashMap<>();

param.put(ResourceResolverFactory.SUBSERVICE,"systemUser");

ResourceResolver resourceResolver = null;

try {

resourceResolver = resourceResolverFactory.getResourceResolver(param);

log.info(resourceResolver.getUserID());

Resource pageResource = resourceResolver.getResource("/content/wknd/jcr:content");

log.info("page"+pageResource.getPath());

Node myNode = pageResource.adaptTo(Node.class);

myNode.setProperty("author","sathya");

Session session = resourceResolver.adaptTo(Session.class);

session.save();

log.info("Node saved sucessfully");

} catch (LoginException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ValueFormatException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (VersionException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (LockException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ConstraintViolationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (RepositoryException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

BrijeshYadav

29-03-2019

By normal java UserTest class, you mean Sling Model class for slightly component? i.e.

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

public class UserTest {

   @SlingObject
   private ResourceResolver resourceResolver;

  ...........................

  ........................

}


Or how you inject or initialize ResourceResolverFactory?

sathya_prakasha

29-03-2019

Hi Yadav,

I am using the same line of code in both the classes . Only difference is usertest is a servlet class and SearchService is a normal java class and calling a addproperty() method from slightly.

Map<String, Object> param = new HashMap<>();

param.put(ResourceResolverFactory.SUBSERVICE,"systemUser");

ResourceResolver resourceResolver = null;

try {

resourceResolver = resourceResolverFactory.getResourceResolver(param);

log.info(resourceResolver.getUserID());

BrijeshYadav

29-03-2019

As you have verified all system user configurations and mapping, so let's dig into the code.
What are the annotations you are using? Could you please share the complete code for both the java classes?

com.aem.example.core.userTest.java
com.aem.example.core.impl.SearchService.java

sathya_prakasha

28-03-2019

Both are same in bundle...am i getting systemUser for class A and anonymous user for class B.

And also we are facing one more issue.. If i create a new package with existing maven project...its not getting deployed.. Anything i am missing here?

sathya_prakasha

28-03-2019

HI Team,

core/pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--

|  Copyright 2017 Adobe Systems Incorporated

|

|  Licensed under the Apache License, Version 2.0 (the "License");

|  you may not use this file except in compliance with the License.

|  You may obtain a copy of the License at

|

|      http://www.apache.org/licenses/LICENSE-2.0

|

|  Unless required by applicable law or agreed to in writing, software

|  distributed under the License is distributed on an "AS IS" BASIS,

|  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

|  See the License for the specific language governing permissions and

|  limitations under the License.

-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>

        <groupId>com.adobe.aem.guides</groupId>

        <artifactId>aem-guides-wknd</artifactId>

        <version>0.0.1-SNAPSHOT</version>

        <relativePath>../pom.xml</relativePath>

    </parent>

    <artifactId>aem-guides-wknd.core</artifactId>

    <packaging>bundle</packaging>

    <name>WKND Sites Project - Core</name>

    <description>Core bundle for WKND Sites Project</description>

    <build>

        <plugins>

            <plugin>

                <groupId>org.apache.sling</groupId>

                <artifactId>maven-sling-plugin</artifactId>

            </plugin>

            <plugin>

                <groupId>org.apache.felix</groupId>

                <artifactId>maven-bundle-plugin</artifactId>

                <extensions>true</extensions>

                <configuration>

                    <instructions>

                        <!-- Import any version of javax.inject, to allow running on multiple versions of AEM -->

                        <Import-Package>javax.inject;version=0.0.0,*</Import-Package>

                        <Sling-Model-Packages>

                            com.adobe.aem.guides.wknd.core

                        </Sling-Model-Packages>

                    </instructions>

                </configuration>

            </plugin>

        </plugins>

    </build>

    <dependencies>

        <!-- OSGi Dependencies -->

        <dependency>

            <groupId>org.osgi</groupId>

            <artifactId>osgi.core</artifactId>

        </dependency>

        <dependency>

            <groupId>org.osgi</groupId>

            <artifactId>osgi.cmpn</artifactId>

        </dependency>

        <dependency>

            <groupId>org.osgi</groupId>

            <artifactId>osgi.annotation</artifactId>

        </dependency>

        <!-- Other Dependencies -->

        <dependency>

            <groupId>org.slf4j</groupId>

            <artifactId>slf4j-api</artifactId>

        </dependency>

        <dependency>

            <groupId>javax.jcr</groupId>

            <artifactId>jcr</artifactId>

        </dependency>

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>servlet-api</artifactId>

        </dependency>

        <dependency>

            <groupId>com.adobe.aem</groupId>

            <artifactId>uber-jar</artifactId>

            <classifier>apis</classifier>

        </dependency>

        <dependency>

            <groupId>org.apache.sling</groupId>

            <artifactId>org.apache.sling.models.api</artifactId>

        </dependency>

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

        </dependency>

        <dependency>

            <groupId>org.mockito</groupId>

            <artifactId>mockito-core</artifactId>

        </dependency>

        <dependency>

            <groupId>junit-addons</groupId>

            <artifactId>junit-addons</artifactId>

        </dependency>

        <dependency>

            <groupId>org.apache.sling</groupId>

            <artifactId>org.apache.sling.testing.sling-mock</artifactId>

        </dependency>

        <dependency>

            <groupId>uk.org.lidalia</groupId>

            <artifactId>slf4j-test</artifactId>

        </dependency>

    </dependencies>

</project>

Gaurav-Behl

MVP

28-03-2019

Could you share stacktrace and pom.xml's maven-bundle-plugin snippet?

Did you follow the tutorial as mentioned and tried above link shared by Scott?

sathya_prakasha

28-03-2019

Thanks for your reply..

I have created two different java class and try to consume same systemuser in both the class for different purpose.

By default ..this systemuser has all the permission(Read & Write)

com.aem.example.core.userTest.java & com.aem.example.core.impl.SearchService.java

UserTest.java - Its working fine

SearchService - Its throwing a anonymous user

Please find the below screenshot for your reference.

1720708_pastedImage_0.png

Gaurav-Behl

MVP

28-03-2019

Could you validate if your bundle's symbolic name is correct per pom.xml - com.adobe.aem.guides.aem-guides-wknd.core

syntax:

<service-name>[:<subservice-name>]=<authorizable id of a JCR system user>

Could you explain more about "systemuser is working some java class not for all .." ?

sathya_prakasha

28-03-2019

Hi ,

Thanks for your reply .

Can you pls check my query here pls . systemuser is working some java class not for all ..

example :

com.aem.example.core.userTest - Its working ..its giving user id as systemUser

com.aem.example.core.impl.SearchService - Its not working ..its giving anonymous