Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user activity check before substitution #2220

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,11 @@ public interface AuthenticationManager {
* @see AuthenticationService#logout()
*/
void logout();

/**
* Checks if user is active
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same

* @param user user to check
* @return true if user is active
*/
boolean isUserActive(User user);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move a code from AuthenticationManager & AuthenticationService to UserManagementService

}
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ public void logout() {
}
}

@Override
public boolean isUserActive(User user) {
try (Transaction ignored = persistence.createTransaction()) {
User foundUser = persistence.getEntityManager().find(User.class, user.getId());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a 'user'

if (foundUser == null) {
throw new NoResultException("User not found");
}
return foundUser.getActive();
}
}

protected AuthenticationDetails authenticateInternal(Credentials credentials) throws LoginException {
AuthenticationDetails details = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ public void logout() {
}
}

@Override
public boolean isUserActive(User user) {
return authenticationManager.isUserActive(user);
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra space

protected LoginException wrapInLoginException(Throwable throwable) {
//noinspection ThrowableResultOfMethodCallIgnored
Throwable rootCause = ExceptionUtils.getRootCause(throwable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,11 @@ public interface AuthenticationService {
* @throws NoUserSessionException if session is absent or expired
*/
void logout();

/**
* Check if user is active
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omit this part when the @return has the same description

* @param user user to check
* @return true if user is active
*/
boolean isUserActive(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.Messages;
import com.haulmont.cuba.gui.Notifications;
import com.haulmont.cuba.gui.components.AbstractAction;
import com.haulmont.cuba.gui.components.Frame;
import com.haulmont.cuba.gui.icons.CubaIcon;
import com.haulmont.cuba.security.auth.AuthenticationService;
import com.haulmont.cuba.security.entity.User;
import com.haulmont.cuba.web.App;
import com.haulmont.cuba.web.AppUI;
Expand All @@ -42,25 +44,36 @@ public ChangeSubstUserAction(User user) {
public void actionPerform(com.haulmont.cuba.gui.components.Component component) {
AppUI ui = AppUI.getCurrent();

WebScreens screens = (WebScreens) ui.getScreens();
if (!isUserActive(user)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's fail fast:

if (notActive) {
    showNotification()
    doRevert();
    return;
}

WebScreens  screens = ...

doRevert();
ui.getNotifications().create(Notifications.NotificationType.ERROR)
.withCaption("User substitution is not allowed")
.withDescription(String.format("User '%s' is disabled", user.getName()))
.show();
} else {
WebScreens screens = (WebScreens) ui.getScreens();
screens.checkModificationsAndCloseAll()
.then(() -> {
App app = ui.getApp();

screens.checkModificationsAndCloseAll()
.then(() -> {
App app = ui.getApp();
try {
app.getConnection().substituteUser(user);
doAfterChangeUser();
} catch (javax.persistence.NoResultException e) {
Messages messages = AppBeans.get(Messages.NAME);
app.getWindowManager().showNotification(
messages.formatMainMessage("substitutionNotPerformed", user.getName()),
Frame.NotificationType.WARNING
);
doRevert();
}
})
.otherwise(this::doRevert);
}
}

try {
app.getConnection().substituteUser(user);
doAfterChangeUser();
} catch (javax.persistence.NoResultException e) {
Messages messages = AppBeans.get(Messages.NAME);
app.getWindowManager().showNotification(
messages.formatMainMessage("substitutionNotPerformed", user.getName()),
Frame.NotificationType.WARNING
);
doRevert();
}
})
.otherwise(this::doRevert);
private static boolean isUserActive(User user) {
return AppBeans.<AuthenticationService>get(AuthenticationService.NAME).isUserActive(user);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

T foo = AppBeans.get(T.class);

}

public void doAfterChangeUser() {
Expand Down