MIFOSX-126 - Objective 1 Complete - new permission regime implemented plus a more intuitive UI
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package org.mifosng.platform.exceptions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A {@link RuntimeException} thrown when permission resources are not found.
|
||||||
|
*/
|
||||||
|
public class PermissionNotFoundException extends
|
||||||
|
AbstractPlatformResourceNotFoundException {
|
||||||
|
|
||||||
|
public PermissionNotFoundException(String code) {
|
||||||
|
super("error.msg.permission.code.invalid", "Permission with Code "
|
||||||
|
+ code + " does not exist", code);
|
||||||
|
}
|
||||||
|
}
|
||||||
+108
-74
@@ -7,6 +7,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
import org.mifosng.platform.accounting.api.commands.RolePermissionCommand;
|
import org.mifosng.platform.accounting.api.commands.RolePermissionCommand;
|
||||||
import org.mifosng.platform.api.commands.RoleCommand;
|
import org.mifosng.platform.api.commands.RoleCommand;
|
||||||
|
import org.mifosng.platform.exceptions.PermissionNotFoundException;
|
||||||
import org.mifosng.platform.exceptions.RoleNotFoundException;
|
import org.mifosng.platform.exceptions.RoleNotFoundException;
|
||||||
import org.mifosng.platform.security.PlatformSecurityContext;
|
import org.mifosng.platform.security.PlatformSecurityContext;
|
||||||
import org.mifosng.platform.user.domain.Permission;
|
import org.mifosng.platform.user.domain.Permission;
|
||||||
@@ -19,103 +20,136 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class RoleWritePlatformServiceJpaRepositoryImpl implements RoleWritePlatformService {
|
public class RoleWritePlatformServiceJpaRepositoryImpl implements
|
||||||
|
RoleWritePlatformService {
|
||||||
|
|
||||||
private final PlatformSecurityContext context;
|
private final PlatformSecurityContext context;
|
||||||
private final RoleRepository roleRepository;
|
private final RoleRepository roleRepository;
|
||||||
|
|
||||||
private final PermissionRepository permissionRepository;
|
private final PermissionRepository permissionRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public RoleWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context, final RoleRepository roleRepository,
|
public RoleWritePlatformServiceJpaRepositoryImpl(
|
||||||
final PermissionRepository permissionRepository) {
|
final PlatformSecurityContext context,
|
||||||
this.context = context;
|
final RoleRepository roleRepository,
|
||||||
this.roleRepository = roleRepository;
|
final PermissionRepository permissionRepository) {
|
||||||
this.permissionRepository = permissionRepository;
|
this.context = context;
|
||||||
}
|
this.roleRepository = roleRepository;
|
||||||
|
this.permissionRepository = permissionRepository;
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@Override
|
@Override
|
||||||
public Long createRole(final RoleCommand command) {
|
public Long createRole(final RoleCommand command) {
|
||||||
|
|
||||||
context.authenticatedUser();
|
context.authenticatedUser();
|
||||||
|
|
||||||
RoleCommandValidator validator = new RoleCommandValidator(command);
|
RoleCommandValidator validator = new RoleCommandValidator(command);
|
||||||
validator.validateForCreate();
|
validator.validateForCreate();
|
||||||
|
|
||||||
List<Permission> selectedPermissions = assembleListOfSelectedPermissions(command.getPermissions());
|
List<Permission> selectedPermissions = assembleListOfSelectedPermissions(command
|
||||||
|
.getPermissions());
|
||||||
|
|
||||||
Role entity = new Role(command.getName(), command.getDescription(), selectedPermissions);
|
Role entity = new Role(command.getName(), command.getDescription(),
|
||||||
|
selectedPermissions);
|
||||||
|
|
||||||
this.roleRepository.save(entity);
|
this.roleRepository.save(entity);
|
||||||
|
|
||||||
return entity.getId();
|
return entity.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@Override
|
@Override
|
||||||
public Long updateRole(RoleCommand command) {
|
public Long updateRole(RoleCommand command) {
|
||||||
|
|
||||||
context.authenticatedUser();
|
context.authenticatedUser();
|
||||||
|
|
||||||
RoleCommandValidator validator = new RoleCommandValidator(command);
|
RoleCommandValidator validator = new RoleCommandValidator(command);
|
||||||
validator.validateForUpdate();
|
validator.validateForUpdate();
|
||||||
|
|
||||||
List<Permission> selectedPermissions = assembleListOfSelectedPermissions(command.getPermissions());
|
List<Permission> selectedPermissions = assembleListOfSelectedPermissions(command
|
||||||
|
.getPermissions());
|
||||||
|
|
||||||
Role role = this.roleRepository.findOne(command.getId());
|
Role role = this.roleRepository.findOne(command.getId());
|
||||||
if (role == null) { throw new RoleNotFoundException(command.getId()); }
|
if (role == null) {
|
||||||
role.update(command, selectedPermissions);
|
throw new RoleNotFoundException(command.getId());
|
||||||
|
}
|
||||||
|
role.update(command, selectedPermissions);
|
||||||
|
|
||||||
this.roleRepository.save(role);
|
this.roleRepository.save(role);
|
||||||
|
|
||||||
return role.getId();
|
return role.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@Override
|
@Override
|
||||||
public Long updateRolePermissions(final RolePermissionCommand command) {
|
public Long updateRolePermissions(final RolePermissionCommand command) {
|
||||||
context.authenticatedUser();
|
context.authenticatedUser();
|
||||||
|
|
||||||
// RoleCommandValidator validator = new RoleCommandValidator(command);
|
final Role role = this.roleRepository.findOne(command.getRoleId());
|
||||||
// validator.validateForUpdate();
|
if (role == null) {
|
||||||
|
throw new RoleNotFoundException(command.getRoleId());
|
||||||
|
}
|
||||||
|
|
||||||
final Role role = this.roleRepository.findOne(command.getRoleId());
|
Collection<Permission> allPermissions = this.permissionRepository
|
||||||
if (role == null) {
|
.findAll();
|
||||||
throw new RoleNotFoundException(command.getRoleId());
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, Boolean> permissions = command.getPermissions();
|
|
||||||
for (String permissionCode : permissions.keySet()) {
|
|
||||||
Boolean selected = permissions.get(permissionCode);
|
|
||||||
if (selected.booleanValue()) {
|
|
||||||
// this.permissionRepository.findOne(id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// role.update(command, selectedPermissions);
|
|
||||||
|
|
||||||
this.roleRepository.save(role);
|
Map<String, Boolean> commandPermissions = command.getPermissions();
|
||||||
|
for (String permissionCode : commandPermissions.keySet()) {
|
||||||
|
Boolean selected = commandPermissions.get(permissionCode);
|
||||||
|
|
||||||
return role.getId();
|
Permission permission = getPermissionByCode(allPermissions,
|
||||||
}
|
permissionCode);
|
||||||
|
|
||||||
private List<Permission> assembleListOfSelectedPermissions(final String[] selectedPermissionsArray) {
|
if (role.getPermissions().contains(permission)) {
|
||||||
List<Long> selectedPermissionIds = new ArrayList<Long>();
|
if (!(selected)) {
|
||||||
List<Permission> selectedPermissions = new ArrayList<Permission>();
|
role.getPermissions().remove(permission);
|
||||||
|
}
|
||||||
|
|
||||||
if (!ObjectUtils.isEmpty(selectedPermissionsArray)) {
|
} else {
|
||||||
for (String selectedId : selectedPermissionsArray) {
|
if (selected) {
|
||||||
selectedPermissionIds.add(Long.valueOf(selectedId));
|
role.getPermissions().add(permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
Collection<Permission> allPermissions = this.permissionRepository.findAll();
|
}
|
||||||
for (Permission permission : allPermissions) {
|
}
|
||||||
if (selectedPermissionIds.contains(permission.getId())) {
|
|
||||||
selectedPermissions.add(permission);
|
this.roleRepository.save(role);
|
||||||
}
|
|
||||||
}
|
return role.getId();
|
||||||
}
|
}
|
||||||
return selectedPermissions;
|
|
||||||
}
|
private Permission getPermissionByCode(
|
||||||
|
Collection<Permission> allPermissions, String permissionCode) {
|
||||||
|
|
||||||
|
if (allPermissions != null) {
|
||||||
|
for (Permission permission : allPermissions) {
|
||||||
|
if (permission.code().equals(permissionCode)) return permission;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new PermissionNotFoundException(permissionCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Permission> assembleListOfSelectedPermissions(
|
||||||
|
final String[] selectedPermissionsArray) {
|
||||||
|
List<Long> selectedPermissionIds = new ArrayList<Long>();
|
||||||
|
List<Permission> selectedPermissions = new ArrayList<Permission>();
|
||||||
|
|
||||||
|
if (!ObjectUtils.isEmpty(selectedPermissionsArray)) {
|
||||||
|
for (String selectedId : selectedPermissionsArray) {
|
||||||
|
selectedPermissionIds.add(Long.valueOf(selectedId));
|
||||||
|
}
|
||||||
|
|
||||||
|
Collection<Permission> allPermissions = this.permissionRepository
|
||||||
|
.findAll();
|
||||||
|
for (Permission permission : allPermissions) {
|
||||||
|
if (selectedPermissionIds.contains(permission.getId())) {
|
||||||
|
selectedPermissions.add(permission);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return selectedPermissions;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user