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); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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.