[opencms-dev] Deactivate a User in java-code

Pujan Kafle kafle.pujan at jimmy.harvard.edu
Thu Jul 22 15:36:53 CEST 2010


Hello Frank,

I did something like this in the past:
My web-interface displays a list of OpenCms users and I provided a 
button to toggle activate/deactivate user.
- getOpenCmsUsers() returns the list of all OpenCms Users. I  am 
creating a hashtable with key "OpenCms userID" and value "CmsUser" and 
then setting it into session.

public List getOpenCmsUsers(){
	List openCmsUsers = null;
	CmsObject cmsObj = null;
	
	// get guest account name
	CmsDefaultUsers cdu = new CmsDefaultUsers();
	String guestUsername = cdu.getUserGuest();
	
	try {

		// get CmsObject as guest
		cmsObj = OpenCms.initCmsObject(cdu.getUserGuest());

		// login as Admin user
		cmsObj.loginUser("Admin","admin");
		
	}catch(CmsException e){

	}
	
	CmsOrgUnitManager oum = OpenCms.getOrgUnitManager();
	try {
		openCmsUsers = oum.getUsers(cmsObj,"",true);
	}
	catch (CmsException e){
		
	}
	return openCmsUsers;
}

public Hashtable<String, CmsUser> getOpenCmsUsersTable(){
	Hashtable<String, CmsUser> openCmsUsersTable = new Hashtable<String, 
CmsUser>();
	List users = getOpenCmsUsers();
	CmsUser c = null;
	
	if (users != null && users.size() > 0){
		Iterator userIter = users.iterator();
		while (userIter.hasNext()){
			c = (CmsUser)userIter.next();
			openCmsUsersTable.put(c.getId().getStringValue().trim(), c);
		}
	}	
	return openCmsUsersTable;
}


-----------

The activate/deactivate button passes CmsUser.getId() (userID field) to 
my handler and I execute the following snippet to toggle 
activate/deactivate:

	Hashtable usersTable = (Hashtable)session.getAttribute("usersHash");

	CmsUser user = (CmsUser)usersTable.get(userID);

	user.setEnabled((user.isEnabled())? false : true);
	try {
		cmsObject.writeUser(user);	
	}
	catch (CmsException e){
		errorMsg = e.getMessage();
		session.setAttribute("errorMsg", errorMsg);
	}

I hope it helps.

Best,
Pujan


On 7/22/2010 4:18 AM, Frank Pertack wrote:
> Hello,
>
> I have to write a class, in which a user can be deavtivated by a web-Interface.
>
> I found out the method "setDisabled()" in the CmsUser is deprecated. I shall use the CmsPrincipal instead.
> But somehow I am not able to instantiate a cmsPrincipal.
> Can anybody help me with a code-snippet, how I shall use the CmsPrincipal?
> - How can I load the User-Data with "readPrincipal(CmsObject cms, CmsUUID id)"?
> - Do I understand it right that the new method is "cmsPrincipal.setEnabled(false)" when I like to disable a user?
>
> Thank you in advance.
>
> Best regards
> Frank
>
> _______________________________________________
> This mail is sent to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://lists.opencms.org/mailman/listinfo/opencms-dev



More information about the opencms-dev mailing list