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 scheduled task for unlocking users #521

Open
wants to merge 1 commit into
base: development
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 @@ -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());
}
}
}

Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down