Completed
Push — master ( 5bbc0e...cb8c8d )
by vistart
30:53 queued 11:15
created

Module::validateIPRanges()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 8
nc 4
nop 3
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\organization\web\organization;
14
15
use rhosocial\organization\Organization;
16
use rhosocial\user\User;
17
use Yii;
18
use yii\base\InvalidParamException;
19
use yii\validators\IpValidator;
20
21
/**
22
 * @version 1.0
23
 * @author vistart <[email protected]>
24
 */
25
class Module extends \yii\base\Module
26
{
27
    const RESULT_SUCCESS = 'success';
28
    const RESULT_FAILED = 'failed';
29
    const SESSION_KEY_MESSAGE = 'session_key_message';
30
    const SESSION_KEY_RESULT = 'session_key_result';
31
32
    /**
33
     * Get organization.
34
     * @param Organization|string|integer $organization
35
     * @return Organization
36
     */
37
    public static function getOrganization($organization)
38
    {
39
        if (!$organization) {
40
            return null;
41
        }
42
        $identityClass = Yii::$app->user->identityClass;
43
        $noInitIdentity = $identityClass::buildNoInitModel();
44
        /* @var $noInitIdentity User */
45
        $class = $noInitIdentity->organizationClass;
46
        if ($organization instanceof $class) {
47
            $organization = $organization->getID();
48
        }
49
        return $class::find()->guidOrId($organization)->one();
50
    }
51
52
    /**
53
     * @param string $entrance
54
     * @return Organization
55
     */
56
    public static function getOrganizationByEntrance($entrance)
57
    {
58
        $entrance = (string)$entrance;
59
        if ($entrance === '') {
60
            throw new InvalidParamException(Yii::t('organization', "Entrance should not be empty."));
61
        }
62
        $identityClass = Yii::$app->user->identityClass;
63
        $noInitIdentity = $identityClass::buildNoInitModel();
64
        /* @var $noInitIdentity User */
65
        $class = $noInitIdentity->organizationClass;
66
        $noInit = $class::buildNoInitModel();
67
        /* @var $noInit Organization */
68
        $settingClass = $noInit->organizationSettingClass;
69
        $setting = $settingClass::find()->andWhere([
70
            $noInit->getNoInitOrganizationSetting()->idAttribute => $class::SETTING_ITEM_JOIN_ENTRANCE_URL,
71
            $noInit->getNoInitOrganizationSetting()->contentAttribute => $entrance,
72
        ])->one();
73
        if (!$setting) {
74
            return null;
75
        }
76
        return $setting->host;
77
    }
78
79
    /**
80
     * Validate IP Ranges.
81
     * @param Organization $organization
82
     * @param string $ip
83
     * @param array $errors
84
     * @return bool
85
     */
86
    public static function validateIPRanges($organization, $ip = null, &$errors = null)
87
    {
88
        if ($ip === null) {
89
            $ip = Yii::$app->request->userIP;
90
        }
91
        $range = $organization->joinIpAddress;
92
        if (empty($range) || !is_string($range)) {
93
            $range = '0.0.0.0/0';
94
        }
95
        $validator = new IpValidator(['ranges' => [$range]]);
96
        return $validator->validate($ip, $errors);
0 ignored issues
show
Bug introduced by
It seems like $errors defined by parameter $errors on line 86 can also be of type array; however, yii\validators\Validator::validate() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
97
    }
98
}
99