#1 2017-01-10 10:36:41

Sreejith S
Member
Registered: 2016-11-16

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

Last edited by Sreejith S (2017-01-10 11:15:40)

Offline

#2 2017-01-10 11:17:35

eduardo
Administrator
Registered: 2016-11-01
Website

Re: Executing scripts via url to add new users

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/scri … ts-via-URL
commit    Whether or not the script should be executed in commit mode.

Cheers,
Eduardo

Offline

#3 2017-01-10 11:27:45

Sreejith S
Member
Registered: 2016-11-16

Re: Executing scripts via url to add new users

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

Offline

#4 2017-01-10 11:53:05

eduardo
Administrator
Registered: 2016-11-01
Website

Re: Executing scripts via url to add new users

Hi Sreejith,

yes, since apikeys are just user properties (https://reportserver.net/en/guides/admi … a-the-URL/) you can use:
userPropertiesService.setPropertyValue(user, "apikey", "yourApiKey");

Cheers,
Eduardo

Offline

#5 2017-01-10 13:32:53

Sreejith S
Member
Registered: 2016-11-16

Re: Executing scripts via url to add new users

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

Last edited by Sreejith S (2017-01-10 13:54:47)

Offline

#6 2017-01-10 14:13:03

eduardo
Administrator
Registered: 2016-11-01
Website

Re: Executing scripts via url to add new users

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");

Offline

#7 2017-01-10 14:56:29

eduardo
Administrator
Registered: 2016-11-01
Website

Re: Executing scripts via url to add new users

Hi Sreejith,

Sreejith S wrote:

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

  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

Offline

#8 2017-01-10 15:09:12

Sreejith S
Member
Registered: 2016-11-16

Re: Executing scripts via url to add new users

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

Offline

#9 2017-01-10 15:18:37

eduardo
Administrator
Registered: 2016-11-01
Website

Re: Executing scripts via url to add new users

Hi Sreejith,

Sreejith S wrote:
-->U<<-serPropertiesService.setPropertyValue(user, "apikey", "kerchg12");

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

Offline

#10 2017-01-11 12:43:01

Sreejith S
Member
Registered: 2016-11-16

Re: Executing scripts via url to add new users

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

Offline

#11 2017-01-11 13:03:58

eduardo
Administrator
Registered: 2016-11-01
Website

Re: Executing scripts via url to add new users

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/ … p/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

Offline

#12 2017-01-11 15:27:40

Sreejith S
Member
Registered: 2016-11-16

Re: Executing scripts via url to add new users

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

Offline

#13 2017-01-11 15:38:39

wonky
Member
Registered: 2016-03-01

Re: Executing scripts via url to add new users

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/t … 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!

Offline

#14 2017-01-11 15:48:57

eduardo
Administrator
Registered: 2016-11-01
Website

Re: Executing scripts via url to add new users

Hi wonky,

For adding a user, as you mention: https://reportserver.net/en/tutorials/t … 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

Offline

#15 2017-01-11 15:50:58

eduardo
Administrator
Registered: 2016-11-01
Website

Re: Executing scripts via url to add new users

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

Offline

#16 2017-01-11 17:11:04

wonky
Member
Registered: 2016-03-01

Re: Executing scripts via url to add new users

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";

Offline

#17 2017-01-18 14:43:42

Sreejith S
Member
Registered: 2016-11-16

Re: Executing scripts via url to add new users

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

Offline

#18 2017-01-18 19:15:05

karolina
Member
Registered: 2014-08-09

Re: Executing scripts via url to add new users

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

Offline

#19 2017-01-18 19:16:37

wonky
Member
Registered: 2016-03-01

Re: Executing scripts via url to add new users

Thanks very much.

Offline

#20 2017-01-18 23:17:37

Tpojka
Member
From: Ratio, Logic
Registered: 2017-01-11
Website

Re: Executing scripts via url to add new users

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.

Offline

#21 2017-01-19 07:01:44

karolina
Member
Registered: 2014-08-09

Re: Executing scripts via url to add new users

Hi,

The comment above the method says to use UserManagerService.setPassword method (implemented in UserManagerServiceImpl)

Karolina

Offline

#22 2017-02-02 17:05:19

eduardo
Administrator
Registered: 2016-11-01
Website

Re: Executing scripts via url to add new users

To summarize:

You can use the following script to set the password: (remember to use the -c flag when executing the script)

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

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


/* create user */
User user = new User();
user.setUsername("sampleUser");
user.setFirstname("Sample");
user.setLastname("Test");
		
		
List<OrganisationalUnit> ouList = userManagerService.getOUsByName("Administration");
for (OrganisationalUnit ou: ouList) {
	ou.addChild(user);
}
userManagerService.persist(user);
		
userManagerService.setPassword(user, "abcdef");

"User created";

Cheers,
Eduardo

Offline

Board footer

Powered by FluxBB