PyAD AD查询是否存在用户是否存在

时间:2017-03-18 15:30:04

标签: python python-2.7 active-directory yaml pyad

我对Python很新,我正在尝试查询AD服务器,并根据某些属性(用户名,firstName,lastName)检查用户是否存在,如果该用户存在则查询他们的组以查看他们是否存在是否在特定组中,如果没有,请将它们添加到该组。我有一个YAML文件,我存储用户的信息,以及我想要添加的组:

ADUser:
    firstName: <value>
    lastName: <value>
    username: <value>
    email: <value>
    group: <value of group I want them to join>

以下是解析yaml文件的代码:

with open("AD.yaml", 'r') as stream:
    try:
        print(yaml.load(stream))
    except yaml.YAMLError as exc:
        print(exc)

我正在使用PyAD库来访问AD并运行我的搜索以及用户创建和组设置。以下是文档的链接:https://zakird.com/pyad/index.html

这是我开始写的:

    q = pyad.adquery.ADQuery()
    q.execute_query(
        attributes = (["firstName", <value>],["lastName", <value>],["username", <value>],["email", <value>])
        where_clause = "objectClass = '*'",
        base_dn = "OU=users, DC=domain, DC=com"
    )

这些是我想用于用户创建,组添加和查询的方法(我试图弄清楚这些是否好看,以及如何将它们与我目前使用的YAML一起使用):

#User Creation
create_user(name, password=None, upn_suffix=None, enable=True, optional_attributes={})
#Find Members of a group
get_memberOfs(recursive=False, scope='all')¶
#Add an object to a group
add_to_group(group)
#Query AD
q = pyad.adquery.ADQuery()
q.execute_query(
    attributes = ["distinguishedName", "description"],
    where_clause = "objectClass = '*'",
    base_dn = "OU=users, DC=domain, DC=com"
)

我只是想知道是否有人可以帮助指出我应该如何设置。不要担心访问实际的AD服务器我只是想象从盒子本身运行它。

这是我到目前为止所做的:

with open("AD.yaml", 'r') as stream:
    try:
        print(yaml.load(stream))
    except yaml.YAMLError as exc:
        print(exc)

def create_User(new_user, group, ):

    q = pyad.adquery.ADQuery()
    user = q.execute_query(
              attributes = ["firstName", "description"],
              where_clause = "objectClass = '*'",
              base_dn = "OU=users, DC=domain, DC=com"
           )
    if user == true:
        if user.is_member_of(group, "")
            logging.info('User is already created and is member of Specified AD Group')
        else
            user.add_to_group(user, group)
    else
        new_user = ADUser.create("%firstName", "%lastname", "" )
        group = ADGroup.from_dn(group)
        group.add_member(new_user)

1 个答案:

答案 0 :(得分:2)

您正在加载用户的信息,但您只是打印它。你应该至少存储它以供进一步使用。除此之外,绝对不需要使用不安全的yaml.load()

from ruamel import yaml

with open("AD.yaml", 'r') as stream:
    try:
        data = yaml.load(stream)
        print(data)
    except yaml.YAMLError as exc:
        print(exc)

你可以调用你的方法:

 user = data['AdUser']
 create_User(user['username'], user['group'])

除了不使用yaml.load()之外,还有一些事情需要考虑:

    在Python中
  • 你不应该在自己编写的方法中使用camel_case(某些库符合非Pythonesc特定的大小写),所以将create_User更改为create_user
  • 为您要注册的其他内容(电子邮件等)添加参数create_user
  • 考虑将文件的顶层设为一个序列,这样您就可以迭代从YAML加载的数据并一次注册多个用户

    - firstName: <value>
      lastName: <value>
      username: <value>
      email: <value>
      group: <value>
    - firstName: <value>
      lastName: <value>
      username: <value>
      email: <value>
      group: <value>
    

    然后执行:

    for user in data:
        create_user(user['username'], user['group'])