Creating your own Principal Adapter

Principal adapters are used by authentication module to retrieve users and roles information. For example retrieve users and roles from some LDAP, or external database that stores all the company users and roles.

You can create your own Principal Adapter

Conditions:

  • The new Principal Adapter class must implement the "PrincipalAdapter" interface.
  • The new Principal Adapter class must be declared under the package "com.openkm.plugin.principal".
  • The new Principal Adapter class must be annotated with "@PluginImplementation".
  • The new Principal Adapter class must extend of "BasePlugin".

Principal Adapter 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 {

    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();
}

The new class must be loaded into the package com.openkm.plugin.principal because application plugins system will try to load from there.

Do not miss the tag @PluginImplementation otherwise, the application plugin system will not be able to retrieve the new class.

More information at Register a new plugin.

To enable the new Principal adapter go to Administration > Configuration parameters > find the parameter named principal.adapter and modify the value with com.openkm.plugin.adapter.OwnPrincipalAdapter

To take effect this change we need to restart the application.

Methods description

MethodTypeDescription

getUsers()

List<CommonUser>

Return the list of all the users.

getRoles()

List<String>

Return the list of all the roles.

getUser(String userId)

CommonUser

Return all user data

getUsersByRole(String role)

List<CommonUser>

Return the list of all the users who have assigned a role.

getRolesByUser(String user)

List<String>

Return the list of all the roles assigned to a user.

String getPassword(String user)

String

Return the password associated to a specific user.

createUser(CommonUser user)

void

Create a new user.

deleteUser(String user)

void

Delete a user.

updateUser(CommonUser user)

void

Update a user.

Password parameter can be null or empty.

createRole(String role, boolean active)

void

Create a new role.

deleteRole(String role)

void

Delete a role.

updateRole(String role, boolean active)

void

Update a role.

assignRole(String user, String role)

void

Grant user with some role.

removeRole(String user, String role)

void

Revoke a role to some user.

getProfiles(boolean filterByActive)

List<Profile>

Return the list of all profiles.

getUserProfile(String userId)

Profile

Return the profile assigned to a user.

setUserProfile(String userId, long profileId)

void

Change the assigned profile to a user.

isManageUsers()

boolean

Returns a boolean that indicates if the user is on manager or not.

isManageRoles()

boolean

Returns a boolean that indicates if the role is on manager or not.

Example of the Principal adapter implementation

The example retrieves users, roles and mails based on properties files.

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");
    }

}