Getting user roles in portal

Hi,
We can get the current user’s name by using principalData.getUserName().
In the same way, can we get the current user’s role? We are storing the users roles in database. The portlet content is shown according to these roles.

So I assume, portal must be carrying the user role detail somewhere. Can we retreive that in anyway?

Thanks and Regards,
Ninad

If you are customizing content based on roles then you should probably be writing skin and shell rules as well as leveraging acl’s based on role membership. Also, if you are using MWS 7.0, then you can use the Security Roles and binding expressions to show/hide controls based on role membership.

My point is, ideally you shouldn’t have to directly query for all the user’s current roles. Maybe you could further illustrate your precise use-case and we can pursue the options.

Regards,
–mark

We want a cutsom message to appear for users who dont have access to certain links. If a user who is not having rights to access a particular portal page, a message should say that ‘please contact this this for rights’.
This is possible if we can get the current roles for the users. Is it possible to get the current role?
Thanks and Regards,
Ninad

Sounds good. Part of the issue would be that if you are using MWS Permissions to decide who has access to links, then you won’t even know that those links don’t exist. So i think we might need to rethink this scenario.

Do you want to send me an email to discuss further?
–mark

mark.imel@webmethods.com

Using 7.0, the easiest way to get Role information about a user is to use the UserModel and RoleModel classes.

For instance, if you want the roles of the current user you would use:
UserModel user = new UserModel();
List roles = user.getRoleMembership();
for (Iterator it = roles.iterator(); it.hasNext()) {
RoleModel role = (RoleModel)it.next();
//Do what you want with a role model
}

If you want to lookup a differen’t users role information, then you can set the id/alias/dn of the UserModel prior to looking up its roles.

Note the Javadocs for these are available at: $PORTAL/doc/javaapi/wm-caf-jsf-impl/index.html

Regards,
–mark

We have a requirement of giving access to particular\e users through portal code. We’r looking for a method which can set users to particalar groups in order to give the access. Can anyone help us on this topic?

You could achieve this using following code:

import com.webMethods.portal.bizPolicy.impl.ContextFactory;
import com.webMethods.portal.bizPolicy.IContext;
import com.webMethods.portal.bizPolicy.biz.IBizPolicyManager;
import com.webMethods.portal.bizPolicy.biz.IBizPolicyNames;
import com.webMethods.portal.bizPolicy.biz.dir.IDirServiceBizPolicy;
import com.webMethods.portal.bizPolicy.biz.dir.IDirSystemBizPolicy;
import com.webMethods.portal.service.dir.IDirPrincipal;
import com.webMethods.portal.service.dir.IDirSystem;
import com.webMethods.portal.system.PortalSystem;

IContext context = ContextFactory.acquireContext(true);
IBizPolicyManager bpm = (IBizPolicyManager) PortalSystem.getPortalSystem().getBizPolicyProvider();
IDirServiceBizPolicy dirServicePolicy = (IDirServiceBizPolicy) bpm.getBizPolicy(IBizPolicyNames.DIRECTORY_SERVICE);
IDirSystemBizPolicy dirSystemPolicy = (IDirSystemBizPolicy) bpm.getBizPolicy(IBizPolicyNames.DIRECTORY_SYSTEM);

IDirPrincipal user = dirSystemPolicy.lookupPrincipalByID(context, “user UID”, IDirSystem.TYPE_USER);
IDirPrincipal group = dirSystemPolicy.lookupPrincipalByID(context, “group UID”, IDirSystem.TYPE_GROUP);
IDirPrincipal role = dirSystemPolicy.lookupPrincipalByID(context, “role name”, IDirSystem.TYPE_ROLE);

dirServicePolicy.addPrincipalToRole(context, user.getDirectoryURI(), role.getDirectoryURI());;
dirServicePolicy.addPrincipalToGroup(context, user.getDirectoryURI(), group.getDirectoryURI());;