parent
b92c0ddd35
commit
840feae566
@ -0,0 +1,43 @@
|
||||
package info.bukova.isspst.security;
|
||||
|
||||
import info.bukova.isspst.Constants;
|
||||
import info.bukova.isspst.Module;
|
||||
import info.bukova.isspst.data.Role;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
|
||||
public abstract class AbstractModuleEvaluator implements Evaluator {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected boolean hasModulePermission(Authentication authentication, Class<?> serviceClass, String permission) {
|
||||
List<Role> roles = (List<Role>) authentication.getAuthorities();
|
||||
String moduleId = "";
|
||||
String perm = permission;
|
||||
|
||||
for (Module m : Constants.MODULES) {
|
||||
if (m.getServiceClass() != null && m.getServiceClass().isAssignableFrom(serviceClass)) {
|
||||
moduleId = m.getId();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
perm += "_" + moduleId;
|
||||
|
||||
for (int i = 0; i < roles.size(); i++) {
|
||||
if (!(roles.get(i) instanceof Role)) {
|
||||
return false;
|
||||
}
|
||||
if (roles.get(i).getAuthority().equals(perm)) {
|
||||
return true;
|
||||
}
|
||||
if (roles.get(i).getAuthority().equals(Constants.ROLE_ADMIN)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package info.bukova.isspst.security;
|
||||
|
||||
import info.bukova.isspst.Constants;
|
||||
import info.bukova.isspst.data.RequirementBase;
|
||||
import info.bukova.isspst.data.RequirementState;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
|
||||
public abstract class AbstractRequirementEvaluator extends AbstractModuleEvaluator implements Evaluator {
|
||||
|
||||
protected abstract Class<?> getServiceClass();
|
||||
|
||||
@Override
|
||||
public boolean evaluate(Authentication authentication,
|
||||
Object targetDomainObject, String permission) {
|
||||
|
||||
if (!hasModulePermission(authentication, getServiceClass(), permission)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RequirementBase req = (RequirementBase) targetDomainObject;
|
||||
|
||||
if (permission.equals(Constants.PERM_EDIT_NEW)) {
|
||||
return req.getState() == RequirementState.NEW;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package info.bukova.isspst.security;
|
||||
|
||||
import info.bukova.isspst.services.requirement.RequirementService;
|
||||
|
||||
public class RequirementEvaluator extends AbstractRequirementEvaluator
|
||||
implements Evaluator {
|
||||
|
||||
@Override
|
||||
protected Class<?> getServiceClass() {
|
||||
return RequirementService.class;
|
||||
}
|
||||
|
||||
}
|
@ -1,46 +1,14 @@
|
||||
package info.bukova.isspst.security;
|
||||
|
||||
import info.bukova.isspst.Constants;
|
||||
import info.bukova.isspst.Module;
|
||||
import info.bukova.isspst.data.Role;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
|
||||
public class ServiceEvaluator implements Evaluator {
|
||||
public class ServiceEvaluator extends AbstractModuleEvaluator implements Evaluator {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean evaluate(Authentication authentication,
|
||||
Object targetDomainObject, String permission) {
|
||||
|
||||
List<Role> roles = (List<Role>) authentication.getAuthorities();
|
||||
String moduleId = "";
|
||||
String perm = permission;
|
||||
|
||||
for (Module m : Constants.MODULES) {
|
||||
if (m.getServiceClass() != null && m.getServiceClass().isAssignableFrom(targetDomainObject.getClass())) {
|
||||
moduleId = m.getId();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
perm += "_" + moduleId;
|
||||
|
||||
for (int i = 0; i < roles.size(); i++) {
|
||||
if (!(roles.get(i) instanceof Role)) {
|
||||
return false;
|
||||
}
|
||||
if (roles.get(i).getAuthority().equals(perm)) {
|
||||
return true;
|
||||
}
|
||||
if (roles.get(i).getAuthority().equals(Constants.ROLE_ADMIN)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return hasModulePermission(authentication, targetDomainObject.getClass(), permission);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,13 @@
|
||||
package info.bukova.isspst.security;
|
||||
|
||||
import info.bukova.isspst.services.requirement.TripRequirementService;
|
||||
|
||||
public class TripRequirementEvaluator extends AbstractRequirementEvaluator
|
||||
implements Evaluator {
|
||||
|
||||
@Override
|
||||
protected Class<?> getServiceClass() {
|
||||
return TripRequirementService.class;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue