File indexing completed on 2024-12-15 04:50:17

0001 /*
0002     This file is part of libkldap.
0003     SPDX-FileCopyrightText: 2004-2006 Szombathelyi György <gyurco@freemail.hu>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include <QStringList>
0011 
0012 #include <QUrl>
0013 
0014 #include "kldap_core_export.h"
0015 #include "ldapdn.h"
0016 
0017 // clazy:excludeall=copyable-polymorphic
0018 
0019 namespace KLDAPCore
0020 {
0021 /**
0022  * @short A special url class for LDAP.
0023  *
0024  * LdapUrl implements an RFC 2255 compliant LDAP Url parser, with minimal
0025  * differences. LDAP Urls implemented by this class has the following format:
0026  * ldap[s]://[user[:password]@]hostname[:port]["/" [dn ["?" [attributes]
0027  * ["?" [scope] ["?" [filter] ["?" extensions]]]]]]
0028  */
0029 class KLDAP_CORE_EXPORT LdapUrl : public QUrl
0030 {
0031 public:
0032     /**
0033      * A class holding the extension name and state whether
0034      * the extension is critical.
0035      */
0036     using Extension = struct {
0037         QString value;
0038         bool critical;
0039     };
0040 
0041     /**
0042      * Describes the scope of the LDAP url.
0043      */
0044     using Scope = enum {
0045         Base, ///< Only the same level as the url.
0046         One, ///< The level of the url and the one below.
0047         Sub ///< All levels below the url's level.
0048     };
0049 
0050     /**
0051      * Constructs an empty LDAP url.
0052      */
0053     LdapUrl();
0054 
0055     /**
0056      * Constructs a LDAP url from a KUrl @p url.
0057      */
0058     explicit LdapUrl(const QUrl &url);
0059 
0060     /**
0061      * Constructs a LDAP url from an other url.
0062      */
0063     LdapUrl(const LdapUrl &other);
0064 
0065     /**
0066      * Overwrites the values of the LDAP url with values
0067      * from an @p other url.
0068      */
0069     LdapUrl &operator=(const LdapUrl &other);
0070 
0071     /**
0072      * Destroys the LDAP url.
0073      */
0074     ~LdapUrl();
0075 
0076     /**
0077      * Sets the @p dn part of the LDAP url.
0078      */
0079     void setDn(const LdapDN &dn);
0080 
0081     /**
0082      * Returns the dn part of the LDAP url.
0083      * This is equal to path() with the slash removed from the beginning.
0084      */
0085     [[nodiscard]] LdapDN dn() const;
0086 
0087     /**
0088      * Sets the @p attributes part of the LDAP url.
0089      */
0090     void setAttributes(const QStringList &attributes);
0091 
0092     /**
0093      * Returns the attributes part of the LDAP url.
0094      */
0095     [[nodiscard]] QStringList attributes() const;
0096 
0097     /**
0098      * Sets the scope part of the LDAP url.
0099      */
0100     void setScope(Scope scope);
0101 
0102     /**
0103      * Returns the scope part of the LDAP url.
0104      */
0105     [[nodiscard]] Scope scope() const;
0106 
0107     /**
0108      * Sets the filter part of the LDAP url.
0109      */
0110     void setFilter(const QString &filter);
0111 
0112     /**
0113      * Returns the filter part of the LDAP url.
0114      */
0115     [[nodiscard]] QString filter() const;
0116 
0117     /**
0118      * Returns whether the specified @p extension exists in the LDAP url.
0119      */
0120     [[nodiscard]] bool hasExtension(const QString &extension) const;
0121 
0122     /**
0123      * Returns the specified @p extension.
0124      */
0125     [[nodiscard]] Extension extension(const QString &extension) const;
0126 
0127     /**
0128      * Returns the specified @p extension.
0129      */
0130     [[nodiscard]] QString extension(const QString &extension, bool &critical) const;
0131 
0132     /**
0133      * Sets the specified extension @p key with the value and criticality in @p extension.
0134      */
0135     void setExtension(const QString &key, const Extension &extension);
0136 
0137     /**
0138      * Sets the specified extension @p key with the @p value and criticality specified.
0139      */
0140     void setExtension(const QString &key, const QString &value, bool critical = false);
0141 
0142     /**
0143      * Sets the specified extension @p key with the @p value and criticality specified.
0144      */
0145     void setExtension(const QString &key, int value, bool critical = false);
0146 
0147     /**
0148      * Removes the specified @p extension.
0149      */
0150     void removeExtension(const QString &extension);
0151 
0152     /**
0153      * Updates the query component from the attributes, scope, filter and extensions.
0154      */
0155     void updateQuery();
0156 
0157     /**
0158      * Parses the query argument of the URL and makes it available via the
0159      * attributes(), extension(), filter() and scope() methods
0160      */
0161     void parseQuery();
0162 
0163 private:
0164     class LdapUrlPrivate;
0165     std::unique_ptr<LdapUrlPrivate> const d;
0166 };
0167 }