| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file contains the class for the session protection settings. |
| 4 |
* |
| 5 |
* @package WP_Defender\Model\Setting |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Model\Setting; |
| 9 |
|
| 10 |
use Calotes\Model\Setting; |
| 11 |
use WP_Defender\Traits\User; |
| 12 |
use WP_Defender\Component\Security_Tweaks\Login_Duration; |
| 13 |
|
| 14 |
/** |
| 15 |
* This class handles the settings for session protection in WP Defender. |
| 16 |
*/ |
| 17 |
class Session_Protection extends Setting { |
| 18 |
use User; |
| 19 |
|
| 20 |
/** |
| 21 |
* Feature status |
| 22 |
* |
| 23 |
* @defender_property |
| 24 |
* @var bool |
| 25 |
*/ |
| 26 |
public $enabled = false; |
| 27 |
|
| 28 |
/** |
| 29 |
* Idle Session Timeout. |
| 30 |
* |
| 31 |
* Set the idle timeout period (in hours) after which inactive sessions will be automatically logged out. |
| 32 |
* |
| 33 |
* @defender_property |
| 34 |
* @var int |
| 35 |
*/ |
| 36 |
public $idle_timeout = 1; |
| 37 |
|
| 38 |
/** |
| 39 |
* User Session Lock. |
| 40 |
* |
| 41 |
* Lock and automatically end user sessions if any selected properties change. |
| 42 |
* |
| 43 |
* @defender_property |
| 44 |
* @var array |
| 45 |
*/ |
| 46 |
public $lock_properties = array(); |
| 47 |
|
| 48 |
/** |
| 49 |
* Login duration. |
| 50 |
* |
| 51 |
* Set a login duration override for all user sessions. Once the duration expires, users will be logged out automatically. |
| 52 |
* The process related to Login Duration will be added in the next releases. |
| 53 |
* |
| 54 |
* @defender_property |
| 55 |
* @var int |
| 56 |
*/ |
| 57 |
public $login_duration = Login_Duration::DEFAULT_DAYS; |
| 58 |
|
| 59 |
/** |
| 60 |
* Select the user roles to apply above session protection to. |
| 61 |
* |
| 62 |
* @defender_property |
| 63 |
* @var array |
| 64 |
*/ |
| 65 |
public $user_roles = array(); |
| 66 |
|
| 67 |
/** |
| 68 |
* Option name. |
| 69 |
* |
| 70 |
* @var string |
| 71 |
*/ |
| 72 |
protected $table = 'wd_session_protection_settings'; |
| 73 |
|
| 74 |
/** |
| 75 |
* Initializes the object before loading. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
protected function before_load(): void { |
| 80 |
$this->login_duration = $this->get_default_duration(); |
| 81 |
$this->user_roles = array( 'administrator' ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Has lock properties? |
| 86 |
* |
| 87 |
* @return bool |
| 88 |
*/ |
| 89 |
public function has_properties(): bool { |
| 90 |
return ! empty( $this->lock_properties ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Is property locked? |
| 95 |
* |
| 96 |
* @param string $property The property to check. |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
public function is_property_locked( $property ): bool { |
| 101 |
return in_array( $property, $this->lock_properties, true ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Return the module slug. |
| 106 |
* |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
public static function get_module_slug(): string { |
| 110 |
return 'session-protection'; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Determines whether session protection is currently active. |
| 115 |
* |
| 116 |
* @return bool True if enabled and roles are set; otherwise, false. |
| 117 |
*/ |
| 118 |
public function is_active(): bool { |
| 119 |
return $this->enabled && count( $this->user_roles ) > 0; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Retrieves the default login duration. |
| 124 |
* |
| 125 |
* @return int Default login duration in days. |
| 126 |
*/ |
| 127 |
public function get_default_duration(): int { |
| 128 |
$tweak_duration = wd_di()->get( Login_Duration::class )->get_tweak_duration(); |
| 129 |
|
| 130 |
return ! empty( $tweak_duration ) |
| 131 |
? $tweak_duration |
| 132 |
: $this->login_duration; |
| 133 |
} |
| 134 |
} |
| 135 |
|