-
Notifications
You must be signed in to change notification settings - Fork 219
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,4 +73,11 @@ public interface AuthenticationManager { | |
* @see AuthenticationService#logout() | ||
*/ | ||
void logout(); | ||
|
||
/** | ||
* Checks if user is active | ||
* @param user user to check | ||
* @return true if user is active | ||
*/ | ||
boolean isUserActive(User user); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move a code from |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,6 +156,12 @@ public void logout() { | |
} | ||
} | ||
|
||
@Override | ||
public boolean isUserActive(User user) { | ||
return authenticationManager.isUserActive(user); | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,4 +73,11 @@ public interface AuthenticationService { | |
* @throws NoUserSessionException if session is absent or expired | ||
*/ | ||
void logout(); | ||
|
||
/** | ||
* Check if user is active | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Omit this part when the |
||
* @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 |
---|---|---|
|
@@ -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; | ||
|
@@ -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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. T foo = AppBeans.get(T.class); |
||
} | ||
|
||
public void doAfterChangeUser() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same