use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.
the class RoleService method getRoleByName.
/**
* Gets the role by name.
*
* @param role the role
* @return the role by name
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public List<Role> getRoleByName(final Set<String> role) {
List<Role> roles = new ArrayList<Role>();
List<RoleEntity> roleEntities = roleRepository.findByNameIn(role);
if (ValidatorUtil.isNull(roleEntities)) {
LOGGER.warn("Roles with name '" + role + "' not found. Error: " + messageUtil.getAttributeInvalid("role", role + ""));
throw new RequestValidationException(messageUtil.getAttributeInvalid("role", role + ""));
}
for (RoleEntity roleEntity : roleEntities) {
if (Status.ACTIVE.getStatusEntity().getStatus().equalsIgnoreCase(roleEntity.getStatusEntity().getStatus())) {
roles.add(RoleConversionUtil.toRole(roleEntity, true, false));
}
}
return roles;
}
use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.
the class UserService method assignUserByRoleId.
/**
* Assign user by role id.
*
* @param roleId the role id
* @param role the role
* @return the role
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Role assignUserByRoleId(final Long roleId, final Role role) {
RoleEntity roleEntity = roleRepository.findByRoleId(roleId);
if (ValidatorUtil.isNull(roleEntity)) {
LOGGER.warn("Role with role id '" + roleId + "' not found. Error: " + messageUtil.getAttributeInvalid("role_id", roleId + ""));
throw new RequestValidationException(messageUtil.getAttributeInvalid("role_id", roleId + ""));
}
roleEntity.getUsers().clear();
if (role.getUserInfo() != null) {
for (UserInfo user : role.getUserInfo()) {
UserEntity userEntity = userRepository.findByUserId(user.getUserId());
roleEntity.getUsers().add(userEntity);
}
}
roleEntity = roleRepository.save(roleEntity);
activityLogger.log(ActivityType.ASSIGN_USERS_BY_ROLE, roleEntity.getName());
LOGGER.info("Users assigned with role successfully (role id: " + roleId + ")");
return RoleConversionUtil.toRole(roleEntity, false, true);
}
use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.
the class UserService method createUser.
/**
* Creates the user.
*
* @param userRequest the user request
* @return the user info
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public UserInfo createUser(final UserInfo userRequest) {
userValidator.validateCreateUser(userRequest);
Set<RoleEntity> roleEntities = roleService.getRolesById(userRequest.getRoleIds());
UserEntity userEntity = UserConversionUtil.toUserEntity(userRequest, roleEntities);
String password = ValidatorUtil.randomAlphaNumeric(16);
userEntity.setPassword(StringUtil.encodeString(password));
userEntity.setIs2FaEnabled(userRequest.getIs2FaEnabled());
userEntity = userRepository.save(userEntity);
LOGGER.info("User with username '" + userEntity.getUsername() + "' created successfully.");
activityLogger.log(ActivityType.CREATE_USER, userRequest.getUsername());
try {
if (userEntity.getUserId() != null) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", userEntity.getName());
map.put("username", userEntity.getUsername());
map.put("password", password);
mailService.send(userEntity.getEmail(), mailUtils.getSubjectAccountUsername(), TemplateService.Template.ACCOUNT_USERNAME, map);
mailService.send(userEntity.getEmail(), mailUtils.getSubjectAccountPassword(), TemplateService.Template.ACCOUNT_PASSWORD, map);
LOGGER.info("Username and password email sent successfully to user(username: " + userEntity.getUsername() + ").");
}
} catch (Exception ex) {
LOGGER.warn("User registration email failed for username:'" + userEntity.getUsername());
}
return UserConversionUtil.toUserInfo(userEntity);
}
use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.
the class PermissionService method getPermissionById.
/**
* Gets the permission by id.
*
* @param permissionId the permission id
* @return the permission by id
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Permission getPermissionById(final Long permissionId) {
PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
if (ValidatorUtil.isNull(permissionEntity)) {
LOGGER.warn("Permission with permissionId '" + permissionId + "' not found. Error: " + messageUtil.getAttributeInvalid("permission_id", permissionId + ""));
throw new RequestValidationException(messageUtil.getAttributeInvalid("permission_id", permissionId + ""));
}
Set<RoleEntity> roleEntityList = roleRepository.findByPermissions_permissionId(permissionId);
return PermissionConversionUtil.toPermission(permissionEntity, roleEntityList);
}
use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.
the class PermissionService method deletePermissionById.
/**
* Delete permission by id.
*
* @param permissionId the permission id
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void deletePermissionById(final Long permissionId) {
PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
if (ValidatorUtil.isNull(permissionEntity)) {
throw new RequestValidationException(messageUtil.getAttributeInvalid("permission_id", permissionId + ""));
}
Set<RoleEntity> roleEntityList = roleRepository.findByPermissions_permissionId(permissionId);
if (roleEntityList.size() > 0) {
String roles = "";
for (RoleEntity roleEntity : roleEntityList) {
roles += !"".equals(roles) ? "," + roleEntity.getName() : roleEntity.getName();
}
LOGGER.warn("Permission with permissionId '" + permissionId + "' not allowed to delete. Error: " + messageUtil.getAttributeDeletionNotAllowed(permissionEntity.getName(), roles));
throw new RequestValidationException(messageUtil.getAttributeDeletionNotAllowed(permissionEntity.getName(), roles));
}
permissionRepository.delete(permissionEntity);
LOGGER.info("Permission(permissionId: " + permissionId + ") deleted successfully.");
activityLogger.log(ActivityType.DELETE_PERMISSION, permissionEntity.getName());
}
Aggregations