diff --git a/application/config/config.sample.php b/application/config/config.sample.php index db8660972..b387eeeae 100644 --- a/application/config/config.sample.php +++ b/application/config/config.sample.php @@ -105,6 +105,7 @@ $config['qrzru_password'] = ''; | 'auth_header_create' False disables user creation if user doesn't exist | 'auth_header_value' Which header provides authenticated username | 'auth_header_text' Display text on login screen +| 'auth_header_club_id' Default club ID to add new users to */ $config['use_auth'] = true; @@ -118,6 +119,7 @@ $config['auth_header_enable'] = false; $config['auth_header_create'] = false; $config['auth_header_value'] = "HTTP_X-Username"; $config['auth_header_text'] = "Login with SSO"; +$config['auth_header_club_id'] = ""; /* |-------------------------------------------------------------------------- diff --git a/application/controllers/Header_auth.php b/application/controllers/Header_auth.php index 8037cc16f..a00f326b4 100644 --- a/application/controllers/Header_auth.php +++ b/application/controllers/Header_auth.php @@ -43,10 +43,33 @@ class Header_auth extends CI_Controller // Look up user by the header value $query = $this->user_model->get($username); if (!$query || $query->num_rows() !== 1) { - $this->session->set_flashdata('error', __('User not found.')); - redirect('user/login'); + + // Config check if create user + if ($this->config->item('auth_header_create')) { + $this->load->model('user_model'); + $club_id = $this->config->item('auth_header_club_id'); + $result = $this->user_model->add_minimal(username: $username, club_id: $club_id); + + switch ($result) { + case EUSERNAMEEXISTS: + $data['username_error'] = sprintf(__("Username %s already in use!"), '' . $this->input->post('user_name') . ''); + break; + case EEMAILEXISTS: + $data['email_error'] = sprintf(__("E-mail %s already in use!"), '' . $this->input->post('user_email') . ''); + break; + case EPASSWORDINVALID: + $data['password_error'] = __("Invalid Password!"); + break; + case OK: + redirect('header_auth/login'); + return; + } + } else { + $this->session->set_flashdata('error', __('User not found.')); + redirect('user/login'); + } } - + $user = $query->row(); @@ -75,6 +98,28 @@ class Header_auth extends CI_Controller ]; $this->input->set_cookie($cookie); + $this->load->model('user_model'); + // Get full user record + $user = $this->user_model->get($username)->row(); + + // Critical: Update session data + $this->user_model->update_session($user->user_id); + $this->user_model->set_last_seen($user->user_id); + + // Set essential session data + $this->session->set_userdata(array( + 'user_id' => $user->user_id, + 'user_name' => $user->user_name, + 'user_type' => $user->user_type, + 'user_stylesheet' => $user->user_stylesheet ?? 'bootstrap', + 'user_column1' => $user->user_column1 ?? 'Mode', + 'user_column2' => $user->user_column2 ?? 'RSTS', + 'user_column3' => $user->user_column3 ?? 'RSTR', + 'user_column4' => $user->user_column4 ?? 'Band', + 'user_column5' => $user->user_column5 ?? 'Country', + // Add other preferences as needed + )); + log_message('info', "User ID [{$user->user_id}] logged in via header auth."); redirect('dashboard'); } diff --git a/application/models/User_model.php b/application/models/User_model.php index 34385a7e6..8c9e66da4 100644 --- a/application/models/User_model.php +++ b/application/models/User_model.php @@ -323,6 +323,82 @@ class User_Model extends CI_Model { } } + /** + * FUNCTION: bool add_minimal($username, $firstname = null, $lastname = null, $callsign = null, $email = null, $club_id = null) + * Add a user with minimal required fields (username only) with option to add to club as user + */ + function add_minimal($username, $firstname = null, $lastname = null, $callsign = null, $email = null, $club_id = null) { + // Check that the username isn't already used + if(!$this->exists($username)) { + $data = array( + 'user_name' => xss_clean($username), + 'user_password' => bin2hex(random_bytes(16)), // Random password + 'user_email' => xss_clean($email) ?? '', + 'user_firstname' => xss_clean($firstname) ?? '', + 'user_lastname' => xss_clean($lastname) ?? '', + 'user_callsign' => strtoupper(xss_clean($callsign)) ?? '', + 'user_type' => 3, + 'user_locator' => '', + 'user_stylesheet' => 'darkly', + 'user_language' => 'english', + 'user_timezone' => '1', + 'user_date_format' => 'd/m/y', + 'user_measurement_base' => 'M', + 'user_column1' => 'Mode', + 'user_column2' => 'RSTS', + 'user_column3' => 'RSTR', + 'user_column4' => 'Band', + 'user_column5' => 'Country', + 'user_qso_end_times' => 0, + 'user_show_profile_image' => 0, + 'user_qth_lookup' => 0, + 'user_sota_lookup' => 0, + 'user_wwff_lookup' => 0, + 'user_pota_lookup' => 0, + 'user_show_notes' => 0, + 'user_quicklog' => 0, + 'user_quicklog_enter' => 0, + 'user_previous_qsl_type' => 0, + 'user_default_band' => 'All', + 'user_lotw_name' => '', + 'user_lotw_password' => '', + 'user_eqsl_name' => '', + 'user_eqsl_password' => '', + 'user_clublog_name' => '', + 'user_clublog_password' => '', + 'user_amsat_status_upload' => 0, + 'user_mastodon_url' => '', + ); + + // Check the email address isn't in use (if provided) + if($email && $this->exists_by_email($email)) { + return EEMAILEXISTS; + } + + // Generate user-slug + if (!$this->load->is_loaded('encryption')) { + $this->load->library('encryption'); + } + $user_slug_base = md5($this->encryption->encrypt($username)); + $user_slug = substr($user_slug_base, 0, USER_SLUG_LENGTH); + $data['slug'] = $user_slug; + + // Add user + $this->db->insert($this->config->item('auth_table'), $data); + $insert_id = $this->db->insert_id(); + + // Add user to club + if ($club_id && is_numeric($club_id)) { + $this->load->model('club_model'); + $this->club_model->alter_member($club_id, $insert_id, 3); + } + + return OK; + } else { + return EUSERNAMEEXISTS; + } + } + // FUNCTION: bool edit() // Edit a user function edit($fields) { diff --git a/install/config/config.php b/install/config/config.php index 71c65a986..e90ff13da 100644 --- a/install/config/config.php +++ b/install/config/config.php @@ -105,6 +105,7 @@ $config['qrzru_password'] = '%qrzru_password%'; | 'auth_header_create' False disables user creation if user doesn't exist | 'auth_header_value' Which header provides authenticated username | 'auth_header_text' Display text on login screen +| 'auth_header_club_id' Default club ID to add new users to */ $config['use_auth'] = true; @@ -118,6 +119,7 @@ $config['auth_header_enable'] = false; $config['auth_header_create'] = false; $config['auth_header_value'] = "HTTP_X-Username"; $config['auth_header_text'] = "Login with SSO"; +$config['auth_header_club_id'] = ""; /* |--------------------------------------------------------------------------