Completed
Push — master ( 9eadf5...6ba699 )
by Andreas
16:50
created

midgard_admin_user_validator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 27
ccs 12
cts 12
cp 1
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A is_username_available() 0 17 5
1
<?php
2
/**
3
 * @package midgard.admin.user
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * Form validation functionality
11
 *
12
 * @package midgard.admin.user
13
 */
14
class midgard_admin_user_validator
15
{
16
    /**
17
     * Test if username is available.
18
     *
19
     * If the formdata contains a person GUID, it is ignored during the search
20
     *
21
     * @var array $fields The form's data
22
     * @return mixed True on success, array of error messages otherwise
23
     */
24 4
    public function is_username_available(array $fields)
25
    {
26 4
        if (!empty($fields["username"])) {
27 4
            $mc = new midgard_collector('midgard_user', 'login', $fields["username"]);
28 4
            $mc->set_key_property('person');
29 4
            $mc->add_constraint('authtype', '=', midcom::get()->config->get('auth_type'));
30 4
            $mc->execute();
31 4
            $keys = $mc->list_keys();
32 4
            if (   count($keys) > 0
33 3
                && (   !isset($fields['person'])
34 4
                    || key($keys) != $fields['person'])) {
35
                return [
36 2
                    "username" => sprintf(midcom::get()->i18n->get_string("username %s is already in use", "midgard.admin.user"), $fields['username'])
37
                ];
38
            }
39
        }
40 3
        return true;
41
    }
42
}
43