agent: |
cGKcSt6sN0qUfZFwn84yAssign Permissions and Licenses
Assign Permissions and Licenses
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
Assign necessary permissions and appropriate Microsoft 365 licenses to the user. This includes access rights to various services and applications as required by their role. DEMO Script: Not to be used in production.
inputs
outputs
# This script does the following: #
# # #
# > Authenticate using Azure credentials. #
# > Retrieve available licenses from Microsoft 365. #
# > Map user roles to specific licenses. #
# > Assign the appropriate licenses to the user based on their role. #
# # #
import requests
from azure.identity import ClientSecretCredential
# Azure AD credentials setup
tenant_id = "your-tenant-id"
client_id = "your-client-id"
client_secret = "your-client-secret"
# user_id = "7f16743b-2b12-4697-b065-d9637a1b30e7" # User ID from user creation step
# user_info to be received from upstream task
user_id = user_info["id"]
# Role to license mapping (example mapping, update as necessary)
role_license_map = {
"Software Developer": "ENTERPRISEPACK", # Office 365 E3
"HR Manager": "SPE_E3", # Office 365 E3 + EMS E3
"Sales Executive": "DYN365_ENTERPRISE_SALES"
}
# User's role (this would be dynamically determined or inputted)
user_role = "Software Developer"
# Initialize the Azure credentials
credential = ClientSecretCredential(tenant_id, client_id, client_secret)
# Function to get the list of available licenses
def get_available_licenses():
url = "https://graph.microsoft.com/v1.0/subscribedSkus"
access_token = credential.get_token('https://graph.microsoft.com/.default').token
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
# Function to assign licenses to a user
def assign_license_to_user(user_id, sku_id):
url = f"https://graph.microsoft.com/v1.0/users/{user_id}/assignLicense"
access_token = credential.get_token('https://graph.microsoft.com/.default').token
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
license_assignments = {
"addLicenses": [{"skuId": sku_id}],
"removeLicenses": []
}
response = requests.post(url, headers=headers, json=license_assignments)
response.raise_for_status()
return response.json()
# Main logic to assign licenses based on the user's role
try:
available_licenses = get_available_licenses()
license_to_assign = next((item for item in available_licenses['value'] if item['skuPartNumber'] == role_license_map[user_role]), None)
if license_to_assign:
result = assign_license_to_user(user_id, license_to_assign['skuId'])
print("License assigned successfully:", result)
else:
print("No matching license found for the role.")
except requests.exceptions.HTTPError as e:
print("HTTP request failed:", e.response.text)
except Exception as e:
print("An error occurred:", e)
copied