Files
SCM-Manager/scm-webapp/src/main/java/sonia/scm/user/DefaultUserManager.java

412 lines
9.7 KiB
Java
Raw Normal View History

/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.user;
import com.github.sdorra.ssp.PermissionActionCheck;
2010-11-28 11:32:41 +01:00
import com.google.inject.Inject;
import com.google.inject.Singleton;
2018-10-17 11:58:37 +02:00
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.ContextEntry;
import sonia.scm.EagerSingleton;
import sonia.scm.HandlerEventType;
import sonia.scm.ManagerDaoAdapter;
import sonia.scm.NotFoundException;
import sonia.scm.SCMContextProvider;
2011-02-12 15:43:27 +01:00
import sonia.scm.TransformFilter;
2011-02-11 19:44:10 +01:00
import sonia.scm.search.SearchRequest;
import sonia.scm.search.SearchUtil;
import sonia.scm.util.CollectionAppender;
2010-11-26 15:37:35 +01:00
import sonia.scm.util.Util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
2019-04-09 13:09:16 +02:00
import java.util.function.Predicate;
/**
*
* @author Sebastian Sdorra
*/
@Singleton @EagerSingleton
public class DefaultUserManager extends AbstractUserManager
{
/** Field description */
2010-12-05 19:26:38 +01:00
public static final String STORE_NAME = "users";
2010-11-26 15:37:35 +01:00
/** the logger for XmlUserManager */
private static final Logger logger =
LoggerFactory.getLogger(DefaultUserManager.class);
2010-11-28 11:32:41 +01:00
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
* @param userDAO
2010-11-28 11:32:41 +01:00
*/
@Inject
public DefaultUserManager(UserDAO userDAO)
2010-11-28 11:32:41 +01:00
{
this.userDAO = userDAO;
this.managerDaoAdapter = new ManagerDaoAdapter<>(userDAO);
2010-11-28 11:32:41 +01:00
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @throws IOException
*/
@Override
public void close() throws IOException
{
// do nothing
}
2010-12-04 15:58:13 +01:00
/**
* Method description
*
*
* @param username
*
* @return
*/
@Override
public boolean contains(String username)
{
return userDAO.contains(username);
2010-12-04 15:58:13 +01:00
}
/**
* Method description
*
*
* @param user
*
* @throws IOException
*/
@Override
public User create(User user) {
2012-03-16 09:44:32 +01:00
String type = user.getType();
if (Util.isEmpty(type)) {
2012-03-16 09:44:32 +01:00
user.setType(userDAO.getType());
}
logger.info("create user {} of type {}", user.getName(), user.getType());
return managerDaoAdapter.create(
user,
UserPermissions::create,
newUser -> fireEvent(HandlerEventType.BEFORE_CREATE, newUser),
newUser -> fireEvent(HandlerEventType.CREATE, newUser)
);
}
@Override
public void delete(User user) {
2018-07-12 10:30:33 +02:00
logger.info("delete user {} of type {}", user.getName(), user.getType());
managerDaoAdapter.delete(
user,
() -> UserPermissions.delete(user.getName()),
toDelete -> fireEvent(HandlerEventType.BEFORE_DELETE, toDelete),
toDelete -> fireEvent(HandlerEventType.DELETE, toDelete)
);
}
/**
* Method description
*
*
* @param context
*/
@Override
public void init(SCMContextProvider context)
{
}
/**
* Method description
*
*
* @param user
*
* @throws IOException
*/
@Override
public void modify(User user) {
logger.info("modify user {} of type {}", user.getName(), user.getType());
managerDaoAdapter.modify(
user,
2018-10-17 11:58:37 +02:00
UserPermissions::modify,
notModified -> fireEvent(HandlerEventType.BEFORE_MODIFY, user, notModified),
notModified -> fireEvent(HandlerEventType.MODIFY, user, notModified));
}
/**
* Method description
*
*
* @param user
*
* @throws IOException
*/
2010-11-26 15:37:35 +01:00
@Override
public void refresh(User user) {
2010-12-05 17:33:29 +01:00
if (logger.isInfoEnabled())
{
logger.info("refresh user {} of type {}", user.getName(), user.getType());
}
UserPermissions.read(user).check();
User fresh = userDAO.get(user.getName());
if (fresh == null)
{
throw new NotFoundException(User.class, user.getName());
}
fresh.copyProperties(user);
}
2011-02-11 19:44:10 +01:00
/**
* Method description
*
*
* @param searchRequest
*
* @return
*/
@Override
public Collection<User> search(final SearchRequest searchRequest)
{
2011-02-12 11:24:08 +01:00
if (logger.isDebugEnabled())
{
logger.debug("search user with query {}", searchRequest.getQuery());
}
final PermissionActionCheck<User> check = UserPermissions.read();
return SearchUtil.search(searchRequest, userDAO.getAll(), new TransformFilter<User, User>() {
2011-02-12 15:43:27 +01:00
@Override
public User accept(User user)
2011-02-11 19:44:10 +01:00
{
2011-02-12 15:43:27 +01:00
User result = null;
if (check.isPermitted(user) && matches(searchRequest, user)) {
2011-02-12 15:43:27 +01:00
result = user.clone();
2011-02-11 19:44:10 +01:00
}
2011-02-12 15:43:27 +01:00
return result;
}
});
2011-02-11 19:44:10 +01:00
}
private boolean matches(SearchRequest searchRequest, User user) {
return SearchUtil.matchesOne(searchRequest, user.getName(), user.getDisplayName(), user.getMail());
}
2011-02-11 19:44:10 +01:00
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param id
*
* @return
*/
2010-11-26 15:37:35 +01:00
@Override
public User get(String id)
{
UserPermissions.read().check(id);
User user = userDAO.get(id);
2010-11-07 15:19:00 +01:00
if (user != null)
{
2010-11-07 15:19:00 +01:00
user = user.clone();
}
return user;
}
/**
* Method description
*
*
* @return
*/
2010-11-26 15:37:35 +01:00
@Override
public Collection<User> getAll()
2011-06-09 22:10:30 +02:00
{
2019-04-09 13:09:16 +02:00
return getAll(user -> true, null);
2011-06-09 22:10:30 +02:00
}
/**
* Method description
*
*
* @param comparator
*
* @return
*/
@Override
2019-04-09 13:09:16 +02:00
public Collection<User> getAll(Predicate<User> filter, Comparator<User> comparator)
{
List<User> users = new ArrayList<>();
PermissionActionCheck<User> check = UserPermissions.read();
for (User user : userDAO.getAll()) {
2019-04-09 13:09:16 +02:00
if (filter.test(user) && check.isPermitted(user)) {
users.add(user.clone());
}
}
if (comparator != null) {
2011-06-09 22:10:30 +02:00
Collections.sort(users, comparator);
}
return users;
}
/**
* Method description
*
*
2011-06-09 21:46:22 +02:00
*
* @param comaparator
* @param start
* @param limit
*
* @return
*/
@Override
public Collection<User> getAll(Comparator<User> comaparator, int start, int limit) {
final PermissionActionCheck<User> check = UserPermissions.read();
return Util.createSubCollection(userDAO.getAll(), comaparator,
new CollectionAppender<User>()
{
@Override
public void append(Collection<User> collection, User item)
{
if (check.isPermitted(item)) {
collection.add(item.clone());
}
}
}, start, limit);
}
2011-06-09 21:46:22 +02:00
/**
* Method description
*
*
* @param start
* @param limit
*
* @return
*/
@Override
public Collection<User> getAll(int start, int limit)
{
return getAll(null, start, limit);
}
2012-03-16 09:44:32 +01:00
/**
* Method description
*
*
* @return
*/
@Override
public String getDefaultType()
{
return userDAO.getType();
}
2011-02-12 19:55:18 +01:00
/**
* Method description
*
*
* @return
*/
@Override
public Long getLastModified()
{
return userDAO.getLastModified();
2011-02-12 19:55:18 +01:00
}
//~--- methods --------------------------------------------------------------
2018-10-17 11:58:37 +02:00
@Override
public void changePasswordForLoggedInUser(String oldPassword, String newPassword) {
User user = get((String) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal());
if (!user.getPassword().equals(oldPassword)) {
2019-03-12 15:10:43 +01:00
throw new InvalidPasswordException(ContextEntry.ContextBuilder.entity("PasswordChange", "-").in(User.class, user.getName()));
2018-10-17 11:58:37 +02:00
}
user.setPassword(newPassword);
managerDaoAdapter.modify(
user,
UserPermissions::changePassword,
notModified -> fireEvent(HandlerEventType.BEFORE_MODIFY, user, notModified),
notModified -> fireEvent(HandlerEventType.MODIFY, user, notModified));
}
@Override
public void overwritePassword(String userId, String newPassword) {
User user = get(userId);
if (user == null) {
throw new NotFoundException(User.class, userId);
2018-10-17 11:58:37 +02:00
}
if (!isTypeDefault(user)) {
2019-03-12 15:10:43 +01:00
throw new ChangePasswordNotAllowedException(ContextEntry.ContextBuilder.entity("PasswordChange", "-").in(User.class, user.getName()), user.getType());
2018-10-17 11:58:37 +02:00
}
user.setPassword(newPassword);
this.modify(user);
}
//~--- fields ---------------------------------------------------------------
private final UserDAO userDAO;
private final ManagerDaoAdapter<User> managerDaoAdapter;
}