How to create a user via ObjectScript for the instance and specify roles and namespace?
I am trying to create users who only have `%SQL` Role for the INSTANCE. But I am unable to find any documentation on the `Security.Users` class.
See:
// Instantiate the Security.Users objectSet userObj = ##class(Security.Users).%New()// Set the username and passwordSet userObj.Name = userNameSet userObj.FullName = userFullNameSet userObj.Namespace = "USER"Set userObj.Roles = "%SQL" Set sc = userObj.ChangePassword(passwd)// Save the user to the databaseSet ss = userObj.%Save()
I cant figure out the "Namespace" property for this class, let alone assign a password. Any help would be appreciated. Also the `ChangePassword` method does not appear to work.
Comments
Ronaldo,
The ChangePassword property isn't for the password, it's to set a flag if the user is supposed to change their password on next login - this property's handy for admins that assign temporary passwords and the user needs to change it to something befitting them on first login. To set the property, it's a simple 'set' like the others:
Set userObj.ChangePassword=1
And to set the initial Password is also a standard 'set':
Set userObj.Password="initialpassword"
Also, if the Roles is only %SQL, but if you're testing the login using Terminal, that user doesn't have enough rights for that functionality and you'll probably get an 'Access Denied' error. You'll either need to (temporarily) assign the %All role to that user to test, or just verify that the settings are getting set in the Management Portal.
As to the NameSpace, if the Namespace doesn't exist (typo, I've done that before) or if the user doesn't have enough rights to access that Namespace, it may not be set correctly.
Lastly, here's the documentation for the Security.Users class: https://docs.intersystems.com/irislatest/csp/documatic/%25CSP.Documatic.cls?LIBRARY=%25SYS&CLASSNAME=Security.Us
Hope this helps!
[[ edit for readability and examples were out of order. ]]
Just FYI, your link is broken. Here's a fixed one.
I use this one quite a bit...
Set tSC=##Class(Security.Users).Create("user","%SQL","pass","NAMESPACE","","","",0,1,,,,,,1,1)