| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.security.ac; |
| 2 | |
| Michael Hanl | 00ef546 | 2016-06-06 17:39:59 +0200 | [diff] [blame] | 3 | import de.ids_mannheim.korap.config.BeansFactory; |
| 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; |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 7 | import de.ids_mannheim.korap.interfaces.db.PolicyHandlerIface; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 8 | import de.ids_mannheim.korap.resources.KustvaktResource; |
| 9 | import de.ids_mannheim.korap.resources.Permissions; |
| 10 | import de.ids_mannheim.korap.security.PolicyCondition; |
| 11 | import de.ids_mannheim.korap.user.User; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 12 | import org.slf4j.Logger; |
| 13 | import org.slf4j.LoggerFactory; |
| 14 | |
| 15 | import java.util.*; |
| 16 | |
| 17 | /** |
| 18 | * @author hanl |
| 19 | * @date 04/03/2014 |
| 20 | */ |
| 21 | public class ConditionManagement { |
| 22 | |
| Michael Hanl | ac113e5 | 2016-01-19 15:49:20 +0100 | [diff] [blame] | 23 | private static final Logger jlog = LoggerFactory |
| 24 | .getLogger(ConditionManagement.class); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 25 | private User user; |
| 26 | private PolicyHandlerIface policydao; |
| 27 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 28 | |
| 29 | public ConditionManagement (User user) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 30 | this.user = user; |
| Michael Hanl | 00ef546 | 2016-06-06 17:39:59 +0200 | [diff] [blame] | 31 | this.policydao = BeansFactory.getKustvaktContext() |
| 32 | .getPolicyDbProvider(); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 33 | |
| 34 | } |
| 35 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 36 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 37 | /** |
| 38 | * adds a user to an existing group |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 39 | * |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 40 | * @param usernames |
| 41 | * @param condition |
| 42 | * @param admin |
| 43 | */ |
| 44 | // todo: conflict resolution |
| 45 | // fixme: not applicable to korap system roles |
| 46 | // only works if there is a policy with that condition and permission set, if not, create one! |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 47 | public void addUser (List<String> usernames, PolicyCondition condition, |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 48 | boolean admin) throws NotAuthorizedException, KustvaktException { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 49 | if (policydao.matchCondition(this.user, condition.getSpecifier(), true) == 1) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 50 | policydao.addToCondition(usernames, condition, admin); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 51 | } |
| 52 | else |
| Michael Hanl | c444602 | 2016-02-12 18:03:17 +0100 | [diff] [blame] | 53 | jlog.error("Users '{}' could not be added to condition '{}'", |
| 54 | usernames, condition.getSpecifier()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 57 | |
| 58 | public void addUser (String username, PolicyCondition condition, |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 59 | boolean admin) throws NotAuthorizedException, KustvaktException { |
| 60 | addUser(Arrays.asList(username), condition, admin); |
| 61 | } |
| 62 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 63 | |
| 64 | public void removeUser (List<String> users, PolicyCondition condition) |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 65 | throws KustvaktException { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 66 | if (policydao.matchCondition(this.user, condition.getSpecifier(), true) == 1) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 67 | policydao.removeFromCondition(users, condition); |
| 68 | } |
| 69 | } |
| 70 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 71 | |
| 72 | public Set<String> getMembers (PolicyCondition condition) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 73 | try { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 74 | if (policydao.matchCondition(this.user, condition.getSpecifier(), |
| 75 | true) == 1) { |
| 76 | return new HashSet<>(policydao.getUsersFromCondition(condition)); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 77 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 78 | } |
| 79 | catch (KustvaktException e) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 80 | return Collections.emptySet(); |
| 81 | } |
| 82 | return Collections.emptySet(); |
| 83 | } |
| 84 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 85 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 86 | @Deprecated |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 87 | public void addUser (KustvaktResource resource, String user, |
| 88 | Permissions.Permission ... pps) throws NotAuthorizedException, |
| 89 | KustvaktException, EmptyResultException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 90 | addUser(resource, Arrays.asList(user), pps); |
| 91 | } |
| 92 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 93 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 94 | @Deprecated |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 95 | public void addUser (KustvaktResource resource, List<String> users, |
| 96 | Permissions.Permission ... pps) throws NotAuthorizedException, |
| 97 | KustvaktException, EmptyResultException { |
| 98 | SecurityManager policies = SecurityManager.findbyId(resource.getId(), |
| 99 | this.user); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 100 | PolicyCondition c = policies.getExtensional(pps); |
| 101 | if (c != null) |
| 102 | this.addUser(users, c, false); |
| 103 | else { |
| 104 | PolicyCondition ex = new PolicyCondition(); |
| 105 | new PolicyBuilder(this.user).setResources(resource) |
| 106 | .addCondition(ex.getSpecifier()).setPermissions(pps) |
| 107 | .create(); |
| 108 | this.addUser(users, ex, false); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | } |