Passed
Push — master ( 76451e...ce8007 )
by Andreas
11:32
created

org_openpsa_user_validator   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 58
c 2
b 0
f 0
dl 0
loc 158
ccs 60
cts 65
cp 0.9231
rs 10
wmc 30

9 Methods

Rating   Name   Duplication   Size   Complexity  
A email_exists() 0 13 4
A email_and_username_exist() 0 15 4
A get_accounthelper() 0 3 1
A username_exists() 0 6 2
A __construct() 0 3 1
A verify_existing_password() 0 16 5
A validate_create_form() 0 13 4
A validate_edit_form() 0 15 5
A password_check() 0 12 4
1
<?php
2
/**
3
 * @package org.openpsa.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 org.openpsa.user
13
 */
14
class org_openpsa_user_validator extends midgard_admin_user_validator
15
{
16
    private midcom_services_i18n_l10n $l10n;
17
18 8
    public function __construct()
19
    {
20 8
        $this->l10n = midcom::get()->i18n->get_l10n('org.openpsa.user');
21
    }
22
23 2
    protected function get_accounthelper(midcom_db_person $person = null) : org_openpsa_user_accounthelper
24
    {
25 2
        return new org_openpsa_user_accounthelper($person);
26
    }
27
28
    /**
29
     * Validation rules for edit forms
30
     *
31
     * @param array $fields The form's data
32
     * @return mixed True on success, array of error messages otherwise
33
     */
34 1
    public function validate_edit_form(array $fields)
35
    {
36 1
        $result = $this->is_username_available($fields);
37
38 1
        if (isset($fields['new_password'])) {
39 1
            $result_password = $this->password_check($fields);
40 1
            if (is_array($result_password)) {
41 1
                $result = (is_array($result)) ? array_merge($result, $result_password) : $result_password;
42
            }
43
        }
44
45 1
        if (is_array($result)) {
46 1
            return $result;
47
        }
48 1
        return $this->verify_existing_password($fields);
49
    }
50
51
    /**
52
     * Validation rules for create forms
53
     *
54
     * @param array $fields The form's data
55
     * @return mixed True on success, array of error messages otherwise
56
     */
57 1
    public function validate_create_form(array $fields)
58
    {
59 1
        $result = $this->is_username_available($fields);
60
61 1
        if (   $fields['password']['switch']
62 1
            && !$this->get_accounthelper()->check_password_strength((string) $fields['password']['password'])) {
63
            $result = ['password' => $this->_l10n->get('password weak')];
0 ignored issues
show
Bug introduced by
The property _l10n does not exist on org_openpsa_user_validator. Did you mean l10n?
Loading history...
64
        }
65
66 1
        if (is_array($result)) {
67
            return $result;
68
        }
69 1
        return $this->verify_existing_password($fields);
70
    }
71
72
    /**
73
     * Validate the existing password
74
     *
75
     * @param array $fields The form's data
76
     * @return mixed True on success, array of error messages otherwise
77
     */
78 2
    public function verify_existing_password(array $fields)
79
    {
80 2
        if (midcom::get()->auth->can_user_do('org.openpsa.user:manage', null, org_openpsa_user_interface::class)) {
81
            //User has the necessary rights, so we're good
82 2
            return true;
83
        }
84 1
        $user = midcom::get()->auth->get_user($fields['person']);
85 1
        if ($user && $user->username) {
86 1
            $account = new midcom_core_account($user->get_storage());
87 1
            if (!midcom_connection::verify_password($fields["current_password"], $account->get_password())) {
88 1
                return [
89 1
                    'current_password' => $this->l10n->get("wrong current password")
90 1
                ];
91
            }
92
        }
93 1
        return true;
94
    }
95
96
    /**
97
     * Test if a username exists
98
     *
99
     * @param array $fields The form's data
100
     * @return mixed True on success, array of error messages otherwise
101
     */
102 2
    public function username_exists(array $fields)
103
    {
104 2
        if ($this->is_username_available(['username' => $fields['username']]) === true) {
105 1
            return ["username" => $this->l10n->get("unknown username")];
106
        }
107 2
        return true;
108
    }
109
110
    /**
111
     * Test is email address exists
112
     *
113
     * @param array $fields The form's data
114
     * @return mixed True on success, array of error messages otherwise
115
     */
116 1
    public function email_exists(array $fields)
117
    {
118 1
        $result = [];
119 1
        $qb = new midgard_query_builder(midcom::get()->config->get('person_class'));
0 ignored issues
show
Bug introduced by
It seems like midcom::get()->config->get('person_class') can also be of type null; however, parameter $class of midgard_query_builder::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
        $qb = new midgard_query_builder(/** @scrutinizer ignore-type */ midcom::get()->config->get('person_class'));
Loading history...
120 1
        $qb->add_constraint('email', '=', $fields["email"]);
121 1
        $count = $qb->count();
122 1
        if ($count == 0) {
123 1
            $result["email"] = $this->l10n->get("unknown email address");
124 1
        } elseif ($count > 1) {
125
            $result["email"] = $this->l10n->get("multiple entries found, cannot continue");
126
        }
127
128 1
        return $result ?: true;
129
    }
130
131
    /**
132
     * Test that both email and username exist
133
     *
134
     * @param array $fields The form's data
135
     * @return mixed True on success, array of error messages otherwise
136
     */
137 1
    public function email_and_username_exist(array $fields)
138
    {
139 1
        $result = [];
140 1
        $user = midcom::get()->auth->get_user_by_name($fields["username"]);
141 1
        if (!$user) {
142
            $result["username"] = $this->l10n->get("no user found with this username and email address");
143
        } else {
144 1
            $qb = new midgard_query_builder(midcom::get()->config->get('person_class'));
0 ignored issues
show
Bug introduced by
It seems like midcom::get()->config->get('person_class') can also be of type null; however, parameter $class of midgard_query_builder::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
            $qb = new midgard_query_builder(/** @scrutinizer ignore-type */ midcom::get()->config->get('person_class'));
Loading history...
145 1
            $qb->add_constraint('email', '=', $fields["email"]);
146 1
            $qb->add_constraint('guid', '=', $user->guid);
147 1
            if ($qb->count() == 0) {
148 1
                $result["username"] = $this->l10n->get("no user found with this username and email address");
149
            }
150
        }
151 1
        return $result ?: true;
152
    }
153
154
    /**
155
     * Test that no previous password is reused & password is strong enough
156
     *
157
     * @param array $fields The form's data
158
     * @return mixed True on success, array of error messages otherwise
159
     */
160 1
    public function password_check(array $fields)
161
    {
162 1
        $result = [];
163
164 1
        $accounthelper = $this->get_accounthelper(new midcom_db_person($fields["person"]));
165 1
        if (!$accounthelper->check_password_reuse($fields['new_password'])){
166
            $result['password'] = $this->l10n->get('password was already used');
167
        }
168 1
        if (!$accounthelper->check_password_strength($fields['new_password'])){
169 1
            $result['password'] = $this->l10n->get('password weak');
170
        }
171 1
        return $result ?: true;
172
    }
173
}
174