| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.security.ac; |
| 2 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 3 | import de.ids_mannheim.korap.config.KustvaktCacheable; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 4 | import de.ids_mannheim.korap.exceptions.EmptyResultException; |
| 5 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 6 | import de.ids_mannheim.korap.exceptions.NotAuthorizedException; |
| 7 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 8 | import de.ids_mannheim.korap.resources.KustvaktResource; |
| 9 | import de.ids_mannheim.korap.resources.Permissions; |
| 10 | import de.ids_mannheim.korap.resources.ResourceFactory; |
| 11 | import de.ids_mannheim.korap.user.User; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 12 | import net.sf.ehcache.CacheManager; |
| 13 | import net.sf.ehcache.Element; |
| 14 | import org.slf4j.Logger; |
| Michael Hanl | ac113e5 | 2016-01-19 15:49:20 +0100 | [diff] [blame] | 15 | import org.slf4j.LoggerFactory; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 16 | |
| 17 | import java.util.Collection; |
| 18 | |
| 19 | /** |
| 20 | * @author hanl |
| 21 | * @date 23/03/2014 |
| 22 | */ |
| 23 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 24 | //todo: use interface (maybe a cachable interface?) and bean instanceing |
| 25 | // todo: if cachable, data integrity needs to be checked! either remove caching or check integrity! |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 26 | @SuppressWarnings("all") |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 27 | public class ResourceHandler extends KustvaktCacheable { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 28 | |
| Michael Hanl | ac113e5 | 2016-01-19 15:49:20 +0100 | [diff] [blame] | 29 | private static Logger jlog = LoggerFactory.getLogger(ResourceHandler.class); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 30 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 31 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 32 | public ResourceHandler () { |
| 33 | super("resources", "key:resources"); |
| 34 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 35 | |
| 36 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 37 | @Deprecated |
| 38 | public <T extends KustvaktResource> T getCache (Object id, Class<T> cz) { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 39 | Element e = CacheManager.getInstance().getCache("resources").get(id); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 40 | if (e != null) |
| 41 | return (T) e.getObjectValue(); |
| 42 | else |
| 43 | return null; |
| 44 | } |
| 45 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 46 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 47 | @Deprecated |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 48 | public <R extends KustvaktResource> void cache (R resource) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 49 | CacheManager.getInstance().getCache("resources") |
| 50 | .put(new Element(resource.getPersistentID(), resource)); |
| 51 | } |
| 52 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 53 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 54 | /** |
| 55 | * @param id |
| 56 | * @param user |
| 57 | * @return |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 58 | * @throws KustvaktException |
| 59 | * if there is no handler registered, resource might |
| 60 | * still be valid, |
| 61 | * only Notauthorized exception will cause a parsing |
| 62 | * error here |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 63 | * @throws NotAuthorizedException |
| 64 | */ |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 65 | public <T extends KustvaktResource> T findbyIntId (Integer id, User user) |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 66 | throws KustvaktException, NotAuthorizedException { |
| 67 | SecurityManager<T> p; |
| 68 | try { |
| 69 | p = SecurityManager.findbyId(id, user); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 70 | } |
| 71 | catch (EmptyResultException e) { |
| Michael Hanl | 99cb963 | 2016-06-29 16:24:40 +0200 | [diff] [blame] | 72 | throw new NotAuthorizedException(StatusCodes.NO_VALUE_FOUND, |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 73 | String.valueOf(id)); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 74 | } |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 75 | return p.getResource(); |
| 76 | } |
| 77 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 78 | |
| 79 | public <T extends KustvaktResource> T findbyStrId (String persistent_id, |
| 80 | User user, String type) throws KustvaktException, |
| 81 | NotAuthorizedException { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 82 | return (T) findbyStrId(persistent_id, user, |
| 83 | ResourceFactory.getResourceClass(type)); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 86 | |
| 87 | public <T extends KustvaktResource> T findbyStrId (String persistent_id, |
| 88 | User user, Class<T> type) throws KustvaktException, |
| 89 | NotAuthorizedException { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 90 | SecurityManager<T> p; |
| 91 | try { |
| 92 | p = SecurityManager.findbyId(persistent_id, user, type); |
| 93 | } |
| 94 | catch (EmptyResultException e) { |
| Michael Hanl | 99cb963 | 2016-06-29 16:24:40 +0200 | [diff] [blame] | 95 | throw new NotAuthorizedException(StatusCodes.NO_VALUE_FOUND, |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 96 | persistent_id); |
| 97 | } |
| 98 | return p.getResource(); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 101 | |
| 102 | public <T extends KustvaktResource> Collection<T> findbyPath (String path, |
| 103 | Class type, User user) throws KustvaktException, |
| 104 | NotAuthorizedException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 105 | return ResourceFinder.search(path, false, user, type); |
| 106 | } |
| 107 | |
| 108 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 109 | public <T extends KustvaktResource> void updateResources (User user, |
| 110 | T ... resources) throws KustvaktException, NotAuthorizedException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 111 | // fixme: what if update fails? then i have a root policy lingering for a resource that is not available?! |
| 112 | // fixme: transaction management |
| 113 | |
| 114 | for (T resource : resources) { |
| 115 | SecurityManager policies; |
| 116 | try { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 117 | policies = SecurityManager.init(resource.getPersistentID(), |
| 118 | user, Permissions.Permission.WRITE); |
| 119 | } |
| 120 | catch (EmptyResultException e) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | policies.updateResource(resource); |
| 124 | } |
| 125 | } |
| 126 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 127 | |
| 128 | public <T extends KustvaktResource> void storeResources (User user, |
| 129 | T ... resources) throws KustvaktException, NotAuthorizedException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 130 | for (T resource : resources) |
| 131 | SecurityManager.register(resource, user); |
| 132 | } |
| 133 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 134 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 135 | @Deprecated |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 136 | public <T extends KustvaktResource> void deleteResources (User user, |
| 137 | String ... ids) throws KustvaktException, NotAuthorizedException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 138 | for (String id : ids) { |
| 139 | SecurityManager policies; |
| 140 | try { |
| 141 | policies = SecurityManager.init(id, user, |
| Michael Hanl | 88b49db | 2016-02-16 17:15:43 +0100 | [diff] [blame] | 142 | Permissions.Permission.DELETE); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 143 | } |
| 144 | catch (EmptyResultException e) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 145 | return; |
| 146 | } |
| 147 | policies.deleteResource(); |
| 148 | } |
| 149 | } |
| 150 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 151 | |
| 152 | public <T extends KustvaktResource> void deleteResources (User user, |
| 153 | T ... resources) throws KustvaktException, NotAuthorizedException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 154 | for (T r : resources) { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 155 | SecurityManager manager; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 156 | try { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 157 | manager = SecurityManager.findbyId(r.getPersistentID(), user, |
| 158 | r.getClass(), Permissions.Permission.DELETE); |
| 159 | } |
| 160 | catch (EmptyResultException e) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 161 | return; |
| 162 | } |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 163 | manager.deleteResource(); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 167 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 168 | @Deprecated |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 169 | public <T extends KustvaktResource> void deleteResources (User user, |
| 170 | Integer ... ids) throws KustvaktException, NotAuthorizedException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 171 | for (Integer id : ids) { |
| 172 | SecurityManager policies; |
| 173 | try { |
| 174 | policies = SecurityManager.findbyId(id, user, |
| Michael Hanl | 88b49db | 2016-02-16 17:15:43 +0100 | [diff] [blame] | 175 | Permissions.Permission.DELETE); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 176 | } |
| 177 | catch (EmptyResultException e) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | policies.deleteResource(); |
| 181 | } |
| 182 | } |
| 183 | } |