|
8 | 8 | import json |
9 | 9 | from plogical.processUtilities import ProcessUtilities |
10 | 10 | from loginSystem.models import Administrator |
11 | | -import CyberCP.settings as settings |
| 11 | +from plogical.acl import ACLManager |
| 12 | +from databases.models import GlobalUserDB |
| 13 | +from plogical import randomPassword |
| 14 | +from cryptography.fernet import Fernet |
| 15 | +from plogical.mysqlUtilities import mysqlUtilities |
12 | 16 | # Create your views here. |
13 | 17 |
|
14 | 18 | def loadDatabaseHome(request): |
@@ -150,27 +154,63 @@ def phpMyAdmin(request): |
150 | 154 | except KeyError: |
151 | 155 | return redirect(loadLoginPage) |
152 | 156 |
|
153 | | -def setupPHPMYAdminSession(request): |
| 157 | +def generateAccess(request): |
154 | 158 | try: |
155 | 159 |
|
156 | 160 | userID = request.session['userID'] |
157 | 161 | admin = Administrator.objects.get(id = userID) |
| 162 | + currentACL = ACLManager.loadedACL(userID) |
158 | 163 |
|
159 | | - execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/databases/databaseManager.py" |
160 | | - execPath = execPath + " generatePHPMYAdminData --userID " + str(userID) |
161 | | - |
162 | | - output = ProcessUtilities.outputExecutioner(execPath) |
163 | | - |
164 | | - if output.find("1,") > -1: |
165 | | - request.session['PMA_single_signon_user'] = admin.userName |
166 | | - request.session['PMA_single_signon_password'] = output.split(',')[1] |
167 | | - data_ret = {'status': 1} |
168 | | - json_data = json.dumps(data_ret) |
169 | | - return HttpResponse(json_data) |
170 | | - else: |
171 | | - data_ret = {'status': 1} |
172 | | - json_data = json.dumps(data_ret) |
173 | | - return HttpResponse(json_data) |
| 164 | + try: |
| 165 | + GlobalUserDB.objects.get(username=admin.userName) |
| 166 | + except: |
| 167 | + |
| 168 | + ## Key generation |
| 169 | + |
| 170 | + keySavePath = '/home/cyberpanel/phpmyadmin_%s' % (admin.userName) |
| 171 | + key = Fernet.generate_key() |
| 172 | + |
| 173 | + writeToFile = open(keySavePath, 'w') |
| 174 | + writeToFile.write(key.decode()) |
| 175 | + writeToFile.close() |
| 176 | + |
| 177 | + command = 'chown root:root %s' % (keySavePath) |
| 178 | + ProcessUtilities.executioner(command) |
| 179 | + |
| 180 | + command = 'chmod 600 %s' % (keySavePath) |
| 181 | + ProcessUtilities.executioner(command) |
| 182 | + |
| 183 | + ## |
| 184 | + |
| 185 | + password = randomPassword.generate_pass() |
| 186 | + f = Fernet(key) |
| 187 | + GlobalUserDB(username=admin, password=f.encrypt(password.encode('utf-8'))).save() |
| 188 | + |
| 189 | + sites = ACLManager.findWebsiteObjects(currentACL, userID) |
| 190 | + |
| 191 | + createUser = 1 |
| 192 | + |
| 193 | + for site in sites: |
| 194 | + for db in site.databases_set.all(): |
| 195 | + mysqlUtilities.addUserToDB(db.dbName, admin.userName, password, createUser) |
| 196 | + createUser = 0 |
| 197 | + |
| 198 | + # execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/databases/databaseManager.py" |
| 199 | + # execPath = execPath + " generatePHPMYAdminData --userID " + str(userID) |
| 200 | + # |
| 201 | + # output = ProcessUtilities.outputExecutioner(execPath) |
| 202 | + # |
| 203 | + # if output.find("1,") > -1: |
| 204 | + # request.session['PMA_single_signon_user'] = admin.userName |
| 205 | + # request.session['PMA_single_signon_password'] = output.split(',')[1] |
| 206 | + # data_ret = {'status': 1} |
| 207 | + # json_data = json.dumps(data_ret) |
| 208 | + # return HttpResponse(json_data) |
| 209 | + # else: |
| 210 | + |
| 211 | + data_ret = {'status': 1} |
| 212 | + json_data = json.dumps(data_ret) |
| 213 | + return HttpResponse(json_data) |
174 | 214 |
|
175 | 215 |
|
176 | 216 | except BaseException as msg: |
|
0 commit comments