-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangePasswordHandler.php
More file actions
110 lines (90 loc) · 3.02 KB
/
Copy pathChangePasswordHandler.php
File metadata and controls
110 lines (90 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* @file plugins/generic/changePassword/ChangePasswordHandler.php
*
* Copyright (c) 2025 Abdul Hadi M.Alaidi
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ChangePasswordHandler
* @ingroup plugins_generic_changePassword
*
* @brief Handler for change password operations
*/
namespace APP\plugins\generic\changePassword;
use PKP\handler\PKPHandler;
use PKP\core\JSONMessage;
use PKP\security\authorization\ContextAccessPolicy;
use PKP\core\PKPRequest;
use PKP\security\Role;
use APP\facades\Repo;
use PKP\security\Validation;
class ChangePasswordHandler extends PKPHandler
{
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();
// Add role assignments for authorization
$this->addRoleAssignment(
[Role::ROLE_ID_SITE_ADMIN, Role::ROLE_ID_MANAGER],
['index', 'changePassword', 'updatePassword']
);
}
/**
* @copydoc PKPHandler::authorize()
*/
public function authorize($request, &$args, $roleAssignments)
{
// Require user to be logged in
$this->addPolicy(new \PKP\security\authorization\UserRequiredPolicy($request));
// Use context access policy for journal-level access
$this->addPolicy(new ContextAccessPolicy($request, $roleAssignments));
return parent::authorize($request, $args, $roleAssignments);
}
/**
* Display the change password page with user list
*
* @param array $args
* @param PKPRequest $request
*
* @return string Template output
*/
public function index($args, $request)
{
return '';
}
/**
* Handle password update operation
*
* @param array $args
* @param PKPRequest $request
*
* @return JSONMessage
*/
public function updatePassword($args, $request)
{
// Get the user ID and new password from the request
$userId = $request->getUserVar('userId');
$newPassword = $request->getUserVar('newPassword');
// Validate input
if (!$userId || !$newPassword) {
return new JSONMessage(false, __('plugins.generic.changePassword.error.missingData'));
}
if (strlen($newPassword) < 6) {
return new JSONMessage(false, __('plugins.generic.changePassword.error.passwordTooShort'));
}
// Get the user from the repository
$user = Repo::user()->get($userId);
if (!$user) {
return new JSONMessage(false, __('plugins.generic.changePassword.error.userNotFound'));
}
// Encrypt and set the new password
$user->setPassword(Validation::encryptCredentials($user->getUsername(), $newPassword));
// Save the user
Repo::user()->edit($user, []);
// Return success message
return new JSONMessage(true, __('plugins.generic.changePassword.passwordChanged'));
}
}