On 2015-03-20 18:59 John Ellinwood wrote: 

> Thanks. the error with my code was that I was trying to remove the ROLE ID, 
> and not the MEMBERSHIP ID. They are different.

Correct :-) 

> Even with the syncope-client, in order to successfully add, then remove, then 
> add again... I have to do a lot of code:
> 
> // Create syncope client and services 
> SyncopeClient sclient = new 
> SyncopeClientFactoryBean().setAddress(address).create(adminUser, 
> adminPassword); 
> UserService userService = sclient.getService(UserService.class); 
> RoleService roleService = sclient.getService(RoleService.class); // Create a 
> user 
> UserTO user = new UserTO(); user.setUsername("test"); 
> user.setPassword("test"); userService.create(user, false); 
> // Find the user ID because the userService methods don't work with name, and 
> I don't know the ID UserTO newUser = 
> userService.search("(username==test)").getResult().get(0);

You can avoid all such search() calls: when calling user's create(), for
example, a Response object is returned which contains a Location header
for the newly created object + the whole UserTO (including the generated
id) as entity; more details at 

http://syncope.apache.org/apidocs/1.2/org/apache/syncope/common/services/UserService.html#create(org.apache.syncope.common.to.UserTO,%20boolean)
[4] 

Moreover, for Users it is also available a service returning user id
from username 

http://syncope.apache.org/apidocs/1.2/org/apache/syncope/common/services/UserService.html#getUsername(java.lang.Long)


More details in the Javadocs or the REST API reference:
http://syncope.apache.org/rest/1.2/index.html 

HTH 
Regards. 

> // Find the role because of the same reason RoleTO role = 
> roleService.search("(name=testrole)").getResult().get(0); // Add the role 
> UserMod userMod = new UserMod(); MembershipMod mod1 = new MembershipMod(); 
> mod1.setRole(role.getId()); userMod.getMembershipsToAdd().add(mod1); 
> userService.update(newUser.getId(), userMod); // Find the updated user so I 
> can get the membership id to delete because it doesn't work on role ids 
> newUser = userService.search("(username==test)").getResult().get(0); 
> MembershipTO membership = newUser.getMembershipMap().get(role.getId()); // 
> Remove the role userMod = new UserMod(); 
> userMod.getMembershipsToRemove().add(membership.getId()); //this is the key 
> here.. membership id userService.update(newUser.getId(), userMod); // Add the 
> same role back again userMod = new UserMod(); 
> userMod.getMembershipsToAdd().add(mod1); userService.update(newUser.getId(), 
> userMod);
> 
> On Fri, Mar 20, 2015 at 6:08 AM, Francesco Chicchiriccò <[email protected]> 
> wrote:
> 
> On 19/03/2015 22:00, John Ellinwood wrote: 
> 
> Sorry, problem including the rest of the log:
> 
> Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: Unique index or 
> primary key violation: "U_MMBRSHP_SYNCOPEUSER_ID_INDEX_C ON 
> PUBLIC.MEMBERSHIP(SYNCOPEUSER_ID, SYNCOPEROLE_ID) VALUES (101, 100, 151)"; 
> SQL statement: INSERT INTO PUBLIC.Membership (ID, CREATIONDATE, CREATOR, 
> LASTCHANGEDATE, LASTMODIFIER, SYNCOPEUSER_ID, SYNCOPEROLE_ID) VALUES (?, ?, 
> ?, ?, ?, ?, ?) [23505-185] {prepstmnt 1618806582 INSERT INTO 
> PUBLIC.Membership (ID, CREATIONDATE, CREATOR, LASTCHANGEDATE, LASTMODIFIER, 
> SYNCOPEUSER_ID, SYNCOPEROLE_ID) VALUES (?, ?, ?, ?, ?, ?, ?)} [code=23505, 
> state=23505]
> 
> I also can't add or remove roles through the UI now. I can't delete the user 
> either. 
> 
> On Thu, Mar 19, 2015 at 4:56 PM, John Ellinwood <[email protected]> 
> wrote:
> 
> I am trying to use the rest api for syncope. I'm trying this on syncope 
> 1.2.2. My rest client is Jersey, but this happens just using RestClient from 
> Firefox as well. 
> 
> I have a user, "user1" id 101. I have a roles. "role1" id 100
> 
> Request: POST http://localhost:9080/syncope/rest/users/101 [1] 
> {"id":101,"membershipsToAdd":[{"role":100}]} Response
> 200 Succes
> 
> Request:
> POST http://localhost:9080/syncope/rest/users/101 [1]
> {"id":101,"membershipsToRemove":[100]} Response 200 Success
> 
> Request: POST http://localhost:9080/syncope/rest/users/101 [1]
> {"id":101,"membershipsToAdd":[{"role":100}]}
> Response 
> 409 Conflict, DataIntegrityViolation. The transaction has been rolled back. 
> See the nested exception for details on the errors that occurred.
> 
> Log: 
> ...Caused by: org.apache.openjpa.lib.jdbc.ReportinSQLException: Unique index 
> or pimary key violation: "U_MMBRSHP_SYNCOPEUSER_ID_INDEX_C ON PUBLIC.
 Hi John,
 I've performed the same operations using Syncope client API (which are
CXF-based) and everything worked flawlessly.
 I assume you are using some 1.2.X release.

 Here's the detail:

 1. created an user (id 103) and a role (id 100) via admin console

 2. run the following code snippet in my sample Syncope client
application [1] (placed it after the "*do* something" comment on
App#main)

 MembershipMod membMod = new MembershipMod();
 membMod.setRole(100);

 UserMod userMod = new UserMod();
 userMod.setId(103);
 userMod.getMembershipsToAdd().add(membMod);

 Response response = userService.update(userMod.getId(), userMod);
 Assert.assertEquals(200, response.getStatus());

 userMod.getMembershipsToAdd().clear();
 userMod.getMembershipsToRemove().add(100L);
 response = userService.update(userMod.getId(), userMod);
 Assert.assertEquals(200, response.getStatus());

 userMod.getMembershipsToRemove().clear();
 userMod.getMembershipsToAdd().add(membMod);
 response = userService.update(userMod.getId(), userMod);
 Assert.assertEquals(200, response.getStatus());

 FYI, here's the generated HTTP calls and responses: [2]

 HTH
 Regards.

 [1] https://github.com/ilgrosso/syncopeRestClient [5]
 [2] https://paste.apache.org/a4iq [6]

-- 
Francesco Chicchiriccò

Tirasa - Open Source Excellence
http://www.tirasa.net/ [2]

Involved at The Apache Software Foundation:
member, Syncope PMC chair, Cocoon PMC, Olingo PMC
http://people.apache.org/~ilgrosso/ [3]

-- 
 Francesco Chicchiriccò

 Tirasa - Open Source Excellence
 http://www.tirasa.net/ [2]

 Involved at The Apache Software Foundation:
 member, Syncope PMC chair, Cocoon PMC, Olingo PMC
 http://people.apache.org/~ilgrosso/ [3] 

Links:
------
[1] http://localhost:9080/syncope/rest/users/101
[2] http://www.tirasa.net/
[3] http://people.apache.org/~ilgrosso/
[4]
http://syncope.apache.org/apidocs/1.2/org/apache/syncope/common/services/UserService.html#create(org.apache.syncope.common.to.UserTO,%20boolean)
[5] https://github.com/ilgrosso/syncopeRestClient
[6] https://paste.apache.org/a4iq

Reply via email to