mirror of
https://github.com/usmannasir/cyberpanel.git
synced 2025-11-08 06:16:08 +01:00
finish allow remote access for db user
This commit is contained in:
@@ -12,13 +12,15 @@ from plogical.acl import ACLManager
|
||||
import plogical.CyberCPLogFileWriter as logging
|
||||
from plogical.mysqlUtilities import mysqlUtilities
|
||||
from websiteFunctions.models import Websites
|
||||
from databases.models import Databases
|
||||
from databases.models import Databases, DBMeta
|
||||
import argparse
|
||||
from loginSystem.models import Administrator
|
||||
import plogical.randomPassword as randomPassword
|
||||
|
||||
class DatabaseManager:
|
||||
|
||||
REMOTE_ACCESS = 'remote_access'
|
||||
|
||||
def loadDatabaseHome(self, request = None, userID = None):
|
||||
try:
|
||||
return render(request, 'databases/index.html')
|
||||
@@ -223,7 +225,6 @@ class DatabaseManager:
|
||||
return ACLManager.loadErrorJson('changePasswordStatus', 0)
|
||||
|
||||
userName = data['dbUserName']
|
||||
dbPassword = data['dbPassword']
|
||||
|
||||
db = Databases.objects.filter(dbUser=userName)
|
||||
|
||||
@@ -233,20 +234,57 @@ class DatabaseManager:
|
||||
else:
|
||||
return ACLManager.loadErrorJson()
|
||||
|
||||
|
||||
res = mysqlUtilities.changePassword(userName, dbPassword)
|
||||
|
||||
if res == 0:
|
||||
data_ret = {'status': 0, 'changePasswordStatus': 0,'error_message': "Please see CyberPanel main log file."}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
|
||||
data_ret = {'status': 1, 'changePasswordStatus': 1, 'error_message': "None"}
|
||||
try:
|
||||
meta = DBMeta.objects.get(database=db[0], key=DatabaseManager.REMOTE_ACCESS)
|
||||
data_ret = {'status': 1, 'dbHost': json.loads(meta.value)['remoteIP']}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
|
||||
except BaseException as msg:
|
||||
data_ret = {'status': 0, 'changePasswordStatus': 0, 'error_message': str(msg)}
|
||||
data_ret = {'status': 1, 'dbHost': 'localhost'}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
|
||||
except BaseException as msg:
|
||||
data_ret = {'status': 0,'error_message': str(msg)}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
|
||||
def allowRemoteIP(self, userID = None, data = None):
|
||||
try:
|
||||
currentACL = ACLManager.loadedACL(userID)
|
||||
|
||||
if ACLManager.currentContextPermission(currentACL, 'listDatabases') == 0:
|
||||
return ACLManager.loadErrorJson('changePasswordStatus', 0)
|
||||
|
||||
userName = data['dbUserName']
|
||||
remoteIP = data['remoteIP']
|
||||
|
||||
db = Databases.objects.filter(dbUser=userName)
|
||||
|
||||
admin = Administrator.objects.get(pk=userID)
|
||||
if ACLManager.checkOwnership(db[0].website.domain, admin, currentACL) == 1:
|
||||
pass
|
||||
else:
|
||||
return ACLManager.loadErrorJson()
|
||||
|
||||
mysqlUtilities.allowRemoteAccess(db[0].dbName, userName, remoteIP)
|
||||
|
||||
metaData = {'remoteIP': remoteIP}
|
||||
|
||||
try:
|
||||
meta = DBMeta.objects.get(database=db[0], key=DatabaseManager.REMOTE_ACCESS)
|
||||
meta.value = json.dumps(metaData)
|
||||
meta.save()
|
||||
except:
|
||||
DBMeta(database=db[0], value = json.dumps(metaData), key=DatabaseManager.REMOTE_ACCESS).save()
|
||||
|
||||
data_ret = {'status': 1}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
|
||||
except BaseException as msg:
|
||||
data_ret = {'status': 0,'error_message': str(msg)}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
|
||||
|
||||
@@ -457,18 +457,13 @@ app.controller('listDBs', function ($scope, $http) {
|
||||
$scope.remoteAccess = function (userName) {
|
||||
|
||||
$scope.dbUsername = userName;
|
||||
alert($scope.dbUsername);
|
||||
return 0;
|
||||
|
||||
$scope.dbLoading = false;
|
||||
$scope.passwordChanged = true;
|
||||
|
||||
|
||||
url = "/dataBases/changePassword";
|
||||
url = "/dataBases/remoteAccess";
|
||||
|
||||
var data = {
|
||||
dbUserName: globalDBUsername,
|
||||
dbPassword: $scope.dbPassword,
|
||||
dbUserName: $scope.dbUsername
|
||||
};
|
||||
|
||||
var config = {
|
||||
@@ -477,33 +472,89 @@ app.controller('listDBs', function ($scope, $http) {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
$http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas);
|
||||
|
||||
|
||||
function ListInitialDatas(response) {
|
||||
|
||||
|
||||
if (response.data.changePasswordStatus == 1) {
|
||||
$scope.notificationsBox = false;
|
||||
$scope.passwordChanged = false;
|
||||
$scope.dbLoading = true;
|
||||
$scope.domainFeteched = $scope.selectedDomain;
|
||||
|
||||
if (response.data.status === 1) {
|
||||
|
||||
$scope.dbHost = response.data.dbHost;
|
||||
|
||||
}
|
||||
else {
|
||||
$scope.notificationsBox = false;
|
||||
$scope.canNotChangePassword = false;
|
||||
$scope.dbLoading = true;
|
||||
$scope.canNotChangePassword = false;
|
||||
$scope.errorMessage = response.data.error_message;
|
||||
new PNotify({
|
||||
title: 'Operation Failed!',
|
||||
text: response.data.error_message,
|
||||
type: 'error'
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function cantLoadInitialDatas(response) {
|
||||
$scope.notificationsBox = false;
|
||||
$scope.couldNotConnect = false;
|
||||
new PNotify({
|
||||
title: 'Operation Failed!',
|
||||
text: 'Could not connect to server, please refresh this page',
|
||||
type: 'error'
|
||||
});
|
||||
$scope.dbLoading = true;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
$scope.allowRemoteIP = function () {
|
||||
|
||||
$scope.dbLoading = false;
|
||||
|
||||
url = "/dataBases/allowRemoteIP";
|
||||
|
||||
var data = {
|
||||
dbUserName: $scope.dbUsername,
|
||||
remoteIP: $scope.remoteIP
|
||||
};
|
||||
|
||||
var config = {
|
||||
headers: {
|
||||
'X-CSRFToken': getCookie('csrftoken')
|
||||
}
|
||||
};
|
||||
|
||||
$http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas);
|
||||
|
||||
function ListInitialDatas(response) {
|
||||
$scope.dbLoading = true;
|
||||
|
||||
if (response.data.status === 1) {
|
||||
|
||||
$scope.remoteAccess($scope.dbUsername);
|
||||
|
||||
new PNotify({
|
||||
title: 'Success',
|
||||
text: 'Changes applied.',
|
||||
type: 'success'
|
||||
});
|
||||
|
||||
}
|
||||
else {
|
||||
new PNotify({
|
||||
title: 'Operation Failed!',
|
||||
text: response.data.error_message,
|
||||
type: 'error'
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function cantLoadInitialDatas(response) {
|
||||
new PNotify({
|
||||
title: 'Operation Failed!',
|
||||
text: 'Could not connect to server, please refresh this page',
|
||||
type: 'error'
|
||||
});
|
||||
$scope.dbLoading = true;
|
||||
|
||||
}
|
||||
|
||||
@@ -137,10 +137,33 @@
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
|
||||
<div class="form-group">
|
||||
<label class="col-sm-12 control-label"
|
||||
style="text-align: center;">{% trans "Contents of .gitignore, use the box below to Add/Edit content of .gitingore file." %}</label>
|
||||
<form name="containerSettingsForm" action="/"
|
||||
class="form-horizontal">
|
||||
<div ng-hide="installationDetailsForm"
|
||||
class="form-group">
|
||||
<label class="col-sm-5 control-label">{% trans "Allow Remote Access on following IP" %}</label>
|
||||
<div class="col-sm-6">
|
||||
<input placeholder="IP Address"
|
||||
name="remoteIP" type="text"
|
||||
class="form-control"
|
||||
ng-model="$parent.remoteIP"
|
||||
required>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<hr>
|
||||
|
||||
<div class="form-group">
|
||||
|
||||
<label class="col-sm-5 control-label"></label>
|
||||
<div class="col-sm-4">
|
||||
<button type="button" ng-click="allowRemoteIP()"
|
||||
class="btn btn-primary btn-lg">{% trans "Save Changes" %}</button>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
|
||||
<hr>
|
||||
|
||||
@@ -154,7 +177,7 @@
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>{$ dbUsername $}</td>
|
||||
<td></td>
|
||||
<td>{$ dbHost $}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@@ -13,7 +13,9 @@ urlpatterns = [
|
||||
|
||||
url(r'^listDBs', views.listDBs, name='listDBs'),
|
||||
|
||||
url(r'^changePassword', views.changePassword, name='changePassword'),
|
||||
url(r'^changePassword$', views.changePassword, name='changePassword'),
|
||||
url(r'^remoteAccess$', views.remoteAccess, name='remoteAccess'),
|
||||
url(r'^allowRemoteIP$', views.allowRemoteIP, name='allowRemoteIP'),
|
||||
url(r'^phpMyAdmin$', views.phpMyAdmin, name='phpMyAdmin'),
|
||||
url(r'^setupPHPMYAdminSession$', views.setupPHPMYAdminSession, name='setupPHPMYAdminSession'),
|
||||
]
|
||||
@@ -131,6 +131,17 @@ def remoteAccess(request):
|
||||
except KeyError:
|
||||
return redirect(loadLoginPage)
|
||||
|
||||
def allowRemoteIP(request):
|
||||
try:
|
||||
userID = request.session['userID']
|
||||
|
||||
dm = DatabaseManager()
|
||||
coreResult = dm.allowRemoteIP(userID, json.loads(request.body))
|
||||
|
||||
return coreResult
|
||||
except KeyError:
|
||||
return redirect(loadLoginPage)
|
||||
|
||||
def phpMyAdmin(request):
|
||||
try:
|
||||
userID = request.session['userID']
|
||||
|
||||
@@ -793,6 +793,27 @@ password=%s
|
||||
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[mysqlUtilities.changePassword]")
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def allowRemoteAccess(dbName, userName, remoteIP):
|
||||
try:
|
||||
|
||||
connection, cursor = mysqlUtilities.setupConnection()
|
||||
|
||||
if connection == 0:
|
||||
return 0
|
||||
cursor.execute("use mysql")
|
||||
|
||||
cursor.execute("update db set Host='%s' where Db='%s'" % (remoteIP, dbName))
|
||||
cursor.execute("update user set Host='%s' where user='%s'" % (remoteIP, userName))
|
||||
|
||||
connection.close()
|
||||
|
||||
return 1
|
||||
|
||||
except BaseException as msg:
|
||||
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[mysqlUtilities.changePassword]")
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def fetchuser(databaseName):
|
||||
try:
|
||||
|
||||
@@ -454,6 +454,113 @@ app.controller('listDBs', function ($scope, $http) {
|
||||
$scope.generatedPasswordView = true;
|
||||
};
|
||||
|
||||
$scope.remoteAccess = function (userName) {
|
||||
|
||||
$scope.dbUsername = userName;
|
||||
$scope.dbLoading = false;
|
||||
|
||||
|
||||
url = "/dataBases/remoteAccess";
|
||||
|
||||
var data = {
|
||||
dbUserName: $scope.dbUsername
|
||||
};
|
||||
|
||||
var config = {
|
||||
headers: {
|
||||
'X-CSRFToken': getCookie('csrftoken')
|
||||
}
|
||||
};
|
||||
|
||||
$http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas);
|
||||
|
||||
function ListInitialDatas(response) {
|
||||
$scope.dbLoading = true;
|
||||
|
||||
if (response.data.status === 1) {
|
||||
|
||||
$scope.dbHost = response.data.dbHost;
|
||||
|
||||
}
|
||||
else {
|
||||
new PNotify({
|
||||
title: 'Operation Failed!',
|
||||
text: response.data.error_message,
|
||||
type: 'error'
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function cantLoadInitialDatas(response) {
|
||||
new PNotify({
|
||||
title: 'Operation Failed!',
|
||||
text: 'Could not connect to server, please refresh this page',
|
||||
type: 'error'
|
||||
});
|
||||
$scope.dbLoading = true;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
$scope.allowRemoteIP = function () {
|
||||
|
||||
$scope.dbLoading = false;
|
||||
|
||||
url = "/dataBases/allowRemoteIP";
|
||||
|
||||
var data = {
|
||||
dbUserName: $scope.dbUsername,
|
||||
remoteIP: $scope.remoteIP
|
||||
};
|
||||
|
||||
var config = {
|
||||
headers: {
|
||||
'X-CSRFToken': getCookie('csrftoken')
|
||||
}
|
||||
};
|
||||
|
||||
$http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas);
|
||||
|
||||
function ListInitialDatas(response) {
|
||||
$scope.dbLoading = true;
|
||||
|
||||
if (response.data.status === 1) {
|
||||
|
||||
$scope.remoteAccess($scope.dbUsername);
|
||||
|
||||
new PNotify({
|
||||
title: 'Success',
|
||||
text: 'Changes applied.',
|
||||
type: 'success'
|
||||
});
|
||||
|
||||
}
|
||||
else {
|
||||
new PNotify({
|
||||
title: 'Operation Failed!',
|
||||
text: response.data.error_message,
|
||||
type: 'error'
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function cantLoadInitialDatas(response) {
|
||||
new PNotify({
|
||||
title: 'Operation Failed!',
|
||||
text: 'Could not connect to server, please refresh this page',
|
||||
type: 'error'
|
||||
});
|
||||
$scope.dbLoading = true;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user