<commit_before>package org.fedorahosted.flies.action;

import javax.faces.event.ValueChangeEvent;
import javax.persistence.NoResultException;

import org.fedorahosted.flies.model.HCommunity;
import org.fedorahosted.flies.model.HPerson;
import org.hibernate.criterion.NaturalIdentifier;
import org.hibernate.criterion.Restrictions;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.core.Conversation;
import org.jboss.seam.faces.FacesMessages;

@Name("communityHome")
@Scope(ScopeType.CONVERSATION)
public class CommunityHome extends SlugHome<HCommunity>{

	private String slug;
	
	@Override
	@Restrict("#{identity.loggedIn}")
	protected HCommunity createInstance() {
		HCommunity instance = super.createInstance();
		instance.setOwner(getEntityManager().find(HPerson.class, 1l));
		return instance;
	}

	public void validateSuppliedId(){
		getInstance(); // this will raise an EntityNotFound exception
					   // when id is invalid and conversation will not
		               // start
	}
	
	public void verifySlugAvailable(ValueChangeEvent e) {
	    String slug = (String) e.getNewValue();
	    validateSlug(slug, e.getComponent().getId());
	}
	
	public boolean validateSlug(String slug, String componentId){
	    if (!isSlugAvailable(slug)) {
	    	FacesMessages.instance().addToControl(
	    			componentId, "This slug is not available");
	    	return false;
	    }
	    return true;
	}
	
	public boolean isSlugAvailable(String slug) {
    	try{
    		getEntityManager().createQuery("from HCommunity c where c.slug = :slug")
    		.setParameter("slug", slug).getSingleResult();
    		return false;
    	}
    	catch(NoResultException e){
    		// pass
    	}
    	return true;
	}
	
	@Override
	public String persist() {
		if(!validateSlug(getInstance().getSlug(), "slug"))
			return null;
		// TODO Auto-generated method stub
		return super.persist();<commit_msg>CommunityHome actions now require the 'admin' role<commit_after>package org.fedorahosted.flies.action;

import javax.faces.event.ValueChangeEvent;
import javax.persistence.NoResultException;

import org.fedorahosted.flies.model.HCommunity;
import org.fedorahosted.flies.model.HPerson;
import org.hibernate.criterion.NaturalIdentifier;
import org.hibernate.criterion.Restrictions;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.faces.FacesMessages;

@Name("communityHome")
@Scope(ScopeType.CONVERSATION)
public class CommunityHome extends SlugHome<HCommunity>{

	private String slug;
	
	@Override
	@Restrict("#{s:hasRole('admin')}")
	protected HCommunity createInstance() {
		HCommunity instance = super.createInstance();
		// FIXME this should be the current user
		instance.setOwner(getEntityManager().find(HPerson.class, 1l));
		return instance;
	}

	public void validateSuppliedId(){
		getInstance(); // this will raise an EntityNotFound exception
					   // when id is invalid and conversation will not
		               // start
	}
	
	public void verifySlugAvailable(ValueChangeEvent e) {
	    String slug = (String) e.getNewValue();
	    validateSlug(slug, e.getComponent().getId());
	}
	
	public boolean validateSlug(String slug, String componentId){
	    if (!isSlugAvailable(slug)) {
	    	FacesMessages.instance().addToControl(
	    			componentId, "This slug is not available");
	    	return false;
	    }
	    return true;
	}
	
	public boolean isSlugAvailable(String slug) {
    	try{
    		getEntityManager().createQuery("from HCommunity c where c.slug = :slug")
    		.setParameter("slug", slug).getSingleResult();
    		return false;
    	}
    	catch(NoResultException e){
    		// pass
    	}
    	return true;
	}
	
	@Override
	@Restrict("#{s:hasRole('admin')}")
	public String persist() {
		if(!validateSlug(getInstance().getSlug(), "slug"))
			return null;
		return super.persist();
{'fraction_non_alphanumeric': 0.11355849241311797, 'fraction_numerical': 0.0004894762604013706, 'mean_word_length': 4.173417721518987, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2145241', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1248, 'n_words': 269}