fix password protection in list wp sites

This commit is contained in:
usmannasir
2025-04-08 05:18:51 +05:00
parent 5fb7d65c1f
commit 0796bb5bf8
2 changed files with 152 additions and 1 deletions

View File

@@ -8468,6 +8468,126 @@ app.controller('WPsiteHome', function ($scope, $http, $timeout, $compile, $windo
}
};
$scope.updateSetting = function(site, setting) {
var settingMap = {
'search-indexing': 'searchIndex',
'debugging': 'debugging',
'password-protection': 'passwordProtection',
'maintenance-mode': 'maintenanceMode'
};
// Toggle the state before sending request
site[settingMap[setting]] = site[settingMap[setting]] === 1 ? 0 : 1;
var data = {
siteId: site.id,
setting: setting,
value: site[settingMap[setting]]
};
var config = {
headers: {
'X-CSRFToken': getCookie('csrftoken')
}
};
$http.post('/websites/UpdateWPSettings', data, config).then(function(response) {
if (response.data.status === 1) {
new PNotify({
title: 'Success',
text: 'Setting updated successfully.',
type: 'success'
});
if (setting === 'password-protection' && site[settingMap[setting]] === 1) {
// Show password protection modal if enabling
site.PPUsername = "";
site.PPPassword = "";
$scope.currentWP = site;
$('#passwordProtectionModal').modal('show');
}
} else {
// Revert the change if update failed
site[settingMap[setting]] = site[settingMap[setting]] === 1 ? 0 : 1;
new PNotify({
title: 'Error',
text: response.data.error_message || 'Failed to update setting.',
type: 'error'
});
}
}).catch(function(error) {
// Revert the change on error
site[settingMap[setting]] = site[settingMap[setting]] === 1 ? 0 : 1;
new PNotify({
title: 'Error',
text: 'Connection failed while updating setting.',
type: 'error'
});
});
};
$scope.submitPasswordProtection = function() {
if (!$scope.currentWP) {
new PNotify({
title: 'Error!',
text: 'No WordPress site selected.',
type: 'error'
});
return;
}
if (!$scope.currentWP.PPUsername || !$scope.currentWP.PPPassword) {
new PNotify({
title: 'Error!',
text: 'Please provide both username and password',
type: 'error'
});
return;
}
var data = {
siteId: $scope.currentWP.id,
setting: 'password-protection',
value: 'enable',
PPUsername: $scope.currentWP.PPUsername,
PPPassword: $scope.currentWP.PPPassword
};
var config = {
headers: {
'X-CSRFToken': getCookie('csrftoken')
}
};
$('#passwordProtectionModal').modal('hide');
$http.post('/websites/UpdateWPSettings', data, config).then(function(response) {
if (response.data.status === 1) {
new PNotify({
title: 'Success!',
text: 'Password protection enabled successfully!',
type: 'success'
});
location.reload();
} else {
new PNotify({
title: 'Error!',
text: response.data.error_message || 'Failed to enable password protection',
type: 'error'
});
// Revert the checkbox state
$scope.currentWP.passwordProtection = !$scope.currentWP.passwordProtection;
}
}).catch(function(error) {
new PNotify({
title: 'Error!',
text: 'Could not connect to server',
type: 'error'
});
// Revert the checkbox state
$scope.currentWP.passwordProtection = !$scope.currentWP.passwordProtection;
});
};
});

View File

@@ -344,7 +344,9 @@
<div class="col-sm-6">
<div class="checkbox">
<label>
<input type="checkbox" ng-model="site.passwordProtection" ng-change="updateSetting(site, 'password-protection')">
<input type="checkbox"
ng-click="updateSetting(site, 'password-protection')"
ng-checked="site.passwordProtection == 1">
Password protection
</label>
</div>
@@ -384,6 +386,35 @@
</div>
</div>
<div class="modal fade" id="passwordProtectionModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Password Protection</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" ng-model="currentWP.PPUsername" required>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" ng-model="currentWP.PPPassword" required>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" ng-click="submitPasswordProtection()">Enable Protection</button>
</div>
</div>
</div>
</div>
<style>
.wp-site-item {
border: 1px solid #ddd;