1<?php
2
3/**
4 * This file is part of the FreeDSx LDAP package.
5 *
6 * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace FreeDSx\Ldap\Control\Ad;
13
14use FreeDSx\Asn1\Asn1;
15use FreeDSx\Asn1\Exception\EncoderException;
16use FreeDSx\Asn1\Exception\PartialPduException;
17use FreeDSx\Asn1\Type\AbstractType;
18use FreeDSx\Asn1\Type\OctetStringType;
19use FreeDSx\Ldap\Control\Control;
20use FreeDSx\Ldap\Exception\ProtocolException;
21
22/**
23 * Implements the AD SetOwner control.
24 *
25 *   SID octetString
26 *
27 * https://msdn.microsoft.com/en-us/library/dn392490.aspx
28 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
29 */
30class SetOwnerControl extends Control
31{
32    /**
33     * @var string
34     */
35    protected $sid;
36
37    /**
38     * @param string $sid
39     */
40    public function __construct(string $sid)
41    {
42        $this->sid = $sid;
43        parent::__construct(self::OID_SET_OWNER, true);
44    }
45
46    /**
47     * @param string $sid
48     * @return $this
49     */
50    public function setSid(string $sid)
51    {
52        $this->sid = $sid;
53
54        return $this;
55    }
56
57    /**
58     * @return string
59     */
60    public function getSid(): string
61    {
62        return $this->sid;
63    }
64
65    /**
66     * {@inheritdoc}
67     */
68    public function toAsn1(): AbstractType
69    {
70        $this->controlValue = Asn1::octetString($this->sid);
71
72        return parent::toAsn1();
73    }
74
75    /**
76     * {@inheritDoc}
77     * @return Control
78     * @throws EncoderException
79     * @throws PartialPduException
80     */
81    public static function fromAsn1(AbstractType $type)
82    {
83        $request = self::decodeEncodedValue($type);
84        if (!$request instanceof OctetStringType) {
85            throw new ProtocolException('A SetOwner control value must be an octet string type.');
86        }
87        /** @var OctetStringType $request */
88        $control = new self(
89            $request->getValue()
90        );
91
92        return self::mergeControlData($control, $type);
93    }
94}
95