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

com.rapidclipse.framework.security.authorization.XmlAuthorizationConfigurationProvider Maven / Gradle / Ivy

There is a newer version: 14.0.0
Show newest version
/*
 * Copyright (C) 2013-2023 by XDEV Software, All Rights Reserved.
 *
 * This file is part of the RapidClipse Application Platform (RAP).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 *
 * Contributors:
 *     XDEV Software - initial API and implementation
 */
package com.rapidclipse.framework.security.authorization;

import static java.util.Objects.requireNonNull;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import com.rapidclipse.framework.security.configuration.xml.XmlConfiguration;
import com.rapidclipse.framework.security.configuration.xml.XmlPermission;
import com.rapidclipse.framework.security.configuration.xml.XmlResource;
import com.rapidclipse.framework.security.configuration.xml.XmlRole;
import com.rapidclipse.framework.security.configuration.xml.XmlSubject;
import com.rapidclipse.framework.security.util.Named;


public class XmlAuthorizationConfigurationProvider implements AuthorizationConfigurationProvider
{
	///////////////////////////////////////////////////////////////////////////
	// static methods //
	///////////////////
	
	public static final AuthorizationConfiguration readConfiguration(final File xmlFile) throws AuthorizationException
	{
		final XmlConfiguration xmlConfig = XmlConfiguration.readFromFile(xmlFile);
		return build(xmlConfig);
	}
	
	public static final AuthorizationConfiguration build(final XmlConfiguration xmlConfig)
	{
		final HashMap>          resourceResources = new HashMap<>();
		final HashMap>          roleRoles         = new HashMap<>();
		final HashMap> rolePermissions   = new HashMap<>();
		final HashMap>          subjectRoles      = new HashMap<>();
		
		for(final XmlResource resource : xmlConfig.resources())
		{
			resourceResources.put(resource.name(), unboxNames(resource.children()));
		}
		
		for(final XmlRole role : xmlConfig.roles())
		{
			// must put role name even if associated collections are null to register the role itself
			roleRoles.put(role.name(), unboxNames(role.roles()));
			rolePermissions.put(role.name(), unboxPermissions(role.permissions()));
		}
		
		for(final XmlSubject subject : xmlConfig.subjects())
		{
			subjectRoles.put(subject.name(), unboxNames(subject.roles()));
		}
		
		// (25.06.2014 TM)TODO: validate referential integrity at string level here?
		
		return AuthorizationConfiguration.New(resourceResources, roleRoles, rolePermissions, subjectRoles);
	}
	
	private static HashSet unboxNames(final List nameds)
	{
		if(nameds == null)
		{
			return null;
		}
		
		final HashSet names = new HashSet<>(nameds.size());
		
		for(final Named named : nameds)
		{
			names.add(named.name());
		}
		
		return names;
	}
	
	private static HashMap unboxPermissions(final List permissions)
	{
		if(permissions == null)
		{
			return null;
		}
		
		final HashMap unboxed = new HashMap<>();
		
		for(final XmlPermission permission : permissions)
		{
			unboxed.put(permission.resource(), permission.factor() == null ? 0 : permission.factor());
		}
		
		return unboxed;
	}
	
	public static final XmlAuthorizationConfigurationProvider New(final File xmlFile)
	{
		return new XmlAuthorizationConfigurationProvider(
			requireNonNull(xmlFile));
	}
	
	///////////////////////////////////////////////////////////////////////////
	// instance fields //
	////////////////////
	
	private final File xmlFile;
	
	///////////////////////////////////////////////////////////////////////////
	// constructors //
	/////////////////
	
	/**
	 * Implementation detail constructor that might change in the future.
	 */
	XmlAuthorizationConfigurationProvider(final File xmlFile)
	{
		super();
		this.xmlFile = xmlFile;
	}
	
	///////////////////////////////////////////////////////////////////////////
	// override methods //
	/////////////////////
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public AuthorizationConfiguration provideConfiguration()
	{
		return readConfiguration(this.xmlFile);
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy