Skip to content

Commit

Permalink
feature: suspend/unsuspend users
Browse files Browse the repository at this point in the history
  • Loading branch information
usmannasir committed Jan 21, 2020
1 parent a080be2 commit b51ef9d
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 5 deletions.
55 changes: 53 additions & 2 deletions userManagment/static/userManagment/userManagment.js
Original file line number Diff line number Diff line change
Expand Up @@ -1686,8 +1686,59 @@ app.controller('listTableUsers', function ($scope, $http) {

};

$scope.controlUserState = function (state) {
alert(state);
$scope.controlUserState = function (userName, state) {
$scope.cyberpanelLoading = false;

var url = "/users/controlUserState";

var data = {
accountUsername: userName,
state : state
};

var config = {
headers: {
'X-CSRFToken': getCookie('csrftoken')
}
};

$http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas);


function ListInitialDatas(response) {
$scope.cyberpanelLoading = true;
if (response.data.status === 1) {
$scope.populateCurrentRecords();
new PNotify({
title: 'Success!',
text: 'Action successfully started.',
type: 'success'
});

} else {

new PNotify({
title: 'Error!',
text: response.data.error_message,
type: 'error'
});


}

}

function cantLoadInitialDatas(response) {

$scope.cyberpanelLoading = false;
new PNotify({
title: 'Error!',
text: 'Could not connect to server, please refresh this page.',
type: 'error'
});


}
}

});
Expand Down
4 changes: 2 additions & 2 deletions userManagment/templates/userManagment/listUsers.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ <h3 class="content-box-header">
<td>
<a ng-show="record.state=='ACTIVE'"
class="btn btn-border btn-alt border-red btn-link font-red" href="#"
ng-click="controlUserState('SUSPEND')" title=""><span>{% trans 'Suspend' %}</span></a>
ng-click="controlUserState(record.name, 'SUSPEND')" title=""><span>{% trans 'Suspend' %}</span></a>
<a ng-show="record.state=='SUSPENDED'"
class="btn btn-border btn-alt border-green btn-link font-green" href="#"
ng-click="controlUserState('ACTIVATE')" title=""><span>{% trans 'Activate' %}</span></a>
ng-click="controlUserState(record.name, 'ACTIVATE')" title=""><span>{% trans 'Activate' %}</span></a>
<a data-toggle="modal" data-target="#settings"
ng-click="editInitial(record.name)"
class="btn btn-border btn-alt border-purple btn-link font-purple" href="#"
Expand Down
1 change: 1 addition & 0 deletions userManagment/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@
url(r'^saveChangesAPIAccess$', views.saveChangesAPIAccess, name="saveChangesAPIAccess"),
url(r'^listUsers$', views.listUsers, name="listUsers"),
url(r'^fetchTableUsers$', views.fetchTableUsers, name="fetchTableUsers"),
url(r'^controlUserState$', views.controlUserState, name="controlUserState"),
]
47 changes: 47 additions & 0 deletions userManagment/userManager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/local/CyberCP/bin/python
import os, sys

sys.path.append('/usr/local/CyberCP')
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
django.setup()
import threading as multi
from plogical.acl import ACLManager
from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging



class UserManager(multi.Thread):

def __init__(self, function, extraArgs):
multi.Thread.__init__(self)
self.function = function
self.extraArgs = extraArgs


def run(self):
try:
if self.function == 'controlUserState':
self.controlUserState()
except:
pass

def controlUserState(self):
try:
websites = ACLManager.findAllSites(self.extraArgs['currentACL'],self.extraArgs['user'])
from websiteFunctions.website import WebsiteManager

wm = WebsiteManager()

if self.extraArgs['state'] == 'SUSPEND':
for items in websites:
data = {'websiteName': items, 'state': 'Suspend'}
wm.submitWebsiteStatus(self.extraArgs['user'].pk, data)
else:
for items in websites:
data = {'websiteName': items, 'state': 'UN-Suspend'}
wm.submitWebsiteStatus(self.extraArgs['user'].pk, data)


except BaseException as msg:
logging.writeToFile(str(msg) + '[Error:UserManager:32]')
57 changes: 56 additions & 1 deletion userManagment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ def submitUserCreation(request):
json_data = json.dumps(data_ret)
return HttpResponse(json_data)


def modifyUsers(request):
try:
userID = request.session['userID']
Expand Down Expand Up @@ -1011,3 +1010,59 @@ def fetchTableUsers(request):

except KeyError:
return redirect(loadLoginPage)

def controlUserState(request):
try:
val = request.session['userID']
try:
if request.method == 'POST':
data = json.loads(request.body)
accountUsername = data['accountUsername']
state = data['state']

user = Administrator.objects.get(userName=accountUsername)

currentACL = ACLManager.loadedACL(val)
loggedUser = Administrator.objects.get(pk=val)

if currentACL['admin'] == 1:
pass
elif user.owner == loggedUser.pk:
pass
elif user.pk == loggedUser.pk:
pass
else:
data_ret = {'fetchStatus': 0, 'error_message': 'Un-authorized access.'}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)

if state == 'SUSPEND':
user.state = 'SUSPENDED'
else:
user.state = 'ACTIVE'

user.save()

extraArgs = {}
extraArgs['user'] = user
extraArgs['currentACL'] = currentACL
extraArgs['state'] = state

from userManagment.userManager import UserManager

um = UserManager('controlUserState', extraArgs)
um.start()

data_ret = {'status': 1}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)

except BaseException as msg:
data_ret = {'status': 0, 'saveStatus': 0, 'error_message': str(msg)}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)

except KeyError:
data_ret = {'status': 0, 'saveStatus': 0, 'error_message': "Not logged in as admin", }
json_data = json.dumps(data_ret)
return HttpResponse(json_data)

0 comments on commit b51ef9d

Please sign in to comment.