You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2018/05/15 12:49:09 UTC

[sling-org-apache-sling-jcr-jackrabbit-usermanager] branch master updated: SLING-7668 - adding adaptorFactory for getting current User/Authorizable from resourceResolve

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-jackrabbit-usermanager.git


The following commit(s) were added to refs/heads/master by this push:
     new 4f7e992  SLING-7668 - adding adaptorFactory for getting current User/Authorizable from resourceResolve
4f7e992 is described below

commit 4f7e992940bb64033d12183eb0da387eaa67a534
Author: Dominik Suess <su...@adobe.com>
AuthorDate: Tue May 15 12:28:42 2018 +0200

    SLING-7668 - adding adaptorFactory for getting current User/Authorizable from resourceResolve
---
 .../impl/AuthorizableAdapterFactory.java           | 63 ++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/src/main/java/org/apache/sling/jackrabbit/usermanager/impl/AuthorizableAdapterFactory.java b/src/main/java/org/apache/sling/jackrabbit/usermanager/impl/AuthorizableAdapterFactory.java
new file mode 100644
index 0000000..2b7ccd4
--- /dev/null
+++ b/src/main/java/org/apache/sling/jackrabbit/usermanager/impl/AuthorizableAdapterFactory.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.sling.jackrabbit.usermanager.impl;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.jackrabbit.api.JackrabbitSession;
+import org.apache.jackrabbit.api.security.user.Authorizable;
+import org.apache.jackrabbit.api.security.user.User;
+import org.apache.jackrabbit.api.security.user.UserManager;
+import org.apache.sling.api.adapter.AdapterFactory;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.osgi.service.component.annotations.Component;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component(service = AdapterFactory.class, property = {
+        AdapterFactory.ADAPTER_CLASSES + "=org.apache.jackrabbit.api.security.user.User",
+        AdapterFactory.ADAPTER_CLASSES + "=org.apache.jackrabbit.api.security.user.Authorizable",
+        AdapterFactory.ADAPTABLE_CLASSES + "=org.apache.sling.api.resource.ResourceResolver" })
+public class AuthorizableAdapterFactory implements AdapterFactory {
+
+    /**
+     * default log
+     */
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public <AdapterType> AdapterType getAdapter(final Object adaptable, final Class<AdapterType> type) {
+        if (adaptable instanceof ResourceResolver && type == User.class) {
+            Session session = ((ResourceResolver) adaptable).adaptTo(Session.class);
+            if (session instanceof JackrabbitSession) {
+                JackrabbitSession jackrabbitSession = (JackrabbitSession) session;
+                try {
+                    UserManager um = jackrabbitSession.getUserManager();
+                    Authorizable authorizable = um.getAuthorizable(jackrabbitSession.getUserID());
+                    return (AdapterType) authorizable;
+                } catch (RepositoryException e) {
+                    log.warn("User cannot read own authorizable.", e);
+                }
+            }
+        }
+        return null;
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
rombert@apache.org.