org.sonar.db.user.AuthorizationDao Maven / Gradle / Ivy
/*
* 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 com.google.common.collect.Sets;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.ibatis.session.SqlSession;
import org.sonar.db.Dao;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
import static com.google.common.collect.Maps.newHashMap;
public class AuthorizationDao implements Dao {
private static final String USER_ID_PARAM = "userId";
private final MyBatis mybatis;
public AuthorizationDao(MyBatis mybatis) {
this.mybatis = mybatis;
}
public Collection keepAuthorizedProjectIds(final DbSession session, final Collection componentIds, @Nullable final Integer userId, final String role) {
if (componentIds.isEmpty()) {
return Collections.emptySet();
}
return DatabaseUtils.executeLargeInputs(componentIds, new Function, List>() {
@Override
public List apply(List partition) {
if (userId == null) {
return session.getMapper(AuthorizationMapper.class).keepAuthorizedProjectIdsForAnonymous(role, componentIds);
} else {
return session.getMapper(AuthorizationMapper.class).keepAuthorizedProjectIdsForUser(userId, role, componentIds);
}
}
});
}
/**
* Keep only authorized user that have the given permission on a given project.
* Please Note that if the permission is 'Anyone' is NOT taking into account by thie method.
*/
public Collection keepAuthorizedUsersForRoleAndProject(final DbSession session, final Collection userIds, final String role, final long projectId) {
return DatabaseUtils.executeLargeInputs(userIds, new SelectUsersByPermissionAndProject(session.getMapper(AuthorizationMapper.class), role, projectId));
}
public boolean isAuthorizedComponentKey(String componentKey, @Nullable Integer userId, String role) {
DbSession session = mybatis.openSession(false);
try {
return keepAuthorizedComponentKeys(session, componentKey, userId, role).size() == 1;
} finally {
MyBatis.closeQuietly(session);
}
}
private static List keepAuthorizedComponentKeys(final DbSession session, final String componentKey, @Nullable final Integer userId, final String role) {
if (userId == null) {
return session.getMapper(AuthorizationMapper.class).keepAuthorizedComponentKeysForAnonymous(role, Sets.newHashSet(componentKey));
} else {
return session.getMapper(AuthorizationMapper.class).keepAuthorizedComponentKeysForUser(userId, role, Sets.newHashSet(componentKey));
}
}
public Collection selectAuthorizedRootProjectsKeys(@Nullable Integer userId, String role) {
SqlSession session = mybatis.openSession(false);
try {
return selectAuthorizedRootProjectsKeys(userId, role, session);
} finally {
MyBatis.closeQuietly(session);
}
}
public Collection selectAuthorizedRootProjectsUuids(@Nullable Integer userId, String role) {
SqlSession session = mybatis.openSession(false);
try {
return selectAuthorizedRootProjectsUuids(userId, role, session);
} finally {
MyBatis.closeQuietly(session);
}
}
public Collection selectAuthorizedRootProjectsKeys(@Nullable Integer userId, String role, SqlSession session) {
String sql;
Map params = newHashMap();
sql = "selectAuthorizedRootProjectsKeys";
params.put(USER_ID_PARAM, userId);
params.put("role", role);
return session.selectList(sql, params);
}
public Collection selectAuthorizedRootProjectsUuids(@Nullable Integer userId, String role, SqlSession session) {
String sql;
Map params = newHashMap();
sql = "selectAuthorizedRootProjectsUuids";
params.put(USER_ID_PARAM, userId);
params.put("role", role);
return session.selectList(sql, params);
}
public List selectGlobalPermissions(@Nullable String userLogin) {
SqlSession session = mybatis.openSession(false);
try {
Map params = newHashMap();
params.put("userLogin", userLogin);
return session.selectList("selectGlobalPermissions", params);
} finally {
MyBatis.closeQuietly(session);
}
}
private static class SelectUsersByPermissionAndProject implements Function, List> {
private final AuthorizationMapper mapper;
private final String role;
private final long projectId;
private SelectUsersByPermissionAndProject(AuthorizationMapper mapper, String role, long projectId) {
this.mapper = mapper;
this.role = role;
this.projectId = projectId;
}
@Override
public List apply(@Nonnull List partitionOfIds) {
return mapper.keepAuthorizedUsersForRoleAndProject(role, projectId, partitionOfIds);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy