paul8263 commented on code in PR #5046:
URL: https://github.com/apache/zeppelin/pull/5046#discussion_r2303363343


##########
zeppelin-server/src/main/java/org/apache/zeppelin/realm/RoleMappingLdapRealm.java:
##########
@@ -0,0 +1,86 @@
+/*
+ *  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.zeppelin.realm;
+
+import org.apache.shiro.authz.AuthorizationInfo;
+import org.apache.shiro.authz.SimpleAuthorizationInfo;
+import org.apache.shiro.realm.ldap.DefaultLdapRealm;
+import org.apache.shiro.realm.ldap.LdapContextFactory;
+import org.apache.shiro.subject.PrincipalCollection;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.naming.NamingException;
+import javax.naming.ldap.LdapContext;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * RoleMappingLdapRealm is a simple LDAP realm that returns configured role 
for specific users.
+ * Example configurations:
+ * ldapRealm = org.apache.zeppelin.realm.RoleMappingLdapRealm
+ * ldapRealm.rolesByUsername = user1:admin,user2:user,user3:user,user4:user
+ * ldapRealm.defaultRole = user
+ */
+public class RoleMappingLdapRealm extends DefaultLdapRealm {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(RoleMappingLdapRealm.class);
+
+  private String defaultRole = "admin";
+
+  private final Map<String, String> rolesByUsername = new LinkedHashMap<>();
+
+
+  public String getDefaultRole() {
+    return defaultRole;
+  }
+
+  public void setDefaultRole(String defaultRole) {
+    this.defaultRole = defaultRole;
+  }
+
+  public Map<String, String> getRolesByUsername() {
+    return rolesByUsername;
+  }
+
+  public void setRolesByUsername(Map<String, String> rolesByUsername) {
+    this.rolesByUsername.putAll(rolesByUsername);
+  }
+
+  @Override
+  public AuthorizationInfo queryForAuthorizationInfo(PrincipalCollection 
principals,
+                                                     LdapContextFactory 
ldapContextFactory) throws NamingException {
+    String username = (String) getAvailablePrincipal(principals);
+    LdapContext ldapContext = ldapContextFactory.getSystemLdapContext();
+    Set<String> roleNames = getRoleNamesForUser(username, ldapContext, 
getUserDnTemplate());
+    return new SimpleAuthorizationInfo(roleNames);
+  }
+
+  public Set<String> getRoleNamesForUser(String username, LdapContext 
ldapContext,
+                                         String userDnTemplate) {
+    String role = rolesByUsername.get(username) == null ? defaultRole : 
rolesByUsername.get(username);
+
+    LOGGER.debug("For user {}, RoleMappingLdapRealm returns {} as the role", 
username, role);
+    Set<String> roles = new HashSet<>();
+    roles.add(role);
+    return roles;

Review Comment:
   OK. I will use Set.of(...)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscr...@zeppelin.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to