Ir al contenido
Otras versiones

Cargando…

Crear su propio adaptador de principales (Principal Adapter)

Los adaptadores de principales (Principal adapters) son utilizados por el módulo de autenticación para obtener la información de usuarios y roles. Por ejemplo, para obtener usuarios y roles de algún LDAP o de una base de datos externa que almacene todos los usuarios y roles de la empresa.

Puede crear su propio Principal Adapter.

Condiciones:

  • La nueva clase Principal Adapter debe implementar la interfaz “PrincipalAdapter”.
  • La nueva clase Principal Adapter debe declararse en el paquete “com.openkm.plugin.principal”.
  • La nueva clase de captura de datos OCR debe estar anotada con “@PluginImplementation”.
  • La nueva clase Principal Adapter debe extender “BasePlugin”.

Interfaz Principal Adapter:

package com.openkm.plugin.principal;
import com.openkm.bean.CommonUser;
import com.openkm.db.bean.Profile;
import com.openkm.principal.PrincipalAdapterException;
import net.xeoh.plugins.base.Plugin;
import java.util.List;
public interface PrincipalAdapter extends Plugin {
List<CommonUser> getUsers() throws PrincipalAdapterException;
List<String> getRoles() throws PrincipalAdapterException;
CommonUser getUser(String userId) throws PrincipalAdapterException;
List<CommonUser> getUsersByRole(String role) throws PrincipalAdapterException;
List<String> getRolesByUser(String user) throws PrincipalAdapterException;
String getPassword(String user) throws PrincipalAdapterException;
/*
* ------------------------------------------------------------------
* These methods only works if using the OpenKM user database.
* ------------------------------------------------------------------
*/
CommonUser createUser(CommonUser user) throws PrincipalAdapterException;
void deleteUser(String user) throws PrincipalAdapterException;
CommonUser updateUser(CommonUser user) throws PrincipalAdapterException;
void createRole(String role, boolean active) throws PrincipalAdapterException;
void deleteRole(String role) throws PrincipalAdapterException;
void updateRole(String role, boolean active) throws PrincipalAdapterException;
void assignRole(String user, String role) throws PrincipalAdapterException;
void removeRole(String user, String role) throws PrincipalAdapterException;
List<Profile> getProfiles(boolean filterByActive) throws PrincipalAdapterException;
Profile getUserProfile(String userId) throws PrincipalAdapterException;
void setUserProfile(String userId, long profileId) throws PrincipalAdapterException;
boolean isManageUsers();
boolean isManageRoles();
}

La nueva clase debe cargarse en el paquete com.openkm.plugin.principal, porque el sistema de plugins de la aplicación intentará cargarla desde ahí.

Método Tipo Descripción
getUsers() List Devuelve una lista de todos los usuarios.
getRoles() List Devuelve una lista de todos los roles.
getUser(String userId) CommonUser Devuelve todos los datos de un usuario.
getUsersByRole(String role) List Devuelve una lista de todos los usuarios a los que se ha asignado un rol.
getRolesByUser(String user) List Devuelve una lista de todos los roles asignados a un usuario.
String getPassword(String user) String Devuelve la contraseña asociada a un usuario concreto.
createUser(CommonUser user) void Crea un usuario nuevo.
deleteUser(String user) void Elimina un usuario.
updateUser(CommonUser user) void Actualiza un usuario.
El parámetro de contraseña puede ser nulo o estar vacío.
createRole(String role, boolean active) void Crea un nuevo rol.
deleteRole(String role) void Elimina un rol.
updateRole(String role, boolean active) void Actualiza un rol.
assignRole(String user, String role) void Asigna un rol a un usuario.
removeRole(String user, String role) void Revoca un rol a un usuario.
getProfiles(boolean filterByActive) List Devuelve una lista con todos los perfiles.
getUserProfile(String userId) Profile Devuelve el perfil asociado a un usuario concreto.
setUserProfile(String userId, long profileId) void Cambia el perfil asignado a un usuario.
isManageUsers() boolean Devuelve un booleano que indica si el usuario es un gestor.
isManageRoles() boolean Devuelve un booleano que indica si el rol está gestionado.

Ejemplo de implementación del adaptador de principales

Sección titulada «Ejemplo de implementación del adaptador de principales»

El ejemplo obtiene usuarios, roles y correos a partir de archivos de propiedades.

package com.openkm.plugin.principal;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.openkm.api.OKMAuth;
import com.openkm.bean.CommonUser;
import com.openkm.core.Config;
import com.openkm.db.bean.Profile;
import com.openkm.plugin.BasePlugin;
import com.openkm.principal.PrincipalAdapterException;
public class UsersRolesPrincipalAdapter extends BasePlugin implements PrincipalAdapter {
private static Logger log = LoggerFactory.getLogger(UsersRolesPrincipalAdapter.class);
@Autowired
private OKMAuth okmAuth;
@Override
public List<CommonUser> getUsers() throws PrincipalAdapterException {
log.debug("getUsers()");
List<CommonUser> list = new ArrayList<>();
Properties prop = new Properties();
try {
prop.load(new FileInputStream(Config.HOME_DIR + "/server/default/conf/props/openkm-users.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for (Enumeration<Object> e = prop.keys(); e.hasMoreElements();) {
String user = (String) e.nextElement();
if (!Config.SYSTEM_USER.equals(user)) {
CommonUser commonUser = okmAuth.getUser(null, user);
list.add(commonUser);
}
}
log.debug("getUsers: {}", list);
return list;
}
@Override
public List<String> getRoles() throws PrincipalAdapterException {
log.debug("getRoles()");
List<String> list = new ArrayList<String>();
Properties prop = new Properties();
try {
prop.load(new FileInputStream(Config.HOME_DIR + "/server/default/conf/props/openkm-roles.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for (Enumeration<Object> e = prop.elements(); e.hasMoreElements();) {
for (StringTokenizer st = new StringTokenizer((String) e.nextElement(), ","); st.hasMoreTokens();) {
String role = st.nextToken();
if (!Config.DEFAULT_ADMIN_ROLE.equals(role) && !list.contains(role)) {
list.add(role);
}
}
}
log.debug("getRoles: {}", list);
return list;
}
@Override
public CommonUser getUser(String userId) throws PrincipalAdapterException {
throw new UnsupportedOperationException("getUser");
}
@Override
public List<CommonUser> getUsersByRole(String role) throws PrincipalAdapterException {
throw new UnsupportedOperationException("getUsersByRole");
}
@Override
public List<String> getRolesByUser(String user) throws PrincipalAdapterException {
throw new UnsupportedOperationException("getRolesByUser");
}
@Override
public String getPassword(String user) throws PrincipalAdapterException {
throw new UnsupportedOperationException("getPassword");
}
@Override
public CommonUser createUser(CommonUser user) throws PrincipalAdapterException {
throw new UnsupportedOperationException("createUser");
}
@Override
public void deleteUser(String user) throws PrincipalAdapterException {
throw new UnsupportedOperationException("deleteUser");
}
@Override
public CommonUser updateUser(CommonUser user) throws PrincipalAdapterException {
throw new UnsupportedOperationException("updateUser");
}
@Override
public void createRole(String role, boolean active) throws PrincipalAdapterException {
throw new UnsupportedOperationException("createRole");
}
@Override
public void deleteRole(String role) throws PrincipalAdapterException {
throw new UnsupportedOperationException("deleteRole");
}
@Override
public void updateRole(String role, boolean active) throws PrincipalAdapterException {
throw new UnsupportedOperationException("updateRole");
}
@Override
public void assignRole(String user, String role) throws PrincipalAdapterException {
throw new UnsupportedOperationException("assignRole");
}
@Override
public void removeRole(String user, String role) throws PrincipalAdapterException {
throw new UnsupportedOperationException("removeRole");
}
@Override
public List<Profile> getProfiles(boolean filterByActive) throws PrincipalAdapterException {
throw new UnsupportedOperationException("getProfiles");
}
@Override
public Profile getUserProfile(String userId) throws PrincipalAdapterException {
throw new UnsupportedOperationException("getUserProfile");
}
@Override
public void setUserProfile(String userId, long profileId) throws PrincipalAdapterException {
throw new UnsupportedOperationException("setUserProfile");
}
@Override
public boolean isManageUsers() {
throw new UnsupportedOperationException("isManageUsers");
}
@Override
public boolean isManageRoles() {
throw new UnsupportedOperationException("isManageRoles");
}
}