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 change or get 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.
}

Example of uuid:

  • Using UUID -> "c41f9ea0-0d6c-45da-bae4-d72b66f42d0f";

Suggested code sample

First, you must create the web service object:

OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

Then you should log in using the method "login". You can access the "login" method from the web service object "ws" as shown below:

ws.login(user, password);

Once you are logged in to the web service, the session is kept in the web service object. Then you can use the other API methods

At this point you can use all the Auth methods in "auth" class as shown below:

Map<String, Integer> grants = ws.auth.getGrantedRoles("373bcdd0-c082-4e7b-addd-e10ef813946e");

Methods

login

Description:

MethodReturn valuesDescription

login(String user, String password)

String

Login process returns an authentication token.

Login token by default has an expiration time of 24h.

After executing the login method, all the next calls will automatically use the authentication token.

Each time the login method is executed the login token is replaced by a newer one.

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

From the API point of view, this method should only be executed the first time a user accesses the API, to create the specific user folder structure.

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

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            System.out.println("Token: " + ws.login(user, password));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

login

Description:

MethodReturn valuesDescription

login(String user, String password, int expiration, boolean restrictIP)

String

Login process returns an authentication token.

Login token by default has an expiration time determined by the expiration value in days.

When restrictIP is true, the token will only work from the source IP address.

After executing the login method, all the next calls will automatically use the authentication token.

Each time the login method is executed the login token is replaced by a newer one.

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

From the API point of view, this method should only be executed the first time a user accesses the API, to create the specific user folder structure.

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

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
        int days = 7;
        boolean restrictIp = true;

        try {
            System.out.println("Token: " + ws.login(user, password, days, restrictIp));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

logout

Description:

MethodReturn valuesDescription

logout()

void

Execute the logout method on the OpenKM side.

The authentication token will be reset.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            ws.logout();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getGrantedUsersAndRoles

Description:

MethodReturn valuesDescription

getGrantedUsersAndRoles(String uuid)

GrantedUsersAndRolesItem

Returns the granted users and roles of a node.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.GrantedUsersAndRolesItem;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String username = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(username, password);

            GrantedUsersAndRolesItem grants = ws.auth.getGrantedUsersAndRoles("3c68b3a1-c65c-4b1e-84b5-9ce2712ca573");
            for (String user : grants.getGrantedUsers().keySet()) {
                System.out.println(user + "->" + grants.getGrantedUsers().get(user));
            }
            for (String role : grants.getGrantedRoles().keySet()) {
                System.out.println(role + "->" + grants.getGrantedRoles().get(role));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getGrantedRoles

Description:

MethodReturn valuesDescription

getGrantedRoles(String uuid)

Map<String, Integer>

Returns the granted roles of a node.

Example:

package com.openkm;

import java.util.Map;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            Map<String, Integer> grants = ws.auth.getGrantedRoles("373bcdd0-c082-4e7b-addd-e10ef813946e");
            for (String role : grants.keySet()) {
                System.out.println(role + "->" + grants.get(role));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getGrantedUsers

Description:

MethodReturn valuesDescription

getGrantedUsers(String uuid)

Map<String, Integer>

Returns the granted users of a node.

Example:

package com.openkm;

import java.util.Map;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String username = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(username, password);
            Map<String, Integer> grants = ws.auth.getGrantedUsers("373bcdd0-c082-4e7b-addd-e10ef813946e");
            for (String user : grants.keySet()) {
                System.out.println(user + "->" + grants.get(user));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getRoles

Description:

MethodReturn valuesDescription

getRoles(boolean showAll)

List<String>

Returns the list of all the roles.

When the showAll variable is set to true, it returns all the roles (enabled and disabled); otherwise it returns only the enabled ones.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/OpenKM";
        String username = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
        try {
            ws.login(username, password);
            for (String role : ws.auth.getRolesByUser("okmAdmin")) {
                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.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/OpenKM";
        String username = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
        try {
            ws.login(username, password);
            for (String role : ws.auth.getRolesByUser("okmAdmin")) {
                System.out.println(role);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getUsers

Description:

MethodReturn valuesDescription

getUsers(boolean showAll)

List<CommonUser>

Returns the list of all the users.

When the showAll variable is set to true, it returns all the users (enabled and disabled); otherwise it returns only the enabled ones.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.CommonUser;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            for (CommonUser commonUser : ws.auth.getUsers(true)) {
                System.out.println(commonUser);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getUser

Description:

MethodReturn valuesDescription

getUser(String userId)

CommonUser

Returns all user data.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.CommonUser;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            CommonUser commonUser = ws.auth.getUser("okmAdmin");
            System.out.print(commonUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getUsersByRole

Description:

MethodReturn valuesDescription

getUsersByRole(String role)

List<CommonUser>

Returns the list of all the users who have been assigned a role.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.CommonUser;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            for (CommonUser commonUser : ws.auth.getUsersByRole("ROLE_ADMIN")) {
                System.out.println(commonUser);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

revokeRole

Description:

MethodReturn valuesDescription

revokeRole(String uuid, String roleId, int permissions, boolean recursive)

void

Removes a role grant on a node.

The recursive parameter only makes sense when the uuid is a folder or a record node.

When the recursive parameter is true, the change will be applied to the node and its descendants.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.Permission;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            // Remove ROLE_USER write grants at the node but not descendants
            ws.auth.revokeRole("4f873d10-654e-4d99-a94f-15466e30a0f6", "ROLE_USER", Permission.ALL_GRANTS, false);

            // Remove all ROLE_ADMIN grants to the node and descendants
            ws.auth.revokeRole("4f873d10-654e-4d99-a94f-15466e30a0f6", "ROLE_ADMIN", Permission.ALL_GRANTS, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

revokeUser

Description:

MethodReturn valuesDescription

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

void

Removes a user grant on a node.

The recursive parameter only makes sense when the uuid is a folder or a record node.

When the recursive parameter is true, the change will be applied to the node and its descendants.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.Permission;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            // Remove sochoa write grants at the node but not descendants
            ws.auth.revokeUser("373bcdd0-c082-4e7b-addd-e10ef813946e", "sochoa", Permission.ALL_GRANTS, false);

            // Remove all okmAdmin grants at the node and descendants
            ws.auth.revokeUser("373bcdd0-c082-4e7b-addd-e10ef813946e", "okmAdmin", Permission.ALL_GRANTS, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

grantRole

Description:

MethodReturn valuesDescription

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

void

Adds a role grant on a node.

The recursive parameter only makes sense when the uuid is a folder or a record node.

When the recursive parameter is true, the change will be applied to the node and its descendants.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.Permission;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            // Add ROLE_USER write grants at the node but not descendants
            ws.auth.grantRole("4f873d10-654e-4d99-a94f-15466e30a0f6", "ROLE_USER", Permission.ALL_GRANTS, false);

            // Add all ROLE_ADMIN grants to the node and descendants
            ws.auth.grantRole("4f873d10-654e-4d99-a94f-15466e30a0f6", "ROLE_ADMIN", Permission.ALL_GRANTS, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

grantUser

Description:

MethodReturn valuesDescription

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

void

Adds a user grant on a node.

The recursive parameter only makes sense when the uuid is a folder or a record node.

When the recursive parameter is true, the change will be applied to the node and its descendants.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.Permission;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            // Add sochoa write grants at the node but not descendants
            ws.auth.grantUser("4f873d10-654e-4d99-a94f-15466e30a0f6", "sochoa", Permission.ALL_GRANTS, false);

            // Add all okmAdmin grants at the node and descendants
            ws.auth.grantUser("4f873d10-654e-4d99-a94f-15466e30a0f6", "okmAdmin", Permission.ALL_GRANTS, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

changeSecurity

Description:

MethodReturn valuesDescription

changeSecurity(String uuid, ChangeSecurity changeSecurity)

void

Changes the security of a node.

Example:

package com.openkm;

import java.util.ArrayList;
import java.util.List;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.ChangeSecurity;
import com.openkm.sdk4j.bean.GrantedRole;
import com.openkm.sdk4j.bean.GrantedUser;
import com.openkm.sdk4j.bean.Permission;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);

            List<GrantedUser> guList = new ArrayList<>();
            GrantedUser gu = new GrantedUser();
            gu.setUser("sochoa");
            gu.setPermissions(Permission.ALL_GRANTS);
            guList.add(gu);

            List<GrantedRole> grList = new ArrayList<>();
            GrantedRole gr = new GrantedRole();
            gr.setRole("ROLE_TEST");
            gr.setPermissions(Permission.READ | Permission.WRITE);
            grList.add(gr);

            ChangeSecurity cs = new ChangeSecurity();
            cs.setGrantedUsersList(guList);
            cs.setGrantedRolesList(grList);
            cs.setRecursive(true);
            ws.auth.changeSecurity("4f873d10-654e-4d99-a94f-15466e30a0f6", cs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

overwriteSecurity

Description:

MethodReturn valuesDescription

overwriteSecurity(String uuid, ChangeSecurity changeSecurity)

void

Overwrites the security of a node.

The ChangeSecurity object is used in changeSecurity and overwriteSecurity methods.

Although values set in the revokeUsers and revokeRoles variables of the ChangeSecurity object will not be taken into consideration when overwriting the security.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.*;
import com.openkm.sdk4j.impl.OKMWebservices;

import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            List<GrantedUser> guList = new ArrayList<>();
            GrantedUser gu = new GrantedUser();
            gu.setUser("sochoa");
            gu.setPermissions(Permission.ALL_GRANTS);
            guList.add(gu);

            List<GrantedRole> grList = new ArrayList<>();
            GrantedRole gr = new GrantedRole();
            gr.setRole("ROLE_TEST");
            gr.setPermissions(Permission.READ | Permission.WRITE);
            grList.add(gr);

            ChangeSecurity cs = new ChangeSecurity();
            cs.setGrantedUsersList(guList);
            cs.setGrantedRolesList(grList);
            cs.setRecursive(true);
            ws.auth.overwriteSecurity("4f873d10-654e-4d99-a94f-15466e30a0f6", cs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

hasSecurityRecursive

Description:

MethodReturn valuesDescription

hasSecurityRecursive()

Boolean

Checks if the user has grants to propagate security recursively.

The configuration parameter named "default.security.recursive.role" is used to indicate the role.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            System.out.println("hasSecurityRecursive = " + ws.auth.hasSecurityRecursive());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

isAdmin

Description:

MethodReturn valuesDescription

isAdmin()

Boolean

Checks if the authenticated user is a member of the administrators.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            System.out.println("Is Administrator " + ws.auth.isAdmin());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getSessionId

Description:

MethodReturn valuesDescription

getSessionId()

String

Gets the HTTP session ID.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            System.out.println(ws.auth.getSessionId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

createUser

Description:

MethodReturn valuesDescription

createUser(String userId, String password, String email, String name, boolean active)

void

Creates a new user.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            ws.auth.createUser("test", "password.2016", "test@mail.com", "User test", true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

deleteUser

Description:

MethodReturn valuesDescription

deleteUser(String userId)

void

Deletes a user.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            ws.auth.deleteUser("test");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

updateUser

Description:

MethodReturn valuesDescription

updateUser(String userId, String password, String email, String name, boolean active)

void

Updates a user.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            ws.auth.updateUser("test", "newpassword", "test@mail.com", "Test", false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

createRole

Description:

MethodReturn valuesDescription

createRole(String roleId, boolean active)

void

Creates a new role.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            ws.auth.createRole("ROLE_TEST", true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

deleteRole

Description:

MethodReturn valuesDescription

deleteRole(String roleId)

void

Deletes a role.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            ws.auth.deleteRole("ROLE_TEST");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

updateRole

Description:

MethodReturn valuesDescription

updateRole(String roleId, boolean active)

void

Updates a role.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            ws.auth.updateRole("ROLE_TEST", true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

assignRole

Description:

MethodReturn valuesDescription

assignRole(String userId, String roleId)

void

Assigns a role to a user.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            ws.auth.assignRole("test", "ROLE_USER");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

removeRole

Description:

MethodReturn valuesDescription

removeRole(String userId, String roleId)

void

Removes a role from a user.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            ws.auth.removeRole("test", "ROLE_USER");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getProfiles

Description:

MethodReturn valuesDescription

getProfiles(boolean filterByActive)

List<Profile>

Returns the list of all profiles.

When the filterByActive parameter is enabled, the method will return only the active profiles; otherwise it will return all available profiles.

Each user is assigned one profile that enables more or fewer of the OpenKM UI features.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.Profile;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            for (Profile profile : ws.auth.getProfiles(true)) {
                System.out.println(profile.getName());
                System.out.println(profile.getActive());
                System.out.println(profile.getPrfMisc());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getUserProfile

Description:

MethodReturn valuesDescription

getUserProfile(String userId)

Profile

Returns the profile assigned to a user.

Each user is assigned one profile that enables more or fewer of the OpenKM UI features.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.Profile;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            Profile profile = ws.auth.getUserProfile("okmAdmin");
            System.out.println(profile.getName());
            System.out.println(profile.getActive());
            System.out.println(profile.getPrfMisc());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

setUserProfile

Description:

MethodReturn valuesDescription

setUserProfile(String userId, long profileId)

void

Changes the assigned profile for a user.

Each user is assigned one profile that enables more or fewer of the OpenKM UI features.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.Profile;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            // Set the profile named "default" to the user
            for (Profile profile : ws.auth.getProfiles(true)) {
                if (profile.getName().equals("default")) {
                    ws.auth.setUserProfile("test", profile.getId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getUserToken

Description:

MethodReturn valuesDescription

getUserToken(String userId)

String

Returns the token for a user.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            String token = ws.auth.getUserToken("okmAdmin");
            System.out.println(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

setUserPermissions

Description:

MethodReturn valuesDescription

setUserPermissions(String uuid, String userId, int permissions, boolean recursive)

void

Updates user permissions on a node.

The recursive parameter only makes sense when the uuid is a folder or a record node.

When the recursive parameter is true, the change will be applied to the node and its descendants.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.Permission;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            // Update permissions of sochoa at the node but not descendants
            ws.auth.setUserPermissions("3c68b3a1-c65c-4b1e-84b5-9ce2712ca573", "sochoa", Permission.READ + Permission.WRITE, false);

            // Update permissions of okmAdmin at the node and descendants
            ws.auth.setUserPermissions("3c68b3a1-c65c-4b1e-84b5-9ce2712ca573", "okmAdmin", Permission.ALL_GRANTS, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

setRolePermissions

Description:

MethodReturn valuesDescription

setRolePermissions(String uuid, String roleId, int permissions, boolean recursive)

void

Updates role permissions on a node.

The recursive parameter only makes sense when the uuid is a folder or a record node.

When the recursive parameter is true, the change will be applied to the node and its descendants.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.Permission;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            // Update permissions of ROLE_USER at the node but not descendants
            ws.auth.setRolePermissions("3c68b3a1-c65c-4b1e-84b5-9ce2712ca573", "ROLE_USER", Permission.READ + Permission.WRITE, false);

            // Update permissions of ROLE_ADMIN at the node and descendants
            ws.auth.setRolePermissions("3c68b3a1-c65c-4b1e-84b5-9ce2712ca573", "ROLE_ADMIN", Permission.ALL_GRANTS, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getUserTenants

Description:

MethodReturn valuesDescription

getUserTenants()

List<Tenant>

Returns the list of all tenants.

Example:

package com.openkm;

import java.util.List;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.Tenant;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            List<Tenant> tenants = ws.auth.getUserTenants();
            for (Tenant tenant : tenants) {
                System.out.println(tenant);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

setUserTenant

Description:

MethodReturn valuesDescription

setUserTenant(long tenantId)

void

Changes the assigned tenant for a user.

A user might have access to several tenants but can have only one assigned.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            long tenantId = 1;
            ws.auth.setUserTenant(tenantId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

isLoginLowercase

Description:

MethodReturn valuesDescription

isLoginLowercase()

void

Returns true when the user must be set in lowercase for login.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            System.out.println(ws.auth.isLoginLowercase());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

isPasswordExpired

Description:

MethodReturn valuesDescription

isPasswordExpired()

void

True when the user's password has expired.

By default the password expiration is disabled. Take a look at the configuration parameter named "user.password.expiration" in the Security configuration parameters section to enable it.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            System.out.println("Password expired: " + ws.auth.isPasswordExpired());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getToken

Description:

MethodReturn valuesDescription

getToken()

void

Gets a new authentication token.

Authentication tokens expire after 24 hours or later (it depends on how they were created). This method helps refresh the current token.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            ws.getToken();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}