<commit_before>/* 
 * Copyright (c) 2006 Stiftung Deutsches Elektronen-Synchroton, 
 * Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY.
 *
 * THIS SOFTWARE IS PROVIDED UNDER THIS LICENSE ON AN "../AS IS" BASIS. 
 * WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND 
 * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. SHOULD THE SOFTWARE PROVE DEFECTIVE 
 * IN ANY RESPECT, THE USER ASSUMES THE COST OF ANY NECESSARY SERVICING, REPAIR OR 
 * CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. 
 * NO USE OF ANY SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
 * DESY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
 * OR MODIFICATIONS.
 * THE FULL LICENSE SPECIFYING FOR THE SOFTWARE THE REDISTRIBUTION, MODIFICATION, 
 * USAGE AND OTHER RIGHTS AND OBLIGATIONS IS INCLUDED WITH THE DISTRIBUTION OF THIS 
 * PROJECT IN THE FILE LICENSE.HTML. IF THE LICENSE IS NOT INCLUDED YOU MAY FIND A COPY 
 * AT HTTP://WWW.DESY.DE/LEGAL/LICENSE.HTM
 */
package org.csstudio.platform.internal.rightsmanagement;

import java.util.HashMap;
import java.util.Map;

import org.csstudio.platform.logging.CentralLogger;
import org.csstudio.platform.security.IAuthorizationProvider;
import org.csstudio.platform.security.IRight;
import org.csstudio.platform.security.RightSet;
import org.csstudio.platform.security.User;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.Platform;

/**
 * The <code>RightsManagementService</code> provides the central CSS core
 * functionalities for the management of <code>Rights</code>.
 * 
 * @author <NAME> & <NAME> & <NAME> & <NAME> & <NAME>
 * @author <NAME> Removed unused listener list
 */
@SuppressWarnings("nls")
public final class RightsManagementService {

	/**
	 * The only one instance of the <code>RightsManagementService</code>.
	 */
	private static RightsManagementService _instance = null;

	/**
	 * Stores the rights of the users.
	 */
	private Map<User, RightSet> _rights = new HashMap<User, RightSet>();
	
	/**
	 * The authorization provider.
	 */
	private IAuthorizationProvider _authProvider = null;

	/**
	 * Private constructor due to singleton pattern.
	 */
	private RightsManagementService() {
	}

	/**
	 * Return the only one instance of the <code>RightsManagementService</code>.
	 * 
	 * @return The only one instance of the <code>RightsManagementService</code>.
	 */
	public static RightsManagementService getInstance() {
		if (_instance == null) {
			_instance = new RightsManagementService();
		}
		return _instance;
	}

	/**
	 * Checks if the given user has the rights to perform the action with the
	 * given id. This method will return <code>false</code> if no rights are
	 * configured for the given action, i.e. by default, all actions can be
	 * executed by all non-anonymous users.
	 * 
	 * @param user
	 *            the user.
	 * @param id
	 *            the action id.
	 * @return <code>true</code> if the user is permitted to perform the
	 *         action, <code>false</code> otherwise.
	 */
	public boolean hasRights(final User user, final String id) {
		return hasRights(user, id, false);
	}
	
	/**
	 * Checks if the given user has the rights to perform the action with the
	 * given id.
	 * 
	 * @param user
	 *            the user.
	 * @param id
	 *            the action id.
	 * @param defaultPermission
	 *            this value will be returned if no rights are configured for
	 *            the given action. Note that if you pass <code>false</code>
	 *            here, the action will be disabled if no authorization plug-in
	 *            is loaded.
	 * @return <code>true</code> if the user is permitted to perform the
	 *         action, <code>false</code> otherwise.
	 */
	public boolean hasRights(final User user, final String id,
			final boolean defaultPermission) {
		RightSet userRights = _rights.get(user);
		RightSet actionRights = getRightsForAction(id);
		if (actionRights.isEmpty()) {
			// If no rights are configured for the action, the default
			// permission passed by the caller is used.
			return defaultPermission;
		} else {<commit_msg>o.c.platform RightsManagementService: ITER found user==null issue<commit_after>/* 
 * Copyright (c) 2006 Stiftung Deutsches Elektronen-Synchroton, 
 * Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY.
 *
 * THIS SOFTWARE IS PROVIDED UNDER THIS LICENSE ON AN "../AS IS" BASIS. 
 * WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND 
 * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. SHOULD THE SOFTWARE PROVE DEFECTIVE 
 * IN ANY RESPECT, THE USER ASSUMES THE COST OF ANY NECESSARY SERVICING, REPAIR OR 
 * CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. 
 * NO USE OF ANY SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
 * DESY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
 * OR MODIFICATIONS.
 * THE FULL LICENSE SPECIFYING FOR THE SOFTWARE THE REDISTRIBUTION, MODIFICATION, 
 * USAGE AND OTHER RIGHTS AND OBLIGATIONS IS INCLUDED WITH THE DISTRIBUTION OF THIS 
 * PROJECT IN THE FILE LICENSE.HTML. IF THE LICENSE IS NOT INCLUDED YOU MAY FIND A COPY 
 * AT HTTP://WWW.DESY.DE/LEGAL/LICENSE.HTM
 */
package org.csstudio.platform.internal.rightsmanagement;

import java.util.HashMap;
import java.util.Map;

import org.csstudio.platform.logging.CentralLogger;
import org.csstudio.platform.security.IAuthorizationProvider;
import org.csstudio.platform.security.IRight;
import org.csstudio.platform.security.RightSet;
import org.csstudio.platform.security.User;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.Platform;

/**
 * The <code>RightsManagementService</code> provides the central CSS core
 * functionalities for the management of <code>Rights</code>.
 * 
 * @author <NAME> & <NAME> & <NAME> & <NAME> & <NAME>
 * @author <NAME> Removed unused listener list
 */
@SuppressWarnings("nls")
public final class RightsManagementService {

	/**
	 * The only one instance of the <code>RightsManagementService</code>.
	 */
	private static RightsManagementService _instance = null;

	/**
	 * Stores the rights of the users.
	 */
	private Map<User, RightSet> _rights = new HashMap<User, RightSet>();
	
	/**
	 * The authorization provider.
	 */
	private IAuthorizationProvider _authProvider = null;

	/**
	 * Private constructor due to singleton pattern.
	 */
	private RightsManagementService() {
	}

	/**
	 * Return the only one instance of the <code>RightsManagementService</code>.
	 * 
	 * @return The only one instance of the <code>RightsManagementService</code>.
	 */
	public static RightsManagementService getInstance() {
		if (_instance == null) {
			_instance = new RightsManagementService();
		}
		return _instance;
	}

	/**
	 * Checks if the given user has the rights to perform the action with the
	 * given id. This method will return <code>false</code> if no rights are
	 * configured for the given action, i.e. by default, all actions can be
	 * executed by all non-anonymous users.
	 * 
	 * @param user
	 *            the user.
	 * @param id
	 *            the action id.
	 * @return <code>true</code> if the user is permitted to perform the
	 *         action, <code>false</code> otherwise.
	 */
	public boolean hasRights(final User user, final String id) {
		return hasRights(user, id, false);
	}
	
	/**
	 * Checks if the given user has the rights to perform the action with the
	 * given id.
	 * 
	 * @param user
	 *            the user.
	 * @param id
	 *            the action id.
	 * @param defaultPermission
	 *            this value will be returned if no rights are configured for
	 *            the given action. Note that if you pass <code>false</code>
	 *            here, the action will be disabled if no authorization plug-in
	 *            is loaded.
	 * @return <code>true</code> if the user is permitted to perform the
	 *         action, <code>false</code> otherwise.
	 */
	public boolean hasRights(final User user, final String id,
			final boolean defaultPermission) {
		RightSet userRights = _rights.get(user);
		RightSet actionRights = getRightsForAction(id);
		if (user == null  ||  actionRights.isEmpty()) {
			// If no rights are configured for the action, the default
			// permission passed by the caller is used.
			return defaultPermission;
		} else {
{'fraction_non_alphanumeric': 0.08618079584775086, 'fraction_numerical': 0.0008650519031141869, 'mean_word_length': 3.5471976401179943, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6749735', 'n_tokens_mistral': 2824, 'n_tokens_neox': 2567, 'n_words': 1009}