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