package org.zanata.action;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;
import org.zanata.dao.AccountDAO;
import org.zanata.dao.ProjectDAO;
import org.zanata.model.HAccount;
import org.zanata.model.HPerson;
import org.zanata.model.HProject;

@Name("projectMaintainerManageAction")
@Scope(ScopeType.PAGE)
public class ProjectMaintainerManageAction implements Serializable
{
   private static final long serialVersionUID = 1L;
   @DataModel
   List<HPerson> allList;
   @DataModelSelection
   HPerson selectedPerson;

   private String slug;

   @In
   ProjectDAO projectDAO;
   @In
   AccountDAO accountDAO;
   @Logger
   Log log;

   public void loadAllMaintainers()
   {
      allList = projectDAO.getProjectMaintainerBySlug(this.slug);
   }

   public HPerson getSelectedPerson()
   {
      return this.selectedPerson;
   }

   public void setSlug(String slug)
   {
      this.slug = slug;
   }

   public String getSlug()
   {
      return this.slug;
   }

   public HProject getProject()
   {
      return projectDAO.getBySlug(this.slug);
   }

   @Restrict("#{s:hasPermission(projectMaintainerManageAction.project, 'update')}")
   public void deleteMaintainer(HPerson person)
   {
      log.debug("try to delete maintainer {0} from slug {1}", person.getName(), this.slug);
      HProject project = projectDAO.getBySlug(this.slug);
      Set<HPerson> personList = project.getMaintainers();
      for (HPerson l : personList)
      {
         if (l.getEmail().equals(person.getEmail()))
         {
            log.debug("remove the person");
            project.getMaintainers().remove(l);
            break;
         }
      }

      projectDAO.makePersistent(project);
      projectDAO.flush();
   }

   @Restrict("#{s:hasPermission(projectMaintainerManageAction.project, 'update')}")
   public String addMaintainers(String account)
   {
      HAccount a = accountDAO.getByUsername(account);
      if (a != null && a.isEnabled())
      {
         HProject project = projectDAO.getBySlug(this.slug);
         project.addMaintainer(a.getPerson());
         projectDAO.makePersistent(project);
         projectDAO.flush();
         log.debug("add {0} into maintainers", account);
         return "success";
      }
      FacesMessages.instance().add("This account does not exist.");
      return "failure";
   }
}

@@ -536,16 +536,122 @@
 og.Log;%0A
+import org.jboss.seam.security.RunAsOperation;%0Aimport org.jboss.seam.security.management.IdentityManager;%0A
 import o
@@ -672,24 +672,24 @@
 AccountDAO;%0A
-
 import org.z
@@ -1086,16 +1086,68 @@
 erson;%0A%0A
+   @In%0A   private IdentityManager identityManager;%0A%0A
    priva
@@ -1902,24 +1902,30 @@
 lug);%0A      
+final 
 HProject pro
@@ -2260,16 +2260,99 @@
      %7D%0A%0A
+      new RunAsOperation()%0A      %7B%0A         public void execute()%0A         %7B%0A      
       pr
@@ -2381,24 +2381,30 @@
 t(project);%0A
+      
       projec
@@ -2417,16 +2417,59 @@
 lush();%0A
+         %7D%0A      %7D.addRole(%22admin%22).run();%0A
    %7D%0A%0A  

fix removing user as maintainer: https://bugzilla.redhat.com/show_bug.cgi?id=892816#c6