Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
[UserSubstitution] Handle user removal #696
Browse files Browse the repository at this point in the history
  • Loading branch information
glebfox committed Oct 17, 2021
1 parent ffb62d7 commit 016a929
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2021 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.jmix.ui.app;

import io.jmix.core.usersubstitution.event.UiUserSubstitutionsChangedEvent;
import io.jmix.core.usersubstitution.event.UserSubstitutionsChangedEvent;
import io.jmix.ui.UiEventPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component("ui_UserSubstitutionsChangedListener")
public class UserSubstitutionsChangedListener {

@Autowired
protected UiEventPublisher uiEventPublisher;

@EventListener
public void onUserSubstitutionsChanged(UserSubstitutionsChangedEvent event) {
uiEventPublisher.publishEvent(new UiUserSubstitutionsChangedEvent(event.getSource()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
* Makes UI-specific preparations to user substitution and performs it using {@link UserSubstitutionManager}
* Checks if there are screens that have unsaved changes and shows dialog window with options:
* <ol>
* <li><b>Discard changes</b> (and close all windows, cleanups background tasks, then performs substitution and recreates main window)</li>
* <li><b>Discard changes</b> (and close all windows, cleanups background tasks, then performs
* substitution and recreates main window)</li>
* <li><b>Cancel</b> (invokes all {@code cancelAction}s)</li>
* </ol>
*/
Expand All @@ -56,21 +57,19 @@ public SubstituteUserAction(UserDetails newSubstitutedUser,
Icons icons,
UserSubstitutionManager substitutionManager) {
super(ID);

this.messages = messages;
this.icons = icons;
this.substitutionManager = substitutionManager;

setCaption(messages.getMessage("actions.Yes"));
setIcon(icons.get(JmixIcon.DIALOG_OK));
this.newSubstitutedUser = newSubstitutedUser;
this.prevSubstitutedUser = oldSubstitutedUser;

setCaption(messages.getMessage("actions.Yes"));
setIcon(icons.get(JmixIcon.DIALOG_OK));
}


@Override
public void actionPerform(Component component) {

AppUI currentUI = AppUI.getCurrent();
if (currentUI == null)
return;
Expand All @@ -83,9 +82,8 @@ public void actionPerform(Component component) {
new BaseAction("discardChanges")
.withCaption(messages.getMessage("discardChanges"))
.withIcon(icons.get(JmixIcon.DIALOG_OK))
.withHandler(event -> {
doSubstituteUser(currentUI);
}),
.withHandler(event ->
doSubstituteUser(currentUI)),
new DialogAction(DialogAction.Type.CANCEL, Action.Status.PRIMARY)
.withHandler(event -> cancel())
)
Expand All @@ -96,9 +94,10 @@ public void actionPerform(Component component) {
}

protected void doSubstituteUser(AppUI currentUI) {
substitutionManager.substituteUser(newSubstitutedUser.getUsername());

currentUI.getApp().removeAllWindows();
currentUI.getApp().cleanupBackgroundTasks();
substitutionManager.substituteUser(newSubstitutedUser.getUsername());
currentUI.getApp().createTopLevelWindow(currentUI);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ public void setFormatter(@Nullable Formatter<? super UserDetails> formatter) {

@EventListener
protected void onUserSubstitutionsChanged(UiUserSubstitutionsChangedEvent event) {
if (Objects.equals(currentUserSubstitution.getAuthenticatedUser(), event.getSource())) {
UserDetails authenticatedUser = currentUserSubstitution.getAuthenticatedUser();
if (Objects.equals(authenticatedUser.getUsername(), event.getSource())) {
refreshUser();
}
}
Expand Down

0 comments on commit 016a929

Please sign in to comment.