Okay with few changes I am able to create a user and also reset the password for the admin account using OD APIs but I see that SecureToken is disabled whereas I was under the assumption that SecureToken gets enabled automatically for admin users.
Any idea why secure token is not getting enabled for new account created via the OD APIs?
I also noticed that its not possible to change the password of an admin user which has Secure token enabled. It throws above error as mentioned in the post.
Create User
func createUserAccount(username: String, fullName: String, password: String) throws {
// Get the local node
let localNode = try ODNode(session: ODSession.default(), type: ODNodeType(kODNodeTypeLocalNodes))
// Generate a unique user ID
let uniqueID = "509"
// Create the user record with properly formatted attributes
let attributes: [String: [String]] = [
kODAttributeTypeFullName: [fullName], // User's full name
kODAttributeTypeUniqueID: [uniqueID], // Unique ID for the user
kODAttributeTypePrimaryGroupID: ["80"], // Default group ID (staff group)
kODAttributeTypeNFSHomeDirectory: ["/Users/\(username)"], // User's home directory
kODAttributeTypeUserShell: ["/bin/bash"] // Default shell
]
// Create a new user record
let userRecord = try localNode.createRecord(
withRecordType: kODRecordTypeUsers,
name: username,
attributes: attributes
)
// Set the user's password
try userRecord.changePassword(nil, toPassword: password)
// Add the user to the "admin" group
let adminGroupRecord = try localNode.record(
withRecordType: kODRecordTypeGroups,
name: "admin",
attributes: nil
)
try adminGroupRecord.addMemberRecord(userRecord)
print("Admin account \(username) created successfully with UID \(uniqueID).")
}