Skip to content

Commit 53c0c03

Browse files
[ZEPPELIN-2913] support for both user and role
Change-Id: I5a83e5701d22ac40b37cc8c3d4c9414ef007b99c
1 parent 663918c commit 53c0c03

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.zeppelin.utils;
18+
19+
import java.io.IOException;
20+
import javax.servlet.ServletRequest;
21+
import javax.servlet.ServletResponse;
22+
import org.apache.shiro.subject.Subject;
23+
import org.apache.shiro.web.filter.authz.RolesAuthorizationFilter;
24+
25+
/**
26+
* Allows access if current user has at least one role of the specified list.
27+
* <p>
28+
* Basically, it's the same as {@link RolesAuthorizationFilter} but using {@literal OR} instead
29+
* of {@literal AND} on the specified roles.
30+
*/
31+
public class AnyOfRolesUserAuthorizationFilter extends RolesAuthorizationFilter {
32+
@Override
33+
public boolean isAccessAllowed(ServletRequest request, ServletResponse response,
34+
Object mappedValue) throws IOException {
35+
final Subject subject = getSubject(request, response);
36+
final String[] rolesArray = (String[]) mappedValue;
37+
38+
if (rolesArray == null || rolesArray.length == 0) {
39+
//no roles specified, so nothing to check - allow access.
40+
return true;
41+
}
42+
43+
for (String roleName : rolesArray) {
44+
if (subject.hasRole(roleName) || subject.getPrincipal().equals(roleName)) {
45+
return true;
46+
}
47+
}
48+
return false;
49+
}
50+
}

0 commit comments

Comments
 (0)