Auth samples

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.

To set READ and WRITE access you should do:

int permission = Permission.READ + Permission.WRITE;

To check if you have permission access you should do:

// permission is a valid integer value
if ((permission | Permission.WRITE) = Permission.WRITE) {
  // Has WRITE grants.
}

On almost 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.

Example of nodeId:

  • Using UUID -> "c41f9ea0-0d6c-45da-bae4-d72b66f42d0f";
  • Using path -> "/okm:root/sample.pdf"

Methods

getGrantedRoles

Description:

MethodReturn valuesDescription

getGrantedRoles(String nodeId)

Map<String, Integer>

Returns 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

Description:

MethodReturn valuesDescription

getGrantedUsers(String nodeId)

Map<String, Integer>

Returns 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

Description:

MethodReturn valuesDescription

getMail(String user)

String

Returns 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

Description:

MethodReturn valuesDescription

getName(String user)

String

Returns 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

Description:

MethodReturn valuesDescription

getRoles()

List<String>

Returns 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

Description:

MethodReturn valuesDescription

getRolesByUser(String user)

List<String>

Returns 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

Description:

MethodReturn valuesDescription

getUsers()

List<String>

Returns 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

Description:

MethodReturn valuesDescription

getUsersByRole(String role)

List<String>

Returns 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

Description:

MethodReturn valuesDescription

revokeRole(String nodeId, String role, int permissions, boolean recursive)

void

Removes role 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 {          // 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

Description:

MethodReturn valuesDescription

revokeUser(String nodeId, String user, int permissions, boolean recursive)

void

Removes 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 {          // 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

Description:

MethodReturn valuesDescription

grantRole(String nodeId, String role, int permissions, boolean recursive)

void

Adds role 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 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

Description:

MethodReturn valuesDescription

grantUser(String nodeId, String user, int permissions, boolean recursive)

void

Adds 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(); } } }

createUser

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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();
        }
    }
}

login

Description:

MethodReturn valuesDescription

login()

void

Simulate first login process

When user login into the application the first time, is called internally a method what creates user specific folders, like /okm:trash/userId etc.

From API point of view, this method should be only executed first time user accessing from API, for creating specific user folder structure.

Otherwise you can get errors, for example PathNotExistsException /okm:trash/userId when deleting objects, because the folders has not been created.

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();
        }
    }
}