2016-06-27 17:20:26 +03:00
|
|
|
|
from base.Scope import Scope
|
|
|
|
|
from base.util.util import Util
|
2016-06-30 12:40:29 +03:00
|
|
|
|
from base.system.system import System
|
2016-06-27 17:20:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Agreement:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
scope = Scope().getInstance()
|
|
|
|
|
self.logger = scope.getLogger()
|
|
|
|
|
self.message_manager = scope.getMessageManager()
|
|
|
|
|
self.messenger = scope.getMessenger()
|
|
|
|
|
self.db_service = scope.getDbService()
|
|
|
|
|
self.ask_path = '/opt/ahenk/base/agreement/ask.py'
|
|
|
|
|
self.logger.debug('[Agreement] Instance initialized.')
|
|
|
|
|
|
|
|
|
|
def agreement_contract_update(self):
|
|
|
|
|
self.messenger.send_direct_message(self.message_manager.agreement_request_msg())
|
|
|
|
|
self.logger.debug('[Agreement] Requested updated agreement contract from lider.')
|
|
|
|
|
|
|
|
|
|
def check_agreement(self, username):
|
|
|
|
|
self.logger.debug('[Agreement] Checking agreement for user {}.'.format(username))
|
|
|
|
|
contract_id = self.get_current_contract_id()
|
|
|
|
|
if contract_id is None:
|
|
|
|
|
self.logger.debug('[Agreement] There is no any contract in database.')
|
2016-06-28 12:23:01 +03:00
|
|
|
|
contract_id = '-1'
|
|
|
|
|
|
2016-08-15 11:22:10 +03:00
|
|
|
|
if self.db_service.select_one_result('agreement', 'id',
|
|
|
|
|
" contract_id='{0}' and username='{1}' and choice='Y' "
|
|
|
|
|
.format(contract_id, username)) is not None:
|
2016-06-28 12:23:01 +03:00
|
|
|
|
return True
|
2016-08-15 11:22:10 +03:00
|
|
|
|
elif self.db_service.select_one_result('agreement', 'id',
|
|
|
|
|
" contract_id='{0}' and username='{1}' and choice='N' ".format(
|
|
|
|
|
contract_id, username)) is not None:
|
2016-06-28 12:23:01 +03:00
|
|
|
|
return False
|
2016-06-27 17:20:26 +03:00
|
|
|
|
else:
|
2016-06-28 12:23:01 +03:00
|
|
|
|
return None
|
2016-06-27 17:20:26 +03:00
|
|
|
|
|
|
|
|
|
def get_current_contract_id(self):
|
|
|
|
|
return self.db_service.select_one_result('contract', 'id', 'id =(select MAX(id) from contract)')
|
|
|
|
|
|
|
|
|
|
def ask(self, username, display):
|
|
|
|
|
|
|
|
|
|
result = self.db_service.select('contract', ['content', 'title', 'id'], 'id =(select MAX(id) from contract)')
|
|
|
|
|
|
|
|
|
|
if result is None or len(result) < 1:
|
|
|
|
|
content = 'Ahenk kurulu bu bilgisayarda ilk defa oturum açıyorsunuz. ' \
|
|
|
|
|
'Devam ederseniz Lider-Ahenk in bilgisayar üzeride yapacağı ' \
|
|
|
|
|
'tüm işlemlere onay vermiş sayılacaksınız. Kabul ediyor musunuz?' \
|
2016-08-15 11:22:10 +03:00
|
|
|
|
' \n(Tanımlanmış zaman aralığında olumlu cevaplandırmadığınız takdirde oturumunuz ' \
|
|
|
|
|
'sonlandırılacaktır.)'
|
2016-06-27 17:20:26 +03:00
|
|
|
|
title = 'Ahenk Kurulu Bilgisayar Kullanım Anlaşması'
|
|
|
|
|
contract_id = '-1'
|
|
|
|
|
else:
|
2016-06-30 12:40:29 +03:00
|
|
|
|
content = str(result[0][0])
|
2016-06-29 12:42:02 +03:00
|
|
|
|
title = result[0][1]
|
|
|
|
|
contract_id = result[0][2]
|
2016-06-28 12:23:01 +03:00
|
|
|
|
try:
|
2016-06-30 12:40:29 +03:00
|
|
|
|
agreement_path = System.Ahenk.received_dir_path() + Util.generate_uuid()
|
|
|
|
|
Util.write_file(agreement_path, content)
|
2016-06-30 15:42:01 +03:00
|
|
|
|
Util.set_permission(agreement_path, 777)
|
2016-08-15 11:22:10 +03:00
|
|
|
|
command = 'export DISPLAY={0};su - {1} -c \'python3 {2} \"$(cat {3})\" \"{4}\"\''.format(display, username,
|
|
|
|
|
self.ask_path,
|
|
|
|
|
agreement_path,
|
|
|
|
|
title)
|
2016-06-30 12:40:29 +03:00
|
|
|
|
result_code, p_out, p_err = Util.execute(command)
|
2016-06-28 12:23:01 +03:00
|
|
|
|
pout = str(p_out).replace('\n', '')
|
|
|
|
|
if pout != 'Error':
|
|
|
|
|
if pout == 'Y':
|
|
|
|
|
self.logger.debug('[Agreement] Agreement was accepted by {}.'.format(username))
|
2016-08-15 11:22:10 +03:00
|
|
|
|
self.db_service.update('agreement', self.db_service.get_cols('agreement'),
|
|
|
|
|
[contract_id, username, Util.timestamp(), 'Y'])
|
2016-06-28 12:23:01 +03:00
|
|
|
|
elif pout == 'N':
|
2016-08-15 11:22:10 +03:00
|
|
|
|
self.db_service.update('agreement', self.db_service.get_cols('agreement'),
|
|
|
|
|
[contract_id, username, Util.timestamp(), 'N'])
|
|
|
|
|
self.logger.debug(
|
|
|
|
|
'[Agreement] Agreement was ignored by {}. Session will be closed'.format(username))
|
2016-06-28 12:23:01 +03:00
|
|
|
|
else:
|
2016-08-15 11:22:10 +03:00
|
|
|
|
self.logger.error(
|
|
|
|
|
'[Agreement] A problem occurred while executing ask.py. Error Message: {}'.format(str(pout)))
|
2016-07-11 17:56:50 +03:00
|
|
|
|
Util.delete_file(agreement_path)
|
2016-06-27 17:20:26 +03:00
|
|
|
|
else:
|
2016-08-15 11:22:10 +03:00
|
|
|
|
self.logger.error(
|
|
|
|
|
'[Agreement] A problem occurred while executing ask.py (Probably argument fault). Error Message: {}'.format(
|
|
|
|
|
str(pout)))
|
2016-06-28 12:23:01 +03:00
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2016-08-15 11:22:10 +03:00
|
|
|
|
self.logger.error(
|
|
|
|
|
'[Agreement] A Problem occurred while displaying agreement. Error Message: {}'.format(str(e)))
|