public function addUser($user_id) { // First, load current users for event $this->getUsers(); // Return if user already in event if (isset($this->_users[$user_id])) { return $this; } // Next, check that the requested user actually exists if (!UserLoader::exists($user_id)) { throw new \Exception("The specified user_id ({$user_id}) does not exist."); } // Ok, add to the list of users $this->_users[$user_id] = UserLoader::fetch($user_id); return $this; }
/** * Processes the request to update an existing user's details, including enabled/disabled status and activation status. * * Processes the request from the user update form, checking that: * 1. The target user's new email address, if specified, is not already in use; * 2. The logged-in user has the necessary permissions to update the posted field(s); * 3. We're not trying to disable the master account; * 4. The submitted data is valid. * This route requires authentication. * Request type: POST * @param int $user_id the id of the user to edit. * @see formUserEdit */ public function updateUser($user_id) { $post = $this->_app->request->post(); // Load the request schema $requestSchema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/user-update.json"); // Get the alert message stream $ms = $this->_app->alerts; // Get the target user $target_user = User::find($user_id); // Get the target user's groups $groups = $target_user->getGroups(); /* // Access control for entire page if (!$this->_app->user->checkAccess('uri_update_user')){ $ms->addMessageTranslated("danger", "ACCESS_DENIED"); $this->_app->halt(403); } */ // Only the master account can edit the master account! if ($target_user->id == $this->_app->config('user_id_master') && $this->_app->user->id != $this->_app->config('user_id_master')) { $ms->addMessageTranslated("danger", "ACCESS_DENIED"); $this->_app->halt(403); } // Remove csrf_token unset($post['csrf_token']); // Set up Fortress to process the request $rf = new \Fortress\HTTPRequestFortress($ms, $requestSchema, $post); if (isset($post['passwordc'])) { unset($post['passwordc']); } // Check authorization for submitted fields, if the value has been changed foreach ($post as $name => $value) { if ($name == "groups" || isset($target_user->{$name}) && $post[$name] != $target_user->{$name}) { // Check authorization if (!$this->_app->user->checkAccess('update_account_setting', ['user' => $target_user, 'property' => $name])) { $ms->addMessageTranslated("danger", "ACCESS_DENIED"); $this->_app->halt(403); } } else { if (!isset($target_user->{$name})) { $ms->addMessageTranslated("danger", "NO_DATA"); $this->_app->halt(400); } } } // Check that we are not disabling the master account if ($target_user->id == $this->_app->config('user_id_master') && isset($post['flag_enabled']) && $post['flag_enabled'] == "0") { $ms->addMessageTranslated("danger", "ACCOUNT_DISABLE_MASTER"); $this->_app->halt(403); } // Check that the email address is not in use if (isset($post['email']) && $post['email'] != $target_user->email && UserLoader::exists($post['email'], 'email')) { $ms->addMessageTranslated("danger", "ACCOUNT_EMAIL_IN_USE", $post); $this->_app->halt(400); } // Sanitize $rf->sanitize(); // Validate, and halt on validation errors. if (!$rf->validate()) { $this->_app->halt(400); } // Remove passwordc $rf->removeFields(['passwordc']); // Get the filtered data $data = $rf->data(); // Update user groups if (isset($data['groups'])) { foreach ($data['groups'] as $group_id => $is_member) { if ($is_member == "1" && !isset($groups[$group_id])) { $target_user->addGroup($group_id); } else { if ($is_member == "0" && isset($groups[$group_id])) { $target_user->removeGroup($group_id); } } } unset($data['groups']); } // Hash password if (isset($data['password'])) { $data['password'] = Authentication::hashPassword($data['password']); } // Update the user and generate success messages foreach ($data as $name => $value) { if ($value != $target_user->{$name}) { $target_user->{$name} = $value; // Custom success messages (optional) if ($name == "flag_enabled") { if ($value == "1") { $ms->addMessageTranslated("success", "ACCOUNT_ENABLE_SUCCESSFUL", ["user_name" => $target_user->user_name]); } else { $ms->addMessageTranslated("success", "ACCOUNT_DISABLE_SUCCESSFUL", ["user_name" => $target_user->user_name]); } } if ($name == "flag_verified") { $ms->addMessageTranslated("success", "ACCOUNT_MANUALLY_ACTIVATED", ["user_name" => $target_user->user_name]); } } } // If we're generating a password reset, create the corresponding event and shoot off an email if (isset($data['flag_password_reset']) && $data['flag_password_reset'] == "1") { // Recheck auth if (!$this->_app->user->checkAccess('update_account_setting', ['user' => $target_user, 'property' => 'flag_password_reset'])) { $ms->addMessageTranslated("danger", "ACCESS_DENIED"); $this->_app->halt(403); } // New password reset event - bypass any rate limiting $target_user->newEventPasswordReset(); $target_user->save(); // Email the user asking to confirm this change password request $twig = $this->_app->view()->getEnvironment(); $template = $twig->loadTemplate("mail/password-reset.twig"); $notification = new Notification($template); $notification->fromWebsite(); // Automatically sets sender and reply-to $notification->addEmailRecipient($target_user->email, $target_user->display_name, ["user" => $target_user, "request_date" => date("Y-m-d H:i:s")]); try { $notification->send(); } catch (\Exception\phpmailerException $e) { $ms->addMessageTranslated("danger", "MAIL_ERROR"); error_log('Mailer Error: ' . $e->errorMessage()); $this->_app->halt(500); } $ms->addMessageTranslated("success", "FORGOTPASS_REQUEST_SENT", ["user_name" => $target_user->user_name]); } $ms->addMessageTranslated("success", "ACCOUNT_DETAILS_UPDATED", ["user_name" => $target_user->user_name]); $target_user->save(); }
/** * Processes the request to update an existing user's details, including enabled/disabled status and activation status. * * Processes the request from the user update form, checking that: * 1. The target user's new email address, if specified, is not already in use; * 2. The logged-in user has the necessary permissions to update the posted field(s); * 3. We're not trying to disable the master account; * 4. The submitted data is valid. * This route requires authentication. * Request type: POST * @param int $user_id the id of the user to edit. * @see formUserEdit */ public function updateUser($user_id) { $post = $this->_app->request->post(); // Load the request schema $requestSchema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/user-update.json"); // Get the alert message stream $ms = $this->_app->alerts; // Get the target user $target_user = UserLoader::fetch($user_id); // Get the target user's groups $groups = $target_user->getGroups(); /* // Access control for entire page if (!$this->_app->user->checkAccess('uri_update_user')){ $ms->addMessageTranslated("danger", "ACCESS_DENIED"); $this->_app->halt(403); } */ // Only the master account can edit the master account! if ($target_user->id == $this->_app->config('user_id_master') && $this->_app->user->id != $this->_app->config('user_id_master')) { $ms->addMessageTranslated("danger", "ACCESS_DENIED"); $this->_app->halt(403); } // Remove csrf_token unset($post['csrf_token']); // Check authorization for submitted fields, if the value has been changed foreach ($post as $name => $value) { if ($name == "groups" || isset($target_user->{$name}) && $post[$name] != $target_user->{$name}) { // Check authorization if (!$this->_app->user->checkAccess('update_account_setting', ['user' => $target_user, 'property' => $name])) { $ms->addMessageTranslated("danger", "ACCESS_DENIED"); $this->_app->halt(403); } } else { if (!isset($target_user->{$name})) { $ms->addMessageTranslated("danger", "NO_DATA"); $this->_app->halt(400); } } } // Check that we are not disabling the master account if ($target_user->id == $this->_app->config('user_id_master') && isset($post['enabled']) && $post['enabled'] == "0") { $ms->addMessageTranslated("danger", "ACCOUNT_DISABLE_MASTER"); $this->_app->halt(403); } if (isset($post['email']) && $post['email'] != $target_user->email && UserLoader::exists($post['email'], 'email')) { $ms->addMessageTranslated("danger", "ACCOUNT_EMAIL_IN_USE", $post); $this->_app->halt(400); } // Set up Fortress to process the request $rf = new \Fortress\HTTPRequestFortress($ms, $requestSchema, $post); // Sanitize $rf->sanitize(); // Validate, and halt on validation errors. if (!$rf->validate()) { $this->_app->halt(400); } // Get the filtered data $data = $rf->data(); // Update user groups if (isset($data['groups'])) { foreach ($data['groups'] as $group_id => $is_member) { if ($is_member == "1" && !isset($groups[$group_id])) { $target_user->addGroup($group_id); } else { if ($is_member == "0" && isset($groups[$group_id])) { $target_user->removeGroup($group_id); } } } unset($data['groups']); } // Update the user and generate success messages foreach ($data as $name => $value) { if ($value != $target_user->{$name}) { $target_user->{$name} = $value; // Custom success messages (optional) if ($name == "enabled") { if ($value == "1") { $ms->addMessageTranslated("success", "ACCOUNT_ENABLE_SUCCESSFUL", ["user_name" => $target_user->user_name]); } else { $ms->addMessageTranslated("success", "ACCOUNT_DISABLE_SUCCESSFUL", ["user_name" => $target_user->user_name]); } } if ($name == "active") { $ms->addMessageTranslated("success", "ACCOUNT_MANUALLY_ACTIVATED", ["user_name" => $target_user->user_name]); } } } $ms->addMessageTranslated("success", "ACCOUNT_DETAILS_UPDATED", ["user_name" => $target_user->user_name]); $target_user->store(); }
public function accountSettings() { // Load the request schema $requestSchema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/account-settings.json"); // Get the alert message stream $ms = $this->_app->alerts; // Access control for entire page if (!$this->_app->user->checkAccess('uri_account_settings')) { $ms->addMessageTranslated("danger", "ACCESS_DENIED"); $this->_app->halt(403); } $data = $this->_app->request->post(); // Remove csrf_token unset($data['csrf_token']); // Check current password if (!isset($data['passwordcheck']) || !$this->_app->user->verifyPassword($data['passwordcheck'])) { $ms->addMessageTranslated("danger", "ACCOUNT_PASSWORD_INVALID"); $this->_app->halt(403); } // Validate new email, if specified if (isset($data['email']) && $data['email'] != $this->_app->user->email) { // Check authorization if (!$this->_app->user->checkAccess('update_account_setting', ['user' => $this->_app->user, 'property' => 'email'])) { $ms->addMessageTranslated("danger", "ACCESS_DENIED"); $this->_app->halt(403); } // Check if address is in use if (UserLoader::exists($data['email'], 'email')) { $ms->addMessageTranslated("danger", "ACCOUNT_EMAIL_IN_USE", $data); $this->_app->halt(400); } } else { $data['email'] = $this->_app->user->email; } // Validate locale, if specified if (isset($data['locale']) && $data['locale'] != $this->_app->user->locale) { // Check authorization if (!$this->_app->user->checkAccess('update_account_setting', ['user' => $this->_app->user, 'property' => 'locale'])) { $ms->addMessageTranslated("danger", "ACCESS_DENIED"); $this->_app->halt(403); } // Validate locale if (!in_array($data['locale'], $this->_app->site->getLocales())) { $ms->addMessageTranslated("danger", "ACCOUNT_SPECIFY_LOCALE"); $this->_app->halt(400); } } else { $data['locale'] = $this->_app->user->locale; } // Validate display_name, if specified if (isset($data['display_name']) && $data['display_name'] != $this->_app->user->display_name) { // Check authorization if (!$this->_app->user->checkAccess('update_account_setting', ['user' => $this->_app->user, 'property' => 'display_name'])) { $ms->addMessageTranslated("danger", "ACCESS_DENIED"); $this->_app->halt(403); } } else { $data['display_name'] = $this->_app->user->display_name; } // Validate password, if specified and not empty if (isset($data['password']) && !empty($data['password'])) { // Check authorization if (!$this->_app->user->checkAccess('update_account_setting', ['user' => $this->_app->user, 'property' => 'password'])) { $ms->addMessageTranslated("danger", "ACCESS_DENIED"); $this->_app->halt(403); } } else { // Do not pass to model if no password is specified unset($data['password']); unset($data['passwordc']); } // Set up Fortress to validate the request $rf = new \Fortress\HTTPRequestFortress($ms, $requestSchema, $data); // Validate if (!$rf->validate()) { $this->_app->halt(400); } // If a new password was specified, hash it if (isset($data['password'])) { $data['password'] = Authentication::hashPassword($data['password']); } // Remove passwordc, passwordcheck unset($data['passwordc']); unset($data['passwordcheck']); // Looks good, let's update with new values! foreach ($data as $name => $value) { $this->_app->user->{$name} = $value; } $this->_app->user->store(); $ms->addMessageTranslated("success", "ACCOUNT_SETTINGS_UPDATED"); }
public function setupMasterAccount() { $post = $this->_app->request->post(); // Get the alert message stream $ms = $this->_app->alerts; // Check the honeypot. 'spiderbro' is not a real field, it is hidden on the main page and must be submitted with its default value for this to be processed. if (!$post['spiderbro'] || $post['spiderbro'] != "http://") { error_log("Possible spam received:" . print_r($this->_app->request->post(), true)); $ms->addMessage("danger", "Aww hellllls no!"); $this->_app->halt(500); // Don't let on about why the request failed ;-) } // Do not allow registering a master account if one has already been created if (UserLoader::exists($this->_app->config('user_id_master'))) { $ms->addMessageTranslated("danger", "MASTER_ACCOUNT_EXISTS"); $this->_app->halt(403); } // Check the configuration token if ($post['root_account_config_token'] != $this->_app->site->root_account_config_token) { $ms->addMessageTranslated("danger", "CONFIG_TOKEN_MISMATCH"); $this->_app->halt(403); } // Load the request schema $requestSchema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/register.json"); // Set up Fortress to process the request $rf = new \Fortress\HTTPRequestFortress($ms, $requestSchema, $post); // Sanitize data $rf->sanitize(); // Validate, and halt on validation errors. $error = !$rf->validate(true); // Get the filtered data $data = $rf->data(); // Remove configuration token, password confirmation from object data $rf->removeFields(['root_account_config_token', 'passwordc']); // Perform desired data transformations. Is this a feature we could add to Fortress? $data['user_name'] = strtolower(trim($data['user_name'])); $data['display_name'] = trim($data['display_name']); $data['email'] = strtolower(trim($data['email'])); $data['active'] = 1; $data['locale'] = $this->_app->site->default_locale; // Halt on any validation errors if ($error) { $this->_app->halt(400); } // Get default primary group (is_default = GROUP_DEFAULT_PRIMARY) $primaryGroup = GroupLoader::fetch(GROUP_DEFAULT_PRIMARY, "is_default"); $data['primary_group_id'] = $primaryGroup->id; // Set default title for new users $data['title'] = $primaryGroup->new_user_title; // Hash password $data['password'] = Authentication::hashPassword($data['password']); // Create the user $user = new User($data, $this->_app->config('user_id_master')); // Add user to default groups, including default primary group $defaultGroups = GroupLoader::fetchAll(GROUP_DEFAULT, "is_default"); $user->addGroup($primaryGroup->id); foreach ($defaultGroups as $group_id => $group) { $user->addGroup($group_id); } // Store new user to database, forcing it to insert the new user $user->store(true); // No activation required $ms->addMessageTranslated("success", "ACCOUNT_REGISTRATION_COMPLETE_TYPE1"); // Update install status $this->_app->site->install_status = "new"; $this->_app->site->root_account_config_token = ""; $this->_app->site->store(); }