Source code for linotp.controllers.license
# -*- coding: utf-8 -*-
import logging
from pylons import request, response, config, tmpl_context as c
from linotp.lib.base import BaseController
from linotp.lib.config import initLinotpConfig
from linotp.lib.reply import sendResult, sendError, sendXMLResult, sendXMLError
from linotp.lib.policy import checkPolicyPre, PolicyException
from linotp.model.meta import Session
from linotp.lib.user import getUserFromRequest
from linotp.lib.util import check_session
import traceback
# We need this for the policies:
from linotp.lib.licensebase import checkEE
EE=checkEE()
del checkEE
if EE:
from linotpee.lib.license import getLicenseInfo, setLicense
else:
from linotp.lib.licensebase import getLicenseInfo, setLicense
audit = config.get('audit')
log = logging.getLogger(__name__)
optional = True
required = False
[docs]class LicenseController(BaseController):
def __before__(self):
audit.initialize()
c.audit['success'] = False
check_session()
def __after__(self):
if c.audit['action'] in ['license/setLicense',
]:
c.audit['administrator'] = getUserFromRequest(request).get("login")
audit.log(c.audit)
[docs] def getLicense(self):
'''
Returns the license information as a JSON object
'''
log.debug("[getLicense] calling license/get ")
valid = False
licInfo = {}
try:
licInfo = getLicenseInfo();
except Exception, e:
log.debug("failed!")
Session.rollback()
return sendError(response,e)
try:
Session.commit()
except Exception, e :
log.error('[getLicense] commit failed')
Session.rollback()
return sendError(response,"Commit failed: " + str(e),0)
#if everything is ok the new counter is 0, which is returned
Session.close()
return sendResult(response,licInfo)
[docs] def setLicense(self):
'''
sets the license.
param: license - containing the license file.
The method must be called as a POST request
'''
log.debug("[setLicense] calling setLicense ")
licInfo = {}
res = "Setting license failed!"
outputXML = False
try:
log.debug("[setLicense] getting POST request")
# check admin authorization
try:
checkPolicyPre('license', 'setLicense', {} )
except PolicyException as pe:
return sendError(response, str(pe), 1)
licField = request.POST['license']
log.info("[setLicense] admin is setting license: %s" % (licField ) )
licString = ""
# In case of normal post requests, it is a "instance" of FieldStorage
if type(licField).__name__=='instance':
log.debug("[setLicense] Field storage: %s",licField)
licString = licField.value
outputXML = True
else:
# we got UTF-8!
licString = licField.encode('utf-8')
log.debug("[setLicense] license %s",licString)
ret = setLicense(licString)
c.audit['success'] = ret
l = getLicenseInfo()
lic_toknum = ''
lic_date = ''
lic_expire = ''
lic_contact_email = ''
if l.get('description'):
lic_toknum = str(l.get('description').get('token-num'))
lic_date = str(l.get('description').get('date'))
lic_expire = str(l.get('description').get('expire'))
lic_contact_email = str(l.get('description').get('contact-email'))
c.audit['info'] = "num: %s, date: %s, exp: %s, email: %s" % (
lic_toknum,
lic_date,
lic_expire,
lic_contact_email,
)
except Exception, e:
Session.rollback()
log.error("[setLicense] failed with error %s" % str(e))
log.error("[setLicense] %s" % traceback.format_exc())
return sendError(response,e)
if (ret == True):
res = "Setting license was successful!"
try:
Session.commit()
except Exception, e :
log.error('[setLicense] commit failed')
Session.rollback()
return sendError(response,"Commit failed: " + str(e),0)
Session.close()
if outputXML:
return sendXMLResult(response, res)
else:
return sendResult(response,res)