1 | <?php |
||
2 | namespace UserManagement\Extension; |
||
3 | |||
4 | use SilverStripe\CMS\Model\SiteTree; |
||
5 | use SilverStripe\Forms\FieldList; |
||
6 | use SilverStripe\Forms\Tab; |
||
7 | use SilverStripe\Forms\TreeDropdownField; |
||
8 | use SilverStripe\ORM\DataExtension; |
||
9 | use SilverStripe\Security\Group; |
||
10 | use SilverStripe\SiteConfig\SiteConfig; |
||
11 | use SilverStripe\Forms\TextareaField; |
||
12 | use SilverStripe\Forms\CheckboxField; |
||
13 | use SilverStripe\Forms\ListboxField; |
||
14 | use SilverStripe\Security\Member; |
||
15 | use SilverStripe\Forms\DropdownField; |
||
16 | |||
17 | /** |
||
18 | * Class UserManagementConfigExtension |
||
19 | * |
||
20 | * @package user-management |
||
21 | */ |
||
22 | class UserManagementConfigExtension extends DataExtension |
||
23 | { |
||
24 | /** |
||
25 | * DB Fields |
||
26 | * @var array |
||
27 | */ |
||
28 | private static $db = [ |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
29 | "ProfileUpdateSuccess" => 'Text', |
||
30 | "ProfileUpdatError" => 'Text', |
||
31 | "EnableSpamProtection" => 'Boolean', |
||
32 | "ExportFields" => 'Text' |
||
33 | |||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * One to many relationship |
||
38 | * @var array |
||
39 | */ |
||
40 | private static $has_one = [ |
||
0 ignored issues
–
show
|
|||
41 | 'LoginCallBackUrl' => SiteTree::class, |
||
42 | 'LoginUrl' => SiteTree::class, |
||
43 | 'LostPasswordUrl' => SiteTree::class, |
||
44 | 'CustomerGroup' => Group::class |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Update the CMS fields |
||
49 | * |
||
50 | * @return \SilverStripe\Forms\FieldList |
||
51 | */ |
||
52 | public function updateCMSFields(FieldList $fields) |
||
53 | { |
||
54 | $fields->insertBefore('Access', $usertab = Tab::create('UserManagement', 'User Management')); |
||
55 | $fields->addFieldToTab( |
||
56 | 'Root.UserManagement', |
||
57 | DropdownField::create( |
||
58 | 'CustomerGroupID', |
||
59 | _t(__CLASS__ . '.CustomerGroup', 'Group to add new customers to'), |
||
60 | Group::get()->map('ID', 'Title') |
||
61 | ) |
||
62 | ); |
||
63 | $fields->addFieldToTab( |
||
64 | 'Root.UserManagement', |
||
65 | TreeDropdownField::create( |
||
66 | 'LoginUrlID', |
||
67 | _t(__CLASS__ . '.LoginUrl', 'Login Url'), |
||
68 | SiteTree::class |
||
69 | ) |
||
70 | ); |
||
71 | $fields->addFieldToTab( |
||
72 | 'Root.UserManagement', |
||
73 | TreeDropdownField::create( |
||
74 | 'LoginCallBackUrlID', |
||
75 | _t(__CLASS__ . '.LoginCallBackUrl', 'Login Call Back Url'), |
||
76 | SiteTree::class |
||
77 | ) |
||
78 | ); |
||
79 | $fields->addFieldToTab( |
||
80 | 'Root.UserManagement', |
||
81 | TreeDropdownField::create( |
||
82 | 'LostPasswordUrlID', |
||
83 | _t(__CLASS__ . '.LostPasswordUrl', 'Lost Password Url'), |
||
84 | SiteTree::class |
||
85 | ) |
||
86 | ); |
||
87 | $fields->addFieldToTab( |
||
88 | 'Root.UserManagement', |
||
89 | TextareaField::create( |
||
90 | 'ProfileUpdateSuccess', |
||
91 | _t(__CLASS__ . '.ProfileUpdateSuccess', 'Profile update Success Message') |
||
92 | ) |
||
93 | ); |
||
94 | $fields->addFieldToTab( |
||
95 | 'Root.UserManagement', |
||
96 | TextareaField::create( |
||
97 | 'ProfileUpdatError', |
||
98 | _t(__CLASS__ . '.ProfileUpdatError', 'Profile update Error Message') |
||
99 | ) |
||
100 | ); |
||
101 | $fields->addFieldToTab( |
||
102 | 'Root.UserManagement', |
||
103 | CheckboxField::create( |
||
104 | 'EnableSpamProtection', |
||
105 | _t(__CLASS__ . '.EnableSpamProtection', 'Enable Spam Protection') |
||
106 | ) |
||
107 | ); |
||
108 | |||
109 | $fields->addFieldToTab( |
||
110 | 'Root.UserManagement', |
||
111 | ListboxField::create( |
||
112 | 'ExportFields', |
||
113 | 'Select Data fields for the report', |
||
114 | $this->getExportFieldNames(), |
||
115 | json_decode($this->owner->ExportFields) |
||
116 | ) |
||
117 | ); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Returns login page id |
||
122 | * |
||
123 | * @return integer |
||
124 | */ |
||
125 | 2 | public function getLoginUrlID() |
|
126 | { |
||
127 | 2 | if (!$this->owner->LoginUrl()->ID && $this->getDBstatus()) { |
|
128 | 1 | $LoginUrl = SiteTree::get() |
|
129 | 1 | ->filter('ClassName', 'UserManagement\Page\UserLoginPage'); |
|
130 | 1 | if ($LoginUrl->count() > 0) { |
|
131 | return $LoginUrl->first()->ID; |
||
132 | } else { |
||
133 | 1 | return; |
|
134 | } |
||
135 | } else { |
||
136 | 1 | return $this->owner->LoginUrl()->ID; |
|
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Returns call back page id |
||
142 | * |
||
143 | * @return integer |
||
144 | */ |
||
145 | 2 | public function getLoginCallBackUrlID() |
|
146 | { |
||
147 | 2 | if (!$this->owner->LoginCallBackUrl()->ID && $this->getDBstatus()) { |
|
148 | 1 | $LoginCallBackUrl = SiteTree::get() |
|
149 | 1 | ->filter('ClassName', 'UserManagement\Page\UserProfilePage'); |
|
150 | 1 | if ($LoginCallBackUrl->count() > 0) { |
|
151 | return $LoginCallBackUrl->first()->ID; |
||
152 | } else { |
||
153 | 1 | return; |
|
154 | } |
||
155 | } else { |
||
156 | 1 | return $this->owner->LoginCallBackUrl()->ID; |
|
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Returns lost password page id |
||
162 | * |
||
163 | * @return integer | null |
||
164 | */ |
||
165 | 2 | public function getLostPasswordUrlID() |
|
166 | { |
||
167 | 2 | if (!$this->owner->LostPasswordUrl()->ID && $this->getDBstatus()) { |
|
168 | 1 | $LostPasswordUrl = SiteTree::get()->filter('ClassName', 'UserManagement\Page\LostPasswordPage'); |
|
169 | 1 | if ($LostPasswordUrl->count() > 0) { |
|
170 | return $LostPasswordUrl->first()->ID; |
||
171 | } else { |
||
172 | 1 | return; |
|
173 | } |
||
174 | } else { |
||
175 | 1 | return $this->owner->LostPasswordUrl()->ID; |
|
176 | } |
||
177 | } |
||
178 | |||
179 | |||
180 | /** |
||
181 | * Returns customer group id |
||
182 | * |
||
183 | * @return integer | null |
||
184 | */ |
||
185 | 2 | public function getCustomerGroupID() |
|
186 | { |
||
187 | 2 | if (!$this->owner->CustomerGroup()->ID && $this->getDBstatus()) { |
|
188 | 1 | $group = \SilverStripe\Security\Group::get()->filter('Title', 'general'); |
|
189 | 1 | if ($group->count() > 0) { |
|
190 | return $group->first()->ID; |
||
191 | } else { |
||
192 | 1 | return; |
|
193 | } |
||
194 | 1 | } elseif ($this->getDBstatus()) { |
|
195 | return $this->owner->CustomerGroup()->ID; |
||
196 | } |
||
197 | 1 | return; |
|
198 | } |
||
199 | |||
200 | /** |
||
201 | * Returns the DB fields exist for Member |
||
202 | * |
||
203 | * @return array |
||
204 | */ |
||
205 | 1 | public function getExportFieldNames() |
|
206 | { |
||
207 | 1 | $memberFields = Member::create()->getFrontEndFields()->dataFieldNames(); |
|
208 | 1 | $memberFields = array_diff($memberFields, ["FirstName", "Surname", "Email", |
|
209 | "TempIDHash", "TempIDExpired", "AutoLoginHash", "AutoLoginExpired", |
||
210 | "PasswordEncryption", "Salt", "Locale", "FailedLoginCount", |
||
211 | "LockedOutUntil", "Password", "PasswordExpiry"]); |
||
212 | 1 | return array_combine($memberFields, $memberFields); |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * Checks the DB status |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | 5 | public function getDBstatus() |
|
221 | { |
||
222 | 5 | if (\SilverStripe\ORM\DB::is_active() && count(\SilverStripe\ORM\DB::table_list()) > 0) { |
|
223 | 1 | return true; |
|
224 | } |
||
225 | 4 | return false; |
|
226 | } |
||
227 | } |
||
228 |