1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.dccon.repository.ldap.transcoder;
18
19 import java.util.Optional;
20 import lombok.extern.slf4j.Slf4j;
21 import org.bremersee.dccon.config.DomainControllerProperties;
22 import org.bremersee.dccon.model.Sid;
23 import org.ldaptive.ad.SecurityIdentifier;
24 import org.ldaptive.io.AbstractBinaryValueTranscoder;
25
26
27
28
29
30
31 @Slf4j
32 public class SidValueTranscoder extends AbstractBinaryValueTranscoder<Sid> {
33
34 private DomainControllerProperties properties;
35
36
37
38
39
40
41 public SidValueTranscoder(DomainControllerProperties properties) {
42 this.properties = properties;
43 }
44
45 @Override
46 public Sid decodeBinaryValue(byte[] value) {
47 return Optional.ofNullable(value)
48 .map(SecurityIdentifier::toString)
49 .map(objectSid -> Sid.builder()
50 .value(objectSid)
51 .systemEntity(isSystemEntity(objectSid))
52 .build())
53 .orElse(null);
54 }
55
56 @Override
57 public byte[] encodeBinaryValue(Sid value) {
58 return Optional.ofNullable(value)
59 .map(Sid::getValue)
60 .map(SecurityIdentifier::toBytes)
61 .orElse(null);
62 }
63
64 @Override
65 public Class<Sid> getType() {
66 return Sid.class;
67 }
68
69 private boolean isSystemEntity(final String objectSid) {
70 if (!objectSid.startsWith(properties.getDefaultSidPrefix())) {
71 return true;
72 }
73 final int index = objectSid.lastIndexOf('-');
74 if (index > -1) {
75 try {
76 return properties
77 .getMaxSystemSidSuffix() >= Integer.parseInt(objectSid.substring(index + 1));
78 } catch (Exception ignored) {
79
80 }
81 }
82 return false;
83 }
84 }