xref: /plugin/pureldap/auth.php (revision a1128cc0f2573cbe769f582d613c9ccb9fc94dee)
179f39653SAndreas Gohr<?php
21078ec26SAndreas Gohr
31078ec26SAndreas Gohruse dokuwiki\plugin\pureldap\classes\ADClient;
41078ec26SAndreas Gohruse dokuwiki\plugin\pureldap\classes\Client;
51078ec26SAndreas Gohr
679f39653SAndreas Gohr/**
779f39653SAndreas Gohr * DokuWiki Plugin pureldap (Auth Component)
879f39653SAndreas Gohr *
979f39653SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
1079f39653SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
1179f39653SAndreas Gohr */
1279f39653SAndreas Gohrclass auth_plugin_pureldap extends DokuWiki_Auth_Plugin
1379f39653SAndreas Gohr{
141078ec26SAndreas Gohr    /** @var Client */
151078ec26SAndreas Gohr    protected $client;
1679f39653SAndreas Gohr
1779f39653SAndreas Gohr    /**
1879f39653SAndreas Gohr     * Constructor.
1979f39653SAndreas Gohr     */
2079f39653SAndreas Gohr    public function __construct()
2179f39653SAndreas Gohr    {
221078ec26SAndreas Gohr        global $conf;
2379f39653SAndreas Gohr        parent::__construct(); // for compatibility
2479f39653SAndreas Gohr
2585916a2dSAndreas Gohr        $this->cando['getUsers'] = true;
2685916a2dSAndreas Gohr        $this->cando['getGroups'] = true;
2779f39653SAndreas Gohr
281078ec26SAndreas Gohr        // prepare the base client
291078ec26SAndreas Gohr        $this->loadConfig();
301078ec26SAndreas Gohr        $this->conf['admin_password'] = conf_decodeString($this->conf['admin_password']);
311078ec26SAndreas Gohr        $this->conf['defaultgroup'] = $conf['defaultgroup'];
321078ec26SAndreas Gohr
331078ec26SAndreas Gohr        $this->client = new ADClient($this->conf); // FIXME decide class on config
3479f39653SAndreas Gohr        $this->success = true;
3579f39653SAndreas Gohr    }
3679f39653SAndreas Gohr
3779f39653SAndreas Gohr
3879f39653SAndreas Gohr    /**
3979f39653SAndreas Gohr     * Log off the current user [ OPTIONAL ]
4079f39653SAndreas Gohr     */
4179f39653SAndreas Gohr    // public function logOff()
4279f39653SAndreas Gohr    // {
4379f39653SAndreas Gohr    // }
4479f39653SAndreas Gohr
4579f39653SAndreas Gohr    /**
4679f39653SAndreas Gohr     * Do all authentication [ OPTIONAL ]
4779f39653SAndreas Gohr     *
4879f39653SAndreas Gohr     * @param string $user Username
4979f39653SAndreas Gohr     * @param string $pass Cleartext Password
5079f39653SAndreas Gohr     * @param bool $sticky Cookie should not expire
5179f39653SAndreas Gohr     *
5279f39653SAndreas Gohr     * @return  bool             true on successful auth
5379f39653SAndreas Gohr     */
5479f39653SAndreas Gohr    //public function trustExternal($user, $pass, $sticky = false)
5579f39653SAndreas Gohr    //{
5679f39653SAndreas Gohr    /* some example:
5779f39653SAndreas Gohr
5879f39653SAndreas Gohr    global $USERINFO;
5979f39653SAndreas Gohr    global $conf;
6079f39653SAndreas Gohr    $sticky ? $sticky = true : $sticky = false; //sanity check
6179f39653SAndreas Gohr
6279f39653SAndreas Gohr    // do the checking here
6379f39653SAndreas Gohr
6479f39653SAndreas Gohr    // set the globals if authed
6579f39653SAndreas Gohr    $USERINFO['name'] = 'FIXME';
6679f39653SAndreas Gohr    $USERINFO['mail'] = 'FIXME';
6779f39653SAndreas Gohr    $USERINFO['grps'] = array('FIXME');
6879f39653SAndreas Gohr    $_SERVER['REMOTE_USER'] = $user;
6979f39653SAndreas Gohr    $_SESSION[DOKU_COOKIE]['auth']['user'] = $user;
7079f39653SAndreas Gohr    $_SESSION[DOKU_COOKIE]['auth']['pass'] = $pass;
7179f39653SAndreas Gohr    $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
7279f39653SAndreas Gohr    return true;
7379f39653SAndreas Gohr
7479f39653SAndreas Gohr    */
7579f39653SAndreas Gohr    //}
7679f39653SAndreas Gohr
771078ec26SAndreas Gohr    /** @inheritDoc */
7879f39653SAndreas Gohr    public function checkPass($user, $pass)
7979f39653SAndreas Gohr    {
801078ec26SAndreas Gohr        // use a separate client from the default one, because this is not a superuser bind
811078ec26SAndreas Gohr        $client = new ADClient($this->conf); // FIXME decide class on config
821078ec26SAndreas Gohr        return $client->authenticate($user, $pass);
8379f39653SAndreas Gohr    }
8479f39653SAndreas Gohr
851078ec26SAndreas Gohr    /** @inheritDoc */
8679f39653SAndreas Gohr    public function getUserData($user, $requireGroups = true)
8779f39653SAndreas Gohr    {
885a3b9122SAndreas Gohr        $info = $this->client->getCachedUser($user, $requireGroups);
891078ec26SAndreas Gohr        return $info ?: false;
9079f39653SAndreas Gohr    }
9179f39653SAndreas Gohr
9279f39653SAndreas Gohr    /**
9379f39653SAndreas Gohr     * Create a new User [implement only where required/possible]
9479f39653SAndreas Gohr     *
9579f39653SAndreas Gohr     * Returns false if the user already exists, null when an error
9679f39653SAndreas Gohr     * occurred and true if everything went well.
9779f39653SAndreas Gohr     *
9879f39653SAndreas Gohr     * The new user HAS TO be added to the default group by this
9979f39653SAndreas Gohr     * function!
10079f39653SAndreas Gohr     *
10179f39653SAndreas Gohr     * Set addUser capability when implemented
10279f39653SAndreas Gohr     *
10379f39653SAndreas Gohr     * @param string $user
10479f39653SAndreas Gohr     * @param string $pass
10579f39653SAndreas Gohr     * @param string $name
10679f39653SAndreas Gohr     * @param string $mail
10779f39653SAndreas Gohr     * @param null|array $grps
10879f39653SAndreas Gohr     *
10979f39653SAndreas Gohr     * @return bool|null
11079f39653SAndreas Gohr     */
11179f39653SAndreas Gohr    //public function createUser($user, $pass, $name, $mail, $grps = null)
11279f39653SAndreas Gohr    //{
11379f39653SAndreas Gohr    // FIXME implement
11479f39653SAndreas Gohr    //    return null;
11579f39653SAndreas Gohr    //}
11679f39653SAndreas Gohr
11779f39653SAndreas Gohr    /**
11879f39653SAndreas Gohr     * Modify user data [implement only where required/possible]
11979f39653SAndreas Gohr     *
12079f39653SAndreas Gohr     * Set the mod* capabilities according to the implemented features
12179f39653SAndreas Gohr     *
12279f39653SAndreas Gohr     * @param string $user nick of the user to be changed
12379f39653SAndreas Gohr     * @param array $changes array of field/value pairs to be changed (password will be clear text)
12479f39653SAndreas Gohr     *
12579f39653SAndreas Gohr     * @return  bool
12679f39653SAndreas Gohr     */
12779f39653SAndreas Gohr    //public function modifyUser($user, $changes)
12879f39653SAndreas Gohr    //{
12979f39653SAndreas Gohr    // FIXME implement
13079f39653SAndreas Gohr    //    return false;
13179f39653SAndreas Gohr    //}
13279f39653SAndreas Gohr
13379f39653SAndreas Gohr    /**
13479f39653SAndreas Gohr     * Delete one or more users [implement only where required/possible]
13579f39653SAndreas Gohr     *
13679f39653SAndreas Gohr     * Set delUser capability when implemented
13779f39653SAndreas Gohr     *
13879f39653SAndreas Gohr     * @param array $users
13979f39653SAndreas Gohr     *
14079f39653SAndreas Gohr     * @return  int    number of users deleted
14179f39653SAndreas Gohr     */
14279f39653SAndreas Gohr    //public function deleteUsers($users)
14379f39653SAndreas Gohr    //{
14479f39653SAndreas Gohr    // FIXME implement
14579f39653SAndreas Gohr    //    return false;
14679f39653SAndreas Gohr    //}
14779f39653SAndreas Gohr
14885916a2dSAndreas Gohr    /** @inheritDoc */
149b21740b4SAndreas Gohr    public function retrieveUsers($start = 0, $limit = 0, $filter = null)
150b21740b4SAndreas Gohr    {
15185916a2dSAndreas Gohr        return array_slice(
15285916a2dSAndreas Gohr            $this->client->getFilteredUsers(
15385916a2dSAndreas Gohr                $filter,
15485916a2dSAndreas Gohr                $this->filterType2FilterMethod('contains')
15585916a2dSAndreas Gohr            ),
15685916a2dSAndreas Gohr            $start,
15785916a2dSAndreas Gohr            $limit);
158b21740b4SAndreas Gohr    }
15979f39653SAndreas Gohr
16079f39653SAndreas Gohr    /**
16179f39653SAndreas Gohr     * Define a group [implement only where required/possible]
16279f39653SAndreas Gohr     *
16379f39653SAndreas Gohr     * Set addGroup capability when implemented
16479f39653SAndreas Gohr     *
16579f39653SAndreas Gohr     * @param string $group
16679f39653SAndreas Gohr     *
16779f39653SAndreas Gohr     * @return  bool
16879f39653SAndreas Gohr     */
16979f39653SAndreas Gohr    //public function addGroup($group)
17079f39653SAndreas Gohr    //{
17179f39653SAndreas Gohr    // FIXME implement
17279f39653SAndreas Gohr    //    return false;
17379f39653SAndreas Gohr    //}
17479f39653SAndreas Gohr
175b21740b4SAndreas Gohr    /** @inheritDoc */
176b21740b4SAndreas Gohr    public function retrieveGroups($start = 0, $limit = 0)
177b21740b4SAndreas Gohr    {
178b21740b4SAndreas Gohr        return array_slice($this->client->getCachedGroups(), $start, $limit);
179b21740b4SAndreas Gohr    }
18079f39653SAndreas Gohr
1816d90d5c8SAndreas Gohr    /** @inheritDoc */
18279f39653SAndreas Gohr    public function isCaseSensitive()
18379f39653SAndreas Gohr    {
1846d90d5c8SAndreas Gohr        return false;
18579f39653SAndreas Gohr    }
18679f39653SAndreas Gohr
18779f39653SAndreas Gohr    /**
18879f39653SAndreas Gohr     * Sanitize a given username
18979f39653SAndreas Gohr     *
19079f39653SAndreas Gohr     * This function is applied to any user name that is given to
19179f39653SAndreas Gohr     * the backend and should also be applied to any user name within
19279f39653SAndreas Gohr     * the backend before returning it somewhere.
19379f39653SAndreas Gohr     *
19479f39653SAndreas Gohr     * This should be used to enforce username restrictions.
19579f39653SAndreas Gohr     *
19679f39653SAndreas Gohr     * @param string $user username
19779f39653SAndreas Gohr     * @return string the cleaned username
19879f39653SAndreas Gohr     */
19979f39653SAndreas Gohr    public function cleanUser($user)
20079f39653SAndreas Gohr    {
201*a1128cc0SAndreas Gohr        return $this->client->cleanUser($user);
20279f39653SAndreas Gohr    }
20379f39653SAndreas Gohr
20479f39653SAndreas Gohr    /**
20579f39653SAndreas Gohr     * Sanitize a given groupname
20679f39653SAndreas Gohr     *
20779f39653SAndreas Gohr     * This function is applied to any groupname that is given to
20879f39653SAndreas Gohr     * the backend and should also be applied to any groupname within
20979f39653SAndreas Gohr     * the backend before returning it somewhere.
21079f39653SAndreas Gohr     *
21179f39653SAndreas Gohr     * This should be used to enforce groupname restrictions.
21279f39653SAndreas Gohr     *
21379f39653SAndreas Gohr     * Groupnames are to be passed without a leading '@' here.
21479f39653SAndreas Gohr     *
21579f39653SAndreas Gohr     * @param string $group groupname
21679f39653SAndreas Gohr     *
21779f39653SAndreas Gohr     * @return string the cleaned groupname
21879f39653SAndreas Gohr     */
21979f39653SAndreas Gohr    public function cleanGroup($group)
22079f39653SAndreas Gohr    {
22179f39653SAndreas Gohr        return $group;
22279f39653SAndreas Gohr    }
22379f39653SAndreas Gohr
2246d90d5c8SAndreas Gohr    /** @inheritDoc */
2251078ec26SAndreas Gohr    public function useSessionCache($user)
2261078ec26SAndreas Gohr    {
2276d90d5c8SAndreas Gohr        return true;
2281078ec26SAndreas Gohr    }
229b21740b4SAndreas Gohr
230b21740b4SAndreas Gohr    /**
231b21740b4SAndreas Gohr     * Convert DokuWiki filter type to method in the library
232b21740b4SAndreas Gohr     *
233b21740b4SAndreas Gohr     * @todo implement with proper constants once #3028 has been implemented
234b21740b4SAndreas Gohr     * @param string $type
235b21740b4SAndreas Gohr     * @return string
236b21740b4SAndreas Gohr     */
23785916a2dSAndreas Gohr    protected function filterType2FilterMethod($type)
23885916a2dSAndreas Gohr    {
239b21740b4SAndreas Gohr        $filtermethods = [
240b21740b4SAndreas Gohr            'contains' => 'contains',
241b21740b4SAndreas Gohr            'startswith' => 'startsWith',
242b21740b4SAndreas Gohr            'endswith' => 'endsWith',
24385916a2dSAndreas Gohr            'equals' => 'equals',
244b21740b4SAndreas Gohr        ];
245b21740b4SAndreas Gohr
246b21740b4SAndreas Gohr        if (isset($filtermethods[$type])) {
247b21740b4SAndreas Gohr            return $filtermethods[$type];
248b21740b4SAndreas Gohr        }
249b21740b4SAndreas Gohr
250b21740b4SAndreas Gohr        return 'equals';
251b21740b4SAndreas Gohr    }
25279f39653SAndreas Gohr}
25379f39653SAndreas Gohr
254