Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 739 |
|
0.00% |
0 / 16 |
CRAP | |
0.00% |
0 / 1 |
| UserController | |
0.00% |
0 / 739 |
|
0.00% |
0 / 16 |
49506 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| pageEmployees | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
42 | |||
| pageUsers | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
42 | |||
| pageUser | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
56 | |||
| pageStoreSettings | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
20 | |||
| formUserCreate | |
0.00% |
0 / 88 |
|
0.00% |
0 / 1 |
756 | |||
| formUserAdminCreate | |
0.00% |
0 / 61 |
|
0.00% |
0 / 1 |
210 | |||
| formUserEditPassword | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
30 | |||
| formStoreEditPassword | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
12 | |||
| formUserEdit | |
0.00% |
0 / 95 |
|
0.00% |
0 / 1 |
930 | |||
| createUser | |
0.00% |
0 / 109 |
|
0.00% |
0 / 1 |
1190 | |||
| createAdminUser | |
0.00% |
0 / 43 |
|
0.00% |
0 / 1 |
272 | |||
| updateUserPassword | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
| updateStorePassword | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
| updateUser | |
0.00% |
0 / 109 |
|
0.00% |
0 / 1 |
3192 | |||
| deleteUser | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\Core\Controllers; |
| 4 | |
| 5 | use BuyerKiosk\StoreController; |
| 6 | |
| 7 | /******* |
| 8 | * |
| 9 | * /users/* |
| 10 | *******/ |
| 11 | |
| 12 | // Handles user-related activities |
| 13 | #[AllowDynamicProperties] |
| 14 | class UserController extends BaseController { |
| 15 | |
| 16 | public function __construct($app){ |
| 17 | $this->_app = $app; |
| 18 | } |
| 19 | public function pageEmployees($typeNum) { |
| 20 | $group = GroupLoader::fetch($typeNum, 'name'); |
| 21 | |
| 22 | if (!$group) { |
| 23 | error_log("ERROR: UserController::pageEmployees - GroupLoader::fetch returned false for typeNum: $typeNum"); |
| 24 | $this->_app->notFound(); |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | $store = new \Store(); |
| 29 | $store->createStore($typeNum); |
| 30 | |
| 31 | $users = $group->getUsers(); |
| 32 | |
| 33 | $revisedUsers = []; |
| 34 | //Filter out the store user and any admin users |
| 35 | foreach ($users as $user) { |
| 36 | // Skip false values that may be returned when a user doesn't exist |
| 37 | if ($user === false) { |
| 38 | continue; |
| 39 | } |
| 40 | if(!preg_match('/^[a-z][a-z]\d+/',$user->user_name) && $user->primary_group_id > 2) { |
| 41 | $revisedUsers[] = $user; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | $name = $group->name; |
| 46 | $icon = $group->icon; |
| 47 | |
| 48 | $this->_app->render('employees.html', [ |
| 49 | 'page' => [ |
| 50 | 'author' => $this->_app->site->author, |
| 51 | 'title' => $name, |
| 52 | 'description' => "A listing of the users for your site. Provides management tools including the ability to edit user details, manually activate users, enable/disable users, and more.", |
| 53 | 'alerts' => $this->_app->alerts->getAndClearMessages(), |
| 54 | 'showAll' => 0 |
| 55 | ], |
| 56 | 'groups' => $this->_app->user->getGroups(), |
| 57 | "box_title" => $group->new_user_title, |
| 58 | "icon" => $icon, |
| 59 | "users" => $revisedUsers, |
| 60 | "store" => getStoreInfo($store) |
| 61 | ]); |
| 62 | } |
| 63 | |
| 64 | public function pageUsers($primary_group_name = null, $typeNum = null){ |
| 65 | if ($primary_group_name) { |
| 66 | $primary_group = GroupLoader::fetch($primary_group_name, 'name'); |
| 67 | if (!$primary_group) { |
| 68 | error_log("ERROR: UserController - GroupLoader::fetch returned false for primary_group_name: $primary_group_name"); |
| 69 | $primary_group = GroupLoader::fetch(GROUP_DEFAULT_PRIMARY, "is_default"); |
| 70 | } |
| 71 | |
| 72 | if (!$primary_group) |
| 73 | $this->_app->notFound(); |
| 74 | |
| 75 | // Access-controlled page |
| 76 | if (!$this->_app->user->checkAccess('uri_group_users', ['primary_group_id' => $primary_group->id])) { |
| 77 | $this->_app->notFound(); |
| 78 | } |
| 79 | |
| 80 | $users = UserLoader::fetchAll($primary_group->id, 'primary_group_id'); |
| 81 | $name = $primary_group->name; |
| 82 | $icon = $primary_group->icon; |
| 83 | |
| 84 | } else { |
| 85 | // Access-controlled page |
| 86 | if (!$this->_app->user->checkAccess('uri_users')) { |
| 87 | $this->_app->notFound(); |
| 88 | } |
| 89 | |
| 90 | $users = UserLoader::fetchAll(); |
| 91 | $name = "Users"; |
| 92 | $icon = "fa fa-users"; |
| 93 | } |
| 94 | |
| 95 | $this->_app->render('users.html', [ |
| 96 | 'page' => [ |
| 97 | 'author' => $this->_app->site->author, |
| 98 | 'title' => $name, |
| 99 | 'description' => "A listing of the users for your site. Provides management tools including the ability to edit user details, manually activate users, enable/disable users, and more.", |
| 100 | 'alerts' => $this->_app->alerts->getAndClearMessages() |
| 101 | ], |
| 102 | "box_title" => $name, |
| 103 | "icon" => $icon, |
| 104 | "users" => $users |
| 105 | ]); |
| 106 | } |
| 107 | |
| 108 | public function pageUser($user_id, $typeNum){ |
| 109 | // Get the user to view |
| 110 | $target_user = UserLoader::fetch($user_id); |
| 111 | |
| 112 | //error_log($user_id); |
| 113 | |
| 114 | $store = new \Store(); |
| 115 | $store->createStore($typeNum); |
| 116 | |
| 117 | // Access-controlled resource |
| 118 | if (!$this->_app->user->checkAccess('uri_users') && !$this->_app->user->checkAccess('uri_group_users', ['primary_group_id' => $target_user->primary_group_id])){ |
| 119 | $this->_app->notFound(); |
| 120 | } |
| 121 | |
| 122 | // Get a list of all groups |
| 123 | $groups = GroupLoader::fetchAll(); |
| 124 | |
| 125 | // Get a list of all locales |
| 126 | $locale_list = $this->_app->site->getLocales(); |
| 127 | |
| 128 | // Determine which groups this user is a member of |
| 129 | $user_groups = $target_user->getGroups(); |
| 130 | foreach ($groups as $group_id => $group){ |
| 131 | $group_list[$group_id] = $group->export(); |
| 132 | if (isset($user_groups[$group_id])) |
| 133 | $group_list[$group_id]['member'] = true; |
| 134 | else |
| 135 | $group_list[$group_id]['member'] = false; |
| 136 | } |
| 137 | |
| 138 | // Determine authorized fields |
| 139 | $fields = ['display_name', 'email', 'title', 'locale', 'groups', 'primary_group_id']; |
| 140 | $show_fields = []; |
| 141 | $disabled_fields = []; |
| 142 | $hidden_fields = []; |
| 143 | foreach ($fields as $field){ |
| 144 | if ($this->_app->user->checkAccess("view_account_setting", ["user" => $target_user, "property" => $field])) |
| 145 | $disabled_fields[] = $field; |
| 146 | else |
| 147 | $hidden_fields[] = $field; |
| 148 | } |
| 149 | |
| 150 | // Always disallow editing username |
| 151 | $disabled_fields[] = "user_name"; |
| 152 | |
| 153 | // Hide password fields for editing user |
| 154 | $hidden_fields[] = "password"; |
| 155 | |
| 156 | $this->_app->render('user_info.html', [ |
| 157 | 'page' => [ |
| 158 | 'author' => $this->_app->site->author, |
| 159 | 'title' => "Users | " . $target_user->user_name, |
| 160 | 'description' => "User information page for " . $target_user->user_name, |
| 161 | 'alerts' => $this->_app->alerts->getAndClearMessages() |
| 162 | ], |
| 163 | "box_id" => 'view-user', |
| 164 | "box_title" => $target_user->user_name, |
| 165 | "target_user" => $target_user, |
| 166 | "groups" => $group_list, |
| 167 | "locales" => $locale_list, |
| 168 | "fields" => [ |
| 169 | "disabled" => $disabled_fields, |
| 170 | "hidden" => $hidden_fields |
| 171 | ], |
| 172 | "buttons" => [ |
| 173 | "hidden" => [ |
| 174 | "submit", "cancel" |
| 175 | ] |
| 176 | ], |
| 177 | "validators" => "{ none: ''}", |
| 178 | "store" => getStoreInfo($store), |
| 179 | ]); |
| 180 | } |
| 181 | public function pageStoreSettings($typeNum, \Store $store) { |
| 182 | // Get the user to edit |
| 183 | $target_user = UserLoader::fetch($typeNum, "user_name"); |
| 184 | |
| 185 | if (!$this->_app->user->checkAccess('uri_edituser') && !$this->_app->user->checkAccess('uri_store_settings') && !$this->_app->user->checkAccess('uri_group_users', ['primary_group_id' => $target_user->primary_group_id])){ |
| 186 | $this->_app->notFound(); |
| 187 | } |
| 188 | |
| 189 | //$get = $this->_app->request->get(); |
| 190 | |
| 191 | $template = "store/settings.html"; |
| 192 | |
| 193 | // Load validator rules |
| 194 | $schema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/user-password.json"); |
| 195 | $validators = new \Fortress\FormValidationAdapter($this->_app->translator, $schema); |
| 196 | |
| 197 | $this->_app->render($template, [ |
| 198 | 'page' => [ |
| 199 | 'title' => "Change Store Password", |
| 200 | 'currentStore' => $typeNum |
| 201 | ], |
| 202 | "box_title" => "Edit ".$store->getCity()."'s Password", |
| 203 | "submit_button" => "Change Password", |
| 204 | "form_action" => $this->_app->site->uri['public'] . "/users/s/".$typeNum."/password", |
| 205 | "target_user" => $target_user, |
| 206 | 'alerts' => $this->_app->alerts->getAndClearMessages(), |
| 207 | "typeNum" => $typeNum, |
| 208 | "buttons" => [ |
| 209 | "hidden" => [ |
| 210 | "edit", "enable", "delete", "activate" |
| 211 | ] |
| 212 | ], |
| 213 | 'store' => getStoreInfo($store), |
| 214 | "validators" => $validators->formValidationRulesJson(), |
| 215 | ]); |
| 216 | |
| 217 | |
| 218 | } |
| 219 | |
| 220 | // Display the form for creating a new user |
| 221 | public function formUserCreate($typeNum){ |
| 222 | // Access-controlled resource |
| 223 | if (!$this->_app->user->checkAccess('create_account')){ |
| 224 | $this->_app->notFound(); |
| 225 | } |
| 226 | $userPrimaryGroup = $this->_app->user->getPrimaryGroup(); |
| 227 | if($userPrimaryGroup->id == 9) { |
| 228 | $storeGroups = []; |
| 229 | $userGroups = $this->_app->user->getGroups(); |
| 230 | foreach($userGroups as $group) { |
| 231 | if(preg_match('/[a-z][a-z][0-9]{2,}/', $group->name)) { |
| 232 | $storeGroups[] = $group->name; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | $get = $this->_app->request->get(); |
| 237 | |
| 238 | if (isset($get['render'])) |
| 239 | $render = $get['render']; |
| 240 | else |
| 241 | $render = "modal"; |
| 242 | $filteredGroups = []; |
| 243 | // Get a list of all groups |
| 244 | $groups = GroupLoader::fetchAll(); |
| 245 | |
| 246 | $hidden_groups = ['Store', 'resale1', 'Administrator', 'fom', 'Beta Testers', 'Winmark', "Shopify"]; |
| 247 | foreach ($groups as $group) { |
| 248 | if($userPrimaryGroup->id == 2) { |
| 249 | $filteredGroups = $groups; |
| 250 | } elseif ($userPrimaryGroup->id == 9) { |
| 251 | if(preg_match('/[a-z][a-z][0-9]{2,}/', $group->name) && in_array($group->name, $storeGroups, TRUE)) { |
| 252 | $filteredGroups[] = $group; |
| 253 | } |
| 254 | if(!preg_match('/[a-z][a-z][0-9]{2,}/', $group->name) && !in_array($group->name,$hidden_groups, TRUE)) { |
| 255 | if($group->role <= $userPrimaryGroup->role) { |
| 256 | $filteredGroups[] = $group; |
| 257 | } |
| 258 | } |
| 259 | } else { |
| 260 | if(!preg_match('/[a-z][a-z][0-9]{2,}/', $group->name) && !in_array($group->name,$hidden_groups, TRUE)) { |
| 261 | if($group->role <= $userPrimaryGroup->role) { |
| 262 | $filteredGroups[] = $group; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | usort($filteredGroups, function($a, $b) { |
| 268 | if($a->role == 0) { |
| 269 | return 1; |
| 270 | } elseif($b->role == 0) { |
| 271 | return -1; |
| 272 | } else { |
| 273 | return $b->role <=> $a->role; // Sort from highest to lowest role |
| 274 | } |
| 275 | }); |
| 276 | |
| 277 | // Get a list of all locales |
| 278 | $locale_list = $this->_app->site->getLocales(); |
| 279 | |
| 280 | // Get default primary group (is_default = GROUP_DEFAULT_PRIMARY) |
| 281 | $primary_group = GroupLoader::fetch(GROUP_DEFAULT_PRIMARY, "is_default"); |
| 282 | |
| 283 | // Get the default groups |
| 284 | $default_groups = GroupLoader::fetchAll(GROUP_DEFAULT, "is_default"); |
| 285 | |
| 286 | // Set default groups, including default primary group |
| 287 | foreach ($filteredGroups as $group){ |
| 288 | $group_id=$group->id; |
| 289 | $group_list[$group_id] = $group->export(); |
| 290 | if (isset($default_groups[$group_id]) || $group_id == 6) |
| 291 | $group_list[$group_id]['member'] = true; |
| 292 | else |
| 293 | $group_list[$group_id]['member'] = false; |
| 294 | } |
| 295 | |
| 296 | $data['primary_group_id'] = $primary_group->id; |
| 297 | // Set default title for new users |
| 298 | $data['title'] = $primary_group->new_user_title; |
| 299 | // Set default locale |
| 300 | $data['locale'] = $this->_app->site->default_locale; |
| 301 | |
| 302 | |
| 303 | |
| 304 | // Create a dummy user to prepopulate fields |
| 305 | $target_user = new User($data); |
| 306 | |
| 307 | if ($render == "modal") |
| 308 | $template = "components/user-info-modal.html"; |
| 309 | else |
| 310 | $template = "components/user-info-panel.html"; |
| 311 | |
| 312 | // Determine authorized fields for those that have default values. Don't hide any fields |
| 313 | $fields = ['title', 'locale', 'groups', 'primary_group_id']; |
| 314 | $show_fields = []; |
| 315 | $disabled_fields = []; |
| 316 | $dailyReportField = 0; |
| 317 | if($this->_app->user->checkAccess("create_account_setting", ["property" => "dailyReport"])) { |
| 318 | $hidden_fields = ["locale","primary_group_id", "match","registered-since", "title"]; |
| 319 | $dailyReportField = 1; |
| 320 | } else { |
| 321 | $hidden_fields = ["locale","primary_group_id", "match","registered-since", "title", "dailyReport"]; |
| 322 | } |
| 323 | |
| 324 | foreach ($fields as $field){ |
| 325 | if ($this->_app->user->checkAccess("create_account_setting", ["user" => $target_user, "property" => $field])) |
| 326 | $show_fields[] = $field; |
| 327 | else |
| 328 | $disabled_fields[] = $field; |
| 329 | } |
| 330 | if(!isset($employees)) { |
| 331 | $employees = []; |
| 332 | } |
| 333 | |
| 334 | // Load validator rules |
| 335 | $schema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/user-create.json"); |
| 336 | $validators = new \Fortress\FormValidationAdapter($this->_app->translator, $schema); |
| 337 | |
| 338 | $this->_app->render($template, [ |
| 339 | "box_id" => $get['box_id'], |
| 340 | "box_title" => "Create a New Employee", |
| 341 | "submit_button" => "Create Employee", |
| 342 | "form_action" => $this->_app->site->uri['public'] . "/users", |
| 343 | "target_user" => $target_user, |
| 344 | "typeNum" => $typeNum, |
| 345 | "dailyReportField" => $dailyReportField, |
| 346 | "groups" => $group_list, |
| 347 | "locales" => $locale_list, |
| 348 | "fields" => [ |
| 349 | "disabled" => $disabled_fields, |
| 350 | "hidden" => $hidden_fields |
| 351 | ], |
| 352 | "buttons" => [ |
| 353 | "hidden" => [ |
| 354 | "edit", "enable", "delete", "activate" |
| 355 | ] |
| 356 | ], |
| 357 | "validators" => $validators->formValidationRulesJson() |
| 358 | ]); |
| 359 | } |
| 360 | public function formUserAdminCreate(){ |
| 361 | // Access-controlled resource |
| 362 | if (!$this->_app->user->checkAccess('create_account')){ |
| 363 | $this->_app->notFound(); |
| 364 | } |
| 365 | $userPrimaryGroup = $this->_app->user->getPrimaryGroup(); |
| 366 | |
| 367 | |
| 368 | $get = $this->_app->request->get(); |
| 369 | |
| 370 | if (isset($get['render'])) |
| 371 | $render = $get['render']; |
| 372 | else |
| 373 | $render = "modal"; |
| 374 | $filteredGroups = []; |
| 375 | // Get a list of all groups |
| 376 | $groups = GroupLoader::fetchAll(); |
| 377 | foreach ($groups as $group) { |
| 378 | if(!preg_match('/[a-z][a-z][0-9]{2,}/', $group->name) && $group->name !== 'Store') { |
| 379 | if($group->role <= $userPrimaryGroup->role) { |
| 380 | $filteredGroups[] = $group; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | // Get a list of all locales |
| 386 | $locale_list = $this->_app->site->getLocales(); |
| 387 | |
| 388 | // Get default primary group (is_default = GROUP_DEFAULT_PRIMARY) |
| 389 | $primary_group = GroupLoader::fetch(GROUP_DEFAULT_PRIMARY, "is_default"); |
| 390 | |
| 391 | // Get the default groups |
| 392 | $default_groups = GroupLoader::fetchAll(GROUP_DEFAULT, "is_default"); |
| 393 | |
| 394 | // Set default groups, including default primary group |
| 395 | foreach ($groups as $group){ |
| 396 | $group_id=$group->id; |
| 397 | $group_list[$group_id] = $group->export(); |
| 398 | if (isset($default_groups[$group_id]) || $group_id == 6) |
| 399 | $group_list[$group_id]['member'] = true; |
| 400 | else |
| 401 | $group_list[$group_id]['member'] = false; |
| 402 | } |
| 403 | |
| 404 | $data['primary_group_id'] = $primary_group->id; |
| 405 | // Set default title for new users |
| 406 | $data['title'] = $primary_group->new_user_title; |
| 407 | // Set default locale |
| 408 | $data['locale'] = $this->_app->site->default_locale; |
| 409 | |
| 410 | // Create a dummy user to prepopulate fields |
| 411 | $target_user = new User($data); |
| 412 | |
| 413 | if ($render == "modal") |
| 414 | $template = "components/user-info-admin-modal.html"; |
| 415 | else |
| 416 | $template = "components/user-info-admin-panel.html"; |
| 417 | |
| 418 | // Determine authorized fields for those that have default values. Don't hide any fields |
| 419 | $fields = ['title', 'locale', 'groups', 'primary_group_id']; |
| 420 | $show_fields = []; |
| 421 | $disabled_fields = []; |
| 422 | $hidden_fields = ["locale"]; |
| 423 | foreach ($fields as $field){ |
| 424 | if ($this->_app->user->checkAccess("update_account_setting", ["user" => $target_user, "property" => $field])) |
| 425 | $show_fields[] = $field; |
| 426 | else |
| 427 | $disabled_fields[] = $field; |
| 428 | } |
| 429 | if(!isset($employees)) { |
| 430 | $employees = []; |
| 431 | } |
| 432 | |
| 433 | // Load validator rules |
| 434 | $schema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/user-create-admin.json"); |
| 435 | $validators = new \Fortress\FormValidationAdapter($this->_app->translator, $schema); |
| 436 | |
| 437 | $this->_app->render($template, [ |
| 438 | "box_id" => $get['box_id'], |
| 439 | "box_title" => "Create User", |
| 440 | "submit_button" => "Create user", |
| 441 | "form_action" => $this->_app->site->uri['public'] . "/users/admin", |
| 442 | "target_user" => $target_user, |
| 443 | "groups" => $group_list, |
| 444 | "locales" => $locale_list, |
| 445 | "employees" => $employees, |
| 446 | "fields" => [ |
| 447 | "disabled" => $disabled_fields, |
| 448 | "hidden" => $hidden_fields |
| 449 | ], |
| 450 | "buttons" => [ |
| 451 | "hidden" => [ |
| 452 | "edit", "enable", "delete", "activate" |
| 453 | ] |
| 454 | ], |
| 455 | "validators" => $validators->formValidationRulesJson() |
| 456 | ]); |
| 457 | } |
| 458 | public function formUserEditPassword($user_id) { |
| 459 | // Get the user to edit |
| 460 | $target_user = UserLoader::fetch($user_id); |
| 461 | |
| 462 | |
| 463 | // Access-controlled resource |
| 464 | if (!$this->_app->user->checkAccess('uri_edituser') && !$this->_app->user->checkAccess('uri_group_users', ['primary_group_id' => $target_user->primary_group_id])){ |
| 465 | $this->_app->notFound(); |
| 466 | } |
| 467 | |
| 468 | $get = $this->_app->request->get(); |
| 469 | |
| 470 | if (isset($get['render'])) |
| 471 | $render = $get['render']; |
| 472 | else |
| 473 | $render = "modal"; |
| 474 | |
| 475 | if ($render == "modal") |
| 476 | $template = "components/user-password-modal2.html"; |
| 477 | else |
| 478 | $template = "components/user-password-panel.html"; |
| 479 | |
| 480 | // Load validator rules |
| 481 | $schema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/user-password.json"); |
| 482 | $validators = new \Fortress\FormValidationAdapter($this->_app->translator, $schema); |
| 483 | |
| 484 | $this->_app->render($template, [ |
| 485 | |
| 486 | "box_id" => $get['box_id'], |
| 487 | "box_title" => "Edit User", |
| 488 | "submit_button" => "Change Password", |
| 489 | "form_action" => $this->_app->site->uri['public'] . "/users/u/".$user_id."/password", |
| 490 | "target_user" => $target_user, |
| 491 | "buttons" => [ |
| 492 | "hidden" => [ |
| 493 | "edit", "enable", "delete", "activate" |
| 494 | ] |
| 495 | ], |
| 496 | "validators" => $validators->formValidationRulesJson() |
| 497 | ]); |
| 498 | |
| 499 | |
| 500 | } |
| 501 | public function formStoreEditPassword($typeNum, \Store $store) { |
| 502 | // Get the user to edit |
| 503 | $target_user = UserLoader::fetch($typeNum, "user_name"); |
| 504 | |
| 505 | |
| 506 | |
| 507 | if (!$this->_app->user->checkAccess('uri_edituser') && !$this->_app->user->checkAccess('uri_group_users', ['primary_group_id' => $target_user->primary_group_id])){ |
| 508 | $this->_app->notFound(); |
| 509 | } |
| 510 | |
| 511 | //$get = $this->_app->request->get(); |
| 512 | |
| 513 | $template = "store/change-password.html"; |
| 514 | |
| 515 | // Load validator rules |
| 516 | $schema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/user-password.json"); |
| 517 | $validators = new \Fortress\FormValidationAdapter($this->_app->translator, $schema); |
| 518 | |
| 519 | $this->_app->render($template, [ |
| 520 | 'page' => [ |
| 521 | 'title' => "Change Store Password", |
| 522 | 'currentStore' => $typeNum |
| 523 | ], |
| 524 | "box_title" => "Edit ".$store->getCity()."'s Password", |
| 525 | "submit_button" => "Change Password", |
| 526 | "form_action" => $this->_app->site->uri['public'] . "/users/s/".$typeNum."/password", |
| 527 | "target_user" => $target_user, |
| 528 | 'alerts' => $this->_app->alerts->getAndClearMessages(), |
| 529 | "typeNum" => $typeNum, |
| 530 | "buttons" => [ |
| 531 | "hidden" => [ |
| 532 | "edit", "enable", "delete", "activate" |
| 533 | ] |
| 534 | ], |
| 535 | 'store' => getStoreInfo($store), |
| 536 | "validators" => $validators->formValidationRulesJson() |
| 537 | ]); |
| 538 | |
| 539 | |
| 540 | } |
| 541 | |
| 542 | // Display the form for editing an existing user |
| 543 | public function formUserEdit($user_id, $typeNum = null){ |
| 544 | // Get the user to edit |
| 545 | $target_user = UserLoader::fetch($user_id); |
| 546 | $userPrimaryGroup = $this->_app->user->getPrimaryGroup(); |
| 547 | |
| 548 | // Access-controlled resource |
| 549 | if (!$this->_app->user->checkAccess('uri_edituser') && !$this->_app->user->checkAccess('uri_group_users', ['primary_group_id' => $target_user->primary_group_id])){ |
| 550 | $this->_app->notFound(); |
| 551 | } |
| 552 | if($userPrimaryGroup->id == 9) { |
| 553 | $storeGroups = []; |
| 554 | $userGroups = $this->_app->user->getGroups(); |
| 555 | foreach($userGroups as $group) { |
| 556 | if(preg_match('/[a-z][a-z][0-9]{2,}/', $group->name)) { |
| 557 | $storeGroups[] = $group->name; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | if(isset($typeNum)) { |
| 563 | $storeController = new StoreController($typeNum); |
| 564 | $store = $storeController->getStore(); |
| 565 | $db = dbConnectByName($store->getDbName()); |
| 566 | |
| 567 | $employees = []; |
| 568 | $query = $db->query("SELECT * FROM employees WHERE active = 1"); |
| 569 | while($row = $query->fetch(\PDO::FETCH_ASSOC)) { |
| 570 | $employees[] = $row; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | $get = $this->_app->request->get(); |
| 575 | |
| 576 | if (isset($get['render'])) |
| 577 | $render = $get['render']; |
| 578 | else |
| 579 | $render = "modal"; |
| 580 | |
| 581 | $filteredGroups = []; |
| 582 | // Get a list of all groups |
| 583 | $groups = GroupLoader::fetchAll(); |
| 584 | $hidden_groups = ['Store', 'resale1', 'Administrator', 'fom', 'Beta Testers', 'Winmark']; |
| 585 | foreach ($groups as $group) { |
| 586 | if($userPrimaryGroup->id == 2) { |
| 587 | $filteredGroups = $groups; |
| 588 | } elseif ($userPrimaryGroup->id == 9) { |
| 589 | if(preg_match('/[a-z][a-z][0-9]{2,}/', $group->name) && in_array($group->name, $storeGroups)) { |
| 590 | //error_log("Store Group Name Match: ". $group->name); |
| 591 | $filteredGroups[] = $group; |
| 592 | } |
| 593 | if(!preg_match('/[a-z][a-z][0-9]{2,}/', $group->name) && !in_array($group->name,$hidden_groups, TRUE)) { |
| 594 | if($group->role <= $userPrimaryGroup->role) { |
| 595 | $filteredGroups[] = $group; |
| 596 | } |
| 597 | } |
| 598 | } else { |
| 599 | if(!preg_match('/[a-z][a-z][0-9]{2,}/', $group->name) && !in_array($group->name,$hidden_groups, TRUE)) { |
| 600 | if($group->role <= $userPrimaryGroup->role) { |
| 601 | $filteredGroups[] = $group; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | } |
| 607 | //reorder the $filteredGroups by $group->role number from highest int to lowest int. If the role is zero put it at the end of the array |
| 608 | usort($filteredGroups, function($a, $b) { |
| 609 | if($a->role == 0) { |
| 610 | return 1; |
| 611 | } elseif($b->role == 0) { |
| 612 | return -1; |
| 613 | } else { |
| 614 | return $b->role <=> $a->role; // Sort from highest to lowest role |
| 615 | } |
| 616 | }); |
| 617 | //error_log(count($filteredGroups)); |
| 618 | |
| 619 | // Get a list of all locales |
| 620 | $locale_list = $this->_app->site->getLocales(); |
| 621 | |
| 622 | // Determine which groups this user is a member of |
| 623 | $user_groups = $target_user->getGroups(); |
| 624 | foreach ($filteredGroups as $group){ |
| 625 | $group_id = $group->id; |
| 626 | $group_list[$group_id] = $group->export(); |
| 627 | if (isset($user_groups[$group_id])) |
| 628 | $group_list[$group_id]['member'] = true; |
| 629 | else |
| 630 | $group_list[$group_id]['member'] = false; |
| 631 | } |
| 632 | |
| 633 | if ($render == "modal") |
| 634 | $template = "components/user-info-modal.html"; |
| 635 | else |
| 636 | $template = "components/user-info-panel.html"; |
| 637 | |
| 638 | // Determine authorized fields |
| 639 | $fields = ['display_name', 'email', 'title', 'password', 'locale', 'groups', 'primary_group_id']; |
| 640 | $show_fields = []; |
| 641 | $disabled_fields = []; |
| 642 | $hidden_fields = []; |
| 643 | foreach ($fields as $field){ |
| 644 | if ($this->_app->user->checkAccess("update_account_setting", ["user" => $target_user, "property" => $field])) |
| 645 | $show_fields[] = $field; |
| 646 | else if ($this->_app->user->checkAccess("view_account_setting", ["user" => $target_user, "property" => $field])) |
| 647 | $disabled_fields[] = $field; |
| 648 | else |
| 649 | $hidden_fields[] = $field; |
| 650 | } |
| 651 | $dailyReportField = 0; |
| 652 | if($userPrimaryGroup->id == 9 || $userPrimaryGroup->id == 2) { |
| 653 | $dailyReportField = 1; |
| 654 | } |
| 655 | |
| 656 | // Always disallow editing username |
| 657 | $disabled_fields[] = "user_name"; |
| 658 | |
| 659 | // Hide password fields for editing user |
| 660 | $hidden_fields[] = "password"; |
| 661 | $hidden_fields = ["locale","primary_group_id", "match","registered-since", "title","password","passwordc"]; |
| 662 | |
| 663 | // Load validator rules |
| 664 | $schema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/user-update.json"); |
| 665 | $validators = new \Fortress\FormValidationAdapter($this->_app->translator, $schema); |
| 666 | |
| 667 | $this->_app->render($template, [ |
| 668 | "box_id" => $get['box_id'], |
| 669 | "box_title" => "Edit Employee", |
| 670 | "submit_button" => "Update Employee", |
| 671 | "form_action" => $this->_app->site->uri['public'] . "/users/u/$user_id", |
| 672 | "target_user" => $target_user, |
| 673 | "groups" => $group_list, |
| 674 | "locales" => $locale_list, |
| 675 | "employees" => $employees, |
| 676 | "dailyReportField" => $dailyReportField, |
| 677 | "fields" => [ |
| 678 | "disabled" => $disabled_fields, |
| 679 | "hidden" => $hidden_fields |
| 680 | ], |
| 681 | "buttons" => [ |
| 682 | "hidden" => [ |
| 683 | "edit", "enable", "delete", "activate" |
| 684 | ] |
| 685 | ], |
| 686 | "validators" => $validators->formValidationRulesJson(), |
| 687 | "typeNum" => $typeNum |
| 688 | ]); |
| 689 | } |
| 690 | |
| 691 | // Create new user |
| 692 | public function createUser(){ |
| 693 | $post = $this->_app->request->post(); |
| 694 | $typeNum = $post['typeNum']; |
| 695 | // DEBUG: view posted data |
| 696 | //error_log(print_r($post, true)); |
| 697 | $log = new \KLogger($_ENV['LOG_DIR']."/users_log.log", \KLogger::DEBUG); |
| 698 | |
| 699 | // Load the request schema |
| 700 | $requestSchema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/user-create.json"); |
| 701 | |
| 702 | // Get the alert message stream |
| 703 | $ms = $this->_app->alerts; |
| 704 | |
| 705 | // Access-controlled resource |
| 706 | if (!$this->_app->user->checkAccess('create_account')){ |
| 707 | //error_log("Access Denied"); |
| 708 | $ms->addMessageTranslated("danger", "ACCESS_DENIED"); |
| 709 | $this->_app->halt(403); |
| 710 | } |
| 711 | if(!isset($post['dailyReport'])) { |
| 712 | $post['dailyReport'] = 0; |
| 713 | } |
| 714 | if($post['dailyReport'] == 1) { |
| 715 | if(!$this->_app->user->checkAccess("create_account_setting", ["property" => "dailyReport"])) { |
| 716 | //error_log("Access: ".(String)$this->_app->user->checkAccess("create_account_setting", ["property" => "dailyReport"])); |
| 717 | $ms->addMessageTranslated("danger", "ACCESS_DENIED"); |
| 718 | $this->_app->halt(403); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | // Set up Fortress to process the request |
| 723 | $rf = new \Fortress\HTTPRequestFortress($ms, $requestSchema, $post); |
| 724 | |
| 725 | // Sanitize data |
| 726 | $rf->sanitize(); |
| 727 | |
| 728 | // Validate, and halt on validation errors. |
| 729 | $error = !$rf->validate(true); |
| 730 | |
| 731 | // Get the filtered data |
| 732 | $data = $rf->data(); |
| 733 | |
| 734 | //error_log(print_r($data['groups'], true)); |
| 735 | |
| 736 | // Remove csrf_token, password confirmation from object data |
| 737 | $rf->removeFields(['csrf_token, passwordc']); |
| 738 | |
| 739 | // Perform desired data transformations on required fields. Is this a feature we could add to Fortress? |
| 740 | $data['user_name'] = strtolower(trim($data['user_name'])); |
| 741 | $data['display_name'] = trim($data['display_name']); |
| 742 | $data['email'] = strtolower(trim($data['email'])); |
| 743 | $data['active'] = 1; |
| 744 | |
| 745 | // Check if username or email already exists |
| 746 | if (UserLoader::exists($data['user_name'], 'user_name')){ |
| 747 | $ms->addMessageTranslated("danger", "ACCOUNT_USERNAME_IN_USE", $data); |
| 748 | $error = true; |
| 749 | } |
| 750 | |
| 751 | if (UserLoader::exists($data['email'], 'email')){ |
| 752 | $ms->addMessageTranslated("danger", "ACCOUNT_EMAIL_IN_USE", $data); |
| 753 | $error = true; |
| 754 | } |
| 755 | $store = new \Store(); |
| 756 | if($store->createStore($typeNum)) { |
| 757 | $db = dbConnectByName($store->getDbName()); |
| 758 | } else { |
| 759 | //$ms->addMessageTranslated("danger", "ACCOUNT_EMAIL_IN_USE", $data); |
| 760 | $error = true; |
| 761 | } |
| 762 | $name = explode(" ", $data['display_name']); |
| 763 | if(!isset($name[1])) { |
| 764 | $ms->addMessageTranslated("danger", "Please Enter First Name Followed By a Space and a Last Name in the \"Full Name\" Field", $data); |
| 765 | $error = true; |
| 766 | } |
| 767 | |
| 768 | |
| 769 | // Halt on any validation errors |
| 770 | if ($error) { |
| 771 | $log->LogDebug("Validation Error"); |
| 772 | $this->_app->halt(400); |
| 773 | } |
| 774 | |
| 775 | // Get default primary group (is_default = GROUP_DEFAULT_PRIMARY) |
| 776 | $primaryGroup = GroupLoader::fetch(GROUP_DEFAULT_PRIMARY, "is_default"); |
| 777 | // Set default values if not specified or not authorized |
| 778 | if (!isset($data['locale']) || !$this->_app->user->checkAccess("create_account_setting", ["property" => "locale"])) |
| 779 | $data['locale'] = $this->_app->site->default_locale; |
| 780 | |
| 781 | if (!isset($data['title']) || !$this->_app->user->checkAccess("create_account_setting", ["property" => "title"])) { |
| 782 | // Set default title for new users |
| 783 | $data['title'] = $primaryGroup->new_user_title; |
| 784 | } |
| 785 | |
| 786 | if (!isset($data['primary_group_id']) || !$this->_app->user->checkAccess("create_account_setting", ["property" => "primary_group_id"])) { |
| 787 | $data['primary_group_id'] = $primaryGroup->id; |
| 788 | } |
| 789 | if($data['primary_group_id'] == 0 || !isset($data['primary_group_id'])) { |
| 790 | $data['primary_group_id'] = 5; |
| 791 | } |
| 792 | if($data['primary_group_id'] == 9) { |
| 793 | $data['dailyReport'] = 1; |
| 794 | } |
| 795 | |
| 796 | if (!isset($data['groups']) || !$this->_app->user->checkAccess("create_account_setting", ["property" => "groups"])) { |
| 797 | //error_log("Access: ".(String)$this->_app->user->checkAccess("create_account_setting", ["property" => "groups"])); |
| 798 | //error_log("Groups: ".count($data['groups'])); |
| 799 | $data['groups'] = GroupLoader::fetchAll(GROUP_DEFAULT, "is_default"); |
| 800 | } |
| 801 | //error_log(print_r($data['groups'], true)); |
| 802 | |
| 803 | // Hash password |
| 804 | $data['password'] = Authentication::hashPassword($data['password']); |
| 805 | $name = explode(" ", $data['display_name']); |
| 806 | // Create the user |
| 807 | $user = new User($data); |
| 808 | |
| 809 | |
| 810 | $highestRole = 0; |
| 811 | $highestGroup = 5; |
| 812 | $addAsBuyer = 0; |
| 813 | // Add user to selected groups |
| 814 | foreach ($data['groups'] as $group_id => $is_member) { |
| 815 | $log->LogDebug("GroupID: ".$group_id); |
| 816 | if ($is_member == "1"){ |
| 817 | $group = GroupLoader::fetch($group_id); |
| 818 | if (!$group) { |
| 819 | $log->LogError("GroupLoader::fetch returned false for group_id: $group_id"); |
| 820 | continue; |
| 821 | } |
| 822 | $log->LogDebug("Group Role: ".$group->role); |
| 823 | if($group->role > $highestRole) { |
| 824 | $highestRole = $group->role; |
| 825 | $highestGroup = $group_id; |
| 826 | $title = $group->name; |
| 827 | } |
| 828 | if($group_id == 6) { |
| 829 | $log->LogDebug("Found Buyer Group"); |
| 830 | $addAsBuyer = 1; |
| 831 | } |
| 832 | $user->addGroup($group_id); |
| 833 | } |
| 834 | } |
| 835 | $log->LogDebug("Add Buyer: ".$addAsBuyer); |
| 836 | |
| 837 | //ADD employee to employee table for the store |
| 838 | |
| 839 | $employeeId = null; |
| 840 | if($addAsBuyer) { |
| 841 | $query = $db->prepare("INSERT INTO employees (login, employeeFirstName, employeeLastName, active, password, source) VALUES(?,?,?,?,'','homegrown')"); |
| 842 | $result = $query->execute(array($data['user_name'], $name[0], $name[1], $addAsBuyer)); |
| 843 | if (!$result) { |
| 844 | $log->LogDebug("Failed Insert Employee"); |
| 845 | $this->_app->halt(400); |
| 846 | } |
| 847 | // Get the created employee ID for linking |
| 848 | $employeeId = $db->lastInsertId(); |
| 849 | } |
| 850 | |
| 851 | $user->primary_group_id = $highestGroup; |
| 852 | |
| 853 | |
| 854 | if(!isset($title) || $title == "") { |
| 855 | $user->title = "Buyer"; |
| 856 | } else { |
| 857 | $user->title = $title; |
| 858 | } |
| 859 | |
| 860 | $storeGroup = GroupLoader::fetch($typeNum, 'name'); |
| 861 | if (!$storeGroup) { |
| 862 | $log->LogError("Failed to fetch store group for typeNum: $typeNum"); |
| 863 | error_log("ERROR: UserController::createUser - GroupLoader::fetch returned false for typeNum: $typeNum"); |
| 864 | return false; |
| 865 | } |
| 866 | $user->addGroup($storeGroup->id); |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | // Store new user to database |
| 872 | $user->store(); |
| 873 | |
| 874 | // Create user-employee link if employee was created (Phase 12) |
| 875 | if ($employeeId && $typeNum) { |
| 876 | try { |
| 877 | $centralDb = dbConnectByName('kiosk_users'); |
| 878 | $linkManager = new \BuyerKiosk\UserEmployee\UserEmployeeLinkManager($centralDb); |
| 879 | $linkManager->createLink( |
| 880 | $user->id, |
| 881 | $typeNum, |
| 882 | $employeeId, |
| 883 | 'manual', |
| 884 | $this->_app->user->id |
| 885 | ); |
| 886 | $log->LogDebug("Created user-employee link: User {$user->id} -> Employee $employeeId in $typeNum"); |
| 887 | } catch (Exception $e) { |
| 888 | $log->LogError("Failed to create user-employee link: " . $e->getMessage()); |
| 889 | // Don't halt - link creation failure shouldn't prevent user creation |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | // Success message |
| 894 | $ms->addMessageTranslated("success", "ACCOUNT_CREATION_COMPLETE", $data); |
| 895 | } |
| 896 | public function createAdminUser(){ |
| 897 | $post = $this->_app->request->post(); |
| 898 | $typeNum = $post['typeNum']; |
| 899 | // DEBUG: view posted data |
| 900 | //error_log(print_r($post, true)); |
| 901 | |
| 902 | // Load the request schema |
| 903 | $requestSchema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/user-create-admin.json"); |
| 904 | |
| 905 | // Get the alert message stream |
| 906 | $ms = $this->_app->alerts; |
| 907 | |
| 908 | // Access-controlled resource |
| 909 | if (!$this->_app->user->checkAccess('create_account')){ |
| 910 | $ms->addMessageTranslated("danger", "ACCESS_DENIED"); |
| 911 | $this->_app->halt(403); |
| 912 | } |
| 913 | |
| 914 | // Set up Fortress to process the request |
| 915 | $rf = new \Fortress\HTTPRequestFortress($ms, $requestSchema, $post); |
| 916 | |
| 917 | // Sanitize data |
| 918 | $rf->sanitize(); |
| 919 | |
| 920 | // Validate, and halt on validation errors. |
| 921 | $error = !$rf->validate(true); |
| 922 | |
| 923 | // Get the filtered data |
| 924 | $data = $rf->data(); |
| 925 | |
| 926 | // Remove csrf_token, password confirmation from object data |
| 927 | $rf->removeFields(['csrf_token, passwordc']); |
| 928 | |
| 929 | // Perform desired data transformations on required fields. Is this a feature we could add to Fortress? |
| 930 | $data['user_name'] = strtolower(trim($data['user_name'])); |
| 931 | $data['display_name'] = trim($data['display_name']); |
| 932 | $data['email'] = strtolower(trim($data['email'])); |
| 933 | $data['active'] = 1; |
| 934 | |
| 935 | // Check if username or email already exists |
| 936 | if (UserLoader::exists($data['user_name'], 'user_name')){ |
| 937 | $ms->addMessageTranslated("danger", "ACCOUNT_USERNAME_IN_USE", $data); |
| 938 | $error = true; |
| 939 | } |
| 940 | |
| 941 | if (UserLoader::exists($data['email'], 'email')){ |
| 942 | $ms->addMessageTranslated("danger", "ACCOUNT_EMAIL_IN_USE", $data); |
| 943 | $error = true; |
| 944 | } |
| 945 | |
| 946 | // Halt on any validation errors |
| 947 | if ($error) { |
| 948 | $this->_app->halt(400); |
| 949 | } |
| 950 | |
| 951 | // Get default primary group (is_default = GROUP_DEFAULT_PRIMARY) |
| 952 | $primaryGroup = GroupLoader::fetch(GROUP_DEFAULT_PRIMARY, "is_default"); |
| 953 | |
| 954 | $data['dailyReport'] = 0; |
| 955 | // Set default values if not specified or not authorized |
| 956 | if (!isset($data['locale']) || !$this->_app->user->checkAccess("update_account_setting", ["property" => "locale"])) |
| 957 | $data['locale'] = $this->_app->site->default_locale; |
| 958 | |
| 959 | if (!isset($data['title']) || !$this->_app->user->checkAccess("update_account_setting", ["property" => "title"])) { |
| 960 | // Set default title for new users |
| 961 | $data['title'] = $primaryGroup->new_user_title; |
| 962 | } |
| 963 | |
| 964 | if (!isset($data['primary_group_id']) || !$this->_app->user->checkAccess("update_account_setting", ["property" => "primary_group_id"])) { |
| 965 | $data['primary_group_id'] = $primaryGroup->id; |
| 966 | } |
| 967 | if($data['primary_group_id'] == 9) { |
| 968 | $data['dailyReport'] = 1; |
| 969 | } |
| 970 | |
| 971 | if (!isset($data['groups']) || !$this->_app->user->checkAccess("update_account_setting", ["property" => "groups"])) { |
| 972 | $data['groups'] = GroupLoader::fetchAll(GROUP_DEFAULT, "is_default"); |
| 973 | } |
| 974 | |
| 975 | // Hash password |
| 976 | $data['password'] = Authentication::hashPassword($data['password']); |
| 977 | |
| 978 | // Create the user |
| 979 | $user = new User($data); |
| 980 | |
| 981 | // Add user to groups, including default primary group |
| 982 | //$user->addGroup($data['primary_group_id']); |
| 983 | foreach ($data['groups'] as $group_id => $is_member) { |
| 984 | if ($is_member == "1"){ |
| 985 | $user->addGroup($group_id); |
| 986 | } |
| 987 | } |
| 988 | // Store new user to database |
| 989 | $user->store(); |
| 990 | |
| 991 | // Success message |
| 992 | $ms->addMessageTranslated("success", "ACCOUNT_CREATION_COMPLETE", $data); |
| 993 | } |
| 994 | |
| 995 | public function updateUserPassword($user_id) { |
| 996 | $post = $this->_app->request->post(); |
| 997 | $requestSchema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/user-password.json"); |
| 998 | $ms = $this->_app->alerts; |
| 999 | |
| 1000 | // Get the target user |
| 1001 | $target_user = UserLoader::fetch($user_id); |
| 1002 | |
| 1003 | // Only the master account can edit the master account! |
| 1004 | if (($target_user->id == $this->_app->config('user_id_master')) && $this->_app->user->id != $this->_app->config('user_id_master')) { |
| 1005 | $ms->addMessageTranslated("danger", "ACCESS_DENIED"); |
| 1006 | $this->_app->halt(403); |
| 1007 | } |
| 1008 | |
| 1009 | // Remove csrf_token |
| 1010 | unset($post['csrf_token']); |
| 1011 | |
| 1012 | if (!$this->_app->user->checkAccess('update_account_setting', ['user' => $target_user, 'property' => 'password'])){ |
| 1013 | $ms->addMessageTranslated("danger", "ACCESS_DENIED"); |
| 1014 | $this->_app->halt(403); |
| 1015 | } |
| 1016 | |
| 1017 | // Set up Fortress to process the request |
| 1018 | $rf = new \Fortress\HTTPRequestFortress($ms, $requestSchema, $post); |
| 1019 | |
| 1020 | // Sanitize |
| 1021 | $rf->sanitize(); |
| 1022 | |
| 1023 | // Validate, and halt on validation errors. |
| 1024 | if (!$rf->validate()) { |
| 1025 | $ms->addMessageTranslated("danger", "NO_DATA"); |
| 1026 | $this->_app->halt(400); |
| 1027 | } |
| 1028 | |
| 1029 | $data = $rf->data(); |
| 1030 | // Update the user and generate success messages |
| 1031 | |
| 1032 | $target_user->password = Authentication::hashPassword($data['password']); |
| 1033 | |
| 1034 | $ms->addMessageTranslated("success", "ACCOUNT_DETAILS_UPDATED", ["user_name" => $target_user->user_name]); |
| 1035 | $target_user->store(); |
| 1036 | |
| 1037 | } |
| 1038 | public function updateStorePassword($typeNum) { |
| 1039 | $post = $this->_app->request->post(); |
| 1040 | $requestSchema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/user-password.json"); |
| 1041 | $ms = $this->_app->alerts; |
| 1042 | |
| 1043 | // Get the target user |
| 1044 | $target_user = UserLoader::fetch($typeNum, "user_name"); |
| 1045 | |
| 1046 | // Only the master account can edit the master account! |
| 1047 | if (($target_user->id == $this->_app->config('user_id_master')) && $this->_app->user->id != $this->_app->config('user_id_master')) { |
| 1048 | $ms->addMessageTranslated("danger", "ACCESS_DENIED"); |
| 1049 | $this->_app->halt(403); |
| 1050 | } |
| 1051 | |
| 1052 | // Remove csrf_token |
| 1053 | unset($post['csrf_token']); |
| 1054 | |
| 1055 | if (!$this->_app->user->checkAccess('update_account_setting', ['user' => $target_user, 'property' => 'password'])){ |
| 1056 | $ms->addMessageTranslated("danger", "ACCESS_DENIED"); |
| 1057 | $this->_app->halt(403); |
| 1058 | } |
| 1059 | |
| 1060 | // Set up Fortress to process the request |
| 1061 | $rf = new \Fortress\HTTPRequestFortress($ms, $requestSchema, $post); |
| 1062 | |
| 1063 | // Sanitize |
| 1064 | $rf->sanitize(); |
| 1065 | |
| 1066 | // Validate, and halt on validation errors. |
| 1067 | if (!$rf->validate()) { |
| 1068 | $ms->addMessageTranslated("danger", "NO_DATA"); |
| 1069 | $this->_app->halt(400); |
| 1070 | } |
| 1071 | |
| 1072 | $data = $rf->data(); |
| 1073 | // Update the user and generate success messages |
| 1074 | |
| 1075 | $target_user->password = Authentication::hashPassword($data['password']); |
| 1076 | |
| 1077 | $ms->addMessageTranslated("success", "ACCOUNT_DETAILS_UPDATED", ["user_name" => $target_user->user_name]); |
| 1078 | $target_user->store(); |
| 1079 | |
| 1080 | } |
| 1081 | // Update user details, enabled/disabled status, activation status, |
| 1082 | public function updateUser($user_id){ |
| 1083 | $post = $this->_app->request->post(); |
| 1084 | $typeNum = isset($post['typeNum']) ? $post['typeNum'] : null; |
| 1085 | //$log = new \KLogger($_ENV['LOG_DIR']."/users_log.log", \KLogger::DEBUG); |
| 1086 | |
| 1087 | //$log->LogDebug("TypeNum: ".$typeNum); |
| 1088 | // DEBUG: view posted data |
| 1089 | //error_log(print_r($post, true)); |
| 1090 | |
| 1091 | // Load the request schema |
| 1092 | $requestSchema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/user-update.json"); |
| 1093 | |
| 1094 | // Get the alert message stream |
| 1095 | $ms = $this->_app->alerts; |
| 1096 | |
| 1097 | // Get the target user |
| 1098 | $target_user = UserLoader::fetch($user_id); |
| 1099 | |
| 1100 | // Get the target user's groups |
| 1101 | $groups = $target_user->getGroups(); |
| 1102 | |
| 1103 | /* |
| 1104 | // Access control for entire page |
| 1105 | if (!$this->_app->user->checkAccess('uri_update_user')){ |
| 1106 | $ms->addMessageTranslated("danger", "ACCESS_DENIED"); |
| 1107 | $this->_app->halt(403); |
| 1108 | } |
| 1109 | */ |
| 1110 | |
| 1111 | // Only the master account can edit the master account! |
| 1112 | if (($target_user->id == $this->_app->config('user_id_master')) && $this->_app->user->id != $this->_app->config('user_id_master')) { |
| 1113 | $ms->addMessageTranslated("danger", "ACCESS_DENIED"); |
| 1114 | $this->_app->halt(403); |
| 1115 | } |
| 1116 | |
| 1117 | |
| 1118 | // Remove csrf_token |
| 1119 | unset($post['csrf_token']); |
| 1120 | |
| 1121 | // Check authorization for submitted fields, if the value has been changed |
| 1122 | foreach ($post as $name => $value) { |
| 1123 | if ($name == "groups" || (isset($target_user->$name) && $post[$name] != $target_user->$name)){ |
| 1124 | // Check authorization |
| 1125 | if (!$this->_app->user->checkAccess('update_account_setting', ['user' => $target_user, 'property' => $name])){ |
| 1126 | $ms->addMessageTranslated("danger", "ACCESS_DENIED"); |
| 1127 | $this->_app->halt(403); |
| 1128 | } |
| 1129 | } else if (!isset($target_user->$name) && $name !== "typeNum" && $name !== "oldEmployeeID") { |
| 1130 | //$log->LogDebug($name); |
| 1131 | $ms->addMessageTranslated("danger", "NO_DATA"); |
| 1132 | //error_log("No Data"); |
| 1133 | $this->_app->halt(400); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | // Check that we are not disabling the master account |
| 1138 | if (($target_user->id == $this->_app->config('user_id_master')) && isset($post['enabled']) && $post['enabled'] == "0"){ |
| 1139 | $ms->addMessageTranslated("danger", "ACCOUNT_DISABLE_MASTER"); |
| 1140 | $this->_app->halt(403); |
| 1141 | } |
| 1142 | //Check that we are not disabling an owner account |
| 1143 | if($target_user->primary_group_id == 9 && isset($post['enabled']) && $post['enabled'] == "0") { |
| 1144 | $ms->addMessageTranslated("danger", "Cannot Disable the Owner's Account. Contact Support if you really need to do this."); |
| 1145 | $this->_app->halt(403); |
| 1146 | } |
| 1147 | |
| 1148 | if (isset($post['email']) && $post['email'] != $target_user->email && UserLoader::exists($post['email'], 'email')){ |
| 1149 | $ms->addMessageTranslated("danger", "ACCOUNT_EMAIL_IN_USE", $post); |
| 1150 | //error_log("Email In Use"); |
| 1151 | $this->_app->halt(400); |
| 1152 | } |
| 1153 | |
| 1154 | // Set up Fortress to process the request |
| 1155 | $rf = new \Fortress\HTTPRequestFortress($ms, $requestSchema, $post); |
| 1156 | |
| 1157 | // Sanitize |
| 1158 | $rf->sanitize(); |
| 1159 | |
| 1160 | // Validate, and halt on validation errors. |
| 1161 | if (!$rf->validate()) { |
| 1162 | //error_log("Validate"); |
| 1163 | $this->_app->halt(400); |
| 1164 | } |
| 1165 | |
| 1166 | // Get the filtered data |
| 1167 | $data = $rf->data(); |
| 1168 | |
| 1169 | $highestRole = 0; |
| 1170 | $highestGroup = 5; |
| 1171 | $activate = 0; |
| 1172 | $groupCount = 0; |
| 1173 | // Update user groups |
| 1174 | $store = new \Store(); |
| 1175 | $store->createStore($typeNum); |
| 1176 | if ($store instanceof \Store && $store !== null && !isset($_POST['enabled'])) { |
| 1177 | $db = dbConnectByName($store->getDbName()); |
| 1178 | //$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
| 1179 | //$db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); |
| 1180 | |
| 1181 | |
| 1182 | if (isset($data['groups'])){ |
| 1183 | foreach ($data['groups'] as $group_id => $is_member) { |
| 1184 | if ($is_member == "1") { |
| 1185 | $group = GroupLoader::fetch($group_id); |
| 1186 | if($group->role > $highestRole) { |
| 1187 | $highestRole = $group->role; |
| 1188 | $highestGroup = $group_id; |
| 1189 | } |
| 1190 | if($group_id > 4 && $group_id < 10) { |
| 1191 | $groupCount++; |
| 1192 | } |
| 1193 | if($group_id == 6) { |
| 1194 | $activate = 1; |
| 1195 | } |
| 1196 | } |
| 1197 | if ($is_member == "1" && !isset($groups[$group_id])){ |
| 1198 | if($group_id == 6) { |
| 1199 | $activate = 1; |
| 1200 | } |
| 1201 | |
| 1202 | $target_user->addGroup($group_id); |
| 1203 | } else if ($is_member == "0" && isset($groups[$group_id])){ |
| 1204 | if($group_id == 6) { |
| 1205 | $activate = 0; |
| 1206 | } |
| 1207 | $target_user->removeGroup($group_id); |
| 1208 | } |
| 1209 | } |
| 1210 | unset($data['groups']); |
| 1211 | } |
| 1212 | |
| 1213 | if($groupCount == 0) { |
| 1214 | //$log->LogDebug("No Groups Selected"); |
| 1215 | // Get default primary group (is_default = GROUP_DEFAULT_PRIMARY) |
| 1216 | $primary_group = GroupLoader::fetch(2, "is_default"); |
| 1217 | $target_user->primary_group_id = $primary_group->id; |
| 1218 | // Get the default groups |
| 1219 | $default_groups = GroupLoader::fetchAll(2, "is_default"); |
| 1220 | foreach($default_groups as $group) { |
| 1221 | $target_user->addGroup($group->id); |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | //$log->LogDebug("Activate: ".$activate); |
| 1226 | $name = explode(" ", $target_user->display_name); |
| 1227 | $new_name = explode(" ", $data['display_name']); |
| 1228 | try { |
| 1229 | $stmt = $db->prepare("SELECT employeeID FROM employees WHERE employeeFirstName = :firstName AND employeeLastName = :lastName OR login = :username"); |
| 1230 | $stmt->bindValue(":firstName", $name[0]); |
| 1231 | $stmt->bindValue(":lastName", $name[1]); |
| 1232 | $stmt->bindValue(":username", $target_user->user_name); |
| 1233 | if($stmt->execute()) { |
| 1234 | $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
| 1235 | if($row && isset($row['employeeID']) && $row['employeeID'] > 0) { |
| 1236 | $stmt = $db->prepare("UPDATE employees SET employeeFirstName = :newFirstName, employeeLastName = :newLastName, active = :active WHERE employeeID = :id"); |
| 1237 | $stmt->bindValue(":newFirstName", $new_name[0]); |
| 1238 | $stmt->bindValue(":newLastName", $new_name[1]); |
| 1239 | $stmt->bindValue(":id", $row['employeeID']); |
| 1240 | $stmt->bindValue(":active", $activate); |
| 1241 | if(!$stmt->execute()) { |
| 1242 | //$log->LogDebug("Failed Update"); |
| 1243 | $this->_app->halt(400); |
| 1244 | } else { |
| 1245 | //$log->LogDebug("Employee Updated"); |
| 1246 | } |
| 1247 | } else { |
| 1248 | //$log->LogDebug($new_name[0]); |
| 1249 | //$log->LogDebug($new_name[1]); |
| 1250 | //$log->LogDebug($target_user->user_name); |
| 1251 | $query = $db->prepare("INSERT INTO employees (login, employeeFirstName, employeeLastName, active, password) VALUES(?,?,?,?,'')"); |
| 1252 | $result = $query->execute(array($target_user->user_name,$new_name[0],$new_name[1],(int)$activate)); |
| 1253 | if(!$result) { |
| 1254 | //$log->LogDebug("Failed Insert"); |
| 1255 | //error_log("Insert"); |
| 1256 | $this->_app->halt(400); |
| 1257 | } else { |
| 1258 | |
| 1259 | //$log->LogDebug("Employee Inserted"); |
| 1260 | } |
| 1261 | } |
| 1262 | } else { |
| 1263 | //error_log("Select"); |
| 1264 | //$log->LogDebug("Failed Select"); |
| 1265 | $this->_app->halt(400); |
| 1266 | } |
| 1267 | } catch (\PDOException $e) { |
| 1268 | //$log->LogError($e->getMessage()); |
| 1269 | //error_log($e->getMessage()); |
| 1270 | $this->_app->halt(400); |
| 1271 | } |
| 1272 | |
| 1273 | $data['primary_group_id'] = $highestGroup; |
| 1274 | } else { |
| 1275 | //$log->LogDebug("Store Failed"); |
| 1276 | //$this->_app->halt(403); |
| 1277 | } |
| 1278 | |
| 1279 | |
| 1280 | // Update the user and generate success messages |
| 1281 | foreach ($data as $name => $value){ |
| 1282 | if ($value != $target_user->$name){ |
| 1283 | $update = 1; |
| 1284 | // Custom success messages (optional) |
| 1285 | if ($name == "enabled") { |
| 1286 | if ($value == "1") { |
| 1287 | $target_user->UpdateAsBuyerInEmployeeTable(1); |
| 1288 | $ms->addMessageTranslated("success", "ACCOUNT_ENABLE_SUCCESSFUL", ["user_name" => $target_user->user_name]); |
| 1289 | } else { |
| 1290 | $target_user->UpdateAsBuyerInEmployeeTable(0); |
| 1291 | $ms->addMessageTranslated("success", "ACCOUNT_DISABLE_SUCCESSFUL", ["user_name" => $target_user->user_name]); |
| 1292 | } |
| 1293 | } |
| 1294 | if ($name == "active") { |
| 1295 | $ms->addMessageTranslated("success", "ACCOUNT_MANUALLY_ACTIVATED", ["user_name" => $target_user->user_name]); |
| 1296 | } |
| 1297 | if($name == "dailyReport") { |
| 1298 | if(!($this->_app->user->primary_group_id == 9 || $this->_app->user->primary_group_id == 2)) { |
| 1299 | $ms->addMessageTranslated("error", "Only Owner Level Accounts Can Update The Daily Email Field"); |
| 1300 | $update = 0; |
| 1301 | } |
| 1302 | } |
| 1303 | if($update) { |
| 1304 | $target_user->$name = $value; |
| 1305 | } |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | $ms->addMessageTranslated("success", "ACCOUNT_DETAILS_UPDATED", ["user_name" => $target_user->user_name]); |
| 1310 | $target_user->store(); |
| 1311 | |
| 1312 | } |
| 1313 | |
| 1314 | // Delete a user, cleaning up their group memberships and any user-specific authorization rules |
| 1315 | public function deleteUser($user_id){ |
| 1316 | $post = $this->_app->request->post(); |
| 1317 | |
| 1318 | // Get the target user |
| 1319 | $target_user = UserLoader::fetch($user_id); |
| 1320 | |
| 1321 | // Get the alert message stream |
| 1322 | $ms = $this->_app->alerts; |
| 1323 | |
| 1324 | $target_user->UpdateAsBuyerInEmployeeTable(0); |
| 1325 | |
| 1326 | // Check authorization |
| 1327 | if (!$this->_app->user->checkAccess('delete_account', ['user' => $target_user])){ |
| 1328 | $ms->addMessageTranslated("danger", "ACCESS_DENIED"); |
| 1329 | $this->_app->halt(403); |
| 1330 | } |
| 1331 | |
| 1332 | // Check that we are not disabling the master account |
| 1333 | if (($target_user->id == $this->_app->config('user_id_master'))){ |
| 1334 | $ms->addMessageTranslated("danger", "ACCOUNT_DELETE_MASTER"); |
| 1335 | $this->_app->halt(403); |
| 1336 | } |
| 1337 | |
| 1338 | $ms->addMessageTranslated("success", "ACCOUNT_DELETION_SUCCESSFUL", ["user_name" => $target_user->user_name]); |
| 1339 | $target_user->delete(); |
| 1340 | unset($target_user); |
| 1341 | } |
| 1342 | |
| 1343 | } |
| 1344 | ?> |