mirror of
https://github.com/usmannasir/cyberpanel.git
synced 2026-03-10 06:10:14 +01:00
Fix account switcher: send fromAccount with every API call instead of relying solely on session
This commit is contained in:
@@ -149,7 +149,12 @@ app.controller('webmailCtrl', ['$scope', '$http', '$sce', '$timeout', function($
|
||||
// ── Helper ───────────────────────────────────────────────
|
||||
function apiCall(url, data, callback, errback) {
|
||||
var config = {headers: {'X-CSRFToken': getCookie('csrftoken')}};
|
||||
$http.post(url, data || {}, config).then(function(resp) {
|
||||
var payload = data || {};
|
||||
// Always send current account so backend uses the right email
|
||||
if ($scope.currentEmail && !payload.fromAccount) {
|
||||
payload.fromAccount = $scope.currentEmail;
|
||||
}
|
||||
$http.post(url, payload, config).then(function(resp) {
|
||||
if (callback) callback(resp.data);
|
||||
}, function(err) {
|
||||
console.error('API error:', url, err);
|
||||
@@ -180,23 +185,28 @@ app.controller('webmailCtrl', ['$scope', '$http', '$sce', '$timeout', function($
|
||||
$scope.switchAccount = function() {
|
||||
var newEmail = $scope.currentEmail;
|
||||
if (!newEmail) return;
|
||||
|
||||
// Reset view state immediately
|
||||
$scope.currentFolder = 'INBOX';
|
||||
$scope.currentPage = 1;
|
||||
$scope.openMsg = null;
|
||||
$scope.viewMode = 'list';
|
||||
$scope.messages = [];
|
||||
$scope.contacts = [];
|
||||
$scope.filteredContacts = [];
|
||||
$scope.sieveRules = [];
|
||||
|
||||
apiCall('/webmail/api/switchAccount', {email: newEmail}, function(data) {
|
||||
if (data.status === 1) {
|
||||
$scope.currentFolder = 'INBOX';
|
||||
$scope.currentPage = 1;
|
||||
$scope.openMsg = null;
|
||||
$scope.viewMode = 'list';
|
||||
$scope.messages = [];
|
||||
$scope.contacts = [];
|
||||
$scope.filteredContacts = [];
|
||||
$scope.sieveRules = [];
|
||||
$scope.loadFolders();
|
||||
$scope.loadSettings();
|
||||
} else {
|
||||
notify(data.error_message || 'Failed to switch account', 'error');
|
||||
console.error('switchAccount failed:', data);
|
||||
}
|
||||
}, function() {
|
||||
notify('Failed to switch account', 'error');
|
||||
}, function(err) {
|
||||
notify('Failed to switch account: ' + (err.status || 'unknown error'), 'error');
|
||||
console.error('switchAccount HTTP error:', err);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -488,6 +498,7 @@ app.controller('webmailCtrl', ['$scope', '$http', '$sce', '$timeout', function($
|
||||
stopDraftAutoSave();
|
||||
|
||||
var fd = new FormData();
|
||||
fd.append('fromAccount', $scope.currentEmail || '');
|
||||
fd.append('to', $scope.compose.to);
|
||||
fd.append('cc', $scope.compose.cc || '');
|
||||
fd.append('bcc', $scope.compose.bcc || '');
|
||||
@@ -508,7 +519,7 @@ app.controller('webmailCtrl', ['$scope', '$http', '$sce', '$timeout', function($
|
||||
}).then(function(resp) {
|
||||
$scope.sending = false;
|
||||
if (resp.data.status === 1) {
|
||||
notify('Message sent.');
|
||||
notify('Message sent from ' + (resp.data.sentFrom || 'unknown'));
|
||||
$scope.viewMode = 'list';
|
||||
$scope.loadMessages();
|
||||
} else {
|
||||
|
||||
@@ -175,6 +175,15 @@
|
||||
<h3>{% trans "Compose" %}</h3>
|
||||
</div>
|
||||
<form name="composeForm" ng-submit="sendMessage()" class="wm-compose-form">
|
||||
<div class="wm-field">
|
||||
<label>{% trans "From" %}</label>
|
||||
<select ng-model="currentEmail"
|
||||
ng-options="a for a in managedAccounts"
|
||||
ng-change="switchAccount()"
|
||||
ng-if="managedAccounts.length > 1" class="form-control"></select>
|
||||
<input type="text" value="{$ currentEmail $}" class="form-control" readonly
|
||||
ng-if="managedAccounts.length <= 1">
|
||||
</div>
|
||||
<div class="wm-field">
|
||||
<label>{% trans "To" %}</label>
|
||||
<input type="text" ng-model="compose.to" class="form-control"
|
||||
@@ -435,6 +444,6 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="{% static 'webmail/webmail.js' %}?v=3"></script>
|
||||
<script src="{% static 'webmail/webmail.js' %}?v=5"></script>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
@@ -46,6 +46,18 @@ class WebmailManager:
|
||||
return self.request.POST.dict()
|
||||
|
||||
def _get_email(self):
|
||||
# Check for explicit email in POST body (from account switcher)
|
||||
# This ensures the correct account is used even if session is stale
|
||||
try:
|
||||
data = json.loads(self.request.body)
|
||||
explicit = data.get('fromAccount', '')
|
||||
if explicit:
|
||||
accounts = self._get_managed_accounts()
|
||||
if explicit in accounts:
|
||||
self.request.session['webmail_email'] = explicit
|
||||
return explicit
|
||||
except Exception:
|
||||
pass
|
||||
return self.request.session.get('webmail_email')
|
||||
|
||||
def _get_master_config(self):
|
||||
@@ -324,6 +336,14 @@ class WebmailManager:
|
||||
|
||||
def apiSendMessage(self):
|
||||
try:
|
||||
# For multipart forms, check fromAccount in POST data
|
||||
if self.request.content_type and 'multipart' in self.request.content_type:
|
||||
from_account = self.request.POST.get('fromAccount', '')
|
||||
if from_account:
|
||||
accounts = self._get_managed_accounts()
|
||||
if from_account in accounts:
|
||||
self.request.session['webmail_email'] = from_account
|
||||
|
||||
email_addr = self._get_email()
|
||||
if not email_addr:
|
||||
return self._error('Not logged in.')
|
||||
@@ -390,7 +410,7 @@ class WebmailManager:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return self._success({'messageId': result['message_id']})
|
||||
return self._success({'messageId': result['message_id'], 'sentFrom': email_addr})
|
||||
except Exception as e:
|
||||
return self._error(str(e))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user