Commit d7b55abd authored by lgcareer's avatar lgcareer Committed by bao liang
Browse files

[Fix issue #1770]check udf and data source in order to fix issue 1770 (#1817)

* if login user is admin,it will has permission to query all udf function

* check udf and data source in order to fix issue 1770

* check udf and data source in order to fix issue 1770

* check udf and data source in order to fix issue 1770

* check udf and data source in order to fix issue 1770

* check udf and data source in order to fix issue 1770

* revert common.properties

* update the test method name

* remove useless code and import in unit test

* refactor has permission and check permission
parent a1b2aa22
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
/*
 * 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.dolphinscheduler.common.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;

/**
 * Authorization type
 */
public enum AuthorizationType {
    /**
     * 0 RESOURCE_FILE;
     * 1 DATASOURCE;
     * 2 UDF;
     */
    RESOURCE_FILE(0, "resource file"),
    DATASOURCE(1, "data source"),
    UDF(2, "udf function");

    AuthorizationType(int code, String descp){
        this.code = code;
        this.descp = descp;
    }

    @EnumValue
    private final int code;
    private final String descp;

    public int getCode() {
        return code;
    }

    public String getDescp() {
        return descp;
    }
}
+21 −10
Original line number Diff line number Diff line
@@ -1772,20 +1772,31 @@ public class ProcessDao {
    }

    /**
     * list unauthorized resource
     * list unauthorized udf function
     * @param userId    user id
     * @param resNames  resource name
     * @return unauthorized resource list
     * @param needChecks  data source id array
     * @return unauthorized udf function list
     */
    public List<String> listUnauthorizedResource(int userId,String[] resNames){
        List<String> resultList = new ArrayList<String>();
    public <T> List<T> listUnauthorized(int userId,T[] needChecks,AuthorizationType authorizationType){
        List<T> resultList = new ArrayList<T>();

        if (ArrayUtils.isNotEmpty(resNames)) {
            Set<String> originResSet = new HashSet<String>(Arrays.asList(resNames));
            List<Resource> authorizedResourceList = resourceMapper.listAuthorizedResource(userId, resNames);
        if (ArrayUtils.isNotEmpty(needChecks)) {
            Set<T> originResSet = new HashSet<T>(Arrays.asList(needChecks));

            Set<String> authorizedResNames = authorizedResourceList.stream().map(t -> t.getAlias()).collect(toSet());
            originResSet.removeAll(authorizedResNames);
            switch (authorizationType){
                case RESOURCE_FILE:
                    Set<String> authorizedResources = resourceMapper.listAuthorizedResource(userId, needChecks).stream().map(t -> t.getAlias()).collect(toSet());
                    originResSet.removeAll(authorizedResources);
                    break;
                case DATASOURCE:
                    Set<Integer> authorizedDatasources = dataSourceMapper.listAuthorizedDataSource(userId,needChecks).stream().map(t -> t.getId()).collect(toSet());
                    originResSet.removeAll(authorizedDatasources);
                    break;
                case UDF:
                    Set<Integer> authorizedUdfs = udfFuncMapper.listAuthorizedUdfFunc(userId, needChecks).stream().map(t -> t.getId()).collect(toSet());
                    originResSet.removeAll(authorizedUdfs);
                    break;
            }

            resultList.addAll(originResSet);
        }
+9 −0
Original line number Diff line number Diff line
@@ -77,4 +77,13 @@ public interface DataSourceMapper extends BaseMapper<DataSource> {
    List<DataSource> listAllDataSourceByType(@Param("type") Integer type);


    /**
     * list authorized UDF function
     * @param userId userId
     * @param dataSourceIds data source id array
     * @return UDF function list
     */
    <T> List<DataSource> listAuthorizedDataSource(@Param("userId") int userId,@Param("dataSourceIds")T[] dataSourceIds);


}
+2 −2
Original line number Diff line number Diff line
@@ -85,10 +85,10 @@ public interface ResourceMapper extends BaseMapper<Resource> {
    String queryTenantCodeByResourceName(@Param("resName") String resName);

    /**
     * list unauthorized resource
     * list authorized resource
     * @param userId userId
     * @param resNames resource names
     * @return resource list
     */
    List<Resource> listAuthorizedResource(@Param("userId") int userId,@Param("resNames")String[] resNames);
    <T> List<Resource> listAuthorizedResource(@Param("userId") int userId,@Param("resNames")T[] resNames);
}
+7 −0
Original line number Diff line number Diff line
@@ -78,5 +78,12 @@ public interface UdfFuncMapper extends BaseMapper<UdfFunc> {
     */
    List<UdfFunc> queryAuthedUdfFunc(@Param("userId") int userId);

    /**
     * list authorized UDF function
     * @param userId userId
     * @param udfIds UDF function id array
     * @return UDF function list
     */
    <T> List<UdfFunc> listAuthorizedUdfFunc (@Param("userId") int userId,@Param("udfIds")T[] udfIds);

}
Loading