2025-01-02 10:22:23 +01:00
< ? php if ( ! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );
class Club extends CI_Controller
{
/**
* Permission Levels :
* 9 - Officer
* 3 - Member
*
* These permission levels are independent of the codeigniter permission levels and managed in the club_permissions table !
2026-02-25 12:01:29 +01:00
* https :// docs . wavelog . org / admin - guide / administration / clubstations /
2025-01-02 10:22:23 +01:00
*/
/**
* @ var array $permissions
*/
2025-01-02 11:03:04 +01:00
private $permissions ;
function __construct () {
parent :: __construct ();
2026-08-01 09:33:03 +02:00
$this -> load -> model ( 'club_model' );
$this -> permissions = $this -> club_model -> permission_levels ();
2025-01-02 11:03:04 +01:00
}
2025-01-02 10:22:23 +01:00
2026-08-01 22:22:37 +02:00
public function index () {
2025-01-02 10:22:23 +01:00
// nothing to display
redirect ( 'dashboard' );
}
public function permissions ( $club_id ) {
$this -> load -> library ( 'form_validation' );
$cid = $this -> security -> xss_clean ( $club_id );
$club = $this -> user_model -> get_by_id ( $cid ) -> row ();
2026-03-28 21:32:59 -05:00
// Check if club managed by SSO
2026-08-01 09:33:03 +02:00
$ssoManaged = $this -> club_model -> is_sso_managed ( $club_id );
2026-03-28 21:32:59 -05:00
2026-08-01 22:22:37 +02:00
if ( ! $this -> user_model -> authorize ( 99 ) && ( ! $this -> user_model -> authorize ( 3 ) || ! $this -> club_model -> club_authorize ( 9 , $cid ))) {
$this -> session -> set_flashdata ( 'error' , __ ( " You're not allowed to do that! " ));
redirect ( 'dashboard' );
}
2025-01-02 10:22:23 +01:00
if ( ! is_numeric ( $cid )) {
$this -> session -> set_flashdata ( 'error' , __ ( " Invalid User ID! " ));
redirect ( 'user' );
}
if ( $club -> clubstation != 1 ) {
$this -> session -> set_flashdata ( 'error' , __ ( " This user is not a club station. " ));
redirect ( 'user' );
}
$data [ 'page_title' ] = __ ( " Club Permissions " );
$data [ 'club' ] = $club ;
$data [ 'club_members' ] = $this -> club_model -> get_club_members ( $cid );
$data [ 'permissions' ] = $this -> permissions ;
2026-03-28 21:32:59 -05:00
$data [ 'sso_managed' ] = $ssoManaged ;
2025-01-02 10:22:23 +01:00
$footerData = [];
$footerData [ 'scripts' ] = [
2026-02-13 16:58:57 +01:00
'assets/js/sections/club_permissions.js' ,
2025-01-02 10:22:23 +01:00
];
$this -> load -> view ( 'interface_assets/header' , $data );
$this -> load -> view ( 'club/permissions' );
$this -> load -> view ( 'interface_assets/footer' , $footerData );
}
public function get_users () {
2026-08-01 22:22:37 +02:00
if ( $this -> input -> method () !== 'post' ) {
$this -> session -> set_flashdata ( 'error' , __ ( " Invalid request method. " ));
redirect ( 'dashboard' );
}
$cid = $this -> input -> post ( 'club_id' , true );
if ( ! $this -> user_model -> authorize ( 99 ) && ( ! $this -> user_model -> authorize ( 3 ) || ! $this -> club_model -> club_authorize ( 9 , $cid ))) {
$this -> session -> set_flashdata ( 'error' , __ ( " You're not allowed to do that! " ));
redirect ( 'dashboard' );
2025-01-02 10:22:23 +01:00
}
$query = ( string ) $this -> input -> post ( 'query' , true ) ? ? '' ;
if ( empty ( $query )) {
header ( 'Content-Type: application/json' );
echo json_encode ([]);
return ;
}
$users = $this -> user_model -> search_users ( $query );
$result = [];
if ( $users != false ) {
foreach ( $users -> result () as $user ) {
$result [] = [
'user_id' => $user -> user_id ,
2025-01-02 11:40:14 +01:00
'user_name' => $user -> user_name ,
2025-01-02 10:22:23 +01:00
'user_callsign' => $user -> user_callsign ,
'user_firstname' => $user -> user_firstname ,
'user_lastname' => $user -> user_lastname
];
}
}
header ( 'Content-Type: application/json' );
echo json_encode ( $result );
}
public function alter_member () {
$club_id = $this -> input -> post ( 'club_id' , true );
$user_id = $this -> input -> post ( 'user_id' , true );
$p_level = $this -> input -> post ( 'permission' , true );
2026-08-01 22:22:37 +02:00
if ( ! $this -> user_model -> authorize ( 99 ) && ( ! $this -> user_model -> authorize ( 3 ) || ! $this -> club_model -> club_authorize ( 9 , $club_id ))) {
$this -> session -> set_flashdata ( 'error' , __ ( " You're not allowed to do that! " ));
redirect ( 'dashboard' );
}
2025-01-02 10:22:23 +01:00
if ( ! is_numeric ( $club_id )) {
$this -> session -> set_flashdata ( 'error' , __ ( " Invalid Club ID! " ));
redirect ( 'dashboard' );
}
$this -> club_model -> alter_member ( $club_id , $user_id , $p_level );
if ( $this -> input -> post ( 'notify_user' , true ) == 'on' ) {
2026-08-01 09:33:03 +02:00
if ( ! $this -> club_model -> notify_member ( $user_id , $club_id , $this -> input -> post ( 'notify_message' , true ))) {
2025-01-02 10:22:23 +01:00
$this -> session -> set_flashdata ( 'error' , __ ( " User could not be notified. Please check your email settings. " ));
}
}
$this -> session -> set_flashdata ( 'success' , __ ( " Club member permissions have been updated. " ));
redirect ( 'club/permissions/' . $club_id );
}
public function delete_member () {
$club_id = $this -> input -> post ( 'club_id' , true );
$user_id = $this -> input -> post ( 'user_id' , true );
2026-08-01 22:22:37 +02:00
if ( ! $this -> user_model -> authorize ( 99 ) && ( ! $this -> user_model -> authorize ( 3 ) || ! $this -> club_model -> club_authorize ( 9 , $club_id ))) {
$this -> session -> set_flashdata ( 'error' , __ ( " You're not allowed to do that! " ));
redirect ( 'dashboard' );
}
2025-01-02 10:22:23 +01:00
if ( ! is_numeric ( $club_id )) {
$this -> session -> set_flashdata ( 'error' , __ ( " Invalid Club ID! " ));
redirect ( 'dashboard' );
}
if ( $this -> club_model -> delete_member ( $club_id , $user_id )) {
$this -> session -> set_flashdata ( 'success' , __ ( " User removed from club. " ));
} else {
$this -> session -> set_flashdata ( 'error' , __ ( " User could not be removed from club. " ));
}
redirect ( 'club/permissions/' . $club_id );
}
2026-08-01 18:36:04 +00:00
/**
* Set one permission level for several members at once .
* Officer ( 9 ) or instance admin ( 99 ) only .
* Endpoint : POST / club / batch_alter_members
*/
public function batch_alter_members () {
if ( $this -> input -> method () !== 'post' ) {
$this -> session -> set_flashdata ( 'error' , __ ( " Invalid request method. " ));
redirect ( 'dashboard' );
}
$club_id = $this -> input -> post ( 'club_id' , true );
$p_level = $this -> input -> post ( 'permission' , true );
// Batch ops require officer or instance admin - no level 6
2026-08-01 22:22:37 +02:00
if ( ! $this -> user_model -> authorize ( 99 ) && ( ! $this -> user_model -> authorize ( 3 ) || ! $this -> club_model -> club_authorize ( 9 , $club_id ))) {
2026-08-01 18:36:04 +00:00
$this -> session -> set_flashdata ( 'error' , __ ( " You're not allowed to do that! " ));
redirect ( 'dashboard' );
}
2026-08-01 22:22:37 +02:00
if ( ! is_numeric ( $club_id )) {
$this -> session -> set_flashdata ( 'error' , __ ( " Invalid Club ID! " ));
redirect ( 'dashboard' );
}
2026-08-01 18:36:04 +00:00
$ids = array_filter ( array_map ( 'intval' , explode ( ',' , ( string ) $this -> input -> post ( 'ids' , true ))));
if ( empty ( $ids )) {
$this -> session -> set_flashdata ( 'error' , __ ( " No members selected. " ));
redirect ( 'club/permissions/' . $club_id );
}
// Validate target ids against real membership before any write
$valid = $this -> club_model -> filter_valid_member_ids ( $club_id , $ids );
if ( empty ( $valid )) {
$this -> session -> set_flashdata ( 'error' , __ ( " None of the selected users are members of this club. " ));
redirect ( 'club/permissions/' . $club_id );
}
// Last-officer orphan protection: demoting to <9 may leave zero officers.
// Only an instance admin may create that state.
if (( int ) $p_level < 9
&& ! $this -> user_model -> authorize ( 99 )
&& $this -> club_model -> remaining_officers ( $club_id , $valid ) < 1 ) {
$this -> session -> set_flashdata ( 'error' , __ ( " Cannot proceed: this would remove the last Club Officer. An instance administrator must perform this action. " ));
redirect ( 'club/permissions/' . $club_id );
}
$result = $this -> club_model -> batch_alter_members ( $club_id , $valid , $p_level );
if ( $result === false ) {
$this -> session -> set_flashdata ( 'error' , __ ( " Club member permissions could not be updated. " ));
redirect ( 'club/permissions/' . $club_id );
}
if ( $result === 0 ) {
$this -> session -> set_flashdata ( 'error' , __ ( " No members were updated. " ));
redirect ( 'club/permissions/' . $club_id );
}
// Notify after a successful commit - email failure must not roll back DB
if ( $this -> input -> post ( 'notify_user' , true ) == 'on' ) {
foreach ( $valid as $uid ) {
if ( ! $this -> club_model -> notify_member ( $uid , $club_id , 'modified_member' )) {
$this -> session -> set_flashdata ( 'error' , __ ( " User could not be notified. Please check your email settings. " ));
}
}
}
$this -> session -> set_flashdata ( 'success' , sprintf ( _ngettext ( " %d member updated. " , " %d members updated. " , $result ), $result ));
redirect ( 'club/permissions/' . $club_id );
}
/**
* Remove several members from a club at once .
* Officer ( 9 ) or instance admin ( 99 ) only .
* Endpoint : POST / club / batch_delete_members
*/
public function batch_delete_members () {
if ( $this -> input -> method () !== 'post' ) {
$this -> session -> set_flashdata ( 'error' , __ ( " Invalid request method. " ));
redirect ( 'dashboard' );
}
$club_id = $this -> input -> post ( 'club_id' , true );
2026-08-01 22:22:37 +02:00
2026-08-01 18:36:04 +00:00
// Batch ops require officer or instance admin - no level 6
2026-08-01 22:22:37 +02:00
if ( ! $this -> user_model -> authorize ( 99 ) && ( ! $this -> user_model -> authorize ( 3 ) || ! $this -> club_model -> club_authorize ( 9 , $club_id ))) {
2026-08-01 18:36:04 +00:00
$this -> session -> set_flashdata ( 'error' , __ ( " You're not allowed to do that! " ));
redirect ( 'dashboard' );
}
2026-08-01 22:22:37 +02:00
if ( ! is_numeric ( $club_id )) {
$this -> session -> set_flashdata ( 'error' , __ ( " Invalid Club ID! " ));
redirect ( 'dashboard' );
}
2026-08-01 18:36:04 +00:00
$ids = array_filter ( array_map ( 'intval' , explode ( ',' , ( string ) $this -> input -> post ( 'ids' , true ))));
if ( empty ( $ids )) {
$this -> session -> set_flashdata ( 'error' , __ ( " No members selected. " ));
redirect ( 'club/permissions/' . $club_id );
}
// Validate target ids against real membership before any write
$valid = $this -> club_model -> filter_valid_member_ids ( $club_id , $ids );
if ( empty ( $valid )) {
$this -> session -> set_flashdata ( 'error' , __ ( " None of the selected users are members of this club. " ));
redirect ( 'club/permissions/' . $club_id );
}
// Last-officer orphan protection: deleting may leave zero officers.
// Only an instance admin may create that state.
if ( ! $this -> user_model -> authorize ( 99 )
&& $this -> club_model -> remaining_officers ( $club_id , $valid ) < 1 ) {
$this -> session -> set_flashdata ( 'error' , __ ( " Cannot proceed: this would remove the last Club Officer. An instance administrator must perform this action. " ));
redirect ( 'club/permissions/' . $club_id );
}
$result = $this -> club_model -> batch_delete_members ( $club_id , $valid );
if ( $result === false ) {
$this -> session -> set_flashdata ( 'error' , __ ( " Users could not be removed from club. " ));
redirect ( 'club/permissions/' . $club_id );
}
if ( $result === 0 ) {
$this -> session -> set_flashdata ( 'error' , __ ( " No members were removed. " ));
redirect ( 'club/permissions/' . $club_id );
}
$this -> session -> set_flashdata ( 'success' , sprintf ( _ngettext ( " %d member removed. " , " %d members removed. " , $result ), $result ));
redirect ( 'club/permissions/' . $club_id );
}
2025-01-02 10:22:23 +01:00
public function switch_modal () {
2026-08-01 09:33:03 +02:00
2025-01-02 10:22:23 +01:00
$this -> load -> library ( 'encryption' );
$cid = $this -> input -> post ( 'club_id' , true );
$data [ 'club_callsign' ] = $this -> input -> post ( 'club_callsign' , true );
$user_id = $this -> session -> userdata ( 'user_id' );
if ( ! $this -> club_model -> club_authorize ( 3 , $cid )) {
$this -> session -> set_flashdata ( 'error' , __ ( " You're not allowed to do that! " ));
redirect ( 'dashboard' );
}
2026-08-01 22:22:37 +02:00
if ( ! is_numeric ( $cid )) {
$this -> session -> set_flashdata ( 'error' , __ ( " Invalid Club ID! " ));
redirect ( 'dashboard' );
}
2025-01-02 10:22:23 +01:00
$data [ 'impersonate_hash' ] = $this -> encryption -> encrypt ( $user_id . '/' . $cid . '/' . time ());
$this -> load -> view ( 'club/clubswitch_modal' , $data );
}
2025-01-02 11:55:22 +00:00
}