PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.3
Defender Security – Malware Scanner, Login Security & Firewall v6.2.3
6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 3.1.1 All 140 releases
defender-security / src / model / setting / class-session-protection.php

class-session-protection.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.3, at src/model/setting/class-session-protection.php

166 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Maximum allowed idle timeout in hours (~24.8 days, aligned with JS setTimeout 32-bit limit).
22 */
23 public const MAX_IDLE_TIMEOUT_HOURS = 596;
24
25 /**
26 * Feature status
27 *
28 * @defender_property
29 * @var bool
30 */
31 public $enabled = false;
32
33 /**
34 * Idle Session Timeout.
35 *
36 * Set the idle timeout period (in hours) after which inactive sessions will be automatically logged out.
37 *
38 * @defender_property
39 * @var int
40 */
41 public $idle_timeout = 1;
42
43 /**
44 * User Session Lock.
45 *
46 * Lock and automatically end user sessions if any selected properties change.
47 *
48 * @defender_property
49 * @var array
50 */
51 public $lock_properties = array();
52
53 /**
54 * Login duration.
55 *
56 * Set a login duration override for all user sessions. Once the duration expires, users will be logged out automatically.
57 * The process related to Login Duration will be added in the next releases.
58 *
59 * @defender_property
60 * @var int
61 */
62 public $login_duration = Login_Duration::DEFAULT_DAYS;
63
64 /**
65 * Select the user roles to apply above session protection to.
66 *
67 * @defender_property
68 * @var array
69 */
70 public $user_roles = array();
71
72 /**
73 * Option name.
74 *
75 * @var string
76 */
77 protected $table = 'wd_session_protection_settings';
78
79 /**
80 * Initializes the object before loading.
81 *
82 * @return void
83 */
84 protected function before_load(): void {
85 $this->login_duration = $this->get_default_duration();
86 $this->user_roles = array( 'administrator' );
87 }
88
89 /**
90 * Clamps idle_timeout to MAX_IDLE_TIMEOUT_HOURS to guard against legacy values saved without a limit.
91 *
92 * @return void
93 */
94 protected function after_load(): void {
95 if ( $this->idle_timeout > self::MAX_IDLE_TIMEOUT_HOURS ) {
96 $this->idle_timeout = self::MAX_IDLE_TIMEOUT_HOURS;
97 }
98 }
99
100 /**
101 * Validates that idle_timeout does not exceed the allowed maximum.
102 *
103 * @return void
104 */
105 protected function after_validate(): void {
106 if ( $this->idle_timeout > self::MAX_IDLE_TIMEOUT_HOURS ) {
107 $this->errors['idle_timeout'] = sprintf(
108 /* translators: %d: maximum allowed hours */
109 esc_html__( 'Idle timeout cannot exceed %d hours (30 days).', 'defender-security' ),
110 self::MAX_IDLE_TIMEOUT_HOURS
111 );
112 }
113 }
114
115 /**
116 * Has lock properties?
117 *
118 * @return bool
119 */
120 public function has_properties(): bool {
121 return array() !== $this->lock_properties;
122 }
123
124 /**
125 * Is property locked?
126 *
127 * @param string $property The property to check.
128 *
129 * @return bool
130 */
131 public function is_property_locked( $property ): bool {
132 return in_array( $property, $this->lock_properties, true );
133 }
134
135 /**
136 * Return the module slug.
137 *
138 * @return string
139 */
140 public static function get_module_slug(): string {
141 return 'session-protection';
142 }
143
144 /**
145 * Determines whether session protection is currently active.
146 *
147 * @return bool True if enabled and roles are set; otherwise, false.
148 */
149 public function is_active(): bool {
150 return $this->enabled && count( $this->user_roles ) > 0;
151 }
152
153 /**
154 * Retrieves the default login duration.
155 *
156 * @return int Default login duration in days.
157 */
158 public function get_default_duration(): int {
159 $tweak_duration = wd_di()->get( Login_Duration::class )->get_tweak_duration();
160
161 return $tweak_duration > 0
162 ? $tweak_duration
163 : $this->login_duration;
164 }
165 }
166