2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
2017-02-26 13:19:35 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
2017-02-26 13:19:35 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2017-02-26 13:19:35 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
2017-02-26 13:19:35 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2017-02-26 13:19:35 +01:00
|
|
|
*/
|
2020-09-17 15:31:47 +02:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
package sonia.scm.security;
|
|
|
|
|
|
2018-07-16 11:26:00 +02:00
|
|
|
import com.github.legman.Subscribe;
|
2017-02-26 13:19:35 +01:00
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import sonia.scm.EagerSingleton;
|
|
|
|
|
import sonia.scm.ModificationHandlerEvent;
|
2017-06-25 19:01:33 +02:00
|
|
|
import sonia.scm.event.HandlerEvent;
|
2017-02-26 13:19:35 +01:00
|
|
|
import sonia.scm.event.ScmEventBus;
|
|
|
|
|
import sonia.scm.group.Group;
|
|
|
|
|
import sonia.scm.group.GroupEvent;
|
|
|
|
|
import sonia.scm.group.GroupModificationEvent;
|
2020-09-17 15:31:47 +02:00
|
|
|
import sonia.scm.repository.Namespace;
|
|
|
|
|
import sonia.scm.repository.NamespaceEvent;
|
|
|
|
|
import sonia.scm.repository.NamespaceModificationEvent;
|
2017-02-26 13:19:35 +01:00
|
|
|
import sonia.scm.repository.Repository;
|
|
|
|
|
import sonia.scm.repository.RepositoryEvent;
|
|
|
|
|
import sonia.scm.repository.RepositoryModificationEvent;
|
2020-09-17 15:31:47 +02:00
|
|
|
import sonia.scm.repository.RepositoryPermission;
|
2017-02-26 13:19:35 +01:00
|
|
|
import sonia.scm.user.User;
|
|
|
|
|
import sonia.scm.user.UserEvent;
|
|
|
|
|
import sonia.scm.user.UserModificationEvent;
|
|
|
|
|
|
2019-08-02 15:02:13 +02:00
|
|
|
import javax.inject.Singleton;
|
2020-09-17 15:31:47 +02:00
|
|
|
import java.util.Collection;
|
2019-08-02 15:02:13 +02:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
/**
|
2019-03-13 12:07:18 +01:00
|
|
|
* Receives all kinds of events, which affects authorization relevant data and fires an
|
2017-02-26 13:19:35 +01:00
|
|
|
* {@link AuthorizationChangedEvent} if authorization data has changed.
|
2019-03-13 12:07:18 +01:00
|
|
|
*
|
2017-02-26 13:19:35 +01:00
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
* @since 1.52
|
|
|
|
|
*/
|
2019-08-02 15:02:13 +02:00
|
|
|
@Singleton
|
2017-02-26 13:19:35 +01:00
|
|
|
@EagerSingleton
|
|
|
|
|
public class AuthorizationChangedEventProducer {
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
/**
|
|
|
|
|
* the logger for AuthorizationChangedEventProducer
|
|
|
|
|
*/
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(AuthorizationChangedEventProducer.class);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a new instance.
|
|
|
|
|
*/
|
|
|
|
|
public AuthorizationChangedEventProducer() {
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
/**
|
|
|
|
|
* Invalidates the cache of a user which was modified. The cache entries for the user will be invalidated for the
|
|
|
|
|
* following reasons:
|
|
|
|
|
* <ul>
|
|
|
|
|
* <li>Admin or Active flag was modified.</li>
|
|
|
|
|
* <li>New user created, for the case of old cache values</li>
|
|
|
|
|
* <li>User deleted</li>
|
|
|
|
|
* </ul>
|
|
|
|
|
*
|
|
|
|
|
* @param event user event
|
|
|
|
|
*/
|
|
|
|
|
@Subscribe
|
|
|
|
|
public void onEvent(UserEvent event) {
|
|
|
|
|
if (event.getEventType().isPost()) {
|
|
|
|
|
if (isModificationEvent(event)) {
|
|
|
|
|
handleUserModificationEvent((UserModificationEvent) event);
|
|
|
|
|
} else {
|
|
|
|
|
handleUserEvent(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-06-25 19:01:33 +02:00
|
|
|
private boolean isModificationEvent(HandlerEvent<?> event) {
|
2017-02-26 13:19:35 +01:00
|
|
|
return event instanceof ModificationHandlerEvent;
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
private void handleUserEvent(UserEvent event) {
|
|
|
|
|
String username = event.getItem().getName();
|
|
|
|
|
logger.debug(
|
|
|
|
|
"fire authorization changed event for user {}, because of user {} event", username, event.getEventType()
|
|
|
|
|
);
|
|
|
|
|
fireEventForUser(username);
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
private void handleUserModificationEvent(UserModificationEvent event) {
|
|
|
|
|
String username = event.getItem().getId();
|
|
|
|
|
User beforeModification = event.getItemBeforeModification();
|
|
|
|
|
if (isAuthorizationDataModified(event.getItem(), beforeModification)) {
|
|
|
|
|
logger.debug(
|
2019-03-13 12:07:18 +01:00
|
|
|
"fire authorization changed event for user {}, because of a authorization relevant field has changed",
|
2017-02-26 13:19:35 +01:00
|
|
|
username
|
|
|
|
|
);
|
|
|
|
|
fireEventForUser(username);
|
|
|
|
|
} else {
|
|
|
|
|
logger.debug(
|
2019-03-13 12:07:18 +01:00
|
|
|
"authorization changed event for user {} is not fired, because no authorization relevant field has changed",
|
2017-02-26 13:19:35 +01:00
|
|
|
username
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isAuthorizationDataModified(User user, User beforeModification) {
|
2019-03-13 12:07:18 +01:00
|
|
|
return user.isActive() != beforeModification.isActive();
|
2017-02-26 13:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void fireEventForUser(String username) {
|
|
|
|
|
sendEvent(AuthorizationChangedEvent.createForUser(username));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Invalidates the whole cache, if a repository has changed. The cache get cleared for one of the following reasons:
|
|
|
|
|
* <ul>
|
|
|
|
|
* <li>New repository created</li>
|
|
|
|
|
* <li>Repository was removed</li>
|
|
|
|
|
* <li>Archived, Public readable or permission field of the repository was modified</li>
|
|
|
|
|
* </ul>
|
|
|
|
|
*
|
|
|
|
|
* @param event repository event
|
|
|
|
|
*/
|
2020-11-09 16:04:54 +01:00
|
|
|
@Subscribe(async = false)
|
2017-02-26 13:19:35 +01:00
|
|
|
public void onEvent(RepositoryEvent event) {
|
|
|
|
|
if (event.getEventType().isPost()) {
|
|
|
|
|
if (isModificationEvent(event)) {
|
|
|
|
|
handleRepositoryModificationEvent((RepositoryModificationEvent) event);
|
|
|
|
|
} else {
|
|
|
|
|
handleRepositoryEvent(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2020-09-17 15:31:47 +02:00
|
|
|
@Subscribe
|
|
|
|
|
public void onEvent(NamespaceEvent event) {
|
2020-09-18 10:58:04 +02:00
|
|
|
if (event.getEventType().isPost() && isModificationEvent(event)) {
|
|
|
|
|
handleNamespaceModificationEvent((NamespaceModificationEvent) event);
|
2020-09-17 15:31:47 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
private void handleRepositoryModificationEvent(RepositoryModificationEvent event) {
|
|
|
|
|
Repository repository = event.getItem();
|
2020-09-17 15:31:47 +02:00
|
|
|
if (isAuthorizationDataModified(repository.getPermissions(), event.getItemBeforeModification().getPermissions())) {
|
|
|
|
|
logger.debug(
|
2020-12-03 09:39:25 +01:00
|
|
|
"fire authorization changed event, because the permissions of repository {} have changed", repository
|
2020-09-18 17:52:49 +02:00
|
|
|
);
|
|
|
|
|
fireEventForEveryUser();
|
|
|
|
|
} else if (!event.getItem().getNamespace().equals(event.getItemBeforeModification().getNamespace())) {
|
|
|
|
|
logger.debug(
|
2020-12-03 09:39:25 +01:00
|
|
|
"fire authorization changed event, because the namespace of repository {} has changed", repository
|
2020-09-17 15:31:47 +02:00
|
|
|
);
|
|
|
|
|
fireEventForEveryUser();
|
|
|
|
|
} else {
|
|
|
|
|
logger.debug(
|
2020-12-03 09:39:25 +01:00
|
|
|
"authorization changed event is not fired, because non relevant field of repository {} has changed", repository
|
2020-09-17 15:31:47 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleNamespaceModificationEvent(NamespaceModificationEvent event) {
|
|
|
|
|
Namespace namespace = event.getItem();
|
|
|
|
|
if (isAuthorizationDataModified(namespace.getPermissions(), event.getItemBeforeModification().getPermissions())) {
|
2017-02-26 13:19:35 +01:00
|
|
|
logger.debug(
|
2020-09-17 15:31:47 +02:00
|
|
|
"fire authorization changed event, because a relevant field of namespace {} has changed", namespace.getNamespace()
|
2017-02-26 13:19:35 +01:00
|
|
|
);
|
|
|
|
|
fireEventForEveryUser();
|
|
|
|
|
} else {
|
|
|
|
|
logger.debug(
|
2020-09-17 15:31:47 +02:00
|
|
|
"authorization changed event is not fired, because non relevant field of namespace {} has changed",
|
|
|
|
|
namespace.getNamespace()
|
2017-02-26 13:19:35 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 10:42:26 +01:00
|
|
|
private boolean isAuthorizationDataModified
|
|
|
|
|
(Collection<RepositoryPermission> newPermissions, Collection<RepositoryPermission> permissionsBeforeModification) {
|
2020-09-17 15:31:47 +02:00
|
|
|
return !(newPermissions.containsAll(permissionsBeforeModification) && permissionsBeforeModification.containsAll(newPermissions));
|
2017-02-26 13:19:35 +01:00
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
private void fireEventForEveryUser() {
|
|
|
|
|
sendEvent(AuthorizationChangedEvent.createForEveryUser());
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2020-12-02 10:42:26 +01:00
|
|
|
private void handleRepositoryEvent(RepositoryEvent event) {
|
2017-02-26 13:19:35 +01:00
|
|
|
logger.debug(
|
2019-03-13 12:07:18 +01:00
|
|
|
"fire authorization changed event, because of received {} event for repository {}",
|
2017-02-26 13:19:35 +01:00
|
|
|
event.getEventType(), event.getItem().getName()
|
|
|
|
|
);
|
|
|
|
|
fireEventForEveryUser();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Invalidates the whole cache if a group permission has changed and invalidates the cached entries of a user, if a
|
|
|
|
|
* user permission has changed.
|
|
|
|
|
*
|
|
|
|
|
* @param event permission event
|
|
|
|
|
*/
|
|
|
|
|
@Subscribe
|
2019-01-16 16:03:02 +01:00
|
|
|
public void onEvent(AssignedPermissionEvent event) {
|
2017-02-26 13:19:35 +01:00
|
|
|
if (event.getEventType().isPost()) {
|
2019-01-16 16:03:02 +01:00
|
|
|
AssignedPermission permission = event.getPermission();
|
2017-02-26 13:19:35 +01:00
|
|
|
if (permission.isGroupPermission()) {
|
|
|
|
|
handleGroupPermissionChange(permission);
|
|
|
|
|
} else {
|
|
|
|
|
handleUserPermissionChange(permission);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2019-01-16 16:03:02 +01:00
|
|
|
private void handleGroupPermissionChange(AssignedPermission permission) {
|
2017-02-26 13:19:35 +01:00
|
|
|
logger.debug(
|
2019-01-16 16:03:02 +01:00
|
|
|
"fire authorization changed event for group {}, because permission {} has changed",
|
|
|
|
|
permission.getName(), permission.getPermission()
|
2017-02-26 13:19:35 +01:00
|
|
|
);
|
|
|
|
|
fireEventForEveryUser();
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2019-01-16 16:03:02 +01:00
|
|
|
private void handleUserPermissionChange(AssignedPermission permission) {
|
2017-02-26 13:19:35 +01:00
|
|
|
logger.debug(
|
2020-12-02 10:42:26 +01:00
|
|
|
"fire authorization changed event for user {}, because permission {} has changed",
|
|
|
|
|
permission.getName(), permission.getPermission()
|
2017-02-26 13:19:35 +01:00
|
|
|
);
|
2019-03-13 12:07:18 +01:00
|
|
|
fireEventForUser(permission.getName());
|
2017-02-26 13:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Invalidates the whole cache, if a group has changed. The cache get cleared for one of the following reasons:
|
|
|
|
|
* <ul>
|
|
|
|
|
* <li>New group created</li>
|
|
|
|
|
* <li>Group was removed</li>
|
|
|
|
|
* <li>Group members was modified</li>
|
|
|
|
|
* </ul>
|
|
|
|
|
*
|
|
|
|
|
* @param event group event
|
|
|
|
|
*/
|
|
|
|
|
@Subscribe
|
|
|
|
|
public void onEvent(GroupEvent event) {
|
|
|
|
|
if (event.getEventType().isPost()) {
|
|
|
|
|
if (isModificationEvent(event)) {
|
2019-03-13 12:07:18 +01:00
|
|
|
handleGroupModificationEvent((GroupModificationEvent) event);
|
2017-02-26 13:19:35 +01:00
|
|
|
} else {
|
|
|
|
|
handleGroupEvent(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleGroupModificationEvent(GroupModificationEvent event) {
|
|
|
|
|
Group group = event.getItem();
|
|
|
|
|
if (isAuthorizationDataModified(group, event.getItemBeforeModification())) {
|
|
|
|
|
logger.debug("fire authorization changed event, because group {} has changed", group.getId());
|
|
|
|
|
fireEventForEveryUser();
|
|
|
|
|
} else {
|
|
|
|
|
logger.debug(
|
2019-03-13 12:07:18 +01:00
|
|
|
"authorization changed event is not fired, because non relevant field of group {} has changed",
|
2017-02-26 13:19:35 +01:00
|
|
|
group.getId()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
private boolean isAuthorizationDataModified(Group group, Group beforeModification) {
|
|
|
|
|
return !group.getMembers().equals(beforeModification.getMembers());
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2020-12-02 10:42:26 +01:00
|
|
|
private void handleGroupEvent(GroupEvent event) {
|
2017-02-26 13:19:35 +01:00
|
|
|
logger.debug(
|
2019-03-13 12:07:18 +01:00
|
|
|
"fire authorization changed event, because of received group event {} for group {}",
|
|
|
|
|
event.getEventType(),
|
2017-02-26 13:19:35 +01:00
|
|
|
event.getItem().getId()
|
|
|
|
|
);
|
2019-03-13 12:07:18 +01:00
|
|
|
fireEventForEveryUser();
|
2017-02-26 13:19:35 +01:00
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
@VisibleForTesting
|
|
|
|
|
protected void sendEvent(AuthorizationChangedEvent event) {
|
|
|
|
|
ScmEventBus.getInstance().post(event);
|
|
|
|
|
}
|
|
|
|
|
}
|