xref: /plugin/struct/types/User.php (revision ba0b95f1bc5b14b42ee79ce152ff4799ccbe1829)
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        // reformat result for jQuery UI Autocomplete
43        $users = array();
44        foreach($logins as $login => $info) {
45            $users[] = array(
46                'label' => $info['name'] . ' [' . $login . ']',
47                'value' => $login
48            );
49        }
50
51        return $users;
52    }
53
54}
55