You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculate-utils-3-core/core/variables/edit_groups.py

155 lines
4.7 KiB

#-*- 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.core.server.decorators import Dec
#from calculate.api.cert_cmd import find_id_cert
setLocalTranslate('cl_core',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_core_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_core_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_core_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_core_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
class VariableClGroupAdd(Variable):
"""
Certificate Identification
"""
opt = ["--add-group"]
metavalue = "ADD_GROUP"
def init(self):
self.help = _("Group name for added")
self.label = _("Group name")
def check(self, group):
name_re = re.compile("^[a-zA-Z_0-9]{3,20}$")
if name_re.findall(user_name):
raise VariableError( \
_("Group name must consist of words, digits and simbol '_'"))