add ui to give user api access

This commit is contained in:
f43nd1r 2018-08-24 04:43:32 +02:00
parent 7734763504
commit 1b6a396ccd
4 changed files with 26 additions and 6 deletions

View file

@ -69,7 +69,7 @@ public class UserService implements Serializable {
} }
@Transactional @Transactional
@PreAuthorize("hasRole(T(com.faendir.acra.model.User$Role).ADMIN)") @PreAuthorize("T(com.faendir.acra.security.SecurityUtils).hasRole(T(com.faendir.acra.model.User$Role).ADMIN)")
public void createUser(@NonNull String username, @NonNull String password) { public void createUser(@NonNull String username, @NonNull String password) {
if (new JPAQuery<>(entityManager).from(USER).where(USER.username.eq(username)).fetchFirst() != null) { if (new JPAQuery<>(entityManager).from(USER).where(USER.username.eq(username)).fetchFirst() != null) {
throw new IllegalArgumentException("Username already exists"); throw new IllegalArgumentException("Username already exists");
@ -77,6 +77,7 @@ public class UserService implements Serializable {
entityManager.persist(new User(username, passwordEncoder.encode(password), Collections.singleton(User.Role.USER))); entityManager.persist(new User(username, passwordEncoder.encode(password), Collections.singleton(User.Role.USER)));
} }
@PreAuthorize("T(com.faendir.acra.security.SecurityUtils).hasRole(T(com.faendir.acra.model.User$Role).ADMIN)")
public PlainTextUser createReporterUser() { public PlainTextUser createReporterUser() {
String username; String username;
do { do {
@ -91,6 +92,7 @@ public class UserService implements Serializable {
} }
@Transactional @Transactional
@PreAuthorize("authentication.name == #user.username")
public boolean changePassword(@NonNull User user, @NonNull String oldPassword, @NonNull String newPassword) { public boolean changePassword(@NonNull User user, @NonNull String oldPassword, @NonNull String newPassword) {
if (checkPassword(user, oldPassword)) { if (checkPassword(user, oldPassword)) {
user.setPassword(passwordEncoder.encode(newPassword)); user.setPassword(passwordEncoder.encode(newPassword));
@ -101,7 +103,7 @@ public class UserService implements Serializable {
} }
@Transactional @Transactional
@PreAuthorize("hasRole(T(com.faendir.acra.model.User$Role).ADMIN)") @PreAuthorize("T(com.faendir.acra.security.SecurityUtils).hasRole(T(com.faendir.acra.model.User$Role).ADMIN)")
public void setAdmin(@NonNull User user, boolean admin) { public void setAdmin(@NonNull User user, boolean admin) {
if (admin) { if (admin) {
user.getRoles().add(User.Role.ADMIN); user.getRoles().add(User.Role.ADMIN);
@ -112,7 +114,18 @@ public class UserService implements Serializable {
} }
@Transactional @Transactional
@PreAuthorize("hasRole(T(com.faendir.acra.model.User$Role).ADMIN)") @PreAuthorize("T(com.faendir.acra.security.SecurityUtils).hasRole(T(com.faendir.acra.model.User$Role).ADMIN)")
public void setApiAccess(@NonNull User user, boolean access) {
if (access) {
user.getRoles().add(User.Role.API);
} else {
user.getRoles().remove(User.Role.API);
}
entityManager.merge(user);
}
@Transactional
@PreAuthorize("T(com.faendir.acra.security.SecurityUtils).hasRole(T(com.faendir.acra.model.User$Role).ADMIN)")
public void setPermission(@NonNull User user, @NonNull App app, @NonNull Permission.Level level) { public void setPermission(@NonNull User user, @NonNull App app, @NonNull Permission.Level level) {
Optional<Permission> permission = user.getPermissions().stream().filter(p -> p.getApp().equals(app)).findAny(); Optional<Permission> permission = user.getPermissions().stream().filter(p -> p.getApp().equals(app)).findAny();
if (permission.isPresent()) { if (permission.isPresent()) {
@ -127,9 +140,10 @@ public class UserService implements Serializable {
private User getDefaultUser() { private User getDefaultUser() {
return new User(acraConfiguration.getUser().getName(), return new User(acraConfiguration.getUser().getName(),
passwordEncoder.encode(acraConfiguration.getUser().getPassword()), passwordEncoder.encode(acraConfiguration.getUser().getPassword()),
Arrays.asList(User.Role.USER, User.Role.ADMIN, User.Role.API)); Arrays.asList(User.Role.USER, User.Role.ADMIN));
} }
@PreAuthorize("T(com.faendir.acra.security.SecurityUtils).hasRole(T(com.faendir.acra.model.User$Role).ADMIN)")
public QueryDslDataProvider<User> getUserProvider() { public QueryDslDataProvider<User> getUserProvider() {
return new QueryDslDataProvider<>(new JPAQuery<>(entityManager).from(USER).where(USER.roles.any().eq(User.Role.USER)).select(USER)); return new QueryDslDataProvider<>(new JPAQuery<>(entityManager).from(USER).where(USER.roles.any().eq(User.Role.USER)).select(USER));
} }

View file

@ -81,6 +81,10 @@ public class UserManagerView extends BaseView {
userService.setAdmin(user, e.getValue()); userService.setAdmin(user, e.getValue());
userGrid.getDataProvider().refreshAll(); userGrid.getDataProvider().refreshAll();
}), new ComponentRenderer(), Messages.ADMIN); }), new ComponentRenderer(), Messages.ADMIN);
userGrid.addColumn(user -> new MyCheckBox(user.getRoles().contains(User.Role.API), !user.getUsername().equals(SecurityUtils.getUsername()), e -> {
userService.setApiAccess(user, e.getValue());
userGrid.getDataProvider().refreshAll();
}), new ComponentRenderer(), Messages.API);
for (App app : dataService.findAllApps()) { for (App app : dataService.findAllApps()) {
userGrid.addColumn(user -> { userGrid.addColumn(user -> {
Permission.Level permission = SecurityUtils.getPermission(app, user); Permission.Level permission = SecurityUtils.getPermission(app, user);

View file

@ -129,3 +129,4 @@ footer=Acrarium wird entwickelt von <a href=https://github.com/F43nd1r>F43nd1r</
blank= blank=
login=Login login=Login
oneArg={0} oneArg={0}
api=API-Zugriff

View file

@ -129,3 +129,4 @@ footer=Acrarium is developed by <a href=https://github.com/F43nd1r>F43nd1r</a>.
blank= blank=
login=Login login=Login
oneArg={0} oneArg={0}
api=API Access