parent
70af9fed6d
commit
07a6d66849
@ -0,0 +1,565 @@
|
||||
#-*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2010-2012 Calculate Ltd. http://www.calculate-linux.org
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import sys, os
|
||||
import subprocess
|
||||
|
||||
from soaplib.serializers.primitive import String, Integer, Boolean
|
||||
from soaplib.serializers.clazz import Array, ClassSerializer
|
||||
from soaplib.service import rpc
|
||||
from calculate.api.server.api_types import ReturnedMessage, getViewForVariables
|
||||
from calculate.api.server.api_types import Field, GroupField, ViewInfo
|
||||
from calculate.lib.datavars import VariableError
|
||||
|
||||
from calculate.api import datavars
|
||||
import traceback
|
||||
|
||||
class GroupInfo(ClassSerializer):
|
||||
"""Parameters for method install"""
|
||||
cl_page_count = String
|
||||
cl_page_offset = String
|
||||
|
||||
class DetailGroupInfo(ClassSerializer):
|
||||
"""Parameters for method install"""
|
||||
cl_group_name = String
|
||||
cl_group_rights = Array(String)
|
||||
|
||||
def catchExcept():
|
||||
class wrapper:
|
||||
def __init__(self,f):
|
||||
self.f = f
|
||||
self.func_name = f.func_name
|
||||
self.func_code = f.func_code
|
||||
self.__doc__ = f.__doc__
|
||||
self.__name__ = f.__name__
|
||||
|
||||
def __call__(self,selfobj,*args,**kwargs):
|
||||
try:
|
||||
return self.f(selfobj,*args,**kwargs)
|
||||
except BaseException as e:
|
||||
view = ViewInfo(groups=[])
|
||||
group = GroupField(name=_("Error"),last=True)
|
||||
group.fields = []
|
||||
group.fields.append(Field(
|
||||
name = "error",
|
||||
label = str(e),
|
||||
default = 'color:red;',
|
||||
element = "error"))
|
||||
view.groups.append(group)
|
||||
print "!!!!EXCEPTION!!!!"
|
||||
for i in apply(traceback.format_exception, sys.exc_info()):
|
||||
print i
|
||||
return view
|
||||
return wrapper
|
||||
|
||||
class ApiWsdl:
|
||||
#def create_req_table(self, )
|
||||
def show_groups_meth (self, dv) :
|
||||
try:
|
||||
page_count = int(dv.Get('cl_page_count'))
|
||||
page_offset = int(dv.Get('cl_page_offset'))
|
||||
#self.printSUCCESS('Current offset = %d' %page_offset)
|
||||
|
||||
if not page_offset:
|
||||
page_offset = 0
|
||||
|
||||
list_group_name = dv.Choice('cl_group_name')
|
||||
|
||||
if not len(list_group_name):
|
||||
self.printSUCCESS('No groups')
|
||||
return True
|
||||
|
||||
dict_group_name = {}
|
||||
list_group_name.sort()
|
||||
for i in range(len(list_group_name)):
|
||||
dict_group_name[i] = list_group_name[i]
|
||||
max_id = len(list_group_name)
|
||||
|
||||
self.printSUCCESS('Current offset = %d' %page_offset)
|
||||
if not page_count:
|
||||
page_count = len(list_group_name)
|
||||
|
||||
head = ['Group','Rights']
|
||||
body = []
|
||||
fields = ['cl_group_name','']
|
||||
|
||||
num_id = len(list_group_name)
|
||||
print 'num_id = ', num_id
|
||||
if page_offset > num_id - 1:
|
||||
i = num_id - 1
|
||||
else:
|
||||
i = page_offset
|
||||
while True:
|
||||
if len(body) == page_count or i >= max_id:
|
||||
break
|
||||
|
||||
#if not i in list_req_id:
|
||||
#i+=1
|
||||
#continue
|
||||
|
||||
dv.Set('cl_group_name', dict_group_name[i])
|
||||
group_rights = ', '.join(dv.Get('cl_group_rights'))
|
||||
body.append([dict_group_name[i],group_rights])
|
||||
i+=1
|
||||
|
||||
if body:
|
||||
self.printTable(head, body, "Groups Table", fields = fields, \
|
||||
onClick = 'detail_group')
|
||||
except Exception,e:
|
||||
for i in apply(traceback.format_exception, sys.exc_info()):
|
||||
print i
|
||||
print 'EXCEPT!!!!!!!!!!', e
|
||||
return True
|
||||
|
||||
def check_req_params (self, dv, info,allvars=False,ordered=None):
|
||||
errors = []
|
||||
keys = sorted(filter(lambda x:x.lower() == x,
|
||||
info._type_info.keys()))
|
||||
if ordered:
|
||||
keys = ordered + filter(lambda x:not x in ordered,
|
||||
keys)
|
||||
for var in keys:
|
||||
# get value of variable from info
|
||||
val = info.__getattribute__(var)
|
||||
# check value if value send of check allvariables
|
||||
if val != None or allvars:
|
||||
try:
|
||||
# if value not send, then get from datavars
|
||||
if val == None:
|
||||
val = dv.Get(var)
|
||||
else:
|
||||
uncomperr = dv.Uncompatible(var)
|
||||
if uncomperr:
|
||||
raise VariableError(uncomperr)
|
||||
if not dv.Uncompatible(var):
|
||||
dv.Set(var, val)
|
||||
except VariableError, e:
|
||||
mess = ''
|
||||
messages = e.message if type(e.message) == list else [e.message]
|
||||
for error in messages:
|
||||
mess += str(error) + '\n'
|
||||
errors.append(ReturnedMessage(type = 'error', field = var,
|
||||
message = mess))
|
||||
return errors
|
||||
|
||||
from calculate.api.server.baseClass import Basic
|
||||
from calculate.api.server.decorators import Dec
|
||||
|
||||
def groupCommon(self,sid,info,methodname):
|
||||
"""
|
||||
Install common method
|
||||
"""
|
||||
try:
|
||||
dv = self.get_cache(sid,methodname,"vars")
|
||||
if not dv:
|
||||
#reload(cl_install)
|
||||
dv = datavars.DataVarsApi()
|
||||
dv.importApi()
|
||||
dv.flIniFile()
|
||||
errors = self.check_req_params(dv, info,
|
||||
ordered=['cl_page_count',
|
||||
'cl_page_offset'],
|
||||
allvars=True)
|
||||
if errors:
|
||||
return errors
|
||||
request_meth = type("requestCommon",(self.Common,
|
||||
ApiWsdl, object), {})
|
||||
pid = self.startprocess(sid, target=request_meth,
|
||||
method="show_groups_meth",
|
||||
auto_delete = True,
|
||||
args_proc = (dv,))
|
||||
returnmess = ReturnedMessage(type = 'pid', message = pid, expert = True)
|
||||
returnmess.type = "pid"
|
||||
returnmess.message = pid
|
||||
dv = self.clear_cache(sid,methodname)
|
||||
return [returnmess]
|
||||
finally:
|
||||
if dv:
|
||||
self.set_cache(sid,methodname,"vars",dv,smart=False)
|
||||
return []
|
||||
|
||||
@rpc(Integer, GroupInfo, _returns = Array(ReturnedMessage))
|
||||
@Dec.check_permissions(["edit-group"])
|
||||
@Dec.console('cl-show-groups')
|
||||
@Dec.gui('Api',_('View Groups'),'user-group-properties,' \
|
||||
'view-certificate-import,application-certificate')
|
||||
def show_groups ( self, sid, info):
|
||||
if not info:
|
||||
mess = 'Field must be int!'
|
||||
errors = []
|
||||
for field in ['cl_page_count', 'cl_page_offset']:
|
||||
errors.append(ReturnedMessage(type = 'error', field = field,
|
||||
message = mess, expert = True))
|
||||
return errors
|
||||
dv = datavars.DataVarsApi()
|
||||
dv.importApi()
|
||||
dv.flIniFile()
|
||||
dv.Set('cl_page_count', info.cl_page_count)
|
||||
dv.Set('cl_page_offset', info.cl_page_offset)
|
||||
self.set_cache(sid, 'show_groups', "vars", dv, smart=False)
|
||||
return self.groupCommon(sid,info,'show_groups')
|
||||
|
||||
@rpc(Integer, Integer, Boolean,_returns = ViewInfo)
|
||||
@catchExcept()
|
||||
def show_groups_view (self, sid, step,expert):
|
||||
dv = datavars.DataVarsApi()
|
||||
dv.importApi()
|
||||
dv.flIniFile()
|
||||
dv.addGroup(_("Groups"),
|
||||
normal=('cl_page_count','cl_page_offset'),
|
||||
next_label=_("Next"))
|
||||
view = getViewForVariables (dv,step,expert)
|
||||
self.set_cache(sid, 'show_groups', "vars", dv, smart=False)
|
||||
return view
|
||||
|
||||
##################### BEGIN DETAIL GROUP METHOD ##############################
|
||||
@rpc(Integer, DetailGroupInfo, _returns = Array(ReturnedMessage))
|
||||
@Dec.check_permissions(["edit-group"])
|
||||
@Dec.console('cl-show-group')
|
||||
#@Dec.gui(_('Api'),_('Detail Request'),'view-certificate-import')
|
||||
def detail_group ( self, sid, info):
|
||||
if not info:
|
||||
mess = 'Field must be in list!'
|
||||
errors = []
|
||||
errors.append(ReturnedMessage(type = 'error', field = 'cl_group_name',
|
||||
message = mess))
|
||||
return errors
|
||||
|
||||
dv = datavars.DataVarsApi()
|
||||
dv.importApi()
|
||||
dv.flIniFile()
|
||||
dv.Set('cl_group_name', info.cl_group_name)
|
||||
self.set_cache(sid, 'detail_request', 'dv', dv, smart = False)
|
||||
return []
|
||||
|
||||
@rpc(Integer, Integer, Boolean, _returns = ViewInfo)
|
||||
@catchExcept()
|
||||
def detail_group_view (self, sid, step, expert):
|
||||
dv = self.get_cache(sid, 'detail_request', 'dv')
|
||||
if not dv:
|
||||
dv = datavars.DataVarsApi()
|
||||
dv.importApi()
|
||||
dv.flIniFile()
|
||||
dv.Get('cl_group_name')
|
||||
|
||||
dv.addGroup(_("Requests detail"),
|
||||
normal=('cl_group_name', 'cl_group_rights',
|
||||
'cl_page_count','cl_page_offset'))
|
||||
|
||||
view = getViewForVariables (dv,step,expert)
|
||||
|
||||
group = GroupField(name='',last=True)
|
||||
group.fields = []
|
||||
group.fields.append(Field(
|
||||
name = "but0",
|
||||
label = _("Back"),
|
||||
value = "show_groups",
|
||||
element = "button"))
|
||||
group.fields.append(Field(
|
||||
name = "but1",
|
||||
label = _("Change"),
|
||||
value = "change_group",
|
||||
element = "button"))
|
||||
group.fields.append(Field(
|
||||
name = "but2",
|
||||
label = _("Delete"),
|
||||
value = "delete_group",
|
||||
element = "button"))
|
||||
view.groups.append(group)
|
||||
self.set_cache(sid, 'show_groups', "vars", dv, smart=False)
|
||||
return view
|
||||
|
||||
############## END DETAIL GROUP METHOD #######################################
|
||||
############### BEGIN CONFIRM GROUP METHODS ###################################
|
||||
#def changeGroupCommon(self,sid,info,methodname):
|
||||
#try:
|
||||
#dv = self.get_cache(sid,methodname,"vars")
|
||||
#errors = self.check_req_params(dv, info,
|
||||
#ordered=['cl_req_id'],
|
||||
#allvars=True)
|
||||
#if errors:
|
||||
#return errors
|
||||
#request_meth = type("delRequestCommon",(self.Common,
|
||||
#ApiWsdl, object), {})
|
||||
#pid = self.startprocess(sid, target=request_meth,
|
||||
#method="confirm_request_meth",\
|
||||
#args_proc = (dv,))
|
||||
#returnmess = ReturnedMessage(type = 'pid', message = pid)
|
||||
#returnmess.type = "pid"
|
||||
#returnmess.message = pid
|
||||
#dv = self.clear_cache(sid,methodname)
|
||||
#return [returnmess]
|
||||
#finally:
|
||||
#if dv:
|
||||
#self.set_cache(sid,methodname,"vars",dv,smart=False)
|
||||
#return []
|
||||
|
||||
#def confirm_request_meth(self, dv):
|
||||
#try:
|
||||
#data_path = dv.Get('cl_api_data')
|
||||
#cert_path = dv.Get('cl_api_cert_path')
|
||||
|
||||
#cl_req_id = str(dv.Get('cl_req_id'))
|
||||
#cl_req_group = str(dv.Get('cl_req_group'))
|
||||
##self.startTask('Confirm Request')
|
||||
#self.printSUCCESS ('<b>'+_('Confirm Request')+'</b>')
|
||||
#self.printSUCCESS ("cl_req_id = %s" %cl_req_id)
|
||||
#self.printSUCCESS ("cl_req_group = %s" %cl_req_group)
|
||||
|
||||
#server_cert = cert_path + '/root.crt'
|
||||
#server_key = cert_path + '/root.key'
|
||||
|
||||
#cl_req = data_path + '/client_certs/%s.csr' %cl_req_id
|
||||
#cl_cert = data_path + '/client_certs/%s.crt' %cl_req_id
|
||||
#if not os.path.exists(cl_req):
|
||||
#self.printERROR (_("Signing Request %s not found") %cl_req)
|
||||
#return False
|
||||
|
||||
#if os.path.exists(cl_cert):
|
||||
#self.printERROR (_("certificate %s already exists") %cl_cert)
|
||||
#return False
|
||||
|
||||
#group = "group:%s" %cl_req_group
|
||||
#config = data_path + '/client_certs/ssl-client.cfg'
|
||||
#if os.path.exists(config):
|
||||
#os.unlink(config)
|
||||
|
||||
#cfg_text = ("[ ssl_client ]\n"
|
||||
#"basicConstraints = CA:FALSE\n"
|
||||
#"nsCertType = client\n"
|
||||
#"keyUsage = digitalSignature, keyEncipherment\n"
|
||||
#"extendedKeyUsage = clientAuth\n"
|
||||
#"nsComment = %s") %group
|
||||
#fc = open(config, 'w')
|
||||
#fc.write(cfg_text)
|
||||
#fc.close()
|
||||
|
||||
#cmd = ("openssl x509 -req -days 11000 -CA %s -CAkey %s -CAcreateserial "
|
||||
#"-extfile %s -extensions ssl_client -in %s -out %s") \
|
||||
#%(server_cert, server_key, config, cl_req, cl_cert)
|
||||
#print cmd
|
||||
#PIPE = subprocess.PIPE
|
||||
#p = subprocess.Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE,
|
||||
#stderr=subprocess.STDOUT, close_fds=True)
|
||||
#p.wait()
|
||||
#self.printSUCCESS ('<b>' + _("certificate %s is signed") %cl_cert \
|
||||
#+ '</b>')
|
||||
##self.endTask()
|
||||
#return True
|
||||
#except (KeyboardInterrupt,):
|
||||
#self.printERROR('Process is interrupted!')
|
||||
#return False
|
||||
|
||||
#@rpc(Integer, DetailRequestInfo, _returns = Array(ReturnedMessage))
|
||||
#@Dec.check_permissions(["edit-group"])
|
||||
##@Dec.console('cl-show-request')
|
||||
##@Dec.gui(_('Api'),_('Confirm Request'),'view-certificate-import')
|
||||
#@catchExcept()
|
||||
#def change_group ( self, sid, info):
|
||||
#dv = self.get_cache(sid,'confirm_request',"vars")
|
||||
#if not dv:
|
||||
#dv = datavars.DataVarsApi()
|
||||
#dv.importApi()
|
||||
#dv.flIniFile()
|
||||
#try:
|
||||
#dv.Set('cl_req_id', info.cl_req_id)
|
||||
#dv.Set('cl_req_group', info.cl_req_group)
|
||||
#except VariableError, e:
|
||||
#errors = []
|
||||
#mess = ''
|
||||
#messages = e.message if type(e.message) == list else [e.message]
|
||||
#for error in messages:
|
||||
#mess += str(error) + '\n'
|
||||
#errors.append(ReturnedMessage(type = 'error',
|
||||
#field = 'cl_req_group', message = mess))
|
||||
#return errors
|
||||
#self.set_cache(sid, 'confirm_request', "vars", dv, smart=False)
|
||||
#return self.changeGroupCommon(sid,info,'change_group')
|
||||
|
||||
#@rpc(Integer, Integer, Boolean, _returns = ViewInfo)
|
||||
#@catchExcept()
|
||||
#def change_group_view (self, sid, step, expert):
|
||||
#dv = self.get_cache(sid, 'confirm_request', 'dv')
|
||||
#if not dv:
|
||||
#dv = datavars.DataVarsApi()
|
||||
#dv.importApi()
|
||||
#dv.flIniFile()
|
||||
#dv.Get('cl_req_id')
|
||||
|
||||
#dv.addGroup(_("Change group"),
|
||||
#normal=('cl_req_id', 'cl_req_user_name', 'cl_req_ip', 'cl_req_mac',
|
||||
#'cl_req_date', 'cl_req_location', 'cl_req_group'),
|
||||
#next_label=_("Done"))
|
||||
|
||||
#view = getViewForVariables (dv,step,expert)
|
||||
|
||||
#group = GroupField(name=_("Change group"),nextlabel=_("Done"),last=True)
|
||||
#group.fields = []
|
||||
#group.fields.append(Field(
|
||||
#name = "but2",
|
||||
#label = "Confirm",
|
||||
#value = "confirm_request",
|
||||
#element = "button"))
|
||||
#view.groups.append(group)
|
||||
#self.set_cache(sid, 'delete_request', "vars", dv, smart=False)
|
||||
#return view
|
||||
############### END CONFIRM GROUP METHODS ###################################
|
||||
|
||||
|
||||
#def delRequestCommon(self,sid,info,methodname):
|
||||
#"""
|
||||
#Install common method
|
||||
#"""
|
||||
#try:
|
||||
#dv = self.get_cache(sid,methodname,"vars")
|
||||
##if not dv:
|
||||
##reload(cl_install)
|
||||
##dv = cl_install.DataVarsInstall()
|
||||
##dv.importInstall()
|
||||
##dv.flIniFile()
|
||||
##initfunc(dv)
|
||||
#errors = self.check_req_params(dv, info,
|
||||
#ordered=['cl_req_id'],
|
||||
#allvars=True)
|
||||
#if errors:
|
||||
#return errors
|
||||
#request_meth = type("delRequestCommon",(self.Common,
|
||||
#ApiWsdl, object), {})
|
||||
#pid = self.startprocess(sid, target=request_meth,
|
||||
#method="del_request_meth",\
|
||||
#args_proc = (dv,))
|
||||
#returnmess = ReturnedMessage(type = 'pid', message = pid)
|
||||
#returnmess.type = "pid"
|
||||
#returnmess.message = pid
|
||||
#dv = self.clear_cache(sid,methodname)
|
||||
#return [returnmess]
|
||||
#finally:
|
||||
#if dv:
|
||||
#self.set_cache(sid,methodname,"vars",dv,smart=False)
|
||||
#return []
|
||||
|
||||
#def del_request_meth(self, dv):
|
||||
##ob = datavars.DataVarsApi()
|
||||
##ob.importApi()
|
||||
|
||||
## set var env
|
||||
##ob.flIniFile()
|
||||
#try:
|
||||
#data_path = dv.Get('cl_api_data')
|
||||
#certbase = dv.Get('cl_api_database')
|
||||
|
||||
#id_del_req = str(dv.Get('cl_req_id'))
|
||||
#self.startTask("id_del_req = %s" %id_del_req)
|
||||
#print "id_del_req = ", id_del_req
|
||||
#request = data_path + '/client_certs/%s.csr' %id_del_req
|
||||
#cert = data_path + '/client_certs/%s.crt' %id_del_req
|
||||
|
||||
## chect exists request and certificate files
|
||||
#if not os.path.exists(request) and not os.path.exists(cert):
|
||||
#self.printERROR (_("Request and certificate with id = %s not found!") \
|
||||
#%id_del_req)
|
||||
#return False
|
||||
|
||||
#if not os.path.exists(request):
|
||||
#self.printERROR (_("request %s not found!") %request)
|
||||
|
||||
#if os.path.exists(cert):
|
||||
#self.printERROR (_("This request has already been signed"))
|
||||
|
||||
## create temp file
|
||||
#ft = open(certbase + '_temp', 'w')
|
||||
#with open(certbase) as fd:
|
||||
#t = fd.read()
|
||||
## See each line
|
||||
#for line in t.splitlines():
|
||||
## and each word in line
|
||||
#words = line.split()
|
||||
## if in line present certificate id
|
||||
#if not words[0] == id_del_req:
|
||||
#ft.write(line + '\n')
|
||||
#ft.close()
|
||||
#fd.close()
|
||||
|
||||
#ft = open(certbase + '_temp', 'rb')
|
||||
#fc = open(certbase, 'wb')
|
||||
#ft.seek(0)
|
||||
#fc.write(ft.read())
|
||||
#ft.close()
|
||||
#fc.close()
|
||||
#os.unlink(certbase + '_temp')
|
||||
#try:
|
||||
#if os.path.exists(request):
|
||||
#os.unlink (request)
|
||||
#self.printSUCCESS (_("request deleted"))
|
||||
#if os.path.exists(cert):
|
||||
#os.unlink (cert)
|
||||
#self.printSUCCESS (_("certificate deleted"))
|
||||
#except:
|
||||
#self.printERROR (_("delete file error!"))
|
||||
#self.endTask()
|
||||
#except KeyboardInterrupt:
|
||||
#self.endTask()
|
||||
#return False
|
||||
#except Exception, e:
|
||||
#msg = e.message
|
||||
#if not msg:
|
||||
#msg = e.reason
|
||||
#self.printERROR (_("Exception!%s") %msg)
|
||||
#return False
|
||||
|
||||
|
||||
#@rpc(Integer, DetailRequestInfo, _returns = Array(ReturnedMessage))
|
||||
#@Dec.check_permissions(["request"])
|
||||
#@Dec.console('cl-show-request')
|
||||
##@Dec.gui(_('Api'),_('Delete Request'),'view-certificate-import')
|
||||
#@catchExcept()
|
||||
#def delete_request ( self, sid, info):
|
||||
##dv = self.get_cache(sid,'delete_request',"vars")
|
||||
##if not dv:
|
||||
#dv = datavars.DataVarsApi()
|
||||
#dv.importApi()
|
||||
#dv.flIniFile()
|
||||
#dv.Set('cl_req_id', info.cl_req_id)
|
||||
#self.set_cache(sid, 'delete_request', "vars", dv, smart=False)
|
||||
#return self.delRequestCommon(sid,info,'delete_request')
|
||||
|
||||
#@rpc(Integer, Integer, Boolean, _returns = ViewInfo)
|
||||
#@catchExcept()
|
||||
#def delete_request_view (self, sid, step, expert):
|
||||
#dv = self.get_cache(sid, 'detail_request', 'dv')
|
||||
#if not dv:
|
||||
#dv = datavars.DataVarsApi()
|
||||
#dv.importApi()
|
||||
#dv.flIniFile()
|
||||
#dv.Get('cl_req_id')
|
||||
|
||||
#dv.addGroup(_("Requests delete"),
|
||||
#normal=('cl_req_id', 'cl_req_user_name', 'cl_req_ip', 'cl_req_mac',
|
||||
#'cl_req_date', 'cl_req_location', 'cl_req_group'),
|
||||
#next_label=_("Done"))
|
||||
|
||||
#view = getViewForVariables (dv,step,expert)
|
||||
|
||||
#group = GroupField(name=_("Requests detail"),nextlabel=_("Done"),last=True)
|
||||
#group.fields = []
|
||||
#group.fields.append(Field(
|
||||
#name = "but2",
|
||||
#label = "Delete",
|
||||
#value = "delete_request",
|
||||
#element = "button"))
|
||||
#view.groups.append(group)
|
||||
#self.set_cache(sid, 'delete_request', "vars", dv, smart=False)
|
||||
#return view
|
@ -0,0 +1,137 @@
|
||||
#-*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2010-2012 Calculate Ltd. http://www.calculate-linux.org
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0 #
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# mode - read only or writeable variable
|
||||
# value - default variable value
|
||||
# select - list of posible values for variable
|
||||
# hide - flag, if it is True, then the variable is not printable
|
||||
# printval - print value of variable
|
||||
|
||||
from calculate.lib.datavars import Variable,ReadonlyVariable,VariableError
|
||||
import os, glob, sys
|
||||
from os import path
|
||||
import OpenSSL
|
||||
|
||||
from calculate.lib.cl_lang import setLocalTranslate
|
||||
from calculate.lib.utils.files import readLinesFile
|
||||
from calculate.api.server.decorators import Dec
|
||||
#from calculate.api.cert_cmd import find_id_cert
|
||||
setLocalTranslate('cl_api',sys.modules[__name__])
|
||||
|
||||
class VariableClGroupName(Variable):
|
||||
"""
|
||||
Certificate Group
|
||||
"""
|
||||
type = "choice"
|
||||
opt = ["--group-name"]
|
||||
metavalue = "GROUP_NAME"
|
||||
|
||||
def init(self):
|
||||
self.help = _("set certificate group")
|
||||
self.label = _("Group name")
|
||||
|
||||
def choice(self):
|
||||
group_rights = self.Get('cl_api_group_rights')
|
||||
|
||||
t = open(group_rights, 'r').read()
|
||||
result = []
|
||||
for line in t.splitlines():
|
||||
words = line.split()
|
||||
|
||||
if not words[0].startswith('#'):
|
||||
result.append(words[0])
|
||||
if not 'all' in result:
|
||||
result.append('all')
|
||||
return result
|
||||
|
||||
#def get(self):
|
||||
#req_id = self.Get('cl_req_id')
|
||||
#data_path = self.Get('cl_api_data')
|
||||
|
||||
#cert_file = data_path + '/client_certs/%s.crt' %req_id
|
||||
#if os.path.exists(cert_file):
|
||||
#fp = open(cert_file, 'r')
|
||||
#cert = fp.read()
|
||||
#fp.close()
|
||||
#certobj = OpenSSL.crypto.load_certificate \
|
||||
#(OpenSSL.SSL.FILETYPE_PEM, cert)
|
||||
#com = certobj.get_extension(certobj.get_extension_count()-1).get_data()
|
||||
#return com.split(':')[1]
|
||||
#return ''
|
||||
|
||||
def check(self, group):
|
||||
group_rights = self.Get('cl_api_group_rights')
|
||||
|
||||
if group == 'all':
|
||||
return
|
||||
t = open(group_rights, 'r').read()
|
||||
for line in t.splitlines():
|
||||
words = line.split()
|
||||
if words[0].startswith('#'):
|
||||
continue
|
||||
if group == words[0]:
|
||||
return
|
||||
raise VariableError(_("Group %s does not exist") %group)
|
||||
|
||||
|
||||
def uniq(seq):
|
||||
seen = set()
|
||||
seen_add = seen.add
|
||||
return [ x for x in seq if x not in seen and not seen_add(x)]
|
||||
|
||||
class VariableClGroupRights(Variable):
|
||||
"""
|
||||
Certificate Group
|
||||
"""
|
||||
type = "choice-list"
|
||||
opt = ["-g"]
|
||||
metavalue = "REQ_GROUP"
|
||||
|
||||
def init(self):
|
||||
self.help = _("set certificate group")
|
||||
self.label = _("Group rights")
|
||||
|
||||
def choice(self):
|
||||
right_list = []
|
||||
for key in Dec.rightsMethods.keys():
|
||||
right_list += Dec.rightsMethods[key]
|
||||
|
||||
uniq_right_list = uniq(right_list)
|
||||
uniq_right_list.sort()
|
||||
return uniq_right_list
|
||||
|
||||
def get(self):
|
||||
group_name = self.Get('cl_group_name')
|
||||
group_rights = self.Get('cl_api_group_rights')
|
||||
|
||||
t = open(group_rights, 'r').read()
|
||||
results = []
|
||||
for line in t.splitlines():
|
||||
words = line.split(' ',1)
|
||||
if words[0] == group_name:
|
||||
methods = words[1].split(',')
|
||||
for i in methods:
|
||||
results.append(i.strip())
|
||||
|
||||
if group_name == 'all' and results == []:
|
||||
right_list = []
|
||||
for key in Dec.rightsMethods.keys():
|
||||
right_list += Dec.rightsMethods[key]
|
||||
|
||||
uniq_right_list = uniq(right_list)
|
||||
uniq_right_list.sort()
|
||||
return uniq_right_list
|
||||
|
||||
return results
|
Loading…
Reference in new issue