-
Notifications
You must be signed in to change notification settings - Fork 8
/
RegistrationChecks.php
191 lines (155 loc) · 6.03 KB
/
RegistrationChecks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2018 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\legal\models;
use humhub\libs\Html;
use humhub\modules\legal\Module;
use humhub\modules\user\models\User;
use Yii;
use yii\base\Exception;
use yii\base\Model;
/* @var $this \humhub\modules\ui\view\components\View */
/**
* Class RegistrationChecks
* @package humhub\modules\legal\models
*/
class RegistrationChecks extends Model
{
public const SETTING_KEY_TERMS = 'acceptedTerms';
public const SETTING_KEY_PRIVACY = 'acceptedPrivacy';
public const SETTING_KEY_AGE = 'acceptedAge';
public $ageCheck;
public $termsCheck;
public $dataPrivacyCheck;
/**
* @var false|string static SETTING_KEY const
*/
public $restrictToSettingKey = false;
/**
* @var User
*/
public $user;
public function rules()
{
$rules = [];
if ($this->showAgeCheck()) {
$rules[] = [['ageCheck'], 'required', 'requiredValue' => 1, 'message' => ''];
}
if ($this->showPrivacyCheck()) {
$rules[] = [['dataPrivacyCheck'], 'required', 'requiredValue' => 1, 'message' => ''];
}
if ($this->showTermsCheck()) {
$rules[] = [['termsCheck'], 'required', 'requiredValue' => 1, 'message' => ''];
}
return $rules;
}
public function attributeLabels()
{
return [
'ageCheck' => Yii::t('LegalModule.base', 'I am older than {age} years', ['age' => \Yii::$app->getModule('legal')->getMinimumAge()]),
'termsCheck' => Yii::t('LegalModule.base', 'I have read and agree to the Terms and Conditions'),
'dataPrivacyCheck' => Yii::t('LegalModule.base', 'I have read and agree to the Privacy Policy'),
];
}
public function attributeHints()
{
/** @var Module $module */
$module = Yii::$app->getModule('legal');
$spacing = ' ';
$hints = [];
$privacyPage = Page::getPage(Page::PAGE_KEY_PRIVACY_PROTECTION);
if ($privacyPage !== null && $module->isPageEnabled(Page::PAGE_KEY_PRIVACY_PROTECTION)) {
$link = Html::a($privacyPage->title, ['/legal/page/view', 'pageKey' => Page::PAGE_KEY_PRIVACY_PROTECTION], ['data-pjax-prevent' => 1, 'target' => '_blank']);
$hints['dataPrivacyCheck'] = $spacing . Yii::t('LegalModule.base', 'More information: {link}', ['link' => $link]);
}
$termsPage = Page::getPage(Page::PAGE_KEY_TERMS);
if ($termsPage !== null && $module->isPageEnabled(Page::PAGE_KEY_TERMS)) {
$link = Html::a($termsPage->title, ['/legal/page/view', 'pageKey' => Page::PAGE_KEY_TERMS], ['data-pjax-prevent' => 1, 'target' => '_blank']);
$hints['termsCheck'] = $spacing . Yii::t('LegalModule.base', 'More information: {link}', ['link' => $link]);
}
return $hints;
}
public function showPrivacyCheck()
{
if ($this->restrictToSettingKey && $this->restrictToSettingKey !== static::SETTING_KEY_PRIVACY) {
return false;
}
/** @var Module $module */
$module = Yii::$app->getModule('legal');
if (Page::getPage(Page::PAGE_KEY_PRIVACY_PROTECTION) && $module->isPageEnabled(Page::PAGE_KEY_PRIVACY_PROTECTION)) {
if ($this->user === null || empty($module->settings->user($this->user)->get(static::SETTING_KEY_PRIVACY))) {
return true;
}
}
return false;
}
public function showTermsCheck()
{
if ($this->restrictToSettingKey && $this->restrictToSettingKey !== static::SETTING_KEY_TERMS) {
return false;
}
/** @var Module $module */
$module = Yii::$app->getModule('legal');
if (Page::getPage(Page::PAGE_KEY_TERMS) && $module->isPageEnabled(Page::PAGE_KEY_TERMS)) {
if ($this->user === null || empty($module->settings->user($this->user)->get(static::SETTING_KEY_TERMS))) {
return true;
}
}
return false;
}
public function showAgeCheck()
{
if ($this->restrictToSettingKey && $this->restrictToSettingKey !== static::SETTING_KEY_AGE) {
return false;
}
/** @var Module $module */
$module = Yii::$app->getModule('legal');
if ($module->showAgeCheck()) {
if ($this->user === null || empty($module->settings->user($this->user)->get(static::SETTING_KEY_AGE))) {
return true;
}
}
return false;
}
public function hasOpenCheck()
{
if ($this->showAgeCheck() || $this->showTermsCheck() || $this->showPrivacyCheck()) {
return true;
}
return false;
}
/**
* @return bool
* @throws Exception
*/
public function save()
{
/** @var Module $module */
$module = Yii::$app->getModule('legal');
if ($this->user === null) {
if (Yii::$app->user->isGuest) {
throw new Exception('Could not save with valid user object!');
}
$this->user = Yii::$app->user->getIdentity();
}
if (!$this->validate()) {
return false;
}
if ($this->showTermsCheck() && $this->termsCheck) {
$module->settings->user($this->user)->set(static::SETTING_KEY_TERMS, true);
$module->settings->user($this->user)->set(static::SETTING_KEY_TERMS . 'Time', time());
}
if ($this->showPrivacyCheck() && $this->dataPrivacyCheck) {
$module->settings->user($this->user)->set(static::SETTING_KEY_PRIVACY, true);
$module->settings->user($this->user)->set(static::SETTING_KEY_PRIVACY . 'Time', time());
}
if ($this->showAgeCheck() && $this->ageCheck) {
$module->settings->user($this->user)->set(static::SETTING_KEY_AGE, true);
$module->settings->user($this->user)->set(static::SETTING_KEY_AGE . 'Time', time());
}
return true;
}
}