]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19084 Add step to deactivate removed users.
authorWojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com>
Mon, 1 May 2023 09:03:12 +0000 (11:03 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 11 May 2023 20:03:13 +0000 (20:03 +0000)
server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubIdentityProvider.java
server/sonar-db-dao/src/it/java/org/sonar/db/property/InternalPropertiesDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/property/InternalPropertiesDao.java
server/sonar-webserver-api/src/test/java/org/sonar/server/util/GlobalLockManagerImplTest.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/DeactivateActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/DeactivateAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/UserDeactivator.java

index 3acf65ff2cd4c6210cec546f057579cbde8b782c..3f72e7c470eb3f2c0e55550e9fe9c8dec355f474 100644 (file)
@@ -35,7 +35,7 @@ import static java.lang.String.format;
 
 public class GitHubIdentityProvider implements OAuth2IdentityProvider {
 
-  static final String KEY = "github";
+  public static final String KEY = "github";
 
   private final GitHubSettings settings;
   private final UserIdentityFactory userIdentityFactory;
index 38899d988ee21d65f06cd13b6ec1fb6e9d050d93..4e0876e1e3cb57cc98706030deeb5ebe07ba82ae 100644 (file)
@@ -63,14 +63,13 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoInteractions;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
+import static org.sonar.db.property.InternalPropertiesDao.LOCK_NAME_MAX_LENGTH;
 
 public class InternalPropertiesDaoIT {
 
   private static final String EMPTY_STRING = "";
   private static final String A_KEY = "a_key";
   private static final String ANOTHER_KEY = "another_key";
-  private static final String VALUE_1 = "one";
-  private static final String VALUE_2 = "two";
   private static final long DATE_1 = 1_500_000_000_000L;
   private static final long DATE_2 = 1_600_000_000_000L;
   private static final String VALUE_SMALL = "some small value";
@@ -391,7 +390,7 @@ public class InternalPropertiesDaoIT {
       .flatMap(s -> s)
       .collect(Collectors.toSet());
 
-    expectKeyNullOrEmptyIAE(() ->  underTest.selectByKeys(dbSession, keysIncludingAnEmptyString));
+    expectKeyNullOrEmptyIAE(() -> underTest.selectByKeys(dbSession, keysIncludingAnEmptyString));
   }
 
   @Test
@@ -548,8 +547,8 @@ public class InternalPropertiesDaoIT {
   }
 
   @Test
-  public void tryLock_throws_IAE_if_lock_name_length_is_16_or_more() {
-    String tooLongName = randomAlphabetic(16 + new Random().nextInt(30));
+  public void tryLock_throws_IAE_if_lock_name_length_is_too_long() {
+    String tooLongName = randomAlphabetic(LOCK_NAME_MAX_LENGTH + new Random().nextInt(30));
 
     assertThatThrownBy(() -> underTest.tryLock(dbSession, tooLongName, 60))
       .isInstanceOf(IllegalArgumentException.class)
index 5d7b79ec26e541f38fdede89cbada33b673f2d15..173f547fae6eb1ed863fe1265aa996ea43cca1cc 100644 (file)
@@ -50,7 +50,7 @@ public class InternalPropertiesDao implements Dao {
    */
   private static final String LOCK_PREFIX = "lock.";
 
-  private static final int KEY_MAX_LENGTH = 20;
+  private static final int KEY_MAX_LENGTH = 40;
   public static final int LOCK_NAME_MAX_LENGTH = KEY_MAX_LENGTH - LOCK_PREFIX.length();
 
   private static final int TEXT_VALUE_MAX_LENGTH = 4000;
index 13cd0d8b8b3cce4577f4117b79442b6053fac0a9..100b4c49144c6f2b8b51faa8fe780a5b627159ce 100644 (file)
@@ -34,17 +34,18 @@ import org.sonar.db.property.InternalPropertiesDao;
 
 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatNoException;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
+import static org.sonar.db.property.InternalPropertiesDao.LOCK_NAME_MAX_LENGTH;
 import static org.sonar.server.util.GlobalLockManager.DEFAULT_LOCK_DURATION_SECONDS;
 
 @RunWith(DataProviderRunner.class)
 public class GlobalLockManagerImplTest {
 
-  private static final int LOCK_NAME_MAX_LENGTH = 15;
   private final DbClient dbClient = mock(DbClient.class);
   private final InternalPropertiesDao internalPropertiesDao = mock(InternalPropertiesDao.class);
   private final DbSession dbSession = mock(DbSession.class);
@@ -64,16 +65,17 @@ public class GlobalLockManagerImplTest {
   }
 
   @Test
-  public void tryLock_fails_with_IAE_if_name_length_is_16_or_more() {
+  public void tryLock_fails_with_IAE_if_name_length_is_more_than_max_or_more() {
     String badLockName = RandomStringUtils.random(LOCK_NAME_MAX_LENGTH + 1 + new Random().nextInt(96));
 
     expectBadLockNameIAE(() -> underTest.tryLock(badLockName), badLockName);
   }
 
   @Test
-  public void tryLock_accepts_name_with_length_15_or_less() {
+  public void tryLock_accepts_name_with_allowed_length() {
     for (int i = 1; i <= LOCK_NAME_MAX_LENGTH; i++) {
-      underTest.tryLock(RandomStringUtils.random(i));
+      String lockName = RandomStringUtils.random(i);
+      assertThatNoException().isThrownBy(() -> underTest.tryLock(lockName));
     }
   }
 
@@ -110,7 +112,7 @@ public class GlobalLockManagerImplTest {
 
   @Test
   @UseDataProvider("randomValidDuration")
-  public void tryLock_with_duration_fails_with_IAE_if_name_length_is_16_or_more(int randomValidDuration) {
+  public void tryLock_with_duration_fails_with_IAE_if_name_length_is_36_or_more(int randomValidDuration) {
     String badLockName = RandomStringUtils.random(LOCK_NAME_MAX_LENGTH + 1 + new Random().nextInt(65));
 
     expectBadLockNameIAE(() -> underTest.tryLock(badLockName, randomValidDuration), badLockName);
@@ -119,7 +121,7 @@ public class GlobalLockManagerImplTest {
   @Test
   @UseDataProvider("randomValidLockName")
   public void tryLock_with_duration_fails_with_IAE_if_duration_is_0(String randomValidLockName) {
-    expectBadDuration(() ->  underTest.tryLock(randomValidLockName, 0),0);
+    expectBadDuration(() -> underTest.tryLock(randomValidLockName, 0), 0);
   }
 
   @Test
@@ -156,7 +158,7 @@ public class GlobalLockManagerImplTest {
   @DataProvider
   public static Object[][] randomValidDuration() {
     return new Object[][] {
-      {1+ new Random().nextInt(2_00)}
+      {1 + new Random().nextInt(2_00)}
     };
   }
 
index 7b51b96eff9681303d5269dd1f5dcaa67dec30c9..5f40a32b2f5502f1bebe855838d37c9f2572d3ca 100644 (file)
@@ -77,7 +77,7 @@ public class DeactivateActionIT {
   private final DbClient dbClient = db.getDbClient();
   private final DbSession dbSession = db.getSession();
   private final UserAnonymizer userAnonymizer = new UserAnonymizer(db.getDbClient(), () -> "anonymized");
-  private final UserDeactivator userDeactivator = new UserDeactivator(dbClient, userSession, userAnonymizer);
+  private final UserDeactivator userDeactivator = new UserDeactivator(dbClient, userAnonymizer);
   private final ManagedInstanceChecker managedInstanceChecker = mock(ManagedInstanceChecker.class);
   private final WsActionTester ws = new WsActionTester(new DeactivateAction(dbClient, userSession, new UserJsonWriter(userSession), userDeactivator, managedInstanceChecker));
 
index 51659894336b42776e77e5cc7be6a00899732025..b5426ae77c4060c78a8c31a684d210e25b0e10e3 100644 (file)
@@ -80,9 +80,8 @@ public class DeactivateAction implements UsersWsAction {
 
   @Override
   public void handle(Request request, Response response) throws Exception {
-    String login;
     userSession.checkLoggedIn().checkIsSystemAdministrator();
-    login = request.mandatoryParam(PARAM_LOGIN);
+    String login = request.mandatoryParam(PARAM_LOGIN);
     checkRequest(!login.equals(userSession.getLogin()), "Self-deactivation is not possible");
     try (DbSession dbSession = dbClient.openSession(false)) {
       preventManagedUserDeactivationIfManagedInstance(dbSession, login);
index 6c1f438b1ab636ce926d62201ab6ab9299c4373c..127a8bafbdf750bf841d33f5d2ba29b3ee8d4d24 100644 (file)
@@ -23,7 +23,6 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.property.PropertyQuery;
 import org.sonar.db.user.UserDto;
-import org.sonar.server.user.UserSession;
 
 import static org.sonar.api.CoreProperties.DEFAULT_ISSUE_ASSIGNEE;
 import static org.sonar.db.permission.GlobalPermission.ADMINISTER;
@@ -32,12 +31,10 @@ import static org.sonar.server.exceptions.NotFoundException.checkFound;
 
 public class UserDeactivator {
   private final DbClient dbClient;
-  private final UserSession userSession;
   private final UserAnonymizer userAnonymizer;
 
-  public UserDeactivator(DbClient dbClient, UserSession userSession, UserAnonymizer userAnonymizer) {
+  public UserDeactivator(DbClient dbClient, UserAnonymizer userAnonymizer) {
     this.dbClient = dbClient;
-    this.userSession = userSession;
     this.userAnonymizer = userAnonymizer;
   }
 
@@ -55,7 +52,6 @@ public class UserDeactivator {
   }
 
   private UserDto doBeforeDeactivation(DbSession dbSession, String login) {
-    checkRequest(!login.equals(userSession.getLogin()), "Self-deactivation is not possible");
     UserDto user = getUserOrThrow(dbSession, login);
     ensureNotLastAdministrator(dbSession, user);
     deleteRelatedData(dbSession, user);