// Get the Principal information (logged in user)
String fullPath = ((BasePrincipal)SecurityHelper.getBestPrincipal(subject, UserPrincipal.class)).getFullPath();
String principalName = ((BasePrincipal)SecurityHelper.getBestPrincipal(subject, UserPrincipal.class)).getName();
// Add an entry for the principal to the site if it doesn't exist
SSOPrincipal principal = this.getPrincipalForSite(ssoSite, fullPath);
if (principal == null )
{
principal = getSSOPrincipa(fullPath);
ssoSite.addPrincipal(principal);
}
else
{
// Check if the entry the user likes to update exists already
Collection remoteForSite = ssoSite.getRemotePrincipals();
if ( remoteForSite != null)
{
if (FindRemoteMatch(principal.getRemotePrincipals(), remoteForSite) != null )
{
// Entry exists can't to an add has to call update
throw new SSOException(SSOException.REMOTE_PRINCIPAL_EXISTS_CALL_UPDATE);
}
}
}
if (principal == null)
throw new SSOException(SSOException.FAILED_ADDING_PRINCIPAL_TO_MAPPING_TABLE_FOR_SITE);
// Create a remote principal and credentials
InternalUserPrincipalImpl remotePrincipal = new InternalUserPrincipalImpl(remoteUser);
remotePrincipal.setFullPath("/sso/user/"+ principalName + "/" + remoteUser);
// New credential object for remote principal
InternalCredentialImpl credential =
new InternalCredentialImpl(remotePrincipal.getPrincipalId(),
pwd, 0, DefaultPasswordCredentialImpl.class.getName());
if ( remotePrincipal.getCredentials() == null)
remotePrincipal.setCredentials(new ArrayList(0));
remotePrincipal.getCredentials().add( credential);
// Add it to Principals remotePrincipals list
principal.addRemotePrincipal(remotePrincipal);
// Update the site remotePrincipals list
ssoSite.getRemotePrincipals().add(remotePrincipal);
// Update database and reset cache