<commit_before>package fedreg.core

import static org.apache.commons.lang.StringUtils.*

class DescriptorEndpointController {

	static allowedMethods = [delete: "POST", create: "POST", toggle:"POST", listEndpoints:"GET"]
	
	// Maps allowed endpoints to internal class representation
	def allowedEndpoints = [singleSignOnServices:"fedreg.core.SingleSignOnService", artifactResolutionServices:"fedreg.core.ArtifactResolutionService", 
							singleLogoutServices:"fedreg.core.SingleLogoutService", assertionConsumerServices:"fedreg.core.AssertionConsumerService", attributeServices:"fedreg.core.AttributeService"]

	def endpointService

	def delete = {
		if(!params.id) {
			log.warn "Endpoint ID was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		if(!params.endpointType) {
			log.warn "Endpoint type was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		def endpoint = Endpoint.get(params.id)
		if(!endpoint) {
			log.warn "Endpoint identified by id $params.id was not located"
			render message(code: 'fedreg.endpoint.nonexistant', args: [params.id])
			response.setStatus(500)
			return
		}
		
		endpointService.delete(endpoint, params.endpointType)
		render message(code: 'fedreg.endpoint.delete.success')
	}
	
	def list = {
		if(!params.id) {
			log.warn "Descriptor ID was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		if(!params.endpointType) {
			log.warn "Endpoint type was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		if(!params.containerID) {
			log.warn "Container ID was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		def endpointType = params.endpointType
		def descriptor = RoleDescriptor.get(params.id)
		if (!descriptor) {
			log.warn "Descriptor was not found for id ${params.id}"
			render message(code: 'fedreg.roledescriptor.nonexistant', args: [params.id])
			response.setStatus(500)
			return
		}
		
		// Determine if we're actually listing from the collaborator (useful for AA endpoints on IDP screen)
		if(!descriptor.hasProperty(endpointType)) {
			if(descriptor.collaborator.hasProperty(endpointType))
				descriptor = descriptor.collaborator
		}
		
		if(allowedEndpoints.containsKey(endpointType) && descriptor.hasProperty(endpointType)) {
			log.debug "Listing endpoints for descriptor ID ${params.id} of type ${endpointType}"
			render template:"/templates/endpoints/list", contextPath: pluginContextPath, model:[endpoints:descriptor."${endpointType}", allowremove:true, endpointType:endpointType, containerID:params.containerID]
		}
		else {
			log.warn "Endpoint ${endpointType} is invalid for Descriptor with id ${params.id}"
			render message(code: 'fedreg.endpoint.invalid', args: [endpointType])
			response.setStatus(500)
			return
		}
	}
	
	def create = {
		if(!params.id) {
			log.warn "Descriptor ID was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		if(!params.endpointType) {
			log.warn "Endpoint type was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		if(!params.binding) {
			log.warn "Binding ID was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		if(!params.location) {
			log.warn "Location URI was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		def endpointType = params.endpointType
		def descriptor = RoleDescriptor.get(params.id)
		if (!descriptor) {
			log.warn "Descriptor was not found for id ${params.id}"
			render message(code: 'fedreg.roledescriptor.nonexistant', args: [params.id])
			response.setStatus(500)
			return
		}
		
		// Determine if we're actually updating the collaborator (useful for AA endpoints on IDP screen)
		if(!descriptor.hasProperty(endpointType)) {
			if(descriptor.collaborator.hasProperty(endpointType))
				descriptor = descriptor.collaborator
		}
		
		def binding = SamlURI.get(params.binding)
		if (!binding) {
			log.warn "SamURI (binding) was not found for id ${params.id}"
			render message(code: 'fedreg.samluri.nonexistant', args: [params.binding])
			response.setStatus(500)
			return
		}
		
		if(allowedEndpoints.containsKey(endpointType) && descriptor.hasProperty(endpointType)) {
			endpointService.create(descriptor, allowedEndpoints.get(endpointType), endpointType, binding, params.location)
			render message(code: 'fedreg.endpoint.create.success')
		}
		else {
			log.warn "Endpoint ${endpointType} is invalid for ${descriptor}, unable to create"
			render message(code: 'fedreg.endpoint.invalid', args: [endpointType])
			response.setStatus(500)
		}
	}
	
	def toggle = {
		if(!params.id) {
			log.warn "Endpoint ID was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.sendError(500)
			return
		}
		
		def endpoint = Endpoint.get(params.id)
		if(!endpoint) {
			log.warn "Endpoint identified by id $params.id was not located"
			render message(code: 'fedreg.endpoint.nonexistant', args: [params.id])
			response.setStatus(500)
			return
		}
	
		endpointService.toggle(endpoint)
		render message(code: 'fedreg.endpoint.toggle.success')
	}
}<commit_msg>Integrate permissions to DescriptorEndpointController<commit_after>package fedreg.core

import static org.apache.commons.lang.StringUtils.*

import org.apache.shiro.SecurityUtils

class DescriptorEndpointController {

	static allowedMethods = [delete: "POST", create: "POST", toggle:"POST", listEndpoints:"GET"]
	
	// Maps allowed endpoints to internal class representation
	def allowedEndpoints = [singleSignOnServices:"fedreg.core.SingleSignOnService", artifactResolutionServices:"fedreg.core.ArtifactResolutionService", 
							singleLogoutServices:"fedreg.core.SingleLogoutService", assertionConsumerServices:"fedreg.core.AssertionConsumerService", attributeServices:"fedreg.core.AttributeService"]

	def endpointService

	def delete = {
		if(!params.id) {
			log.warn "Endpoint ID was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		if(!params.endpointType) {
			log.warn "Endpoint type was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		def endpoint = Endpoint.get(params.id)
		if(!endpoint) {
			log.warn "Endpoint identified by id $params.id was not located"
			render message(code: 'fedreg.endpoint.nonexistant', args: [params.id])
			response.setStatus(500)
			return
		}
		
		if(SecurityUtils.subject.isPermitted("descriptor:${endpoint.descriptor.id}:endpoint:remove")) {
			endpointService.delete(endpoint, params.endpointType)
			render message(code: 'fedreg.endpoint.delete.success')
		}
		else {
			log.warn("Attempt to remove $endpoint by $authenticatedUser was denied, incorrect permission set")
			response.sendError(403)
		}
	}
	
	def list = {
		if(!params.id) {
			log.warn "Descriptor ID was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		if(!params.endpointType) {
			log.warn "Endpoint type was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		if(!params.containerID) {
			log.warn "Container ID was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		def endpointType = params.endpointType
		def descriptor = RoleDescriptor.get(params.id)
		if (!descriptor) {
			log.warn "Descriptor was not found for id ${params.id}"
			render message(code: 'fedreg.roledescriptor.nonexistant', args: [params.id])
			response.setStatus(500)
			return
		}
		
		// Determine if we're actually listing from the collaborator (useful for AA endpoints on IDP screen)
		if(!descriptor.hasProperty(endpointType)) {
			if(descriptor.collaborator.hasProperty(endpointType))
				descriptor = descriptor.collaborator
		}
		
		if(allowedEndpoints.containsKey(endpointType) && descriptor.hasProperty(endpointType)) {
			log.debug "Listing endpoints for descriptor ID ${params.id} of type ${endpointType}"
			render template:"/templates/endpoints/list", contextPath: pluginContextPath, model:[endpoints:descriptor."${endpointType}", allowremove:true, endpointType:endpointType, containerID:params.containerID]
		}
		else {
			log.warn "Endpoint ${endpointType} is invalid for Descriptor with id ${params.id}"
			render message(code: 'fedreg.endpoint.invalid', args: [endpointType])
			response.setStatus(500)
			return
		}
	}
	
	def create = {
		if(!params.id) {
			log.warn "Descriptor ID was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		if(!params.endpointType) {
			log.warn "Endpoint type was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		if(!params.binding) {
			log.warn "Binding ID was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		if(!params.location) {
			log.warn "Location URI was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.setStatus(500)
			return
		}
		
		def endpointType = params.endpointType
		def descriptor = RoleDescriptor.get(params.id)
		if (!descriptor) {
			log.warn "Descriptor was not found for id ${params.id}"
			render message(code: 'fedreg.roledescriptor.nonexistant', args: [params.id])
			response.setStatus(500)
			return
		}
		
		if(SecurityUtils.subject.isPermitted("descriptor:${descriptor.id}:endpoint:create")) {
			// Determine if we're actually updating the collaborator (useful for AA endpoints on IDP screen)
			if(!descriptor.hasProperty(endpointType)) {
				if(descriptor.collaborator.hasProperty(endpointType))
					descriptor = descriptor.collaborator
			}
		
			def binding = SamlURI.get(params.binding)
			if (!binding) {
				log.warn "SamURI (binding) was not found for id ${params.id}"
				render message(code: 'fedreg.samluri.nonexistant', args: [params.binding])
				response.setStatus(500)
				return
			}
		
			if(allowedEndpoints.containsKey(endpointType) && descriptor.hasProperty(endpointType)) {
				endpointService.create(descriptor, allowedEndpoints.get(endpointType), endpointType, binding, params.location)
				render message(code: 'fedreg.endpoint.create.success')
			}
			else {
				log.warn "Endpoint ${endpointType} is invalid for ${descriptor}, unable to create"
				render message(code: 'fedreg.endpoint.invalid', args: [endpointType])
				response.setStatus(500)
			}
		}
		else {
			log.warn("Attempt to create endpoint for $descriptor by $authenticatedUser was denied, incorrect permission set")
			response.sendError(403)
		}
	}
	
	def toggle = {
		if(!params.id) {
			log.warn "Endpoint ID was not present"
			render message(code: 'fedreg.controllers.namevalue.missing')
			response.sendError(500)
			return
		}
		
		def endpoint = Endpoint.get(params.id)
		if(!endpoint) {
			log.warn "Endpoint identified by id $params.id was not located"
			render message(code: 'fedreg.endpoint.nonexistant', args: [params.id])
			response.setStatus(500)
			return
		}
	
		if(SecurityUtils.subject.isPermitted("descriptor:${endpoint.descriptor.id}:endpoint:toggle")) {
			endpointService.toggle(endpoint)
			render message(code: 'fedreg.endpoint.toggle.success')
		}
		else {
			log.warn("Attempt to toggle endpoint for $descriptor by $authenticatedUser was denied, incorrect permission set")
			response.sendError(403)
		}
	}
}
{'fraction_non_alphanumeric': 0.11370262390670553, 'fraction_numerical': 0.009246147438567264, 'mean_word_length': 4.403240324032403, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6545094', 'n_tokens_mistral': 4230, 'n_tokens_neox': 3531, 'n_words': 805}