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
|
|
|
|
14
|
|
|
namespace rhosocial\organization\web\organization\controllers; |
15
|
|
|
|
16
|
|
|
use rhosocial\organization\exceptions\OrganizationNotFoundException; |
17
|
|
|
use rhosocial\organization\forms\JoinOrganizationForm; |
18
|
|
|
use rhosocial\organization\Organization; |
19
|
|
|
use rhosocial\organization\web\organization\Module; |
20
|
|
|
use Yii; |
21
|
|
|
use yii\base\InvalidParamException; |
22
|
|
|
use yii\filters\AccessControl; |
23
|
|
|
use yii\filters\VerbFilter; |
24
|
|
|
use yii\web\BadRequestHttpException; |
25
|
|
|
use yii\web\Controller; |
26
|
|
|
use yii\web\Response; |
27
|
|
|
use yii\web\UnauthorizedHttpException; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class JoinController |
31
|
|
|
* @package rhosocial\organization\web\organization\controllers |
32
|
|
|
* @version 1.0 |
33
|
|
|
* @author vistart <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class JoinController extends Controller |
36
|
|
|
{ |
37
|
|
|
public $layout = 'main'; |
38
|
|
|
|
39
|
|
|
public $joinSuccessMessage; |
40
|
|
|
public $joinFailedMessage; |
41
|
|
|
public $exitSuccessMessage; |
42
|
|
|
public $exitFailedMessage; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Initialize messages. |
46
|
|
|
*/ |
47
|
|
|
protected function initMessages() |
48
|
|
|
{ |
49
|
|
|
if (!is_string($this->joinSuccessMessage) || empty($this->joinSuccessMessage)) { |
50
|
|
|
$this->joinSuccessMessage = Yii::t('organization', 'Joined.'); |
51
|
|
|
} |
52
|
|
|
if (!is_string($this->joinFailedMessage) || empty($this->joinFailedMessage)) { |
53
|
|
|
$this->joinFailedMessage = Yii::t('organization', 'Failed to join.'); |
54
|
|
|
} |
55
|
|
|
if (!is_string($this->exitSuccessMessage) || empty($this->exitSuccessMessage)) { |
56
|
|
|
$this->exitSuccessMessage = Yii::t('organization', 'Exited.'); |
57
|
|
|
} |
58
|
|
|
if (!is_string($this->exitFailedMessage) || empty($this->exitFailedMessage)) { |
59
|
|
|
$this->exitFailedMessage = Yii::t('organization', 'Failed to exit.'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
66
|
|
|
public function init() |
67
|
|
|
{ |
68
|
|
|
$this->initMessages(); |
69
|
|
|
parent::init(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
75
|
|
|
public function behaviors() |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
|
|
'access' => [ |
79
|
|
|
'class' => AccessControl::class, |
80
|
|
|
'rules' => [ |
81
|
|
|
[ |
82
|
|
|
'allow' => false, |
83
|
|
|
'roles' => ['?'], |
84
|
|
|
], |
85
|
|
|
[ |
86
|
|
|
'allow' => true, |
87
|
|
|
'roles' => ['@'], |
88
|
|
|
], |
89
|
|
|
], |
90
|
|
|
], |
91
|
|
|
'verbs' => [ |
92
|
|
|
'class' => VerbFilter::class, |
93
|
|
|
'actions' => [ |
94
|
|
|
'join' => ['post'], |
95
|
|
|
'exit' => ['post'], |
96
|
|
|
] |
97
|
|
|
], |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $entrance |
103
|
|
|
* @return Organization |
104
|
|
|
* @throws BadRequestHttpException |
105
|
|
|
* @throws OrganizationNotFoundException |
106
|
|
|
*/ |
107
|
|
|
public static function getOrganization($entrance) |
108
|
|
|
{ |
109
|
|
|
try { |
110
|
|
|
$organization = Module::getOrganizationByEntrance($entrance); |
111
|
|
|
if (!$organization) { |
112
|
|
|
throw new OrganizationNotFoundException(); |
113
|
|
|
} |
114
|
|
|
} catch (InvalidParamException $ex) { |
115
|
|
|
throw new BadRequestHttpException($ex->getMessage()); |
116
|
|
|
} |
117
|
|
|
return $organization; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $entrance |
122
|
|
|
* @return Response|string |
123
|
|
|
*/ |
124
|
|
|
public function actionIndex($entrance) |
125
|
|
|
{ |
126
|
|
|
$organization = static::getOrganization($entrance); |
127
|
|
|
$model = new JoinOrganizationForm(['organization' => $organization]); |
128
|
|
|
return $this->render('index', [ |
129
|
|
|
'model' => $model, |
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $entrance |
135
|
|
|
* @return Response|string |
136
|
|
|
* @throws UnauthorizedHttpException |
137
|
|
|
*/ |
138
|
|
|
public function actionJoin($entrance) |
139
|
|
|
{ |
140
|
|
|
$organization = static::getOrganization($entrance); |
141
|
|
|
$user = Yii::$app->user->identity; |
142
|
|
|
if ($organization->creator->equals($user)) { |
143
|
|
|
return $this->redirect(['index', 'entrance' => $entrance]); |
144
|
|
|
} |
145
|
|
|
if (!Module::validateIPRanges($organization, Yii::$app->request->userIP, $errors)) { |
146
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED); |
147
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->joinFailedMessage . ' ' . Yii::t('organization', 'Your current IP address is not allowed.')); |
148
|
|
|
return $this->redirect(['index', 'entrance' => $entrance]); |
149
|
|
|
} |
150
|
|
|
$model = new JoinOrganizationForm(['organization' => $organization]); |
151
|
|
|
if (!empty($organization->joinPassword) && (!$model->load(Yii::$app->request->post()) || !$model->validate('password'))) { |
|
|
|
|
152
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED); |
153
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->joinFailedMessage . ($model->hasErrors('password') ? ' ' . $model->getFirstError('password') : '')); |
154
|
|
|
return $this->redirect(['index', 'entrance' => $entrance]); |
155
|
|
|
} |
156
|
|
|
try { |
157
|
|
|
if ($organization->addMember($user)) { |
158
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS); |
159
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->joinSuccessMessage); |
160
|
|
|
} else { |
161
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED); |
162
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->joinFailedMessage); |
163
|
|
|
} |
164
|
|
|
} catch (\Exception $ex) { |
165
|
|
|
throw new UnauthorizedHttpException($ex->getMessage()); |
166
|
|
|
} |
167
|
|
|
return $this->redirect(['index', 'entrance' => $entrance]); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $entrance |
172
|
|
|
* @return Response |
173
|
|
|
* @throws UnauthorizedHttpException |
174
|
|
|
*/ |
175
|
|
|
public function actionExit($entrance) |
176
|
|
|
{ |
177
|
|
|
$organization = static::getOrganization($entrance); |
178
|
|
|
$user = Yii::$app->user->identity; |
179
|
|
|
if ($organization->creator->equals($user)) { |
180
|
|
|
return $this->redirect(['index', 'entrance' => $entrance]); |
181
|
|
|
} |
182
|
|
|
$model = new JoinOrganizationForm(['organization' => $organization]); |
183
|
|
|
if (!empty($organization->joinPassword) && (!$model->load(Yii::$app->request->post()) || !$model->validate('password'))) { |
|
|
|
|
184
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED); |
185
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->exitFailedMessage . ($model->hasErrors('password') ? ' ' . $model->getFirstError('password') : '')); |
186
|
|
|
return $this->redirect(['index', 'entrance' => $entrance]); |
187
|
|
|
} |
188
|
|
|
try { |
189
|
|
|
if ($organization->removeMember($user)) { |
190
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS); |
191
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->exitSuccessMessage); |
192
|
|
|
} else { |
193
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED); |
194
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->exitFailedMessage); |
195
|
|
|
} |
196
|
|
|
} catch (\Exception $ex) { |
197
|
|
|
throw new UnauthorizedHttpException($ex->getMessage()); |
198
|
|
|
} |
199
|
|
|
return $this->redirect(['index', 'entrance' => $entrance]); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: