]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJulien Lancelot <julien.lancelot@gmail.com>
Thu, 18 Jul 2013 15:57:09 +0000 (17:57 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Thu, 18 Jul 2013 16:20:58 +0000 (18:20 +0200)
sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java
sonar-server/src/main/java/org/sonar/server/issue/IssueBulkChangeService.java
sonar-server/src/main/java/org/sonar/server/issue/IssueFilterService.java
sonar-server/src/test/java/org/sonar/server/issue/InternalRubyIssueServiceTest.java
sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceTest.java
sonar-server/src/test/java/org/sonar/server/issue/IssueFilterServiceTest.java

index 52c557a2e041e68a2ec83fe555078f0699fe5e1d..b9846e8232b2e61b58d033c63961c70d352eb6d7 100644 (file)
@@ -412,7 +412,7 @@ public class InternalRubyIssueService implements ServerComponent {
   }
 
   public boolean canUserShareIssueFilter(){
-    return issueFilterService.canShareFilter(UserSession.get().login());
+    return issueFilterService.canShareFilter(UserSession.get());
   }
 
   public String serializeFilterQuery(Map<String, Object> filterQuery) {
index b6a0cff6ba26e27b1b9c985c492d094f5210e391..55cf6efb5e1e13631df1a9c56773edc7de19bad9 100644 (file)
@@ -32,11 +32,8 @@ import org.sonar.api.issue.internal.IssueChangeContext;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.issue.IssueNotifications;
 import org.sonar.core.issue.db.IssueStorage;
-import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.user.UserSession;
 
-import javax.annotation.CheckForNull;
-
 import java.util.Date;
 import java.util.List;
 
@@ -66,36 +63,18 @@ public class IssueBulkChangeService {
     IssueBulkChangeResult result = new IssueBulkChangeResult();
     IssueQueryResult issueQueryResult = issueFinder.find(IssueQuery.builder().issueKeys(issueBulkChangeQuery.issues()).pageSize(-1).requiredRole(UserRole.USER).build());
     List<Issue> issues = issueQueryResult.issues();
-    List<Action> bulkActions = newArrayList();
-    for (String actionName : issueBulkChangeQuery.actions()) {
-      Action action = getAction(actionName);
-      if (action == null) {
-        throw new BadRequestException("The action : '"+ actionName + "' is unknown");
-      }
-      if (action.verify(issueBulkChangeQuery.properties(actionName), issues, userSession)) {
-        bulkActions.add(action);
-      }
-    }
+    List<Action> bulkActions = getActionsToApply(issueBulkChangeQuery, issues, userSession);
 
     IssueChangeContext issueChangeContext = IssueChangeContext.createUser(new Date(), userSession.login());
     for (Issue issue : issues) {
       ActionContext actionContext = new ActionContext(issue, issueChangeContext);
       for (Action action : bulkActions) {
-        try {
-          if (applyAction(action, actionContext, issueBulkChangeQuery)) {
-            result.addIssueChanged(issue);
-          } else {
-            result.addIssueNotChanged(issue);
-          }
-        } catch (Exception e) {
-          result.addIssueNotChanged(issue);
-          LOG.info("An error occur when trying to apply the action : "+ action.key() + " on issue : "+ issue.key() + ". This issue has been ignored.", e);
-        }
+        applyAction(action, actionContext, issueBulkChangeQuery, result);
       }
       if (result.issuesChanged().contains(issue)) {
         // Apply comment action only on changed issues
         if (issueBulkChangeQuery.hasComment()) {
-          applyAction(getAction(CommentAction.KEY), actionContext, issueBulkChangeQuery);
+          applyAction(getAction(CommentAction.KEY), actionContext, issueBulkChangeQuery, result);
         }
         issueStorage.save((DefaultIssue) issue);
         issueNotifications.sendChanges((DefaultIssue) issue, issueChangeContext, issueQueryResult);
@@ -105,18 +84,38 @@ public class IssueBulkChangeService {
     return result;
   }
 
-  private boolean applyAction(Action action, ActionContext actionContext, IssueBulkChangeQuery issueBulkChangeQuery){
-    return action.supports(actionContext.issue()) && action.execute(issueBulkChangeQuery.properties(action.key()), actionContext);
+  private List<Action> getActionsToApply(IssueBulkChangeQuery issueBulkChangeQuery, List<Issue> issues, UserSession userSession) {
+    List<Action> bulkActions = newArrayList();
+    for (String actionName : issueBulkChangeQuery.actions()) {
+      Action action = getAction(actionName);
+      if (action.verify(issueBulkChangeQuery.properties(actionName), issues, userSession)) {
+        bulkActions.add(action);
+      }
+    }
+    return bulkActions;
+  }
+
+  private void applyAction(Action action, ActionContext actionContext, IssueBulkChangeQuery issueBulkChangeQuery, IssueBulkChangeResult result) {
+    Issue issue = actionContext.issue();
+    try {
+      if (action.supports(issue) && action.execute(issueBulkChangeQuery.properties(action.key()), actionContext)) {
+        result.addIssueChanged(issue);
+      } else {
+        result.addIssueNotChanged(issue);
+      }
+    } catch (Exception e) {
+      result.addIssueNotChanged(issue);
+      LOG.info("An error occur when trying to apply the action : " + action.key() + " on issue : " + issue.key() + ". This issue has been ignored.", e);
+    }
   }
 
-  @CheckForNull
   private Action getAction(final String actionKey) {
     return Iterables.find(actions, new Predicate<Action>() {
       @Override
       public boolean apply(Action action) {
         return action.key().equals(actionKey);
       }
-    }, null);
+    });
   }
 
   static class ActionContext implements Action.Context {
index 0b54f6edf76e88a918d257da51c0f2ac63b7618d..ccf2e6a4187b9de91d8ef1edd536a89ffd9f8263 100644 (file)
@@ -205,8 +205,12 @@ public class IssueFilterService implements ServerComponent {
     return issueFilterDto;
   }
 
-  public boolean canShareFilter(String user){
-    return authorizationDao.selectGlobalPermissions(user).contains(Permission.DASHBOARD_SHARING.key());
+  public boolean canShareFilter(UserSession userSession){
+    if (userSession.isLoggedIn()) {
+      String user = userSession.login();
+      return hasUserSharingPermission(user);
+    }
+    return false;
   }
 
   String getLoggedLogin(UserSession userSession) {
@@ -242,7 +246,7 @@ public class IssueFilterService implements ServerComponent {
   }
 
   private void verifyCurrentUserCanShareFilter(DefaultIssueFilter issueFilter, String user) {
-    if (issueFilter.shared() && !canShareFilter(user)) {
+    if (issueFilter.shared() && !hasUserSharingPermission(user)) {
       throw new ForbiddenException("User cannot own this filter because of insufficient rights");
     }
   }
@@ -334,4 +338,8 @@ public class IssueFilterService implements ServerComponent {
     return new IssueFilterResult(issueQueryResult, issueQuery);
   }
 
+  private boolean hasUserSharingPermission(String user){
+    return authorizationDao.selectGlobalPermissions(user).contains(Permission.DASHBOARD_SHARING.key());
+  }
+
 }
index 7edbdb503ccf48567a453302c3a84c51fdc67c9a..eb3fc3c561dc04c61b07bac231d1a317f699996b 100644 (file)
@@ -540,7 +540,7 @@ public class InternalRubyIssueServiceTest {
   @Test
   public void should_check_if_user_can_share_issue_filter(){
     service.canUserShareIssueFilter();
-    verify(issueFilterService).canShareFilter(anyString());
+    verify(issueFilterService).canShareFilter(any(UserSession.class));
   }
 
   @Test
index 702816322240f7174dc2577213c369fa8cc41ecd..6a9ac61b342216f7e9ef689057950e61d68dabd6 100644 (file)
@@ -32,13 +32,13 @@ import org.sonar.api.issue.internal.IssueChangeContext;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.issue.IssueNotifications;
 import org.sonar.core.issue.db.IssueStorage;
-import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.UnauthorizedException;
 import org.sonar.server.user.MockUserSession;
 import org.sonar.server.user.UserSession;
 
 import java.util.List;
 import java.util.Map;
+import java.util.NoSuchElementException;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static com.google.common.collect.Maps.newHashMap;
@@ -277,7 +277,7 @@ public class IssueBulkChangeServiceTest {
       service.execute(issueBulkChangeQuery, userSession);
       fail();
     } catch (Exception e) {
-      assertThat(e).isInstanceOf(BadRequestException.class).hasMessage("The action : 'unknown' is unknown");
+      assertThat(e).isInstanceOf(NoSuchElementException.class);
     }
     verifyZeroInteractions(issueStorage);
     verifyZeroInteractions(issueNotifications);
index 0cd775e1f329396f0b5e906af04d17b1bb9670cd..101715be0db9880f81661919efe5821c9b798161 100644 (file)
@@ -652,6 +652,20 @@ public class IssueFilterServiceTest {
     verify(issueFilterSerializer).deserialize("componentRoots=struts");
   }
 
+  @Test
+  public void user_can_share_filter_if_logged_and_own_sharing_permission(){
+    when(authorizationDao.selectGlobalPermissions("john")).thenReturn(newArrayList(Permission.DASHBOARD_SHARING.key()));
+    UserSession userSession = MockUserSession.create().setLogin("john");
+    assertThat(service.canShareFilter(userSession)).isTrue();
+
+    userSession = MockUserSession.create().setLogin(null);
+    assertThat(service.canShareFilter(userSession)).isFalse();
+
+    when(authorizationDao.selectGlobalPermissions("john")).thenReturn(Collections.<String>emptyList());
+    userSession = MockUserSession.create().setLogin("john");
+    assertThat(service.canShareFilter(userSession)).isFalse();
+  }
+
   private static class Matches extends BaseMatcher<IssueFilterDto> {
 
     private final IssueFilterDto referenceFilter;