Hello Guys,
Here is the code or just a function to find out the groups of which user is a member of.
Please find the below code, you can use it from any class just by passing ApplicationContext and the Group Name as a parameters.
public static boolean isCurrentUserInGroup(ApplicationContext context, String groupName)
{
// The authority service returns authorities with their AuthorityDAO type
// prefix prepended to the name, so we'll do that as well
groupName = AuthorityType.GROUP.getPrefixString() + groupName;
final ServiceRegistry serviceRegistry = (ServiceRegistry)
context.getBean(ServiceRegistry.SERVICE_REGISTRY);
// get the current user
String currentUserName =
serviceRegistry.getAuthenticationService().getCurrentUserName();
// get the list of groups this user is a member of
Set authorityList = serviceRegistry.getAuthorityService().getContainingAuthorities
(AuthorityType.GROUP, currentUserName, false);
System.out.println("authorityList--"+authorityList);
// look in the list to see if groupName is in there. If so, return true;
return authorityList.contains(groupName);
}
Here is the code or just a function to find out the groups of which user is a member of.
Please find the below code, you can use it from any class just by passing ApplicationContext and the Group Name as a parameters.
public static boolean isCurrentUserInGroup(ApplicationContext context, String groupName)
{
// The authority service returns authorities with their AuthorityDAO type
// prefix prepended to the name, so we'll do that as well
groupName = AuthorityType.GROUP.getPrefixString() + groupName;
final ServiceRegistry serviceRegistry = (ServiceRegistry)
context.getBean(ServiceRegistry.SERVICE_REGISTRY);
// get the current user
String currentUserName =
serviceRegistry.getAuthenticationService().getCurrentUserName();
// get the list of groups this user is a member of
Set
(AuthorityType.GROUP, currentUserName, false);
System.out.println("authorityList--"+authorityList);
// look in the list to see if groupName is in there. If so, return true;
return authorityList.contains(groupName);
}
No comments:
Post a Comment