use of org.usermanagement.dao.entity.PermissionEntity in project open-kilda by telstra.
the class RoleService method createRole.
/**
* Creates the role.
*
* @param role the role
* @return the role
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Role createRole(final Role role) {
roleValidator.validateRole(role);
Set<PermissionEntity> permissionEntities = new HashSet<>();
List<PermissionEntity> permissionEntityList = permissionRepository.findAll();
for (Long permissionId : role.getPermissionId()) {
PermissionEntity permissionEntity = permissionEntityList.parallelStream().filter((entity) -> entity.getPermissionId().equals(permissionId)).findFirst().orElse(null);
if (!ValidatorUtil.isNull(permissionEntity)) {
permissionEntities.add(permissionEntity);
} else {
LOGGER.warn("Permission with id '" + permissionId + "' not found.");
throw new RequestValidationException(messageUtil.getAttributeNotFound("permission"));
}
}
RoleEntity roleEntity = RoleConversionUtil.toRoleEntity(role, permissionEntities);
roleRepository.save(roleEntity);
activityLogger.log(ActivityType.CREATE_ROLE, role.getName());
LOGGER.info("Role with name '" + roleEntity.getName() + "' created successfully.");
return RoleConversionUtil.toRole(roleEntity, true, false);
}
use of org.usermanagement.dao.entity.PermissionEntity in project open-kilda by telstra.
the class RoleService method getRolesByPermissionId.
/**
* Gets the roles by permission id.
*
* @param permissionId the permission id
* @return the roles by permission id
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Permission getRolesByPermissionId(final Long permissionId) {
PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
Set<RoleEntity> roleEntityList = roleRepository.findByPermissions_permissionId(permissionId);
return RoleConversionUtil.toPermissionByRole(roleEntityList, permissionEntity);
}
use of org.usermanagement.dao.entity.PermissionEntity in project open-kilda by telstra.
the class UserService method populateUserInfo.
/**
* Adds user information in session.
*
* @param userInfo the userInfo
* @param username who's information is added in session.
*/
public void populateUserInfo(final UserInfo userInfo, final String username) {
UserEntity user = getUserByUsername(username);
Set<RoleEntity> roleEntities = user.getRoles();
Set<String> roles = new HashSet<String>();
Set<String> permissions = new HashSet<String>();
for (RoleEntity roleEntity : roleEntities) {
roles.add(roleEntity.getName());
userInfo.setRole("ROLE_ADMIN");
if (user.getUserId() != 1) {
Set<PermissionEntity> permissionEntities = roleEntity.getPermissions();
for (PermissionEntity permissionEntity : permissionEntities) {
if (permissionEntity.getStatusEntity().getStatusCode().equalsIgnoreCase(Status.ACTIVE.getCode()) && !permissionEntity.getIsAdminPermission()) {
permissions.add(permissionEntity.getName());
}
}
}
}
if (user.getUserId() == 1) {
List<PermissionEntity> permissionEntities = permissionRepository.findAll();
for (PermissionEntity permissionEntity : permissionEntities) {
permissions.add(permissionEntity.getName());
}
}
userInfo.setUserId(user.getUserId());
userInfo.setUsername(user.getUsername());
userInfo.setName(user.getName());
userInfo.setRoles(roles);
userInfo.setPermissions(permissions);
userInfo.setIs2FaEnabled(user.getIs2FaEnabled());
}
use of org.usermanagement.dao.entity.PermissionEntity in project open-kilda by telstra.
the class PermissionService method updatePermission.
/**
* Update permission.
*
* @param permissionId the permission id
* @param permission the permission
* @return the permission
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Permission updatePermission(final Long permissionId, final Permission permission) {
permissionValidator.validateUpdatePermission(permission, 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 + ""));
}
permissionEntity = PermissionConversionUtil.toUpatePermissionEntity(permission, permissionEntity);
permissionRepository.save(permissionEntity);
activityLogger.log(ActivityType.UPDATE_PERMISSION, permissionEntity.getName());
LOGGER.info("Permission(permissionId: " + permissionId + ") updated successfully.");
return PermissionConversionUtil.toPermission(permissionEntity, null);
}
use of org.usermanagement.dao.entity.PermissionEntity in project open-kilda by telstra.
the class RoleConversionUtil method toRole.
/**
* To role.
*
* @param roleEntity the role entity
* @param withPermissions the with permissions
* @param withUsers the with users
* @return the role
*/
public static Role toRole(final RoleEntity roleEntity, final boolean withPermissions, final boolean withUsers) {
Role role = new Role();
role.setName(roleEntity.getName());
role.setRoleId(roleEntity.getRoleId());
role.setStatus(roleEntity.getStatusEntity().getStatus());
role.setDescription(roleEntity.getDescription());
if (withPermissions) {
List<Permission> permissionList = new ArrayList<Permission>();
if (!ValidatorUtil.isNull(roleEntity.getPermissions())) {
for (PermissionEntity permissionEntity : roleEntity.getPermissions()) {
permissionList.add(PermissionConversionUtil.toPermission(permissionEntity, null));
}
role.setPermissions(permissionList);
}
}
if (withUsers) {
List<UserInfo> userInfoList = new ArrayList<>();
for (UserEntity userEntity : roleEntity.getUsers()) {
if (userEntity.getUserId() != 1) {
UserInfo userInfo = new UserInfo();
userInfo.setUserId(userEntity.getUserId());
userInfo.setName(userEntity.getName());
userInfoList.add(userInfo);
}
}
role.setUserInfo(userInfoList);
}
return role;
}
Aggregations