xref: /plugin/pureldap/vendor/freedsx/ldap/src/FreeDSx/Ldap/Operations.php (revision dad993c57a70866aa1db59c43f043769c2eb7ed0)
10b3fd2d3SAndreas Gohr<?php
2*dad993c5SAndreas Gohr
30b3fd2d3SAndreas Gohr/**
40b3fd2d3SAndreas Gohr * This file is part of the FreeDSx LDAP package.
50b3fd2d3SAndreas Gohr *
60b3fd2d3SAndreas Gohr * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
70b3fd2d3SAndreas Gohr *
80b3fd2d3SAndreas Gohr * For the full copyright and license information, please view the LICENSE
90b3fd2d3SAndreas Gohr * file that was distributed with this source code.
100b3fd2d3SAndreas Gohr */
110b3fd2d3SAndreas Gohr
120b3fd2d3SAndreas Gohrnamespace FreeDSx\Ldap;
130b3fd2d3SAndreas Gohr
140b3fd2d3SAndreas Gohruse FreeDSx\Asn1\Type\AbstractType;
150b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Entry\Attribute;
160b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Entry\Change;
170b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Entry\Dn;
180b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Entry\Entry;
190b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Entry\Rdn;
20*dad993c5SAndreas Gohruse FreeDSx\Ldap\Exception\UnexpectedValueException;
210b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\Request\AbandonRequest;
220b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\Request\AddRequest;
230b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\Request\AnonBindRequest;
240b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\Request\CancelRequest;
250b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\Request\CompareRequest;
260b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\Request\DeleteRequest;
270b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\Request\ExtendedRequest;
280b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\Request\ModifyDnRequest;
290b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\Request\ModifyRequest;
300b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\Request\PasswordModifyRequest;
310b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\Request\SaslBindRequest;
320b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\Request\SearchRequest;
330b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\Request\SimpleBindRequest;
340b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\Request\UnbindRequest;
350b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Protocol\ProtocolElementInterface;
360b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Search\Filter\FilterInterface;
370b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Search\Filters;
380b3fd2d3SAndreas Gohr
390b3fd2d3SAndreas Gohr/**
400b3fd2d3SAndreas Gohr * Provides a set of factory methods to help quickly construct different operations/requests.
410b3fd2d3SAndreas Gohr *
420b3fd2d3SAndreas Gohr * @author Chad Sikorra <Chad.Sikorra@gmail.com>
430b3fd2d3SAndreas Gohr */
440b3fd2d3SAndreas Gohrclass Operations
450b3fd2d3SAndreas Gohr{
460b3fd2d3SAndreas Gohr    /**
470b3fd2d3SAndreas Gohr     * A request to abandon an ongoing operation.
480b3fd2d3SAndreas Gohr     *
490b3fd2d3SAndreas Gohr     * @param int $id
500b3fd2d3SAndreas Gohr     * @return AbandonRequest
510b3fd2d3SAndreas Gohr     */
520b3fd2d3SAndreas Gohr    public static function abandon(int $id)
530b3fd2d3SAndreas Gohr    {
540b3fd2d3SAndreas Gohr        return new AbandonRequest($id);
550b3fd2d3SAndreas Gohr    }
560b3fd2d3SAndreas Gohr
570b3fd2d3SAndreas Gohr    /**
580b3fd2d3SAndreas Gohr     * Add an entry to LDAP.
590b3fd2d3SAndreas Gohr     *
600b3fd2d3SAndreas Gohr     * @param Entry $entry
610b3fd2d3SAndreas Gohr     * @return AddRequest
620b3fd2d3SAndreas Gohr     */
630b3fd2d3SAndreas Gohr    public static function add(Entry $entry)
640b3fd2d3SAndreas Gohr    {
650b3fd2d3SAndreas Gohr        return new AddRequest($entry);
660b3fd2d3SAndreas Gohr    }
670b3fd2d3SAndreas Gohr
680b3fd2d3SAndreas Gohr    /**
690b3fd2d3SAndreas Gohr     * A simple bind request with a username and password.
700b3fd2d3SAndreas Gohr     *
710b3fd2d3SAndreas Gohr     * @param string $username
720b3fd2d3SAndreas Gohr     * @param string $password
730b3fd2d3SAndreas Gohr     * @return SimpleBindRequest
740b3fd2d3SAndreas Gohr     */
750b3fd2d3SAndreas Gohr    public static function bind(string $username, string $password)
760b3fd2d3SAndreas Gohr    {
770b3fd2d3SAndreas Gohr        return new SimpleBindRequest($username, $password);
780b3fd2d3SAndreas Gohr    }
790b3fd2d3SAndreas Gohr
800b3fd2d3SAndreas Gohr    /**
810b3fd2d3SAndreas Gohr     * A SASL bind request with a specific mechanism and their associated options.
820b3fd2d3SAndreas Gohr     *
830b3fd2d3SAndreas Gohr     * @param array $options
840b3fd2d3SAndreas Gohr     * @param string $mechanism
850b3fd2d3SAndreas Gohr     * @param string|null $credentials
860b3fd2d3SAndreas Gohr     * @return SaslBindRequest
870b3fd2d3SAndreas Gohr     */
880b3fd2d3SAndreas Gohr    public static function bindSasl(array $options = [], string $mechanism = '', ?string $credentials = null)
890b3fd2d3SAndreas Gohr    {
900b3fd2d3SAndreas Gohr        return new SaslBindRequest($mechanism, $credentials, $options);
910b3fd2d3SAndreas Gohr    }
920b3fd2d3SAndreas Gohr
930b3fd2d3SAndreas Gohr    /**
940b3fd2d3SAndreas Gohr     * An anonymous bind request.
950b3fd2d3SAndreas Gohr     *
960b3fd2d3SAndreas Gohr     * @param string $username
970b3fd2d3SAndreas Gohr     * @return AnonBindRequest
980b3fd2d3SAndreas Gohr     */
990b3fd2d3SAndreas Gohr    public static function bindAnonymously(string $username = '')
1000b3fd2d3SAndreas Gohr    {
1010b3fd2d3SAndreas Gohr        return new AnonBindRequest($username);
1020b3fd2d3SAndreas Gohr    }
1030b3fd2d3SAndreas Gohr
1040b3fd2d3SAndreas Gohr    /**
1050b3fd2d3SAndreas Gohr     * Cancel a specific operation. Pass either the message ID or the LdapMessage object.
1060b3fd2d3SAndreas Gohr     */
1070b3fd2d3SAndreas Gohr    public static function cancel(int $messageId): CancelRequest
1080b3fd2d3SAndreas Gohr    {
1090b3fd2d3SAndreas Gohr        return new CancelRequest($messageId);
1100b3fd2d3SAndreas Gohr    }
1110b3fd2d3SAndreas Gohr
1120b3fd2d3SAndreas Gohr    /**
1130b3fd2d3SAndreas Gohr     * A comparison operation to check if an entry has an attribute with a certain value.
1140b3fd2d3SAndreas Gohr     *
1150b3fd2d3SAndreas Gohr     * @return CompareRequest
1160b3fd2d3SAndreas Gohr     */
1170b3fd2d3SAndreas Gohr    public static function compare(string $dn, string $attributeName, string $value): CompareRequest
1180b3fd2d3SAndreas Gohr    {
1190b3fd2d3SAndreas Gohr        return new CompareRequest($dn, Filters::equal($attributeName, $value));
1200b3fd2d3SAndreas Gohr    }
1210b3fd2d3SAndreas Gohr
1220b3fd2d3SAndreas Gohr    /**
1230b3fd2d3SAndreas Gohr     * Delete an entry from LDAP by its DN.
1240b3fd2d3SAndreas Gohr     */
1250b3fd2d3SAndreas Gohr    public static function delete(string $dn): DeleteRequest
1260b3fd2d3SAndreas Gohr    {
1270b3fd2d3SAndreas Gohr        return new DeleteRequest($dn);
1280b3fd2d3SAndreas Gohr    }
1290b3fd2d3SAndreas Gohr
1300b3fd2d3SAndreas Gohr    /**
1310b3fd2d3SAndreas Gohr     * Perform an extended operation.
1320b3fd2d3SAndreas Gohr     *
1330b3fd2d3SAndreas Gohr     * @param null|AbstractType|ProtocolElementInterface|string $value
1340b3fd2d3SAndreas Gohr     */
1350b3fd2d3SAndreas Gohr    public static function extended(string $name, $value = null): ExtendedRequest
1360b3fd2d3SAndreas Gohr    {
1370b3fd2d3SAndreas Gohr        return new ExtendedRequest($name, $value);
1380b3fd2d3SAndreas Gohr    }
1390b3fd2d3SAndreas Gohr
1400b3fd2d3SAndreas Gohr    /**
1410b3fd2d3SAndreas Gohr     * Perform modification(s) on an LDAP entry.
1420b3fd2d3SAndreas Gohr     */
1430b3fd2d3SAndreas Gohr    public static function modify(string $dn, Change ...$changes): ModifyRequest
1440b3fd2d3SAndreas Gohr    {
1450b3fd2d3SAndreas Gohr        return new ModifyRequest($dn, ...$changes);
1460b3fd2d3SAndreas Gohr    }
1470b3fd2d3SAndreas Gohr
1480b3fd2d3SAndreas Gohr    /**
1490b3fd2d3SAndreas Gohr     * Move an LDAP entry to a new parent DN location.
150*dad993c5SAndreas Gohr     *
151*dad993c5SAndreas Gohr     * @throws UnexpectedValueException
1520b3fd2d3SAndreas Gohr     */
1530b3fd2d3SAndreas Gohr    public static function move(string $dn, string $newParentDn): ModifyDnRequest
1540b3fd2d3SAndreas Gohr    {
1550b3fd2d3SAndreas Gohr        $dnObj = new Dn($dn);
1560b3fd2d3SAndreas Gohr
1570b3fd2d3SAndreas Gohr        return new ModifyDnRequest($dn, $dnObj->getRdn()->toString(), true, $newParentDn);
1580b3fd2d3SAndreas Gohr    }
1590b3fd2d3SAndreas Gohr
1600b3fd2d3SAndreas Gohr    /**
1610b3fd2d3SAndreas Gohr     * Creates a password modify extended operation.
1620b3fd2d3SAndreas Gohr     */
1630b3fd2d3SAndreas Gohr    public static function passwordModify(string $username, string $oldPassword, string $newPassword): PasswordModifyRequest
1640b3fd2d3SAndreas Gohr    {
1650b3fd2d3SAndreas Gohr        return new PasswordModifyRequest($username, $oldPassword, $newPassword);
1660b3fd2d3SAndreas Gohr    }
1670b3fd2d3SAndreas Gohr
1680b3fd2d3SAndreas Gohr    /**
1690b3fd2d3SAndreas Gohr     * Quit is an alias for unbind. This is more indicative of what an unbind actually does.
1700b3fd2d3SAndreas Gohr     */
1710b3fd2d3SAndreas Gohr    public static function quit(): UnbindRequest
1720b3fd2d3SAndreas Gohr    {
1730b3fd2d3SAndreas Gohr        return self::unbind();
1740b3fd2d3SAndreas Gohr    }
1750b3fd2d3SAndreas Gohr
1760b3fd2d3SAndreas Gohr    /**
1770b3fd2d3SAndreas Gohr     * Rename an LDAP entry by modifying its RDN.
1780b3fd2d3SAndreas Gohr     *
1790b3fd2d3SAndreas Gohr     * @param string|Rdn $rdn
1800b3fd2d3SAndreas Gohr     */
1810b3fd2d3SAndreas Gohr    public static function rename(string $dn, $rdn, bool $deleteOldRdn = true): ModifyDnRequest
1820b3fd2d3SAndreas Gohr    {
1830b3fd2d3SAndreas Gohr        return new ModifyDnRequest($dn, $rdn, $deleteOldRdn);
1840b3fd2d3SAndreas Gohr    }
1850b3fd2d3SAndreas Gohr
1860b3fd2d3SAndreas Gohr    /**
1870b3fd2d3SAndreas Gohr     * Search LDAP with a given filter, scope, etc to retrieve a set of entries.
1880b3fd2d3SAndreas Gohr     *
189*dad993c5SAndreas Gohr     * @param string|Attribute ...$attributes
1900b3fd2d3SAndreas Gohr     */
1910b3fd2d3SAndreas Gohr    public static function search(FilterInterface $filter, ...$attributes): SearchRequest
1920b3fd2d3SAndreas Gohr    {
1930b3fd2d3SAndreas Gohr        return new SearchRequest($filter, ...$attributes);
1940b3fd2d3SAndreas Gohr    }
1950b3fd2d3SAndreas Gohr
1960b3fd2d3SAndreas Gohr    /**
1970b3fd2d3SAndreas Gohr     * Search for a specific base DN object to read. This sets a 'present' filter for the 'objectClass' attribute to help
1980b3fd2d3SAndreas Gohr     * simplify it.
1990b3fd2d3SAndreas Gohr     *
200*dad993c5SAndreas Gohr     * @param string|Attribute ...$attributes
2010b3fd2d3SAndreas Gohr     */
2020b3fd2d3SAndreas Gohr    public static function read(string $baseDn, ...$attributes): SearchRequest
2030b3fd2d3SAndreas Gohr    {
2040b3fd2d3SAndreas Gohr        return (new SearchRequest(Filters::present('objectClass'), ...$attributes))->base($baseDn)->useBaseScope();
2050b3fd2d3SAndreas Gohr    }
2060b3fd2d3SAndreas Gohr
2070b3fd2d3SAndreas Gohr    /**
2080b3fd2d3SAndreas Gohr     * Search a single level list from a base DN object.
2090b3fd2d3SAndreas Gohr     *
210*dad993c5SAndreas Gohr     * @param string|Attribute ...$attributes
2110b3fd2d3SAndreas Gohr     */
2120b3fd2d3SAndreas Gohr    public static function list(FilterInterface $filter, string $baseDn, ...$attributes): SearchRequest
2130b3fd2d3SAndreas Gohr    {
2140b3fd2d3SAndreas Gohr        return (new SearchRequest($filter, ...$attributes))->base($baseDn)->useSingleLevelScope();
2150b3fd2d3SAndreas Gohr    }
2160b3fd2d3SAndreas Gohr
2170b3fd2d3SAndreas Gohr    /**
2180b3fd2d3SAndreas Gohr     * A request to unbind. This actually causes the server to terminate the client connection.
2190b3fd2d3SAndreas Gohr     *
2200b3fd2d3SAndreas Gohr     * @return UnbindRequest
2210b3fd2d3SAndreas Gohr     */
2220b3fd2d3SAndreas Gohr    public static function unbind(): UnbindRequest
2230b3fd2d3SAndreas Gohr    {
2240b3fd2d3SAndreas Gohr        return new UnbindRequest();
2250b3fd2d3SAndreas Gohr    }
2260b3fd2d3SAndreas Gohr
2270b3fd2d3SAndreas Gohr    /**
2280b3fd2d3SAndreas Gohr     * A request to determine who is currently authorized against LDAP for the current session.
2290b3fd2d3SAndreas Gohr     *
2300b3fd2d3SAndreas Gohr     * @return ExtendedRequest
2310b3fd2d3SAndreas Gohr     */
2320b3fd2d3SAndreas Gohr    public static function whoami(): ExtendedRequest
2330b3fd2d3SAndreas Gohr    {
2340b3fd2d3SAndreas Gohr        return new ExtendedRequest(ExtendedRequest::OID_WHOAMI);
2350b3fd2d3SAndreas Gohr    }
2360b3fd2d3SAndreas Gohr}
237