Creating your own login module
The class must implement the PrincipalAdapter interface.
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 {
/** * Method to retrieve all users from a authentication source. * * @return A Collection with all the users. * @throws PrincipalAdapterException If any error occurs. */ List<CommonUser> getUsers() throws PrincipalAdapterException;
/** * Method to retrieve all roles from a authentication source. * * @return A Collection with all the roles. * @throws PrincipalAdapterException If any error occurs. */ List<String> getRoles() throws PrincipalAdapterException;
/** * Method to retrieve user data * * @return the user data * @throws PrincipalAdapterException If any error occurs. */ CommonUser getUser(String userId) throws PrincipalAdapterException;
/** * Method to retrieve all users from a role. * * @return A Collection with all the users within a role. * @throws PrincipalAdapterException If any error occurs. */ List<CommonUser> getUsersByRole(String role) throws PrincipalAdapterException;
/** * Method to retrieve all roles from a user. * * @return A Collection with all the roles of the user. * @throws PrincipalAdapterException If any error occurs. */ List<String> getRolesByUser(String user) throws PrincipalAdapterException;
/** * Method to retrieve the user password * * @param user A user id. * @return The password of the user. * @throws PrincipalAdapterException If any error occurs. */ String getPassword(String user) throws PrincipalAdapterException;
/* * ------------------------------------------------------------------ * These methods only works if using the OpenKM user database. * ------------------------------------------------------------------ */ /** * Method to create a new user * * @param user User data * @return User * @throws PrincipalAdapterException If any error occurs. */ CommonUser createUser(CommonUser user) throws PrincipalAdapterException;
/** * Method to create a delete a user * * @param user A user id. * @throws PrincipalAdapterException If any error occurs. */ void deleteUser(String user) throws PrincipalAdapterException;
/** * Update user information * * @param user User data * @return User * @throws PrincipalAdapterException If any error occurs. */ CommonUser updateUser(CommonUser user) throws PrincipalAdapterException;
/** * Method to create a new role * * @param role A role id. * @throws PrincipalAdapterException If any error occurs. */ void createRole(String role, boolean active) throws PrincipalAdapterException;
/** * Method to create a delete a role * * @param role A role id. * @throws PrincipalAdapterException If any error occurs. */ void deleteRole(String role) throws PrincipalAdapterException;
/** * Update role information * * @param role A role id.. * @throws PrincipalAdapterException If any error occurs. */ void updateRole(String role, boolean active) throws PrincipalAdapterException;
/** * Method to assign a role * * @param user A user id. * @param role A role id. * @throws PrincipalAdapterException If any error occurs. */ void assignRole(String user, String role) throws PrincipalAdapterException;
/** * Method to remove a role * * @param user A user id. * @param role A role id. * @throws PrincipalAdapterException If any error occurs. */ void removeRole(String user, String role) throws PrincipalAdapterException;
/** * Get all profiles * * @param filterByActive * @return A list with all the registered profiles * @throws PrincipalAdapterException */ List<Profile> getProfiles(boolean filterByActive) throws PrincipalAdapterException;
/** * Get user profile * * @return The profile assigned to this user * @throws PrincipalAdapterException */ Profile getUserProfile(String userId) throws PrincipalAdapterException;
/** * Set the user profile * * @param userId The user id. * @param profileId The profile to be assigned. * @throws PrincipalAdapterException */ void setUserProfile(String userId, long profileId) throws PrincipalAdapterException;
boolean isManageUsers();
boolean isManageRoles();}PrincipalAdapter sample based on properties
Section titled “PrincipalAdapter sample based on properties”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"); }}