From 7f465fd4ad804b6d3cba010146cffd5626670ca2 Mon Sep 17 00:00:00 2001 From: Changsung Kim Date: Fri, 2 Jan 2015 14:47:52 +0900 Subject: [PATCH] fixes `Assign to me` and `Create by me` on issue view. * Issue When one user is not a project member and manager and is a group member or manager, `Assign to me` and `Create by me` do not work. Because current user is not listed on assignee's and author's combo box. * Solution Current user will be listed on assignee's and author's combo box. Private-issue: 1849 --- app/models/Project.java | 31 ++--------- app/models/User.java | 54 +++++++++++++++---- app/views/issue/partial_searchform.scala.html | 18 +++---- 3 files changed, 58 insertions(+), 45 deletions(-) diff --git a/app/models/Project.java b/app/models/Project.java index bfa7be1bb..f726d24e6 100644 --- a/app/models/Project.java +++ b/app/models/Project.java @@ -23,7 +23,6 @@ import com.avaje.ebean.Ebean; import com.avaje.ebean.ExpressionList; import com.avaje.ebean.Page; -import controllers.UserApp; import models.enumeration.ProjectScope; import models.enumeration.RequestState; import models.enumeration.ResourceType; @@ -468,31 +467,7 @@ public List members() { * If the project has a group and is protected or public, it returns all project and group members. */ public List getAssignableUsers() { - Set users = new HashSet<>(); - - // member of this project. - List pus = members(); - for(ProjectUser pu : pus) { - users.add(pu.user); - } - - // member of the group - if(hasGroup()) { - List ous = (isPublic() || isProtected()) ? this.organization.users : this.organization.getAdmins(); - for(OrganizationUser ou : ous) { - users.add(ou.user); - } - } - - // sorting - List result = new ArrayList<>(users); - Collections.sort(result, User.USER_NAME_COMPARATOR); - - if (UserApp.currentUser().isSiteManager()) { - result.add(UserApp.currentUser()); - } - - return result; + return User.findUsersByProjectAndOrganization(this); } public List getAssignableUsersAndAssignee(Issue issue) { @@ -505,6 +480,10 @@ public List getAssignableUsersAndAssignee(Issue issue) { return users; } + public boolean isProjectOrOrganizationUser(User user) { + return User.findUsersByProjectAndOrganization(this).contains(user); + } + public boolean isAssignableUser(User user) { return getAssignableUsers().contains(user); } diff --git a/app/models/User.java b/app/models/User.java index fb3b7d084..01ed00e16 100644 --- a/app/models/User.java +++ b/app/models/User.java @@ -24,7 +24,10 @@ import java.util.*; import javax.persistence.*; +import javax.persistence.OrderBy; +import javax.persistence.criteria.Expression; +import com.avaje.ebean.*; import controllers.UserApp; import models.enumeration.*; import models.resource.GlobalResource; @@ -39,14 +42,11 @@ import play.data.validation.Constraints.*; import play.db.ebean.Model; import play.db.ebean.Transactional; +import scala.reflect.internal.Trees; import utils.JodaDateUtil; import utils.ReservedWordsValidator; -import com.avaje.ebean.Ebean; -import com.avaje.ebean.ExpressionList; -import com.avaje.ebean.Page; -import com.avaje.ebean.RawSql; -import com.avaje.ebean.RawSqlBuilder; +import static com.avaje.ebean.Expr.eq; @Table(name = "n4user") @Entity @@ -327,6 +327,34 @@ public static List findUsersByProject(Long projectId) { .findList(); } + public static List findUsersByProjectAndOrganization(Project project) { + Set users = new HashSet<>(); + + // member of this project. + List pus = project.members(); + for(ProjectUser pu : pus) { + users.add(pu.user); + } + + // member of the group + if(project.hasGroup()) { + List ous = (project.isPublic() || project.isProtected()) ? project.organization.users : project.organization.getAdmins(); + for(OrganizationUser ou : ous) { + users.add(ou.user); + } + } + + // sorting + List result = new ArrayList<>(users); + Collections.sort(result, USER_NAME_COMPARATOR); + + if (UserApp.currentUser().isSiteManager()) { + result.add(UserApp.currentUser()); + } + + return result; + } + @Transient public Long avatarId() { List attachments = Attachment.findByContainer(avatarAsResource()); @@ -567,13 +595,16 @@ public String avatarUrl() { } /** - * All user post a issue at a project whose id is {@code projectId} + * Find issue authors and {@code currentUser} + * + * Issue authors are found in a project whose id is {@code projectId}. * + * @param currentUser * @param projectId * @return */ - public static List findIssueAuthorsByProjectId(long projectId) { - String sql = "select distinct user.id, user.name, user.login_id from issue issue, n4user user where issue.author_id = user.id"; + public static List findIssueAuthorsByProjectIdAndMe(User currentUser, long projectId) { + String sql = "select distinct user.id, user.name, user.login_id from issue issue, n4user user where issue.author_id = user.id or user.id = " + currentUser.id; return createUserSearchQueryWithRawSql(sql).where() .eq("issue.project_id", projectId) .orderBy("user.name ASC") @@ -586,8 +617,11 @@ public static List findIssueAuthorsByProjectId(long projectId) { * @param projectId * @return */ - public static List findIssueAssigneeByProjectId(long projectId) { - return Assignee.finder.where().eq("project.id", projectId).orderBy("user.name ASC").findList(); + public static List findIssueAssigneeByProjectIdAndMe(User currentUser, long projectId) { + String sql = "SELECT id, name FROM n4user WHERE id = " + currentUser.id + " or id IN (SELECT DISTINCT user_id FROM assignee WHERE id IN (SELECT DISTINCT assignee_id FROM issue WHERE project_id = " + projectId + "))"; + return User.find.setRawSql(RawSqlBuilder.parse(sql).create()) + .orderBy("name ASC") + .findList(); } /** diff --git a/app/views/issue/partial_searchform.scala.html b/app/views/issue/partial_searchform.scala.html index 1de52b359..723d931c4 100644 --- a/app/views/issue/partial_searchform.scala.html +++ b/app/views/issue/partial_searchform.scala.html @@ -41,12 +41,12 @@