mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
Add some more supporting functions
Do a little bit more error handling and checking
This commit is contained in:
@@ -17,9 +17,6 @@
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
#
|
||||
|
||||
import sys
|
||||
sys.path.insert(0, ".")
|
||||
sys.path.insert(0, "..")
|
||||
import ldap
|
||||
from ipa_server.context import context
|
||||
import ipautil
|
||||
@@ -109,6 +106,43 @@ def get_entry_by_dn (dn, sattrs=None):
|
||||
# logging.info("IPA: get_entry_by_dn '%s'" % dn)
|
||||
return get_base_entry(dn, searchfilter, sattrs)
|
||||
|
||||
# User support
|
||||
|
||||
def is_user_unique(uid):
|
||||
"""Return True if the uid is unique in the tree, False otherwise."""
|
||||
# FIXME
|
||||
# uid = self.__safe_filter(uid)
|
||||
searchfilter = "(&(uid=%s)(objectclass=posixAccount))" % uid
|
||||
|
||||
try:
|
||||
entry = get_sub_entry("cn=accounts," + basedn, searchfilter, ['dn','uid'])
|
||||
return False
|
||||
# except ipaerror.exception_for(ipaerror.LDAP_NOT_FOUND):
|
||||
except Exception:
|
||||
return True
|
||||
|
||||
def uid_too_long(uid):
|
||||
"""Verify that the new uid is within the limits we set. This is a
|
||||
very narrow test.
|
||||
|
||||
Returns True if it is longer than allowed
|
||||
False otherwise
|
||||
"""
|
||||
if not isinstance(uid,basestring) or len(uid) == 0:
|
||||
# It is bad, but not too long
|
||||
return False
|
||||
# logging.debug("IPA: __uid_too_long(%s)" % uid)
|
||||
try:
|
||||
config = get_ipa_config()
|
||||
maxlen = int(config.get('ipamaxusernamelength', 0))
|
||||
if maxlen > 0 and len(uid) > maxlen:
|
||||
return True
|
||||
except Exception, e:
|
||||
# logging.debug("There was a problem " + str(e))
|
||||
pass
|
||||
|
||||
return False
|
||||
|
||||
def update_entry (oldentry, newentry):
|
||||
"""Update an LDAP entry
|
||||
|
||||
@@ -130,10 +164,14 @@ def update_entry (oldentry, newentry):
|
||||
res = context.conn.getConn().updateEntry(moddn, oldentry, newentry)
|
||||
return res
|
||||
|
||||
def add_entry(entry):
|
||||
"""Add a new entry"""
|
||||
return context.conn.getConn().addEntry(entry)
|
||||
|
||||
def uniq_list(x):
|
||||
"""Return a unique list, preserving order and ignoring case"""
|
||||
myset = {}
|
||||
return [set.setdefault(e.lower(),e) for e in x if e.lower() not in myset]
|
||||
return [myset.setdefault(e.lower(),e) for e in x if e.lower() not in myset]
|
||||
|
||||
def get_schema():
|
||||
"""Retrieves the current LDAP schema from the LDAP server."""
|
||||
|
||||
@@ -24,6 +24,7 @@ Lightwieght XML-RPC client using Python standard library xmlrpclib.
|
||||
"""
|
||||
|
||||
import xmlrpclib
|
||||
import socket
|
||||
from ipalib.backend import Backend
|
||||
from ipalib.util import xmlrpc_marshal
|
||||
from ipalib import api
|
||||
@@ -44,7 +45,10 @@ class xmlrpc(Backend):
|
||||
client = self.get_client()
|
||||
command = getattr(client, name)
|
||||
params = xmlrpc_marshal(*args, **kw)
|
||||
return command(*params)
|
||||
# return command(*args, **kw)
|
||||
try:
|
||||
return command(*params)
|
||||
except socket.error, e:
|
||||
print e[1]
|
||||
return False
|
||||
|
||||
api.register(xmlrpc)
|
||||
|
||||
@@ -28,7 +28,6 @@ from ipalib import api
|
||||
from ipa_server import servercore
|
||||
from ipa_server import ipaldap
|
||||
import ldap
|
||||
from ipa_server.context import context
|
||||
|
||||
|
||||
class group(frontend.Object):
|
||||
@@ -82,7 +81,7 @@ class group_add(crud.Add):
|
||||
for g in group:
|
||||
entry.setValues(g, group[g])
|
||||
|
||||
result = context.conn.getConn().addEntry(entry)
|
||||
result = servercore.add_entry(entry)
|
||||
return result
|
||||
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ from ipalib import api
|
||||
from ipa_server import servercore
|
||||
from ipa_server import ipaldap
|
||||
import ldap
|
||||
from ipa_server.context import context
|
||||
|
||||
# Command to get the idea how plugins will interact with api.env
|
||||
class envtest(frontend.Command):
|
||||
@@ -94,6 +93,13 @@ class user_add(crud.Add):
|
||||
|
||||
user['uid'] = args[0]
|
||||
|
||||
if not servercore.is_user_unique(user['uid']):
|
||||
# FIXME, specific error
|
||||
raise SyntaxError("user already exists")
|
||||
if servercore.uid_too_long(user['uid']):
|
||||
# FIXME, specific error
|
||||
raise SyntaxError("uid is too long")
|
||||
|
||||
# dn is set here, not by the user
|
||||
try:
|
||||
del user['dn']
|
||||
@@ -159,8 +165,12 @@ class user_add(crud.Add):
|
||||
for u in user:
|
||||
entry.setValues(u, user[u])
|
||||
|
||||
result = context.conn.getConn().addEntry(entry)
|
||||
result = servercore.add_entry(entry)
|
||||
return result
|
||||
def forward(self, *args, **kw):
|
||||
result = super(crud.Add, self).forward(*args, **kw)
|
||||
if result != False:
|
||||
print result
|
||||
|
||||
api.register(user_add)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user