Create or List Management Portal Users and Roles programatically
Hi,
To add or get information about a user for the management portal you can [within the portal]:
System Administration > Security Management > Users
Is it possible to:
1. List a user, user's properties including roles
2. Add a user and set user's properties including roles
3. Set a user's password
for the management portal using a program eg. Objectscript and/or SQL?
If so, how can this be done?
It would be great if you have examples.
Comments
- To list a user and their properties, including roles, you can use ObjectScript commands with the
Security.Usersclass in the%SYSnamespace. An example of listing the properties of a user is:
NEW $NAMESPACE
SET $NAMESPACE="%SYS"
SET status = ##class(Security.Users).Get("Username", .properties)
IF $$$ISOK(status) {
ZWRITE properties
}
This retrieves the user's properties into a reference variable and displays them [1][2].
- To add a user and set their properties, including roles, you can use the
Createmethod of theSecurity.Usersclass. For example:
NEW $NAMESPACE
SET $NAMESPACE="%SYS"
SET properties("FullName") = "New User"
SET properties("Password") = "securepassword"
SET properties("UserRoles") = "%Developer,%Operator"
SET status = ##class(Security.Users).Create("NewUser", .properties)
IF $$$ISOK(status) {
WRITE "User created successfully."
}
This creates a user with specified roles and other details [1][2].
- To set a user's password, you can use the
Modifymethod to update the relevant property, as in:
NEW $NAMESPACE
SET $NAMESPACE="%SYS"
SET status = ##class(Security.Users).Get("Username", .properties)
IF $$$ISOK(status) {
SET properties("Password") = "newpassword"
SET status = ##class(Security.Users).Modify("Username", .properties)
IF $$$ISOK(status) {
WRITE "Password updated successfully."
}
}
This updates the user's password [1][2].
Sources:
@Phillip Wu
This code is mostly ok but there is not need to use $NEW NAMESPACE
Thanks for the info.
However for "roles" SQL gives different information to that from ObjectScript:
select * from Security.Roles
ID
Description
GrantedRoles
Name
NameLowerCase
Version
However,
%SYS>Set status = ##class(Security.Roles).Get("anyrole",.properties)
%SYS>write status
1
%SYS>zwrite properties
properties("Description")="Super User Role"
properties("GrantedRoles")="NormalUserRole"
properties("Resources")="%DB_IRISSYS:R,%Service_CacheDirect:U,%Service_Object:U,%Service_WebGateway:U,%System_CallOut:U"
Why is that?
Also check this doc
https://docs.intersystems.com/iris20251/csp/docbook/DocBook.UI.Page.cls…
Hi Philip,
https://community.intersystems.com/post/unwrap-roles-recursively
you can refer to this above post. This post says how to fetch the current roles of the user. You use a similar way to insert them.