e9adcf3073f97ee49c54c39085fb7c5258b2b713
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / security / PermissionPropertyTester.java
1 package eu.etaxonomy.taxeditor.security;
2
3 import java.util.ArrayList;
4 import java.util.EnumSet;
5 import java.util.Iterator;
6 import java.util.List;
7
8 import javax.management.relation.Role;
9
10 import org.apache.commons.lang.StringUtils;
11 import org.eclipse.core.expressions.PropertyTester;
12 import org.eclipse.jface.viewers.IStructuredSelection;
13
14 import eu.etaxonomy.cdm.model.common.CdmBase;
15 import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
16 import eu.etaxonomy.taxeditor.store.CdmStore;
17
18 public class PermissionPropertyTester extends PropertyTester {
19
20 private static final String HAS_ROLES = "hasRoles";
21 private static final String HAS_PERMISSIONS = "hasPermissions";
22
23 private static final String CREATE = CRUD.CREATE.toString();
24 private static final String READ = CRUD.READ.toString();
25 private static final String UPDATE = CRUD.UPDATE.toString();
26 private static final String DELETE = CRUD.DELETE.toString();
27
28 @Override
29 public boolean test(Object receiver, String property, Object[] args,
30 Object expectedValue) {
31
32
33 if(property.equals(HAS_PERMISSIONS)){
34
35 return checkHasPermission(receiver, args);
36 }
37
38 if(property.equals(HAS_ROLES)){
39 return checkHasRoles(property, args);
40 }
41
42 return false;
43 }
44
45 private boolean checkHasRoles(String property, Object[] args) {
46 List<Role> roles = new ArrayList<Role>();
47 for(int i = 0; i < args.length; i++){
48 roles.add((Role)args[i]);
49 }
50 // return CdmStore.currentAuthentiationHasOneOfRoles(roles.get(0)); // FIXME
51 return false;
52
53 }
54
55 private boolean checkHasPermission(Object receiver, Object[] args) {
56 EnumSet<CRUD> crudSet = crudSetFromArgs(args);
57
58
59 List<Object> selectedElements = ((IStructuredSelection) receiver).toList();
60 for(Object selectedElement : selectedElements){
61 if(selectedElement instanceof CdmBase && ! CdmStore.currentAuthentiationHasPermission((CdmBase) selectedElement, crudSet)){
62 return false;
63 }
64 }
65
66 return true;
67 }
68
69 private EnumSet<CRUD> crudSetFromArgs(Object[] args) {
70 EnumSet<CRUD> crudSet = EnumSet.noneOf(CRUD.class);
71 for(int i = 0; i < args.length; i++){
72 try {
73 crudSet.add(CRUD.valueOf(args[i].toString()));
74 } catch (Exception e){
75 System.err.println("could not add " + args[i]);
76 }
77 }
78 return crudSet;
79 }
80
81 }