RestrictedAccessDelegate.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package at.acdp.opcur.opc;
  2. /*
  3. * Copyright (c) 2016 Kevin Herron
  4. *
  5. * All rights reserved. This program and the accompanying materials
  6. * are made available under the terms of the Eclipse Public License v1.0
  7. * and Eclipse Distribution License v1.0 which accompany this distribution.
  8. *
  9. * The Eclipse Public License is available at
  10. * http://www.eclipse.org/legal/epl-v10.html
  11. * and the Eclipse Distribution License is available at
  12. * http://www.eclipse.org/org/documents/edl-v10.html.
  13. */
  14. import java.util.Optional;
  15. import java.util.Set;
  16. import java.util.function.Function;
  17. import org.eclipse.milo.opcua.sdk.core.AccessLevel;
  18. import org.eclipse.milo.opcua.sdk.server.Session;
  19. import org.eclipse.milo.opcua.sdk.server.api.nodes.VariableNode;
  20. import org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext;
  21. import org.eclipse.milo.opcua.sdk.server.nodes.delegates.AttributeDelegate;
  22. import org.eclipse.milo.opcua.sdk.server.nodes.delegates.DelegatingAttributeDelegate;
  23. import org.eclipse.milo.opcua.stack.core.UaException;
  24. import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.ubyte;
  28. public class RestrictedAccessDelegate extends DelegatingAttributeDelegate {
  29. private static final Set<AccessLevel> INTERNAL_ACCESS = AccessLevel.READ_WRITE;
  30. private final Logger logger = LoggerFactory.getLogger(getClass());
  31. private final Function<Object, Set<AccessLevel>> accessLevelsFn;
  32. public RestrictedAccessDelegate(Function<Object, Set<AccessLevel>> accessLevelsFn) {
  33. this(null, accessLevelsFn);
  34. }
  35. public RestrictedAccessDelegate(AttributeDelegate parent, Function<Object, Set<AccessLevel>> accessLevelsFn) {
  36. super(parent);
  37. this.accessLevelsFn = accessLevelsFn;
  38. }
  39. @Override
  40. public UByte getUserAccessLevel(AttributeContext context, VariableNode node) throws UaException {
  41. Optional<Object> identity = context.getSession().map(Session::getIdentityObject);
  42. Set<AccessLevel> accessLevels = identity.map(accessLevelsFn).orElse(INTERNAL_ACCESS);
  43. return ubyte(AccessLevel.getMask(accessLevels));
  44. }
  45. }