All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.server.user.DefaultUserService Maven / Gradle / Ivy

There is a newer version: 7.2.1
Show newest version
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.server.user;

import com.google.common.base.Strings;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.CheckForNull;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.user.RubyUserService;
import org.sonar.api.user.User;
import org.sonar.api.user.UserFinder;
import org.sonar.api.user.UserQuery;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.user.index.UserDoc;
import org.sonar.server.user.index.UserIndex;
import org.sonar.server.util.RubyUtils;

import static org.sonar.server.util.Validation.checkMandatoryParameter;

public class DefaultUserService implements RubyUserService {

  private final UserIndex userIndex;
  private final UserUpdater userUpdater;
  private final UserFinder finder;
  private final UserSession userSession;

  public DefaultUserService(UserIndex userIndex, UserUpdater userUpdater, UserFinder finder, UserSession userSession) {
    this.userIndex = userIndex;
    this.userUpdater = userUpdater;
    this.finder = finder;
    this.userSession = userSession;
  }

  @Override
  @CheckForNull
  public User findByLogin(String login) {
    return finder.findByLogin(login);
  }

  @Override
  public List find(Map params) {
    UserQuery query = parseQuery(params);
    return finder.find(query);
  }

  private UserQuery parseQuery(Map params) {
    UserQuery.Builder builder = UserQuery.builder();
    if (RubyUtils.toBoolean(params.get("includeDeactivated")) == Boolean.TRUE) {
      builder.includeDeactivated();
    }
    builder.logins(RubyUtils.toStrings(params.get("logins")));
    builder.searchText((String) params.get("s"));
    return builder.build();
  }

  @CheckForNull
  public UserDoc getByLogin(String login) {
    return userIndex.getNullableByLogin(login);
  }

  public boolean create(Map params) {
    String password = (String) params.get("password");
    String passwordConfirmation = (String) params.get("password_confirmation");
    checkMandatoryParameter(password, "Password");
    checkMandatoryParameter(passwordConfirmation, "Password confirmation");
    if (!StringUtils.equals(password, passwordConfirmation)) {
      throw new BadRequestException("user.password_doesnt_match_confirmation");
    }

    NewUser newUser = NewUser.create()
      .setLogin((String) params.get("login"))
      .setName((String) params.get("name"))
      .setEmail((String) params.get("email"))
      .setScmAccounts(RubyUtils.toStrings(params.get("scm_accounts")))
      .setPassword(password);
    return userUpdater.create(newUser);
  }

  public void deactivate(String login) {
    if (Strings.isNullOrEmpty(login)) {
      throw new BadRequestException("Login is missing");
    }
    userSession.checkPermission(GlobalPermissions.SYSTEM_ADMIN);
    if (Objects.equals(userSession.getLogin(), login)) {
      throw new BadRequestException("Self-deactivation is not possible");
    }
    userUpdater.deactivateUserByLogin(login);
  }

  public void index() {
    userUpdater.index();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy