AppserverIo\Appserver\ServletEngine\Security\Realm::createPrincipal PHP Method

createPrincipal() protected method

Identify and return an instance implementing the PrincipalInterface that represens the authenticated user for the specified Subject. The Principal is constructed by scanning the list of Principals returned by the LoginModule. The first Principal object that matches one of the class names supplied as a "user class" is the user Principal. This object is returned to the caller. Any remaining principal objects returned by the LoginModules are mapped to roles, but only if their respective classes match one of the "role class" classes. If a user Principal cannot be constructed, return NULL.
protected createPrincipal ( string $username, AppserverIo\Psr\Security\Auth\Subject $subject, AppserverIo\Psr\Security\Auth\Login\LoginContextInterface $loginContext ) : AppserverIo\Security\PrincipalInterface
$username string The associated user name
$subject AppserverIo\Psr\Security\Auth\Subject The Subject representing the logged-in user
$loginContext AppserverIo\Psr\Security\Auth\Login\LoginContextInterface Associated with the Principal so {@link LoginContext#logout()} can be called later
return AppserverIo\Security\PrincipalInterface the principal object
    protected function createPrincipal(string $username, Subject $subject, LoginContextInterface $loginContext)
    {
        // initialize the roles and the user principal
        $roles = new ArrayList();
        $userPrincipal = null;
        // scan the Principals for this Subject
        foreach ($subject->getPrincipals() as $principal) {
            // query whether or not the principal found is a group principal
            if ($principal instanceof GroupInterface && $principal->getName()->equals(new String(Util::DEFAULT_GROUP_NAME))) {
                // if yes, add the role name
                foreach ($principal->getMembers() as $role) {
                    $roles->add($role->getName());
                }
                // query whether or not the principal found is a user principal
            } elseif ($userPrincipal == null && $principal instanceof PrincipalInterface) {
                $userPrincipal = $principal;
            } else {
                // do nothing, because we've no principal or group to deal with
            }
        }
        // return the resulting Principal for our authenticated user
        return new GenericPrincipal($username, null, $roles, $userPrincipal, $loginContext);
    }