aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@gmail.com>2013-08-14 17:43:52 +0200
committerJulien Lancelot <julien.lancelot@gmail.com>2013-08-14 17:43:52 +0200
commit3554f0dddfbacee2e11c6c73ca084cfca1d2bb2d (patch)
treec2e7589d13472c943be9934a66e4470bf3f925b1 /sonar-server
parentadde7cf9216f9a0ccf78409078838d826d816f4a (diff)
downloadsonarqube-3554f0dddfbacee2e11c6c73ca084cfca1d2bb2d.tar.gz
sonarqube-3554f0dddfbacee2e11c6c73ca084cfca1d2bb2d.zip
SONAR-4475 Add method to check that at least one admin exists
Diffstat (limited to 'sonar-server')
-rw-r--r--sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java7
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/controllers/groups_controller.rb4
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/controllers/users_controller.rb17
-rw-r--r--sonar-server/src/main/webapp/javascripts/application.js2
-rw-r--r--sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceTest.java19
5 files changed, 37 insertions, 12 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java b/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java
index 3fdcc998ab8..c6e22f94435 100644
--- a/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java
+++ b/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java
@@ -24,6 +24,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.ServerComponent;
import org.sonar.api.security.DefaultGroups;
+import org.sonar.api.utils.MessageException;
import org.sonar.core.permission.ComponentPermissionFacade;
import org.sonar.core.permission.Permission;
import org.sonar.core.user.*;
@@ -71,6 +72,12 @@ public class InternalPermissionService implements ServerComponent {
}
}
+ public void checkAtLeatOneSysAdminExists(){
+ if (roleDao.countUserWithPermission(Permission.SYSTEM_ADMIN.key()) == 0){
+ throw new MessageException(null, "global_permissions.error.need_at_lest_one_admin", null);
+ }
+ }
+
private void applyPermissionTemplate(String templateKey, String componentId) {
permissionFacade.applyPermissionTemplate(templateKey, Long.parseLong(componentId));
}
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/groups_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/groups_controller.rb
index f2d5fd5d878..1105fa5292b 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/groups_controller.rb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/groups_controller.rb
@@ -23,7 +23,7 @@ class GroupsController < ApplicationController
before_filter :admin_required
def index
- @groups = Group.find(:all, :order => 'name')
+ @groups = Group.all(:order => 'name')
if params[:id]
@group = Group.find(params[:id])
else
@@ -72,7 +72,7 @@ class GroupsController < ApplicationController
end
def to_index(errors, id)
- if !errors.empty?
+ unless errors.empty?
flash[:error] = errors.full_messages.join("<br/>\n")
end
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/users_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/users_controller.rb
index 97807f9a749..98bcbad7e30 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/users_controller.rb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/users_controller.rb
@@ -130,17 +130,16 @@ class UsersController < ApplicationController
end
def destroy
- @user = User.find(params[:id])
-
- if current_user.id==@user.id
- flash[:error] = 'Please log in with another user in order to delete yourself.'
-
- else
- Api.users.deactivate(@user.login)
+ begin
+ user = User.find(params[:id])
+ Api.users.deactivate(user.login)
flash[:notice] = 'User is deleted.'
+ rescue Java::OrgSonarServerExceptions::HttpException => exception
+ error = exception.cause
+ flash[:error] = (error.getMessage ? error.getMessage : Api::Utils.message(error.l10nKey, :params => error.l10nParams.to_a))
end
- to_index(@user.errors, nil)
+ redirect_to(:action => 'index', :id => nil)
end
def select_group
@@ -158,7 +157,7 @@ class UsersController < ApplicationController
end
def to_index(errors, id)
- if !errors.empty?
+ unless errors.empty?
flash[:error] = errors.full_messages.join("<br/>\n")
end
diff --git a/sonar-server/src/main/webapp/javascripts/application.js b/sonar-server/src/main/webapp/javascripts/application.js
index 773170b1a38..501fd589a24 100644
--- a/sonar-server/src/main/webapp/javascripts/application.js
+++ b/sonar-server/src/main/webapp/javascripts/application.js
@@ -320,8 +320,8 @@ function openModalWindow(url, options) {
$j('input[type=submit]', obj).removeAttr('disabled');
errorElt.show();
errorElt.html(xhr.responseText);
- // otherwise replace modal window by the returned text
} else {
+ // otherwise replace modal window by the returned text
$j("#modal").html(xhr.responseText);
}
}
diff --git a/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceTest.java b/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceTest.java
index 6dd2f92fca6..04d8daceb6a 100644
--- a/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceTest.java
@@ -30,6 +30,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.security.DefaultGroups;
+import org.sonar.api.utils.MessageException;
import org.sonar.core.permission.ComponentPermissionFacade;
import org.sonar.core.permission.Permission;
import org.sonar.core.user.*;
@@ -40,6 +41,8 @@ import org.sonar.server.user.MockUserSession;
import java.util.Map;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.*;
@@ -191,6 +194,22 @@ public class InternalPermissionServiceTest {
verify(permissionFacade).applyPermissionTemplate("my_template_key", 3L);
}
+ @Test
+ public void should_check_at_least_one_sys_admin_exists() throws Exception {
+ // One admin exists -> all is ok
+ when(roleDao.countUserWithPermission(anyString())).thenReturn(1);
+ service.checkAtLeatOneSysAdminExists();
+
+ // No admin -> fail
+ try {
+ when(roleDao.countUserWithPermission(anyString())).thenReturn(0);
+ service.checkAtLeatOneSysAdminExists();
+ fail();
+ } catch (Exception e){
+ assertThat(e).isInstanceOf(MessageException.class);
+ }
+ }
+
protected static class MatchesUserRole extends BaseMatcher<UserRoleDto> {
private final UserRoleDto referenceDto;