StyleTang commented on a change in pull request #6: URL: https://github.com/apache/rocketmq-dashboard/pull/6#discussion_r690464731
########## File path: src/main/java/org/apache/rocketmq/dashboard/permisssion/PermissionAspect.java ########## @@ -0,0 +1,82 @@ +/* + * 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.rocketmq.dashboard.permisssion; + +import java.util.List; +import java.util.Map; +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import org.apache.rocketmq.dashboard.config.RMQConfigure; +import org.apache.rocketmq.dashboard.exception.ServiceException; +import org.apache.rocketmq.dashboard.model.UserInfo; +import org.apache.rocketmq.dashboard.service.PermissionService; +import org.apache.rocketmq.dashboard.util.WebUtil; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +@Aspect +@Component +public class PermissionAspect { + + public static final String ORDINARY = "ordinary"; + public static final String ADMIN = "admin"; + + @Resource + private RMQConfigure configure; + + @Resource + private PermissionService permissionService; + + /** + * @Permission can be applied to the Controller class to implement Permission verification on all methods in the class + * can also be applied to methods in a class for fine control + */ + @Pointcut("@annotation(org.apache.rocketmq.dashboard.permisssion.Permission) || @within(org.apache.rocketmq.dashboard.permisssion.Permission)") + private void permission() { + + } + + @Around("permission()") + public Object checkPermission(ProceedingJoinPoint joinPoint) throws Throwable { + if (configure.isLoginRequired()) { + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + String url = request.getRequestURI(); + UserInfo userInfo = (UserInfo) request.getSession().getAttribute(WebUtil.USER_INFO); + if (userInfo == null || userInfo.getUser() == null) { + throw new ServiceException(-1, "user not login"); + } + int type = userInfo.getUser().getType(); + // 1:admin 0:ordinary + String loginUserRole = type == 1 ? ADMIN : ORDINARY; + // if it is admin, it could access all resources + if (loginUserRole == ADMIN) { + return joinPoint.proceed(); + } + Map<String, List<String>> map = permissionService.queryRolePerms(); Review comment: The permission check logic should not be here. It should belongs to PermissionService. suggest to use ``` permissionService.checkUriAvailable(uri , userInfo); ``` In this permission check aspect, just invoke it. ########## File path: src/main/java/org/apache/rocketmq/dashboard/permisssion/PermissionAspect.java ########## @@ -0,0 +1,82 @@ +/* + * 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.rocketmq.dashboard.permisssion; + +import java.util.List; +import java.util.Map; +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import org.apache.rocketmq.dashboard.config.RMQConfigure; +import org.apache.rocketmq.dashboard.exception.ServiceException; +import org.apache.rocketmq.dashboard.model.UserInfo; +import org.apache.rocketmq.dashboard.service.PermissionService; +import org.apache.rocketmq.dashboard.util.WebUtil; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +@Aspect +@Component +public class PermissionAspect { + + public static final String ORDINARY = "ordinary"; + public static final String ADMIN = "admin"; + + @Resource + private RMQConfigure configure; + + @Resource + private PermissionService permissionService; + + /** + * @Permission can be applied to the Controller class to implement Permission verification on all methods in the class + * can also be applied to methods in a class for fine control + */ + @Pointcut("@annotation(org.apache.rocketmq.dashboard.permisssion.Permission) || @within(org.apache.rocketmq.dashboard.permisssion.Permission)") + private void permission() { + + } + + @Around("permission()") + public Object checkPermission(ProceedingJoinPoint joinPoint) throws Throwable { + if (configure.isLoginRequired()) { + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + String url = request.getRequestURI(); + UserInfo userInfo = (UserInfo) request.getSession().getAttribute(WebUtil.USER_INFO); + if (userInfo == null || userInfo.getUser() == null) { + throw new ServiceException(-1, "user not login"); + } + int type = userInfo.getUser().getType(); + // 1:admin 0:ordinary + String loginUserRole = type == 1 ? ADMIN : ORDINARY; Review comment: Can we add a UserRoleEnum, it seems so many place use it. ########## File path: src/main/java/org/apache/rocketmq/dashboard/service/impl/PermissionServiceImpl.java ########## @@ -0,0 +1,119 @@ +/* + * 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.rocketmq.dashboard.service.impl; + +import com.alibaba.fastjson.JSONObject; +import java.io.File; +import java.io.FileReader; +import java.io.InputStream; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import javax.annotation.Resource; +import org.apache.rocketmq.dashboard.config.RMQConfigure; +import org.apache.rocketmq.dashboard.exception.ServiceException; +import org.apache.rocketmq.dashboard.service.PermissionService; +import org.apache.rocketmq.srvutil.FileWatchService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Service; +import org.yaml.snakeyaml.Yaml; + +@Service +public class PermissionServiceImpl implements PermissionService, InitializingBean { + + @Resource + RMQConfigure configure; + + FileBasedPermissionStore fileBasedPermissionStore; + + @Override + public Map<String, List<String>> queryRolePerms() { + return fileBasedPermissionStore.rolePerms; + } + + @Override + public void afterPropertiesSet() { + if (configure.isLoginRequired()) { + fileBasedPermissionStore = new FileBasedPermissionStore(configure); + } + } + + public static class FileBasedPermissionStore { Review comment: There are some duplicate code here, it would be better if we can reuse it. ``` org.apache.rocketmq.dashboard.service.impl.UserServiceImpl.FileBasedUserInfoStore ``` ########## File path: src/main/java/org/apache/rocketmq/dashboard/service/impl/PermissionServiceImpl.java ########## @@ -0,0 +1,119 @@ +/* + * 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.rocketmq.dashboard.service.impl; + +import com.alibaba.fastjson.JSONObject; +import java.io.File; +import java.io.FileReader; +import java.io.InputStream; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import javax.annotation.Resource; +import org.apache.rocketmq.dashboard.config.RMQConfigure; +import org.apache.rocketmq.dashboard.exception.ServiceException; +import org.apache.rocketmq.dashboard.service.PermissionService; +import org.apache.rocketmq.srvutil.FileWatchService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Service; +import org.yaml.snakeyaml.Yaml; + +@Service +public class PermissionServiceImpl implements PermissionService, InitializingBean { + + @Resource + RMQConfigure configure; + + FileBasedPermissionStore fileBasedPermissionStore; Review comment: suggest to use private but not default. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
