Home > Blockchain >  Returning a value that is not excluded in python
Returning a value that is not excluded in python

Time:01-19

I would like to create a function that would check if a given account is not a technical account. If the account is technical, we skip it, if not, we display account details.

technical accounts are saved in the addc_accounts_excluded.json file:

{
    "sAMAccountName": [
        "spiderman",
        "ironman"
    ]
}

all accounts are saved in the encoded_users_from_LDAP.json file:

{
    "entries": [
        {
            "attributes": {
                "cn": "Bruce Wayne",
                "primaryGroupID": 513,
                "sAMAccountName": "batman",
                "userAccountControl": 514,
                "whenCreated": "2016-04-19 10:06:25 00:00"
            },
            "dn": "CN=Bruce Wayne,OU=Users,OU=DC-COMICS,DC=universum,DC=local"
        },
        {
            "attributes": {
                "cn": "Clark Kent",
                "primaryGroupID": 513,
                "sAMAccountName": "superman",
                "userAccountControl": 514,
                "whenCreated": "2016-04-19 10:06:25 00:00"
            },
            "dn": "CN=Clark Kent,OU=Users,OU=DC-COMICS,DC=universum,DC=local"
        },
        {
            "attributes": {
                "cn": "Peter Parker",
                "primaryGroupID": 513,
                "sAMAccountName": "spiderman",
                "userAccountControl": 514,
                "whenCreated": "2016-04-19 10:06:25 00:00"
            },
            "dn": "CN=Peter Parker,OU=Users,OU=DC-COMICS,DC=universum,DC=local"
        }
    ]
}

my python code:

### load data from JSON files
# addc_accounts_excluded.json
accounts_excluded = root_path   json_dir   accounts_excluded_file
with open(accounts_excluded, 'r', encoding="UTF-8") as file:
    data = json.load(file)
    excluded_users = data['sAMAccountName']

# encoded_users_from_LDAP.json
encoded_retrived_users = root_path   json_dir   "test.json"
with open(encoded_retrived_users, 'r', encoding="UTF-8") as file:
    data = json.load(file)
    retrived_users = data['entries']


def user_validation(): # this what I need to validate accounts


for user in retrived_users:
    attributes = user['attributes']
    sAMAccountName = attributes['sAMAccountName']
    if ('is the user excluded? if not do it'):
        print(sAMAccountName)
        print(attributes['cn'])    

CodePudding user response:

If my understanding of your problem is correct, this should work:

def user_validation(user: str, excluded_users: str):
  return user not in excluded_users

CodePudding user response:

At the moment I solved the problem like this:

def user_validation(suspect):
    account = True
    for account_checked in excluded_users:
        if (account_checked == suspect):
            account = False
    return account


for user in retrived_users:
    attributes = user['attributes']
    sAMAccountName = attributes['sAMAccountName']
    cn = attributes['cn']
    if(user_validation(sAMAccountName) == True):
        print("---------------------------")
        print(sAMAccountName)
        print(attributes['cn'])
  • Related