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.
Para crear su propio adaptador de principales debe crear una nueva clase que implemente la interfaz PrincipalAdapter:
package com.openkm.principal;
import net.xeoh.plugins.base.Plugin;
import java.util.List;
import com.openkm.dao.bean.Profile;
public interface PrincipalAdapter extends Plugin {
public List<String> getUsers() throws PrincipalAdapterException; public List<String> getRoles() throws PrincipalAdapterException; public List<String> getUsersByRole(String role) throws PrincipalAdapterException; public List<String> getRolesByUser(String user) throws PrincipalAdapterException; public String getMail(String user) throws PrincipalAdapterException; public String getName(String user) throws PrincipalAdapterException; public String getPassword(String user) throws PrincipalAdapterException;
/* * ------------------------------------------------------------------ * These methods only works if using the OpenKM user database. * ------------------------------------------------------------------ */ public void createUser(String user, String password, String email, String name, boolean active) throws PrincipalAdapterException; public void deleteUser(String user) throws PrincipalAdapterException; public void updateUser(String user, String password, String email, String name, boolean active) throws PrincipalAdapterException; public void createRole(String role, boolean active) throws PrincipalAdapterException; public void deleteRole(String role) throws PrincipalAdapterException; public void updateRole(String role, boolean active) throws PrincipalAdapterException; public void assignRole(String user, String role) throws PrincipalAdapterException; public void removeRole(String user, String role) throws PrincipalAdapterException; public List<Profile> getProfiles(boolean filterByActive) throws PrincipalAdapterException; public Profile getUserProfile(String userId) throws PrincipalAdapterException; public void setUserProfile(String userId, long profileId) throws PrincipalAdapterException;}La nueva clase debe cargarse en el paquete com.openkm.principal, porque el sistema de plugins de la aplicación intentará cargarla desde ahí. Vea el ejemplo a continuación:
Descripción de los métodos
Sección titulada «Descripción de los métodos»| Método | Tipo | Descripción |
|---|---|---|
| getUsers() | List |
Devuelve una lista de ID de usuarios. |
| getRoles() | List |
Devuelve una lista de ID de roles. |
| getUsersByRole(String role)() | List |
Devuelve una lista de ID de usuarios filtrada por un rol. |
| getRolesByUser(String user) | List |
Devuelve una lista de ID de roles filtrada por un usuario. |
| getMail(String user) | String | Devuelve el correo asociado a un usuario concreto. |
| getName(String user) | String | Devuelve el nombre asociado a un usuario concreto. |
| String getPassword(String user) | String | Devuelve la contraseña asociada a un usuario concreto. |
| createUser(String user, String password, String email, String name, boolean active) | void | Crea un nuevo usuario. |
| deleteUser(String user) | void | Elimina un usuario. |
| updateUser(String user, String password, String email, String name, boolean active) | void | Actualiza los valores del 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 | Concede 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 | Establece el perfil de un usuario concreto. |
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.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 net.xeoh.plugins.base.annotations.PluginImplementation;
import org.apache.commons.lang.NotImplementedException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import com.openkm.core.Config;import com.openkm.dao.bean.Profile;
@PluginImplementationpublic class UsersRolesPrincipalAdapter implements PrincipalAdapter { private static Logger log = LoggerFactory.getLogger(UsersRolesPrincipalAdapter.class);
@Override public List<String> getUsers() throws PrincipalAdapterException { log.debug("getUsers()"); List<String> list = new ArrayList<String>(); 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)) { list.add(user); } }
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 List<String> getUsersByRole(String role) throws PrincipalAdapterException { throw new NotImplementedException("getUsersByRole"); }
@Override public List<String> getRolesByUser(String user) throws PrincipalAdapterException { throw new NotImplementedException("getRolesByUser"); }
@Override public String getMail(String user) throws PrincipalAdapterException { log.debug("getMail({})", user); Properties prop = new Properties(); String mail = null;
try { prop.load(new FileInputStream(Config.HOME_DIR + "/server/default/conf/props/openkm-emails.properties")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
mail = prop.getProperty(user); log.debug("getMail: {}", mail); return mail; }
@Override public String getName(String user) throws PrincipalAdapterException { log.debug("getName({})", user); Properties prop = new Properties(); String name = null;
try { prop.load(new FileInputStream(Config.HOME_DIR + "/server/default/conf/props/openkm-names.properties")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
name = prop.getProperty(user); log.debug("getName: {}", name); return name; }
@Override public String getPassword(String user) throws PrincipalAdapterException { throw new UnsupportedOperationException("Not implemented"); }
@Override public void createUser(String user, String password, String email, String name, boolean active) throws PrincipalAdapterException { throw new NotImplementedException("createUser"); }
@Override public void deleteUser(String user) throws PrincipalAdapterException { throw new NotImplementedException("deleteUser"); }
@Override public void updateUser(String user, String password, String email, String name, boolean active) throws PrincipalAdapterException { throw new NotImplementedException("updateUser"); }
@Override public void createRole(String role, boolean active) throws PrincipalAdapterException { throw new NotImplementedException("createRole"); }
@Override public void deleteRole(String role) throws PrincipalAdapterException { throw new NotImplementedException("deleteRole"); }
@Override public void updateRole(String role, boolean active) throws PrincipalAdapterException { throw new NotImplementedException("updateRole"); }
@Override public void assignRole(String user, String role) throws PrincipalAdapterException { throw new NotImplementedException("assignRole"); }
@Override public void removeRole(String user, String role) throws PrincipalAdapterException { throw new NotImplementedException("removeRole"); }
@Override public List<Profile> getProfiles(boolean filterByActive) throws PrincipalAdapterException { throw new NotImplementedException("getProfiles"); }
@Override public Profile getUserProfile(String userId) throws PrincipalAdapterException { throw new NotImplementedException("getUserProfile"); }
@Override public void setUserProfile(String userId, long profileId) throws PrincipalAdapterException { throw new NotImplementedException("setUserProfile"); }}