This repository has been archived on 2023-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
ACMS/src/Server/ulCommands.py

171 lines
4.7 KiB
Python

#!/usr/bin/env python3.5
# -*- coding: utf-8 -*-
"""
List of command.
"""
from Common.socketCommands import sendData, recvData
from .errorHandler import serverSendErrHandler, serverRecvErrHandler
from .CommandException import CommandException
__authors__ = "Tatiyk, HorlogeSkynet, CaumartinYann"
__copyright__ = "Copyright 2017, ACMS"
__license__ = "GPLv3"
__status__ = "Production"
__date__ = "03/30/2017"
def adduserController(command, client, userList, acl):
if len(command) == 2:
if not acl.isAdministrator(client.username):
raise CommandException("You\'re not allowed to perform this operation.")
sendData(client.sock, 'NEW_USER_PROCEDURE', serverSendErrHandler, client.sock)
result = recvData(client.sock, serverRecvErrHandler, client.sock)
if result and result.startswith('USER_PASSWORD:'):
try:
userList.addUser(command[1], result.split(':', 1)[1])
return "New user " + command[1] + " successfully added."
except:
raise
else:
raise CommandException("Procedure aborted.")
else:
raise CommandException("Wrong number of arguments.")
def removeuserController(command, client, userList, acl):
if len(command) == 2:
if acl.isAdministrator(client.username):
try:
userList.removeUser(command[1])
return "User " + command[1] + " successfully removed."
except:
raise
else:
raise CommandException("You\'re not allowed to perform this operation.")
else:
raise CommandException("Wrong number of arguments.")
def addgroupController(command, client, acl):
if len(command) == 2:
if acl.isAdministrator(client.username):
try:
acl.createGroup(command[1])
return "The group " + command[1] + " has been successfully created."
except:
raise
else:
raise CommandException("You\'re not allowed to perform this operation.")
else:
raise CommandException("Wrong number of arguments.")
def delgroupController(command, client, acl):
if len(command) == 2:
if acl.isAdministrator(client.username):
try:
acl.removeGroup(command[1])
return "The group " + command[1] + " has been successfully deleted."
except:
raise
else:
raise CommandException("You\'re not allowed to perform this operation.")
else:
raise CommandException("Wrong number of arguments.")
def addusergroupController(command, client, acl):
if len(command) == 3:
if acl.isAdministrator(client.username):
try:
acl.addUserInGroup(command[1], command[2])
return "User " + command[1] + " added in " + command[2] + '.'
except:
raise
else:
raise CommandException("You\'re not allowed to perform this operation.")
else:
raise CommandException("Wrong number of arguments.")
def removeusergroupController(command, client, acl):
if len(command) == 3:
if acl.isAdministrator(client.username):
if command[1] != 'admin':
try:
acl.removeUserFromGroup(command[1], command[2])
return "User " + command[1] + " successfully removed from " + command[2] + '.'
except:
raise
else:
raise CommandException("You can\'t remove the privileges of this special user.")
else:
raise CommandException("You\'re not allowed to perform this operation.")
else:
raise CommandException("Wrong number of arguments.")
def passwdController(command, client, userList, acl):
nbArgs = len(command)
if nbArgs == 1 or nbArgs == 2:
if nbArgs == 2 and not acl.isAdministrator(client.username):
raise CommandException("You\'re not allowed to perform this operation.")
sendData(client.sock, 'CHANGE_PASSWD_PROCEDURE', serverSendErrHandler, client.sock)
result = recvData(client.sock, serverRecvErrHandler, client.sock)
if result and result.startswith('OLD_PASSWORD:'):
if userList.checkUserPassword(client.username, result.split(':', 1)[1]):
sendData(client.sock, 'OLD_PASSWORD_OK', serverSendErrHandler, client.sock)
result = recvData(client.sock, serverRecvErrHandler, client.sock)
if result and result.startswith('NEW_PASSWORD:'):
try:
userList.updateUserPassword(client.username if nbArgs == 1 else command[1], result.split(':', 1)[1])
return "Done."
except:
raise CommandException("Procedure aborted.")
else:
raise CommandException("Procedure aborted.")
else:
raise CommandException("Your current password is incorrect...")
else:
raise CommandException("Procedure aborted.")
else:
raise CommandException("Wrong number of arguments.")
def groupsController(username, userList, acl):
if acl.isAdministrator(username):
return "List of users and their respective groups:\n\n\t" + '\n\t'.join([x + ': ' + ', '.join(acl.getGroupsByUser(x)) for x in userList.getUsersList()])
else:
return "You\'re in these groups:\n\n\t" + '\n\t'.join(acl.getGroupsByUser(username))