xref: /plugin/struct/types/User.php (revision 914921fb92301c70150ca8f040ad4d23523f569c)
1<?php
2namespace plugin\struct\types;
3
4use plugin\struct\meta\StructException;
5
6class User extends AbstractBaseType {
7
8    protected $config = array(
9        'fullname' => true,
10        'autocomplete' => array(
11            'mininput' => 2,
12            'maxresult' => 5,
13        ),
14    );
15
16    /**
17     * Autocompletion for user names
18     *
19     * @todo should we have any security mechanism? Currently everybody can look up users
20     * @return array
21     */
22    public function handleAjax() {
23        /** @var \DokuWiki_Auth_Plugin $auth */
24        global $auth;
25        global $INPUT;
26
27        if(!$auth->canDo('getUsers')) {
28            throw new StructException('The user backend can not search for users');
29        }
30
31        // check minimum length
32        $lookup = trim($INPUT->str('search'));
33        if(utf8_strlen($lookup) < $this->config['autocomplete']['mininput']) return array();
34
35        // find users by login, fill up with names if wanted
36        $max = $this->config['autocomplete']['maxresult'];
37        $logins = (array) $auth->retrieveUsers(0, $max, array('user'=>$lookup));
38        if((count($logins) < $max) && $this->config['fullname']) {
39            $logins = array_merge($logins, (array) $auth->retrieveUsers(0, $max, array('name'=>$lookup)));
40        }
41
42        // clean up result
43        $users = array();
44        foreach($logins as $login => $info) {
45            $users[$login] = $login.' - '.$info['name'];
46        }
47
48        return $users;
49    }
50
51}
52