Completed
Push — master ( 6a1094...c4becb )
by Andreas
27:56
created

midgard_admin_user_plugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 35
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A _on_initialize() 0 6 1
A generate_password() 0 20 3
1
<?php
2
/**
3
 * @package midgard.admin.user
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * user editor interface
11
 *
12
 * @package midgard.admin.user
13
 */
14
class midgard_admin_user_plugin extends midcom_baseclasses_components_plugin
15
{
16 16
    public function _on_initialize()
17
    {
18 16
        midcom::get()->auth->require_user_do('midgard.admin.user:access', null, 'midgard_admin_user_plugin');
19 16
        midgard_admin_asgard_plugin::prepare_plugin($this->_l10n->get('midgard.admin.user'), $this->_request_data);
20
21 16
        $this->add_stylesheet(MIDCOM_STATIC_URL . '/midgard.admin.user/usermgmt.css');
22 16
    }
23
24
    /**
25
     * Generate one password
26
     *
27
     * @param int $length
28
     */
29 8
    public static function generate_password(int $length = 8, bool $no_similars = true) : string
30
    {
31
        // Safety
32 8
        if ($length < 3) {
33 6
            $length = 8;
34
        }
35
36
        // Valid characters for default password (PONDER: make configurable ?)
37 8
        if ($no_similars) {
38 8
            $first_last_chars = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
39
        } else {
40
            $first_last_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
41
        }
42 8
        $passwdchars = $first_last_chars . '.,-*!:+=()/&%$<>?#@';
43
44
        //make sure password doesn't begin or end in punctuation character
45 8
        $password = midcom_helper_misc::random_string(1, $first_last_chars);
46 8
        $password .= midcom_helper_misc::random_string($length - 2, $passwdchars);
47 8
        $password .= midcom_helper_misc::random_string(1, $first_last_chars);
48 8
        return $password;
49
    }
50
}
51