Ir al contenido
Otras versiones

Cargando…

OKMAuth

Esta página aún no está disponible en tu idioma.

Used for managing security and users. For example, add or remove grants on a node, create or modify users, or get profiles. 

The class com.openkm.bean.Permission contains permission values (READ, WRITE, etc.). You should use it in combination with methods that change or retrieve security grants.

In almost all methods you’ll see a parameter named “nodeId”. The value of this parameter can be a valid node UUID (folder, document, mail, record).

Also, in all methods you’ll see a parameter named “token”. Because cron tasks are executed in the background without authentication, methods used in this scenario might use the token parameter. From the default application execution context you must use the “null” value, which indicates that the application should use the “user session”.

Description:

Method Return values Description
login() void Simulates the user UI login process.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
okmAuth.login();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
login(String user, String pass) String The login process returns an authentication token.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
okmAuth.login("userId","password");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
grantUser(String token, String nodeId, String user, int permissions, boolean recursive) void Add a user grant on a node.
  • The parameter ‘recursive’ only applies when the nodeId is a folder or record node.
  • When the parameter ‘recursive’ is true, the change will be applied to the node and its descendants.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.bean.Permission;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
// Add sochoa write grants at the node but not descendants
okmAuth.grantUser(null, "b153c4b7-3d1c-4589-bd42-0ed0f34fd338", "sochoa", Permission.ALL_GRANTS, false);
// Add all okmAdmin grants at the node and descendants
okmAuth.grantUser(null, "b153c4b7-3d1c-4589-bd42-0ed0f34fd338", "okmAdmin", Permission.ALL_GRANTS, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
revokeUser(String token, String nodeId, String user, int permissions, boolean recursive) void Remove a user grant on a node.
  • The parameter ‘recursive’ only applies when the nodeId is a folder or a record node.
  • When the parameter ‘recursive’ is true, the change will be applied to the node and its descendants.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.bean.Permission;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
// Add sochoa write grants at the node but not descendants
okmAuth.revokeUser(null, "b153c4b7-3d1c-4589-bd42-0ed0f34fd338", "sochoa", Permission.ALL_GRANTS, false);
// Remove all okmAdmin grants at the node and descendants
okmAuth.revokeUser(null, "b153c4b7-3d1c-4589-bd42-0ed0f34fd338", "okmAdmin", Permission.ALL_GRANTS, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getGrantedUsers(String token, String nodeId) Map<String, Integer> Returns the granted users of a node.

Example:

package com.openkm;
import java.util.Map;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
Map<String, Integer> grants = okmAuth.getGrantedUsers(null, "b153c4b7-3d1c-4589-bd42-0ed0f34fd338");
for (String role : grants.keySet()) {
System.out.println(role + "->" + grants.get(role));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
grantRole(String token, String nodeId, String role, int permissions, boolean recursive) void Add a role grant on a node.
  • The parameter ‘recursive’ only applies when the nodeId is a folder or record node.
  • When the parameter ‘recursive’ is true, the change will be applied to the node and its descendants.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.bean.Permission;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
// Add ROLE_USER write grants at the node but not descendants
okmAuth.grantRole(null, "b153c4b7-3d1c-4589-bd42-0ed0f34fd338", "ROLE_USER", Permission.ALL_GRANTS, false);
// Add all ROLE_ADMIN grants to the node and descendants
okmAuth.grantRole(null, "b153c4b7-3d1c-4589-bd42-0ed0f34fd338", "ROLE_ADMIN", Permission.ALL_GRANTS, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
revokeRole(String token, String nodeId, String role, int permissions, boolean recursive) void Remove a role grant on a node.
  • The parameter ‘recursive’ only applies when the nodeId is a folder or record node.
  • When the parameter ‘recursive’ is true, the change will be applied to the node and its descendants.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.bean.Permission;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
// Remove ROLE_USER write grants at the node but not descendants
okmAuth.revokeRole(null, "b153c4b7-3d1c-4589-bd42-0ed0f34fd338", "ROLE_USER", Permission.ALL_GRANTS, false);
// Remove all ROLE_ADMIN grants to the node and descendants
okmAuth.revokeRole(null, "b153c4b7-3d1c-4589-bd42-0ed0f34fd338", "ROLE_ADMIN", Permission.ALL_GRANTS, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getGrantedRoles(String token, String nodeId) Map<String, Integer> Returns the granted roles of a node.

Example:

package com.openkm;
import java.util.Map;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
Map<String, Integer> grants = okmAuth.getGrantedRoles(null, "b153c4b7-3d1c-4589-bd42-0ed0f34fd338");
for (String role : grants.keySet()) {
System.out.println(role + "->" + grants.get(role));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getUsers(String token) List Returns the list of all users.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.bean.CommonUser;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
for (CommonUser user : okmAuth.getUsers(null)) {
System.out.println(user.getId());
System.out.println(user.getName());
System.out.println(user.getEmail());
System.out.println(user.getActive());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getRoles(String token) List Returns the list of all roles.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
for (String role : okmAuth.getRoles(null)) {
System.out.println(role);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getUsersByRole(String token, String role) List Returns the list of all users who have been assigned a role.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.bean.CommonUser;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
for (CommonUser user : okmAuth.getUsersByRole(null, "ROLE_ADMIN")) {
System.out.println(user.getId());
System.out.println(user.getName());
System.out.println(user.getEmail());
System.out.println(user.getActive());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getRolesByUser(String token, String user) List Returns the list of all roles assigned to a user.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
for (String role : okmAuth.getRolesByUser(null, "okmAdmin")) {
System.out.println(role);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
public void changeSecurity(String token, String nodeId, Map<String, Integer> grantUsers, Map<String, Integer> revokeUsers,
            Map<String, Integer> grantRoles, Map<String, Integer> revokeRoles, boolean recursive)
void Change the security of a node.

Example:

package com.openkm;
import java.util.HashMap;
import java.util.Map;
import com.openkm.api.OKMAuth;
import com.openkm.bean.Permission;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
String nodeId = "b153c4b7-3d1c-4589-bd42-0ed0f34fd338";
Map<String, Integer> grantUsers = new HashMap<>();
Map<String, Integer> revokeUsers = new HashMap<>();
Map<String, Integer> grantRoles = new HashMap<>();
grantRoles.put("ROLE_TEST", Permission.READ | Permission.WRITE);
Map<String, Integer> revokeRoles = new HashMap<>();
okmAuth.changeSecurity(null, nodeId, grantUsers, revokeUsers, grantRoles, revokeRoles, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
public void overwriteSecurity(String token, String nodeId, Map<String, Integer> grantUsers, 
            Map<String, Integer> grantRoles,  boolean recursive)
void Overwrite the security of a node.

Example:

package com.openkm;
import java.util.HashMap;
import java.util.Map;
import com.openkm.api.OKMAuth;
import com.openkm.bean.Permission;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
String nodeId = "b153c4b7-3d1c-4589-bd42-0ed0f34fd338";
Map<String, Integer> grantUsers = new HashMap<>();
Map<String, Integer> grantRoles = new HashMap<>();
grantRoles.put("ROLE_TEST", Permission.READ | Permission.WRITE);
okmAuth.overwriteSecurity(null, nodeId, grantUsers, grantRoles, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
createUser(String token, User user) void Create a new user.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.bean.User;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
User user = new User();
user.setId("test");
user.setPassword("password.2019");
user.setEmail("some@mail.com");
user.setName("User Name");
user.setActive(true);
okmAuth.createUser(null, user);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
deleteUser(String token, String user) void Delete a user.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
okmAuth.deleteUser(null, "test");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
updateUser(String token, User user) void Update a user.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.bean.User;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
User user = new User();
user.setId("test");
user.setPassword("newpassword");
user.setEmail("some@mail.com");
user.setName("Test");
user.setActive(false);
okmAuth.updateUser(null, user);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
createRole(String token, Role role) void Create a new role.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.bean.Role;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
Role role = new Role();
role.setId("ROLE_TEST");
role.setActive(true);
okmAuth.createRole(null, role);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
deleteRole(String token, String role) void Delete a role.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
okmAuth.deleteRole(null, "ROLE_TEST");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
updateRole(String token, String role, boolean active) void Update a role.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
public class Test {
public static void main(String[] args) {
try {
OKMAuth.getInstance().updateRole(null, "ROLE_TEST",true);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
assignRole(String token, String user, String role) void Assign a role to a user.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
okmAuth.assignRole(null, "test", "ROLE_USER");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
removeRole(String token, String user, String role) void Remove a role from a user.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
okmAuth.removeRole(null, "test", "ROLE_USER");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getProfiles(String token, boolean filterByActive) List Returns the list of all profiles.
  • When the parameter ‘filterByActive’ is enabled, the method will return only the active profiles; otherwise it will return all available profiles.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.db.bean.Profile;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
for (Profile profile : okmAuth.getProfiles(null, true)) {
System.out.println(profile);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getUserProfile(String token, String userId) Profile Returns the profile assigned to a user.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
System.out.println(okmAuth.getUserProfile(null, "okmAdmin"));
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
setUserProfile(String token, String userId, long profileId) void Change the profile assigned to a user.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.db.bean.Profile;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
// Set the profile named "default" to the user
for (Profile profile : okmAuth.getProfiles(null, true)) {
if (profile.getName().equals("default")) {
okmAuth.setUserProfile(null, "okmAdmin", profile.getId());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
logout() void Execute the logout method on the OpenKM side.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
okmAuth.logout(null);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getUser(String token, String userId) CommonUser Returns user data.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.bean.CommonUser;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
CommonUser commonUser = okmAuth.getUser(null, "okmAdmin");
System.out.print(commonUser);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
public void setUserPermissions(String token, String nodeId, String user, int permissions, boolean recursive) void Update user permissions on a node.
  • The parameter ‘recursive’ only applies when the UUID is a folder or record node.
  • When the parameter ‘recursive’ is true, the changes will be applied to the node and its descendants.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.bean.Permission;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
// Update permissions of sochoa at the node but not descendants
okmAuth.setUserPermissions(null, "3c68b3a1-c65c-4b1e-84b5-9ce2712ca573", "sochoa", Permission.READ + Permission.WRITE, false);
// Update permissions of okmAdmin at the node and descendants
okmAuth.setUserPermissions(null, "3c68b3a1-c65c-4b1e-84b5-9ce2712ca573", "okmAdmin", Permission.ALL_GRANTS, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
setRolePermissions(String token, String nodeId, String role, int permissions, boolean recursive) void Update role permissions on a node.
  • The parameter ‘recursive’ only applies when the UUID is a folder or record node.
  • When the parameter ‘recursive’ is true, the changes will be applied to the node and its descendants.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.bean.Permission;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
// Update permissions of ROLE_USER at the node but not descendants
okmAuth.setRolePermissions(null, "3c68b3a1-c65c-4b1e-84b5-9ce2712ca573", "ROLE_USER", Permission.READ + Permission.WRITE, false);
// Update permissions of ROLE_ADMIN at the node and descendants
okmAuth.setRolePermissions(null, "3c68b3a1-c65c-4b1e-84b5-9ce2712ca573", "ROLE_ADMIN", Permission.ALL_GRANTS, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getUserTenants(String token) List Returns the list of all tenants to which the user has access.

Example:

package com.openkm;
import java.util.List;
import com.openkm.api.OKMAuth;
import com.openkm.db.bean.Tenant;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
List<Tenant> tenants = okmAuth.getUserTenants(null);
for (Tenant tenant : tenants) {
System.out.println(tenant);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
setUserTenant(String token,  long tenantId) void Change the tenant assigned to a user.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
long tenantId = 1;
okmAuth.setUserTenant(null, tenantId);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
isPasswordExpired(String token) Boolean Check whether the user’s password has expired.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
Boolean isExpired = okmAuth.isPasswordExpired(null);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
resetPassword(String userId) void Sends an email with a link that, if the user clicks it, will reset the password.

Example:

package com.openkm;
import com.openkm.api.OKMAuth;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
String userId = "okmAdmin";
okmAuth.resetPassword(userId);
} catch (Exception e) {
e.printStackTrace();
}
}
}