Creating users

From Protege Wiki

Jump to: navigation, search

Adding and removing users programmatically in client-server mode

Adding users

In client-server mode (multi-user mode), one can create a user/password programmatically with the following code:

try {
    server = (RemoteServer) Naming.lookup("//" + serverName + "/" + Server.getBoundName());

    //optional - check if the server allows the creation of new users
    //boolean allowsUserCreate = server.allowsCreateUsers();
    
    server.createUser(username, password);

    } catch (Exception e) {
        Log.getLogger().severe(Log.toString(e));
    }


After creating a new user, the server will automatically save the metaproject file.


Note: The server is configured by default not to allow the creation of new users in the UI (this does not apply to the programmatic access). To enable the creation of users in the UI, add in the protege.properties file from the Protege installation directory on the server the following line:

server.allow.create.users=true


Removing users

Currently there is no method in the RemoteServer interface that allows the deletion of users. We plan to add it soon. Until then, you can use a ProtegeJob to execute a remote job on the server that deletes a user instance from the server metaproject.

A ProtegeJob defines a unit of work that is written on the client, but executed on the server.

The implementation of the remote job for deleting a user is done in class RemoveUserJob that is shown below. (You can copy this class in your code and use it.)


package testing;

import java.util.ArrayList;

import edu.stanford.smi.protege.exception.ProtegeException;
import edu.stanford.smi.protege.model.KnowledgeBase;
import edu.stanford.smi.protege.server.metaproject.MetaProject;
import edu.stanford.smi.protege.server.metaproject.ProjectInstance;
import edu.stanford.smi.protege.server.metaproject.User;
import edu.stanford.smi.protege.server.metaproject.impl.WrappedProtegeInstanceImpl;
import edu.stanford.smi.protege.util.ProtegeJob;

public class RemoveUserJob extends ProtegeJob {
	private String userName;
	
	public RemoveUserJob(KnowledgeBase kb, String userName) {
		super(kb);
		this.userName = userName;
	}
	
	@Override
	public Object run() throws ProtegeException {
		ProjectInstance metaProjectInstance = getMetaProjectInstance();
		MetaProject metaproject = metaProjectInstance.getMetaProject();
		
		User user = metaproject.getUser(userName);
		if (user != null) {
			((WrappedProtegeInstanceImpl) user).getProtegeInstance().delete();
		
			ArrayList errors = new ArrayList();
			metaproject.save(errors);				
		}
		
		return null;
	}		
}


To code to invoke the remove user job from a Protege client application is as following:

        //kb - the client kb; 2nd arg - the user name to be deleted
	RemoveUserJob removeUserJob = new RemoveUserJob(kb, "Guest");
	removeUserJob.execute();
Personal tools