All Downloads are FREE. Search and download functionalities are using the official Maven repository.

groovity.portal.lib.roles.grvt Maven / Gradle / Ivy

There is a newer version: 2.1.0-beta.1
Show newest version
/*******************************************************************************
 * © 2018 Disney | ABC Television Group
 *
 * Licensed under the Apache License, Version 2.0 (the "Apache License")
 * with the following modification; you may not use this file except in
 * compliance with the Apache License and the following modification to it:
 * Section 6. Trademarks. is deleted and replaced with:
 *
 * 6. Trademarks. This License does not grant permission to use the trade
 *     names, trademarks, service marks, or product names of the Licensor
 *     and its affiliates, except as required to comply with Section 4(c) of
 *     the License and to reproduce the content of the NOTICE file.
 *
 * You may obtain a copy of the Apache License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Apache License with the above modification is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the Apache License for the specific
 * language governing permissions and limitations under the Apache License.
 *******************************************************************************/
import static java.lang.reflect.Modifier.isStatic;

@Field Map rolesConfiguration;
@Field Map> pathRoles;
@Field Map pathTree;

def load(){
	rolesConfiguration = getClass().getClassLoader().scriptClasses.findAll{
		it.declaredFields.find{ 
			if(it.name=='portal' && isStatic(it.modifiers)){
				it.setAccessible(true);
				return it.get(null)?.roles 
			}
		}
	}.collectEntries{
		[it,it.portal]
	};
	//log.info("role config is ${rolesConfiguration}");
	pathRoles = [:];
	def pathAdder = { path, roles ->
		def pl = pathRoles[path];
		if(pl==null){
			pl = [];
			pathRoles[path] = pl;
		}
		roles.each{
			if(it!='anon' && it!='auth' && !pl.contains(it)){
				pl.add(it);
			}
		}
		if(!pl.contains('admin')){
			pl.add('admin');
		}
	}
	rolesConfiguration.each { entry ->
		String sourcePath = entry.value.get('path')
		if(!sourcePath){
			sourcePath = entry.key.classLoader.sourcePath - '.grvt';
		}
		//log.info("loading property paths for ${sourcePath}");
		def roles = entry.value.roles;
		pathAdder(sourcePath,roles)
		int ls = sourcePath.lastIndexOf("/");
		while(ls >= 0){
			pathAdder(sourcePath.substring(0,ls+1),roles)
			//log.info("added parent path ${sourcePath.substring(0,ls+1)}");
			ls = sourcePath.lastIndexOf("/",ls-1);
		}
	}
	//log.info("All role paths are ${pathRoles}");
	pathRecursion = { dir, pathMap ->
		int depth = dir.count("/");
		int nextDepth = depth+1;
		def allPaths = pathRoles.keySet();
		def childFiles = allPaths.findAll{ !it.endsWith("/") && it.count("/")==depth && it.startsWith(dir) }
		def childDirs = allPaths.findAll{ it.endsWith("/") && it.count("/")==nextDepth && it.startsWith(dir) }
		childDirs.each{ childDir->
			def childMap = [:];
			pathRecursion(childDir,childMap);
			pathMap[childDir - dir] = childMap;
		}
		childFiles.each{
			pathMap[it - dir] = it;
		}
	}
	pathTree = [:];
	pathRecursion("/",pathTree);
	//log.info("Path tree is ${pathTree}");
}


@Function(info="Return a complete map of script classes to their known roles")
public Map getRolesConfiguration(){
	rolesConfiguration
}

@Function(info="Return a list of know roles for the given path")
public List getRoles(String path){
	return pathRoles.get(path);
}

@Function(info="return a map of paths to a list of possible roles")
public Map> getPathRoles(){
	pathRoles;
}

@Function(info="return a recursive map of the available roles namespace")
public Map getPathTree(){
	pathTree;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy