Tuesday, October 22, 2013

WebLogic: check if a Group exists

When you create users and need to assign them to Groups, chances are that you will have also to dynamically create those groups. Luckily there is a function atnt.groupExists('somegroup').

This will work only if the JMSGroup doesn't exist:
conect(...)
atnr = cmo.getSecurityConfiguration().getDefaultRealm().lookupAuthenticationProvider('DefaultAuthenticator')
atnr.createGroup('JMSGroup', 'JMSGroup')


the second time you will get a "weblogic.management.utils.AlreadyExistsException: [Security:090267]Group JMSGroup" exception. You can decide to simply catch and ignore the exception.
If you do viewMBean(atnr) you will notice that there is a host of operations available:

setGroupDescription
changeUserPassword
setUserDescription
listMemberGroups
removeMemberFromGroup
groupExists
getGroupDescription
advance
getUserDescription
haveCurrent
listGroupMembers
unSet
getSupportedUserAttributeType
getUserAttributeValue
wls_getDisplayName
userExists
close
isSet
createGroup
listGroups
resetUserPassword
createUser
removeUser
addMemberToGroup
listAllUsersInGroup
setUserAttributeValue
importData
isMember
removeGroup
listUsers
exportData
isUserAttributeNameSupported
getCurrentName

so the code becomes:
#ROLES contains a CSV list of groups for the user USERNAME 
for role in ROLES.split(','):
    if not atnr.groupExists(role):
        atnr.createGroup(role, role)
        print "WARNING: I have  created group ", role
    print "adding ", USERNAME, "to group", role
    atnr.addMemberToGroup(role, USERNAME)


No comments: