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-2.1-server/pym/cl_ldap.py

318 lines
11 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#-*- coding: utf-8 -*-
#Copyright 2008 Calculate Pack, http://www.calculate-linux.ru
#
# 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 cl_base
import sys
Version = "calculate-server 0.0.1"
tr = cl_base.lang()
tr.setLanguage(sys.modules[__name__])
class cl_ldap:
def getChapterNumber(self,NameChapter):
# Показать номер раздела по его имени
num = 0
for i in self.chapter:
if i[0] == NameChapter:
return num
num += 1
return False
def __init__(self, cmdName):
self.cmdName = cmdName
self.shortOpt = []
#короткие опции командной строки
self.longOpt = []
#длинные опции командной строки
#optEnd = ""
#if "user" in self.cmdName and not "mod" in self.cmdName:
#optEnd = _("user")
#elif "group" in self.cmdName and not "mod" in self.cmdName:
#optEnd = _("group")
servName = ""
if "user" in self.cmdName:
servName = _("user")
elif "group" in self.cmdNamee:
servName = _("group")
self.chapter = [\
# расположение разделов на странице
# имя раздела, видимый или невидимый, кол. "\n" после
# названия раздела, кол. "\n" после раздела
("Copyright",False,0,2),
(_("Usage"),True,0,1),
("Function",False,0,2),
(_("Examples"),True,1,1),
(_("Options"),True,1,1),
(_("Services"),True,1,1),
(_("Informative output"),True,1,0),
]
self.chapterBloc = []
# разделы справки
for i in self.chapter:
self.chapterBloc.append("")
# имена используемых программ и их номера для доступа к переменным
# self.data
self.progName = { 'cl-groupadd':0,
'cl-groupdel':1,
'cl-groupmod':2,
'cl-useradd':3,
'cl-userdel':4,
'cl-usermod':5,
}
self.data = [\
# Options
{'progAccess':(3,),
'chortOption':"p",
'longOption':"password",
'optVal':"file",
'helpChapter':_("Options"),
'help':_("input user password")
},
# Services
{'helpChapter':_("Services"),
'help':" samba \t\tsamba " + servName + "\n"
},
{'helpChapter':_("Services"),
'help':" mail \t\t\tmail " + servName + "\n"
},
{'helpChapter':_("Services"),
'help':" ftp \t\t\tftp " + servName + "\n"
},
{'helpChapter':_("Services"),
'help':" proxy \t\tproxy " + servName + "\n"
},
{'helpChapter':_("Services"),
'help':" radius \t\tradius " + servName + "\n"
},
{'helpChapter':_("Services"),
'help':" jabber \t\tjabber " + servName + "\n"
},
{'helpChapter':_("Services"),
'help':" addressbook \t\taddressbook " + servName + "\n"
},
{'helpChapter':_("Services"),
'help':" dhcp \t\t\tdhcp " + servName + "\n"
},
{'helpChapter':_("Services"),
'help':" named \t\tnamed " + servName + "\n"
},
{'helpChapter':_("Services"),
'help':" wiki \t\t\twiki " + servName + "\n"
},
# Informative output
{'chortOption':"h",
'longOption':"help",
'helpChapter':_("Informative output"),
'help':_("display this help and exit")
},
{
#'progAccess':(3,),
'helpChapter':"Copyright",
'help':Version
},
{
'progAccess':(3,),
'helpChapter':_("Usage"),
'help': self.cmdName + " " + " [" + _("options") + "] " + _("user") +\
" " + _("service")
},
{
'progAccess':(0,),
'helpChapter':"Function",
'help':_("Adds group in LDAP directory of service")
},
{
'progAccess':(1,),
'helpChapter':"Function",
'help':_("Deletes group from LDAP of directory of service")
},
{
'progAccess':(2,),
'helpChapter':"Function",
'help':_("Modifies group profiles from LDAP of directory of service")
},
{
'progAccess':(3,),
'helpChapter':"Function",
'help':_("Adds user in LDAP directory of service")
},
{
'progAccess':(4,),
'helpChapter':"Function",
'help':_("Deletes user from LDAP of directory of service")
},
{
'progAccess':(5,),
'helpChapter':"Function",
'help':_("Modifies user profiles from LDAP of directory of service")
},
{
'progAccess':(0,),
'helpChapter':_("Examples"),
'help':" " + _("cl-groupadd -smfw guest\t # add group guest \
in services:\n\
\t\t\t\t # samba, mail, ftp, wiki.")
},
{
'progAccess':(1,),
'helpChapter':_("Examples"),
'help':" " + _("cl-groupdel -smfw guest\t # delete group \
guest in services:\n\
\t\t\t\t # samba, mail, ftp, wiki.""")
},
{
'progAccess':(2,),
'helpChapter':_("Examples"),
'help':""
},
{
'progAccess':(3,),
'helpChapter':_("Examples"),
'help':" " + "cl-useradd guest samba #" +_("add user guest in \
service samba") + ".\n"
},
{
'progAccess':(4,),
'helpChapter':_("Examples"),
'help': " " + _("cl-userdel -smfw guest\t # delete user \
guest in services:\n\
\t\t\t\t # samba, mail, ftp, wiki.""")
},
{
'progAccess':(5,),
'helpChapter':_("Examples"),
'help':""
},
]
self.__setParamHelp()
def addChapterHelp(self,numChapter,helpTxt):
# Добавить в раздел помощи numChapteк тектстовую строку helpTxt
self.chapterBloc[numChapter] += helpTxt
return True
def addData(self,dataHash):
# На будущее (добавляет опции)
self.data.append(dataHash)
return True
def access(self,dataHash):
# доступна ли опция вызывающей программе
numProg = self.progName[self.cmdName]
if dataHash.has_key('progAccess'):
if numProg in dataHash['progAccess']:
return True
else:
return False
else:
return True
def __setParamHelp(self):
# записать параметры в справку
sp = []
for par in self.data:
if par.has_key("chortOption"):
if self.access(par):
sp.append(par["chortOption"])
sp.sort()
for shortOption in sp:
for par in self.data:
if par.has_key("chortOption"):
if par["chortOption"] == shortOption:
tab = "\t\t"
if par.has_key("optVal"):
longOption = par["longOption"] + "=" +\
par["optVal"]
else:
longOption = par["longOption"]
if len(longOption) >= 8:
tab = "\t"
elif len(longOption) >= 16:
tab = ""
numChapter = self.getChapterNumber(par['helpChapter'])
helpTxt = " -" + par["chortOption"] + ", --" +\
longOption + tab + par['help'] + "\n"
self.addChapterHelp(numChapter,helpTxt)
break
for par in self.data:
if not par.has_key("chortOption") and \
self.access(par):
helpTxt = par['help']
numChapter = self.getChapterNumber(par['helpChapter'])
self.addChapterHelp(numChapter,helpTxt)
def getHelp(self):
# Выдать справку
help = ""
for numChapter in range(len(self.chapter)):
chapter = self.chapter[numChapter]
nameChapter = chapter[0]
visibleChapter = chapter[1]
beforeStrChapter = chapter[2]
afterStrChapter = chapter[3]
bef = ""
for i in range(beforeStrChapter):
bef += "\n"
aft = ""
for i in range(afterStrChapter):
aft += "\n"
if visibleChapter:
help += nameChapter + ": " + bef
help += self.chapterBloc[numChapter] + aft
return help
def getAllOpt(self,typeOpt="short"):
# Выдать все действующие опции
if typeOpt=="short":
if len(self.shortOpt) == 0:
for par in self.data:
if par.has_key("chortOption") and self.access(par):
if par.has_key("optVal"):
self.shortOpt.append(par["chortOption"]+':')
else:
self.shortOpt.append(par["chortOption"])
return "".join(self.shortOpt)
if typeOpt=="long":
if len(self.longOpt) == 0:
for par in self.data:
if par.has_key("longOption") and self.access(par):
if par.has_key("optVal"):
self.longOpt.append(par["longOption"]+'=')
else:
self.longOpt.append(par["longOption"])
return self.longOpt
def getShortOpt(self,option):
# Из любой опции получаем короткую опцию
for par in self.data:
if par.has_key("chortOption") and self.access(par):
if par["longOption"] == option or \
par["chortOption"] == option:
return par["chortOption"]
break
return ""