org.sonar.db.user.UserDao Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonar-db Show documentation
Show all versions of sonar-db Show documentation
Create and request SonarQube schema
/*
* 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.db.user;
import com.google.common.base.Function;
import java.util.Collection;
import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.apache.ibatis.session.SqlSession;
import org.sonar.api.user.UserQuery;
import org.sonar.api.utils.System2;
import org.sonar.db.Dao;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
import org.sonar.db.RowNotFoundException;
public class UserDao implements Dao {
private final MyBatis mybatis;
private final System2 system2;
public UserDao(MyBatis mybatis, System2 system2) {
this.mybatis = mybatis;
this.system2 = system2;
}
public UserDto selectUserById(long userId) {
SqlSession session = mybatis.openSession(false);
try {
return selectUserById(session, userId);
} finally {
MyBatis.closeQuietly(session);
}
}
public UserDto selectUserById(SqlSession session, long userId) {
return session.getMapper(UserMapper.class).selectUser(userId);
}
/**
* Search for user by login. Disabled users are ignored.
*
* @return the user, null if user not found
*/
@CheckForNull
public UserDto selectActiveUserByLogin(String login) {
DbSession session = mybatis.openSession(false);
try {
return selectActiveUserByLogin(session, login);
} finally {
MyBatis.closeQuietly(session);
}
}
@CheckForNull
public UserDto selectActiveUserByLogin(DbSession session, String login) {
UserMapper mapper = session.getMapper(UserMapper.class);
return mapper.selectUserByLogin(login);
}
/**
* Select users by logins, including disabled users. An empty list is returned
* if list of logins is empty, without any db round trips.
*/
public List selectByLogins(DbSession session, Collection logins) {
return DatabaseUtils.executeLargeInputs(logins, new SelectByLogins(mapper(session)));
}
public List selectByLogins(Collection logins) {
DbSession session = mybatis.openSession(false);
try {
return selectByLogins(session, logins);
} finally {
MyBatis.closeQuietly(session);
}
}
private static class SelectByLogins implements Function, List> {
private final UserMapper mapper;
private SelectByLogins(UserMapper mapper) {
this.mapper = mapper;
}
@Override
public List apply(@Nonnull List partitionOfLogins) {
return mapper.selectByLogins(partitionOfLogins);
}
}
public List selectUsers(UserQuery query) {
SqlSession session = mybatis.openSession(false);
try {
UserMapper mapper = session.getMapper(UserMapper.class);
return mapper.selectUsers(query);
} finally {
MyBatis.closeQuietly(session);
}
}
public UserDto insert(SqlSession session, UserDto dto) {
session.getMapper(UserMapper.class).insert(dto);
return dto;
}
public UserDto update(SqlSession session, UserDto dto) {
session.getMapper(UserMapper.class).update(dto);
return dto;
}
/**
* Deactivate a user and drops all his preferences.
* @return false if the user does not exist, true if the existing user has been deactivated
*/
public boolean deactivateUserByLogin(DbSession dbSession, String login) {
UserMapper mapper = dbSession.getMapper(UserMapper.class);
UserDto dto = mapper.selectUserByLogin(login);
if (dto == null) {
return false;
}
mapper.removeUserFromGroups(dto.getId());
mapper.deleteUserActiveDashboards(dto.getId());
mapper.deleteUnsharedUserDashboards(dto.getId());
mapper.deleteUnsharedUserIssueFilters(dto.getLogin());
mapper.deleteUserIssueFilterFavourites(dto.getLogin());
mapper.deleteUnsharedUserMeasureFilters(dto.getId());
mapper.deleteUserMeasureFilterFavourites(dto.getId());
mapper.deleteUserProperties(dto.getId());
mapper.deleteUserRoles(dto.getId());
mapper.deactivateUser(dto.getId(), system2.now());
dbSession.commit();
return true;
}
@CheckForNull
public UserDto selectByLogin(DbSession session, String login) {
return mapper(session).selectByLogin(login);
}
public UserDto selectOrFailByLogin(DbSession session, String login) {
UserDto user = selectByLogin(session, login);
if (user == null) {
throw new RowNotFoundException(String.format("User with login '%s' has not been found", login));
}
return user;
}
public List selectByScmAccountOrLoginOrEmail(DbSession session, String scmAccountOrLoginOrEmail) {
String like = new StringBuilder().append("%")
.append(UserDto.SCM_ACCOUNTS_SEPARATOR).append(scmAccountOrLoginOrEmail)
.append(UserDto.SCM_ACCOUNTS_SEPARATOR).append("%").toString();
return mapper(session).selectNullableByScmAccountOrLoginOrEmail(scmAccountOrLoginOrEmail, like);
}
/**
* Check if an active user with the given email exits in database
*
* Please note that email is case insensitive, result for searching '[email protected]' or '[email protected]' will be the same
*/
public boolean doesEmailExist(DbSession dbSession, String email){
return mapper(dbSession).countByEmail(email.toLowerCase()) > 0;
}
protected UserMapper mapper(DbSession session) {
return session.getMapper(UserMapper.class);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy