PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.2
Defender Security – Malware Scanner, Login Security & Firewall v6.2.2
6.3.0 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 All 141 releases
defender-security / src / component / audit / class-password-audit.php

class-password-audit.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.2, at src/component/audit/class-password-audit.php

111 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles user password related audit events.
4 *
5 * @package WP_Defender\Component\Audit
6 */
7
8 namespace WP_Defender\Component\Audit;
9
10 use WP_User;
11 use WP_Defender\Traits\User;
12 use WP_Defender\Model\Audit_Log;
13
14 /**
15 * Handles user password related audit events, distinguishing between self and admin scenarios.
16 *
17 * @since 5.8.0
18 */
19 class Password_Audit extends Audit_Event {
20 use User;
21
22 public const ACTION_PASSWORD_CHANGED = 'password-changed';
23 public const CONTEXT_PASSWORD = 'password';
24
25 /**
26 * Returns the hooks to monitor password events.
27 *
28 * @return array
29 */
30 public function get_hooks(): array {
31 return array(
32 'password_reset' => array(
33 'args' => array( 'user' ),
34 'event_type' => Audit_Log::EVENT_TYPE_USER,
35 'context' => self::CONTEXT_PASSWORD,
36 'action_type' => self::ACTION_PASSWORD_CHANGED,
37 'callback' => array( $this, 'password_reset' ),
38 ),
39 );
40 }
41
42 /**
43 * Logs a user profile update event.
44 *
45 * @param int $user_id The ID of the user whose profile is being updated.
46 *
47 * @return bool|array False if the user is not found, otherwise an array containing the log message.
48 */
49 public function profile_update( int $user_id ): bool|array {
50 $user = get_user_by( 'id', $user_id );
51 if ( ! $user ) {
52 return false;
53 }
54
55 return $this->build_log( $user );
56 }
57
58 /**
59 * Helper to build the log message.
60 *
61 * @param WP_User $user The user object.
62 *
63 * @return array
64 */
65 public function build_log( WP_User $user ): array {
66 $username = $user->user_login;
67 $blog_name = is_multisite() ? '[' . get_bloginfo( 'name' ) . ']' : '';
68 $current_user = get_current_user_id();
69 // User changed own password.
70 if ( 0 === $current_user || $current_user === $user->ID ) {
71 return array(
72 sprintf(
73 /* translators: 1: Blog name, 2: Username */
74 esc_html__( '%1$s %2$s changed their password', 'defender-security' ),
75 $blog_name,
76 $username
77 ),
78 self::CONTEXT_PASSWORD,
79 );
80 }
81 // Admin changed another user's password.
82 $current_display = $this->get_user_display( $current_user );
83
84 return array(
85 sprintf(
86 /* translators: 1: Blog name, 2: Admin username, 3: Affected username */
87 esc_html__( '%1$s %2$s changed the password for user: %3$s', 'defender-security' ),
88 $blog_name,
89 $current_display,
90 $username
91 ),
92 self::CONTEXT_PASSWORD,
93 );
94 }
95
96 /**
97 * Callback for logging password reset events( via reset link )
98 *
99 * @param WP_User $user The user.
100 *
101 * @return bool|array
102 */
103 public function password_reset( $user ): bool|array {
104 if ( ! $user instanceof WP_User ) {
105 return false;
106 }
107
108 return $this->build_log( $user );
109 }
110 }
111