diff --git a/pyad/adcontainer.py b/pyad/adcontainer.py index 2461b8b..ce81460 100644 --- a/pyad/adcontainer.py +++ b/pyad/adcontainer.py @@ -26,14 +26,17 @@ def __create_object(self, type_, name): prefixed_name = '='.join((prefix,name)) return self._ldap_adsi_obj.Create(type_, prefixed_name) - def create_user(self, name, password=None, upn_suffix=None, enable=True,optional_attributes={}): + def create_user(self, sAMAccountName, cn=None, password=None, upn_suffix=None, enable=True,optional_attributes={}): """Create a new user object in the container""" try: if not upn_suffix: upn_suffix = self.get_domain().get_default_upn() - upn = '@'.join((name, upn_suffix)) - obj = self.__create_object('user', name) - obj.Put('sAMAccountName', optional_attributes.get('sAMAccountName', name)) + upn = '@'.join((sAMAccountName, upn_suffix)) + if not cn: + obj = self.__create_object('user', sAMAccountName) + else: + obj = self.__create_object('user', cn) + obj.Put('sAMAccountName', optional_attributes.get('sAMAccountName', sAMAccountName)) obj.Put('userPrincipalName', upn) obj.SetInfo() pyadobj = ADUser.from_com_object(obj) diff --git a/pyad/adsearch.py b/pyad/adsearch.py index b46c6cf..2be34df 100644 --- a/pyad/adsearch.py +++ b/pyad/adsearch.py @@ -17,6 +17,32 @@ def by_cn(cn, search_base=None, options={}): type="GC") return _ad_query_obj.get_single_result()['distinguishedName'] +def by_sam(sAMAccountName, search_base=None, options={}): + if not search_base: + if not ADBase.default_domain: + raise Exception("Unable to detect default domain. Must specify search base.") + search_base = ADBase.default_domain + _ad_query_obj.reset() + + _ad_query_obj.execute_query(where_clause=("sAMAccountName = '%s'" % sAMAccountName), + base_dn=search_base, + options=options, + type="GC") + return _ad_query_obj.get_single_result()['distinguishedName'] + +def by_mail(mail, search_base=None, options={}): + if not search_base: + if not ADBase.default_domain: + raise Exception("Unable to detect default domain. Must specify search base.") + search_base = ADBase.default_domain + _ad_query_obj.reset() + + _ad_query_obj.execute_query(where_clause=("mail = '%s'" % mail), + base_dn=search_base, + options=options, + type="GC") + return _ad_query_obj.get_single_result()['distinguishedName'] + def by_upn(upn, search_base=None, options={}): if not search_base: if not ADBase.default_forest: @@ -56,9 +82,9 @@ def all_results_by_cn(cn, search_base=None, options={}): def all_results_by_upn(upn, search_base=None, options={}): if not search_base: - if not ADBase.default_forest: - raise Exception("Unable to detect default forest. Must specify search base.") - search_base = ADBase.default_forest + if not ADBase.default_domain: + raise Exception("Unable to detect default domain. Must specify search base.") + search_base = ADBase.default_domain _ad_query_obj.reset() _ad_query_obj.execute_query(where_clause=("userPrincipalName = '%s'" % upn), base_dn=search_base, diff --git a/pyad/aduser.py b/pyad/aduser.py index d0d0644..de643fc 100644 --- a/pyad/aduser.py +++ b/pyad/aduser.py @@ -6,11 +6,12 @@ class ADUser(ADObject): @classmethod - def create(cls, name, container_object, password=None, upn_suffix=None, + def create(cls, sAMAccountName, container_object, cn=None, password=None, upn_suffix=None, enable=True, optional_attributes={}): """Creates and returns a new active directory user""" return container_object.create_user( - name=name, + sAMAccountName=sAMAccountName, + cn=cn, password=password, upn_suffix=upn_suffix, enable=enable,