1<?php
2/**
3 * This file is part of the FreeDSx LDAP package.
4 *
5 * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace FreeDSx\Ldap\Operation\Request;
12
13use FreeDSx\Asn1\Asn1;
14use FreeDSx\Asn1\Type\AbstractType;
15use FreeDSx\Asn1\Type\SequenceType;
16use FreeDSx\Ldap\Exception\ProtocolException;
17
18/**
19 * RFC 3062. A password modify extended request.
20 *
21 * PasswdModifyRequestValue ::= SEQUENCE {
22 *     userIdentity    [0]  OCTET STRING OPTIONAL
23 *     oldPasswd       [1]  OCTET STRING OPTIONAL
24 *     newPasswd       [2]  OCTET STRING OPTIONAL }
25 *
26 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
27 */
28class PasswordModifyRequest extends ExtendedRequest
29{
30    /**
31     * @var null|string
32     */
33    protected $userIdentity;
34
35    /**
36     * @var null|string
37     */
38    protected $oldPassword;
39
40    /**
41     * @var null|string
42     */
43    protected $newPassword;
44
45    /**
46     * @param null|string $userIdentity
47     * @param null|string $oldPassword
48     * @param null|string $newPassword
49     */
50    public function __construct(?string $userIdentity = null, ?string $oldPassword = null, ?string $newPassword = null)
51    {
52        $this->userIdentity = $userIdentity;
53        $this->oldPassword = $oldPassword;
54        $this->newPassword = $newPassword;
55        parent::__construct(self::OID_PWD_MODIFY);
56    }
57
58    /**
59     * @return null|string
60     */
61    public function getUsername(): ?string
62    {
63        return $this->userIdentity;
64    }
65
66    /**
67     * @param null|string $username
68     * @return $this
69     */
70    public function setUsername(?string $username)
71    {
72        $this->userIdentity = $username;
73
74        return $this;
75    }
76
77    /**
78     * @return null|string
79     */
80    public function getNewPassword(): ?string
81    {
82        return $this->newPassword;
83    }
84
85    /**
86     * @param null|string $newPassword
87     * @return $this
88     */
89    public function setNewPassword(?string $newPassword)
90    {
91        $this->newPassword = $newPassword;
92
93        return $this;
94    }
95
96    /**
97     * @return null|string
98     */
99    public function getOldPassword(): ?string
100    {
101        return $this->oldPassword;
102    }
103
104    /**
105     * @param null|string $oldPassword
106     * @return $this
107     */
108    public function setOldPassword(?string $oldPassword)
109    {
110        $this->oldPassword = $oldPassword;
111
112        return $this;
113    }
114
115    /**
116     * {@inheritdoc}
117     */
118    public function toAsn1(): AbstractType
119    {
120        $this->requestValue = Asn1::sequence();
121
122        if ($this->userIdentity !== null) {
123            $this->requestValue->addChild(Asn1::context(0, Asn1::octetString($this->userIdentity)));
124        }
125        if ($this->oldPassword !== null) {
126            $this->requestValue->addChild(Asn1::context(1, Asn1::octetString($this->oldPassword)));
127        }
128        if ($this->newPassword !== null) {
129            $this->requestValue->addChild(Asn1::context(2, Asn1::octetString($this->newPassword)));
130        }
131
132        return parent::toAsn1();
133    }
134
135    /**
136     * {@inheritdoc}
137     */
138    public static function fromAsn1(AbstractType $type)
139    {
140        $request = self::decodeEncodedValue($type);
141        if ($request === null) {
142            return new self();
143        }
144        if (!($request instanceof SequenceType)) {
145            throw new ProtocolException('The password modify request is malformed.');
146        }
147
148        $userIdentity = null;
149        $oldPasswd = null;
150        $newPasswd = null;
151        foreach ($request as $value) {
152            /** @var AbstractType $value */
153            if ($value->getTagClass() !== AbstractType::TAG_CLASS_CONTEXT_SPECIFIC) {
154                throw new ProtocolException('The password modify request is malformed');
155            }
156            if ($value->getTagNumber() === 0) {
157                $userIdentity = $value;
158            } elseif ($value->getTagNumber() === 1) {
159                $oldPasswd = $value;
160            } elseif ($value->getTagNumber() === 2) {
161                $newPasswd = $value;
162            }
163        }
164
165        return new self(
166            $userIdentity !== null ? $userIdentity->getValue() : null,
167            $oldPasswd !== null ? $oldPasswd->getValue() : null,
168            $newPasswd !== null ? $newPasswd->getValue() : null
169        );
170    }
171}
172