diff --git a/devicehive-frontend/src/main/java/com/devicehive/service/UserService.java b/devicehive-frontend/src/main/java/com/devicehive/service/UserService.java index eddabe510..c5af60c11 100644 --- a/devicehive-frontend/src/main/java/com/devicehive/service/UserService.java +++ b/devicehive-frontend/src/main/java/com/devicehive/service/UserService.java @@ -121,14 +121,16 @@ public UserVO updateUser(@NotNull Long id, UserUpdate userToUpdate, UserVO curUs return existing; } - final boolean isClient = UserRole.CLIENT.equals(curUser.getRole()); - if (isClient) { - if (userToUpdate.getLogin().isPresent() || - userToUpdate.getStatus().isPresent() || - userToUpdate.getRole().isPresent()) { - logger.error("Can't update user with id {}: users with the 'client' role not allowed to change their " + - "login, status or role", id); - throw new HiveException(Messages.ADMIN_PERMISSIONS_REQUIRED, FORBIDDEN.getStatusCode()); + if (curUser != null) { + final boolean isClient = UserRole.CLIENT.equals(curUser.getRole()); + if (isClient) { + if (userToUpdate.getLogin().isPresent() || + userToUpdate.getStatus().isPresent() || + userToUpdate.getRole().isPresent()) { + logger.error("Can't update user with id {}: users with the 'client' role not allowed to change their " + + "login, status or role", id); + throw new HiveException(Messages.ADMIN_PERMISSIONS_REQUIRED, FORBIDDEN.getStatusCode()); + } } } @@ -173,6 +175,11 @@ public UserVO updateUser(@NotNull Long id, UserUpdate userToUpdate, UserVO curUs return userDao.merge(existing); } + @Transactional(propagation = Propagation.REQUIRED) + public UserVO updateUser(@NotNull Long id, UserUpdate userToUpdate) { + return updateUser(id, userToUpdate, null); + } + /** * Allows user access to given device type * diff --git a/devicehive-frontend/src/main/java/com/devicehive/util/UserUnlockScheduler.java b/devicehive-frontend/src/main/java/com/devicehive/util/UserUnlockScheduler.java new file mode 100644 index 000000000..afcf9fe07 --- /dev/null +++ b/devicehive-frontend/src/main/java/com/devicehive/util/UserUnlockScheduler.java @@ -0,0 +1,59 @@ +package com.devicehive.util; + +/* + * #%L + * DeviceHive Frontend Logic + * %% + * Copyright (C) 2016 DataArt + * %% + * 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. + * #L% + */ + +import com.devicehive.model.enums.UserStatus; +import com.devicehive.model.rpc.ListUserRequest; +import com.devicehive.model.updates.UserUpdate; +import com.devicehive.service.UserService; +import com.devicehive.vo.UserVO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +public class UserUnlockScheduler { + + private static final Logger logger = LoggerFactory.getLogger(UserUnlockScheduler.class); + + private final UserService userService; + + @Autowired + public UserUnlockScheduler(UserService userService) { + this.userService = userService; + } + + @Scheduled(fixedRateString = "${user.unlock.interval.ms:1800000}") + public void unlockUsers() { + ListUserRequest listUserRequest = new ListUserRequest(); + listUserRequest.setStatus(UserStatus.LOCKED_OUT.getValue()); + + userService.list(listUserRequest).thenAccept(users -> { + UserUpdate userUpdate = new UserUpdate(); + userUpdate.setStatus(UserStatus.ACTIVE.getValue()); + + users.stream().map(UserVO::getId).forEach(id -> userService.updateUser(id, userUpdate)); + logger.debug("Successfully unlocked {} users", users.size()); + }); + } +} diff --git a/devicehive-frontend/src/main/resources/application.properties b/devicehive-frontend/src/main/resources/application.properties index ee8a098aa..5d649d06a 100644 --- a/devicehive-frontend/src/main/resources/application.properties +++ b/devicehive-frontend/src/main/resources/application.properties @@ -37,6 +37,7 @@ swagger.port=80 # Custom configuration properties app.executor.size=20 +user.unlock.interval.ms=1800000 #Hazelcast properties hazelcast.group.name=dev