Auth samples
Basics
Section titled “Basics”The class com.openkm.sdk4j.bean.Permission contains permission values ( READ, WRITE, etc. ). You should use it in combination with methods that are changing or getting security grants.
On most methods you’ll see parameter named “nodeId”. The value of this parameter can be some valid node UUID ( folder, document, mail, record ) or node path.
Methods
Section titled “Methods”getGrantedRoles
Section titled “getGrantedRoles”Description:
| Method | Return values | Description |
|---|---|---|
| getGrantedRoles(String nodeId) | Map<String, Integer> | Return the granted roles of a node. |
Example:
package com.openkm;
import java.util.Map;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password); try { Map<String, Integer> grants = ws.getGrantedRoles("/okm:root"); for (String role : grants.keySet()) { System.out.println(role + "->" + grants.get(role)); } } catch (Exception e) { e.printStackTrace(); } }}getGrantedUsers
Section titled “getGrantedUsers”Description:
| Method | Return values | Description |
|---|---|---|
| getGrantedUsers(String nodeId) | Map<String, Integer> | Return the granted users of a node. |
Example:
package com.openkm;
import java.util.Map;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { Map<String, Integer> grants = ws.getGrantedUsers("/okm:root"); for (String role : grants.keySet()) { System.out.println(role + "->" + grants.get(role)); } } catch (Exception e) { e.printStackTrace(); } }}getMail
Section titled “getMail”Description:
| Method | Return values | Description |
|---|---|---|
| getMail(String user) | String | Return the mail of a valid user. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { System.out.println(ws.getMail("okmAdmin")); } catch (Exception e) { e.printStackTrace(); } }}getName
Section titled “getName”Description:
| Method | Return values | Description |
|---|---|---|
| getName(String user) | String | Return the name of a valid user. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { System.out.println(ws.getName("okmAdmin")); } catch (Exception e) { e.printStackTrace(); } }}getRoles
Section titled “getRoles”Description:
| Method | Return values | Description |
|---|---|---|
| getRoles() | List |
Return the list of all the roles. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { for (String role : ws.getRoles()) { System.out.println(role); } } catch (Exception e) { e.printStackTrace(); } }}getRolesByUser
Section titled “getRolesByUser”Description:
| Method | Return values | Description |
|---|---|---|
| getRolesByUser(String user) | List |
Return the list of all the roles assigned to a user. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { for (String role : ws.getRolesByUser("okmAdmin")) { System.out.println(role); } } catch (Exception e) { e.printStackTrace(); } }}getUsers
Section titled “getUsers”Description:
| Method | Return values | Description |
|---|---|---|
| getUsers() | List |
Return the list of all the users. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { for (String user : ws.getUsers()) { System.out.println(user); } } catch (Exception e) { e.printStackTrace(); } }}getUsersByRole
Section titled “getUsersByRole”Description:
| Method | Return values | Description |
|---|---|---|
| getUsersByRole(String role) | List |
Return the list of all the users who have assigned a role. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { for (String user : ws.getUsersByRole("ROLE_ADMIN")) { System.out.println(user); } } catch (Exception e) { e.printStackTrace(); } }}revokeRole
Section titled “revokeRole”Description:
| Method | Return values | Description |
|---|---|---|
| revokeRole(String nodeId, String role, int permissions, boolean recursive) | void | Remove role grant on a node. |
The parameter recursive only has sense when the nodeId is a folder or a record node.
When parameter recursive is true, the change will be applied to the node and its descendants.
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.bean.Permission;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { // Remove ROLE_USER write grants at the node but not descendants ws.revokeRole("/okm:root", "ROLE_USER", Permission.ALL_GRANTS, false);
// Remove all ROLE_ADMIN grants to the node and descendants ws.revokeRole("/okm:root", "ROLE_ADMIN", Permission.ALL_GRANTS, true); } catch (Exception e) { e.printStackTrace(); } }}revokeUser
Section titled “revokeUser”Description:
| Method | Return values | Description |
|---|---|---|
| revokeUser(String nodeId, String user, int permissions, boolean recursive) | void | Remove user grant on a node. |
The parameter recursive only has sense when the nodeId is a folder or a record node.
When parameter recursive is true, the change will be applied to the node and descendants.
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.bean.Permission;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { // Remove john write grants at the node but not descendants ws.revokeUser("/okm:root", "john", Permission.ALL_GRANTS, false);
// Remove all okmAdmin grants at the node and descendants ws.revokeUser("/okm:root", "okmAdmin", Permission.ALL_GRANTS, true); } catch (Exception e) { e.printStackTrace(); } }}grantRole
Section titled “grantRole”Description:
| Method | Return values | Description |
|---|---|---|
| grantRole(String nodeId, String role, int permissions, boolean recursive) | void | Add role grant on a node. |
The parameter recursive only has sense when the nodeId is a folder or a record node.
When parameter recursive is true, the change will be applied to the node and descendants.
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.bean.Permission;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { // Add ROLE_USER write grants at the node but not descendants ws.grantRole("/okm:root", "ROLE_USER", Permission.ALL_GRANTS, false);
// Add all ROLE_ADMIN grants to the node and descendants ws.grantRole("/okm:root", "ROLE_ADMIN", Permission.ALL_GRANTS, true); } catch (Exception e) { e.printStackTrace(); } }}grantUser
Section titled “grantUser”Description:
| Method | Return values | Description |
|---|---|---|
| grantUser(String nodeId, String user, int permissions, boolean recursive) | void | Add user grant on a node. |
The parameter recursive only has sense when the nodeId is a folder or record node.
When parameter recursive is true, the change will be applied to the node and descendants.
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.bean.Permission;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { // Add john write grants at the node but not descendants ws.grantUser("/okm:root", "john", Permission.ALL_GRANTS, false);
// Add all okmAdmin grants at the node and descendants ws.grantUser("/okm:root", "okmAdmin", Permission.ALL_GRANTS, true); } catch (Exception e) { e.printStackTrace(); } }}getProfiles
Section titled “getProfiles”Description:
| Method | Return values | Description |
|---|---|---|
| getProfiles(boolean filterByActive) | List |
Return the list of all profiles. |
The parameter filterByActive when enabled the method will return only the active profiles, otherwise will return all available profiles.
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.bean.ProfileSimple;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { for (ProfileSimple profile : ws.getProfiles(true)) { System.out.println(profile); } } catch (Exception e) { e.printStackTrace(); } }}getUserProfile
Section titled “getUserProfile”Description:
| Method | Return values | Description |
|---|---|---|
| getUserProfile(String userId) | ProfileSimple | Return the profile assigned to a user. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { System.out.println(ws.getUserProfile("okmAdmin")); } catch (Exception e) { e.printStackTrace(); } }}setUserProfile
Section titled “setUserProfile”Description:
| Method | Return values | Description |
|---|---|---|
| setUserProfile(String userId, long profileId) | void | Change the assigned profile to a user. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.bean.ProfileSimple;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { // Set the profile named "default" to the user for (ProfileSimple profile : ws.getProfiles(true)) { if (profile.getName().equals("default")) { ws.setUserProfile("okmAdmin", profile.getId()); } } } catch (Exception e) { e.printStackTrace(); } }}createUser
Section titled “createUser”Description:
| Method | Return values | Description |
|---|---|---|
| createUser(String user, String password, String email, String name, boolean active) | void | Create a new user. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password); try { ws.createUser("test", "password.2016", "some@mail.com", "User Name", true); } catch (Exception e) { e.printStackTrace(); } }}deleteUser
Section titled “deleteUser”Description:
| Method | Return values | Description |
|---|---|---|
| deleteUser(String user) | void | Delete a user. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password); try { ws.deleteUser("test"); } catch (Exception e) { e.printStackTrace(); } }}updateUser
Section titled “updateUser”Description:
| Method | Return values | Description |
|---|---|---|
| updateUser(String user, String password, String email, String name, boolean active) | void | Update a user. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password); try { ws.updateUser("test", "newpassword", "some@mail.com", "Test", false); } catch (Exception e) { e.printStackTrace(); } }}createRole
Section titled “createRole”Description:
| Method | Return values | Description |
|---|---|---|
| createRole(String role, boolean active) | void | Create a new role. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password); try { ws.createRole("ROLE_TEST", true); } catch (Exception e) { e.printStackTrace(); } }}deleteRole
Section titled “deleteRole”Description:
| Method | Return values | Description |
|---|---|---|
| deleteRole(String role) | void | Delete a role. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password); try { ws.deleteRole("ROLE_TEST"); } catch (Exception e) { e.printStackTrace(); } }}updateRole
Section titled “updateRole”Description:
| Method | Return values | Description |
|---|---|---|
| updateRole(String role, boolean active) | void | Update a role. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password); try { ws.updateRole("ROLE_TEST",true); } catch (Exception e) { e.printStackTrace(); } }}assignRole
Section titled “assignRole”Description:
| Method | Return values | Description |
|---|---|---|
| assignRole(String user, String role) | void | Assign role to a user. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password); try { ws.assignRole("test", "ROLE_USER"); } catch (Exception e) { e.printStackTrace(); } }}removeRole
Section titled “removeRole”Description:
| Method | Return values | Description |
|---|---|---|
| removeRole(String user, String role) | void | Remove a role from a user. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password); try { ws.removeRole("test", "ROLE_USER"); } catch (Exception e) { e.printStackTrace(); } }}changeSecurity
Section titled “changeSecurity”Description:
| Method | Return values | Description |
|---|---|---|
| changeSecurity(ChangeSecurity changeSecurity) | void | Change the security of a node. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.bean.ChangeSecurity;import com.openkm.sdk4j.bean.GrantedRole;import com.openkm.sdk4j.bean.GrantedRoleList;import com.openkm.sdk4j.bean.Permission;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password); try { ChangeSecurity cs = new ChangeSecurity(); cs.setNodeId("b9736924-bb97-4e2c-8450-138c21e0c9d5"); GrantedRoleList grList = new GrantedRoleList(); GrantedRole gr = new GrantedRole(); gr.setRole("ROLE_TEST"); gr.setPermissions(Permission.READ | Permission.WRITE); grList.getList().add(gr); cs.setGrantedRolesList(grList); ws.changeSecurity(cs); } catch (Exception e) { e.printStackTrace(); } }}Description:
| Method | Return values | Description |
|---|---|---|
| login() | void | Simulate first login process |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test2 { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password); try { ws.login(); } catch (Exception e) { e.printStackTrace(); } }}