Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
rhbz841037 - Extend user deletion to handle constraint violations.
Browse files Browse the repository at this point in the history
Extend Seam's out-of-the-box UserAction bean to catch constraint violation exceptions and display them as a Faces message instead of a generic catch-all message.
  • Loading branch information
Carlos Munoz committed Jul 20, 2012
1 parent 844da71 commit 4f328db
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
72 changes: 72 additions & 0 deletions zanata-war/src/main/java/org/zanata/action/UserAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.action;

import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;

import org.hibernate.exception.ConstraintViolationException;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.international.StatusMessage;
import org.jboss.seam.security.management.IdentityManager;

import static org.jboss.seam.ScopeType.CONVERSATION;
import static org.jboss.seam.annotations.Install.APPLICATION;

/**
* Extension of Seam management's UserAction class' behaviour.
*
* @see {@link org.jboss.seam.security.management.action.UserAction}
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@Name("org.jboss.seam.security.management.userAction")
@Scope(CONVERSATION)
@Install(precedence = APPLICATION)
public class UserAction extends org.jboss.seam.security.management.action.UserAction
{
@In
private IdentityManager identityManager;

@In
private EntityManager entityManager;

@In
private Map<String, String> messages;

public void deleteUser( String userName )
{
try
{
identityManager.deleteUser(userName);
// NB: Need to call flush here to be able to catch the persistence exception, otherwise it would be caught by Seam.
entityManager.flush();
}
catch (PersistenceException e)
{
FacesMessages.instance().add(StatusMessage.Severity.ERROR, messages.get("jsf.UserManager.delete.constraintViolation.error") );
}
}
}
3 changes: 3 additions & 0 deletions zanata-war/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,9 @@ jsf.Update=Update
jsf.UrlExample=e.g. http://example.com/zanata or http://zanata.example.com
jsf.UrlToolTip=The base URL for the server, including the application context path (no final slash)
jsf.UserManager=User Manager

jsf.UserManager.delete.constraintViolation.error=This user cannot be removed from the system. You can deactivate the user instead.

jsf.Username=Username
jsf.UsernameToolTip=The username should be all in lower case.
jsf.Variant=Variant
Expand Down
2 changes: 1 addition & 1 deletion zanata-war/src/main/webapp/admin/usermanager.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<s:fragment rendered="#{s:hasPermission('seam.user', 'update')}">
<h:commandButton value="#{messages['jsf.Edit']}" action="#{userAction.editUser(userSearch.selectedUser)}"/>
</s:fragment>
<h:commandButton value="#{messages['jsf.Delete']}" action="#{identityManager.deleteUser(userSearch.selectedUser)}"
<h:commandButton value="#{messages['jsf.Delete']}" action="#{userAction.deleteUser(userSearch.selectedUser)}"
rendered="#{s:hasPermission('seam.user', 'delete')}"
onclick="return confirmDelete()"/>
</h:column>
Expand Down

0 comments on commit 4f328db

Please sign in to comment.