Executing scripts via url to add new users

Hi,

We are trying to run a script via url to create new users , we are able to execute the code via the same but users are not getting created in the User Management tab.

The same script is running successfully and getting users created through the RS terminal.

I have an another query , is it possible to provide apikey via the script?

Below is the script which we have used:-

import net.datenwerke.security.service.usermanager.UserManagerService;
import net.datenwerke.security.service.usermanager.entities.User;
import javax.servlet.http.HttpServletRequest


def userService = GLOBALS.getInstance(UserManagerService.class);


def user = new User();
user.setUsername("sampletest");
user.setFirstname("Paul");
user.setLastname("Testuser");


def root = userService.getRoots().get(0);
root.addChild(user);


userService.persist(user);

return "added new user";

Could you please look into this issue.

Regards,
Sreejith

Hi Sreejith,

with the terminal, you should run “exec -c script.groovy” in order to commit the changes. I guess you executed this way, since it is working on the terminal.
Are you passing this argument to the URL too ?

https://reportserver.net/en/guides/script/chapters/Basics/#Executing-Scripts-via-URL
commit Whether or not the script should be executed in commit mode.

Cheers,
Eduardo

Hi Eduardo,

Thanks a lot for your reply , issue was with the commit parameter.

We are actually planning to create users and apikey through scripts , is it possible to set apikey via scripts.

Regards,
Sreejith

Hi Sreejith,

yes, since apikeys are just user properties (https://reportserver.net/en/guides/admin/chapters/Executing-Reports-via-the-URL/) you can use:
userPropertiesService.setPropertyValue(user, “apikey”, “yourApiKey”);

Cheers,
Eduardo

Hi Eduardo,

I have added UserPropertiesService to the script but while executing the same , I am getting the following error:-

------- SCRIPT ERROR INFO -------
Script execution failed.
error message: No signature of method: static net.datenwerke.security.service.usermanager.UserPropertiesService.setPropertyValue() is applicable for argument types: (net.datenwerke.security.service.usermanager.entities.User, java.lang.String, java.lang.String) values: [Paulson Tester, apikey, kerchg12] (groovy.lang.MissingMethodException)
script arguments: null

Also, how to add new users to a specific folder, for example Users folder. And also assign these users to a specific group.

Could you look into this.

Regards,
Sreejith

Hi Sreejith,

is this script not working?

import net.datenwerke.security.service.usermanager.entities.User;
import net.datenwerke.security.service.usermanager.UserManagerService;
import net.datenwerke.security.service.usermanager.UserPropertiesService;


UserManagerService userManagerService = GLOBALS.getRsService(UserManagerService.class);
UserPropertiesService userPropertiesService = GLOBALS.getRsService(UserPropertiesService.class);
	

User user = userManagerService.getUserByName("root");
  		
userPropertiesService.setPropertyValue(user, "myProp", "abc");

Hi Sreejith,

  User user = userManagerService.getUserByName("myUser");
  List<OrganisationalUnit> ouList = userManagerService.getOUsByName("myOU");
  for (OrganisationalUnit ou: ouList) {
  	ou.addChild(user);
  }
  		
  Group  gr = userManagerService.getGroupByName("myGroup");
  gr.addUser(user);

Cheers,
Eduardo

Hi Eduardo,

This is the script which I am trying to execute currently:-

I am trying to create a new user and assign apikey property to the same user.

import net.datenwerke.security.service.usermanager.UserManagerService;
import net.datenwerke.security.service.usermanager.entities.User;
import net.datenwerke.security.service.usermanager.UserPropertiesService;

UserManagerService userManagerService = GLOBALS.getRsService(UserManagerService.class);
UserPropertiesService userPropertiesService = GLOBALS.getRsService(UserPropertiesService.class);
	
 		
def userService = GLOBALS.getInstance(UserManagerService.class);

def user = new User();
user.setUsername("sampler");
user.setFirstname("Paulson");
user.setLastname("Tester");

UserPropertiesService.setPropertyValue(user, "apikey", "kerchg12");

def root = userService.getRoots().get(0);
root.addChild(user);

userService.persist(user);

return "added new user";

Regards,
Sreejith

Hi Sreejith,

You are calling a static method, but this method is not static, take a look at my script:

userPropertiesService.setPropertyValue(user, “myProp”, “abc”);

Greets,
Eduardo

Hi Eduardo,

Thanks , that made the issue solved.

We are trying to use an application database users and their properties (team they belongs etc.) to compare with users in RS database.We want to know what DB is used for storing users in RS, what are impacted tables (like: users, user_organizations, maybe_some_pivot_tables etc.) or how is structured part which is impacting user authentication/authorization in RS.

Regards,
Sreejith

Hi Sreejith,

you can use the API for getting the team the users belong to, user organizations, groups, etc. Like in my post #7.
Take a look at the API for this. The API is available on the sourceforge downloads: https://sourceforge.net/projects/dw-rs/files/src/3.0/RS3.0.2-5855-2016-05-29-17-55-24-apidoc-reportserver.zip/download
There you can see a list of the available services and links to the APIs.

The tables used are in the internal database your reportserver installation uses. You can see it under administration → datasources → datasource root → internal datasources
But you shouldn’t need this. You should just use the API.

Greets,
Eduardo

Hi Eduardo,

Thanks for the reply.

Regarding the useradd script which we were working earlier , I have an issue that I am able to create OU and add users independently , but while trying to perform these two operation simultaneously.

Could you please provide the necessary coding for the same.

Regards,
Sreejith

Hey Eduardo,

Just to be completely clear, is there a single complete sample script for adding an OU, user, and adding the user to the OU?

I feel like we are doing the 4 blind men and the elephant here.

Creating a user is covered at https://reportserver.net/en/tutorials/tutorial-scripting/ but OU is not found on that page, and I can’t find anything about adding an OU in script via any Google search I can come up with.

Thanks!

Hi wonky,

For adding a user, as you mention: https://reportserver.net/en/tutorials/tutorial-scripting/
For adding a user to an existing OU: see my post #7
For adding a new OU: new OrganisationalUnit(), and then add it to an existing node with addChild(), so, again, analogously as #7.

Cheers,
Eduardo

As I mentioned in #11, you can download the APIs from sourceforge.

Update, we’re able to create OUs, users, and attach the users to the OU in a single script now:

import net.datenwerke.security.service.usermanager.UserManagerService;
import net.datenwerke.security.service.usermanager.entities.User;
import net.datenwerke.security.service.usermanager.UserPropertiesService;
import net.datenwerke.security.service.usermanager.entities.OrganisationalUnit;
import net.datenwerke.security.service.usermanager.entities.AbstractUserManagerNode;
import net.datenwerke.security.service.usermanager.entities.Group;
//import net.datenwerke.rs.terminal.service.terminal.TerminalService;

UserManagerService userManagerService = GLOBALS.getRsService(UserManagerService.class);
UserPropertiesService userPropertiesService = GLOBALS.getRsService(UserPropertiesService.class);

 		
/* load service */
def userService = GLOBALS.getInstance(UserManagerService.class);
//def terminal = GLOBALS.getInstance(TerminalService.class);

/* create user */
def user = new User();
user.setUsername("samply");
user.setFirstname("Paulson");
user.setLastname("Testr");

/* Assigning apikey */

userPropertiesService.setPropertyValue(user, "apikey", "xxxxxx");

/* Adding the Users to OU */

User users = userManagerService.getUserByName(user.getUsername());
OrganisationalUnit targetNode  = new OrganisationalUnit("Test");
AbstractUserManagerNode umRoot = userManagerService.getRoots().get(0);
umRoot.addChild(targetNode);
	
	 List<OrganisationalUnit> ouList = userManagerService.getOUsByName("Test");
 for (OrganisationalUnit ou: ouList) {
 ou.addChild(user); 
 }

/* persist user */
//userManagerService.persist(targetNode);
userService.persist(user);

return "added new user";

Hi

Now we are trying to set password by the help of script , while executing the report we are getting an error message which says “error message: No signature of method: net.datenwerke.security.service.usermanager.entities.User.setPassword() is applicable for argument types: (java.lang.String)” .

We have tried this by using the method “User.setPassword”. Is it possible to set password from within the script.

could you please help us to sort this.

Regards,
Sreejith

Hi,

If you look into RS API, net.datenwerke.security.service.usermanager.entities.User class,
you’ll see that the method User.setPassword takes two parameters and - anyway - is not recommended:

/**
* Usually you would not want to call this directly. Use {@link UserManagerService.setPassword} instead
* 
* @param password
* @param passwordHasher
*/

public void setPassword(String password, PasswordHasher passwordHasher) {
	String hashedPassword = passwordHasher.hashPassword(password);

	this.password = hashedPassword;
	}

Karolina

Thanks very much.

Hi Karolina.
I tried to understand this part of code and trying to implement after import of PasswordHasher library I have got errors.
Last error I have got is:

Script execution failed.
error message: javax.script.ScriptException: java.lang.NullPointerException (java.lang.NullPointerException)

and that was after my use of next code

PasswordHasher passwordHasher;
String password = "password1";
user.setPassword(password, passwordHasher);

Clearly I am doing wrong. Thing is I would like to set user password on the fly from the script.
I would use private encryption algorhitm and string that is got I would like to set as password.
Is that possible in a way I am thinking of it?
Some data I would like to pass with HTTP request but not password itself of course (probably with JWT), and I am thinking on a way of setting password on groovy side that I could use in the future.