Returns the actual application instance thatrepresents the application
context of this request.
public static getApplicationContext ( ) : AppserverIo\Psr\Application\ApplicationInterface | ||
Результат | AppserverIo\Psr\Application\ApplicationInterface | The actual application context |
/** * JSON encodes the passed content and returns it. * * @param mixed $content The content to be JSON encoded * * @return \AppserverIo\Apps\Api\TransferObject\EncodedViewData The DTO with the encoded content */ public function encode($content) { // load the application instance $application = RequestHandler::getApplicationContext(); // this is necessary to load the PSR-4 compatible Swagger library \Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(function ($className) use($application) { // load the application directory $webappPath = $application->getWebappPath(); $shortName = str_replace("Swagger\\Annotations\\", DIRECTORY_SEPARATOR, $className); // prepare the annotation filename for the Swagger annotations $file = sprintf('%s/vendor/zircote/swagger-php/src/Annotations/%s.php', $webappPath, $shortName); // query whether the file exists or not if (file_exists($file)) { require $file; return class_exists($className); } // return FALSE if the class can't be loaded return false; }); // serialize the passed content return new EncodedViewData($this->getContentType(), SerializerBuilder::create()->build()->serialize($content, 'json')); }
/** * Execute the rolesQuery against the dsJndiName to obtain the roles for the authenticated user. * * @param \AppserverIo\Lang\String $username The username to load the roles for * @param \AppserverIo\Lang\String $lookupName The lookup name for the datasource * @param \AppserverIo\Lang\String $rolesQuery The query to load the roles * @param \AppserverIo\Psr\Spi\LoginModuleInterface $aslm The login module to add the roles to * * @return array An array of groups containing the sets of roles * @throws \AppserverIo\Appserver\ServletEngine\Security\Logi\LoginException Is thrown if an error during login occured */ public static function getRoleSets(string $username, string $lookupName, string $rolesQuery, LoginModuleInterface $aslm) { try { // initialize the map for the groups $setsMap = new HashMap(); // load the application context $application = RequestHandler::getApplicationContext(); /** @var \AppserverIo\Appserver\Core\Api\Node\DatabaseNode $databaseNode */ $databaseNode = $application->getNamingDirectory()->search($lookupName)->getDatabase(); // prepare the connection parameters and create the DBAL connection $connection = DriverManager::getConnection(ConnectionUtil::get($application)->fromDatabaseNode($databaseNode)); // try to load the principal's roles from the database $statement = $connection->prepare($rolesQuery); $statement->bindParam(1, $username); $statement->execute(); // query whether or not we've a password found or not $row = $statement->fetch(\PDO::FETCH_NUM); // query whether or not we've found at least one role if ($row == false) { // try load the unauthenticated identity if ($aslm->getUnauthenticatedIdentity() == null) { throw new FailedLoginException('No matching username found in Roles'); } // we're running with an unauthenticatedIdentity so create an empty roles set and return return array(new SimpleGroup(Util::DEFAULT_GROUP_NAME)); } do { // load the found name and initialize the group name with a default value $name = $row[0]; $groupName = Util::DEFAULT_GROUP_NAME; // query whether or not we've to initialize a default group if (isset($row[1])) { $groupName = $row[1]; } // query whether or not the group already exists in the set if ($setsMap->exists($groupName) === false) { $group = new SimpleGroup(new String($groupName)); $setsMap->add($groupName, $group); } else { $group = $setsMap->get($groupName); } try { // add the user to the group $group->addMember($aslm->createIdentity(new String($name))); // log a message $application->getNamingDirectory()->search(NamingDirectoryKeys::SYSTEM_LOGGER)->debug(sprintf('Assign user to role: %s', $name)); } catch (\Exception $e) { $application->getNamingDirectory()->search(NamingDirectoryKeys::SYSTEM_LOGGER)->error(sprintf('Failed to create principal: %s', $name)); } // load one group after another } while ($row = $statement->fetch(\PDO::FETCH_OBJ)); } catch (NamingException $ne) { throw new LoginException($ne->__toString()); } catch (\PDOException $pdoe) { throw new LoginException($pdoe->__toString()); } // close the prepared statement if ($statement != null) { try { $statement->closeCursor(); } catch (\Exception $e) { $application->getNamingDirectory()->search(NamingDirectoryKeys::SYSTEM_LOGGER)->error($e->__toString()); } } // close the DBAL connection if ($connection != null) { try { $connection->close(); } catch (\Exception $e) { $application->getNamingDirectory()->search(NamingDirectoryKeys::SYSTEM_LOGGER)->error($e->__toString()); } } // return the prepared groups return $setsMap->toArray(); }
/** * Returns the password for the user from the naming directory. * * @return \AppserverIo\Lang\String The user's password * @throws \AppserverIo\Appserver\Psr\Security\Auth\Login\LoginException Is thrown if password can't be loaded */ protected function getUsersPassword() { try { // load the application context $application = RequestHandler::getApplicationContext(); // load and return the user's password or throw an exception return new String($application->search(sprintf('%s/%s', $this->userPathPrefix, $this->getUsername()))); } catch (\Exception $e) { throw new LoginException('No matching username found in naming directory'); } }
/** * Returns the password for the user from the sharedMap data. * * @return \AppserverIo\Lang\String The user's password * @throws \AppserverIo\Psr\Security\Auth\Login\LoginException Is thrown if password can't be loaded */ protected function getUsersPassword() { // load the application context $application = RequestHandler::getApplicationContext(); /** @var \AppserverIo\Appserver\Core\Api\Node\DatabaseNode $databaseNode */ $databaseNode = $application->getNamingDirectory()->search($this->lookupName)->getDatabase(); // prepare the connection parameters and create the DBAL connection $connection = DriverManager::getConnection(ConnectionUtil::get($application)->fromDatabaseNode($databaseNode)); // try to load the principal's credential from the database $statement = $connection->prepare($this->principalsQuery); $statement->bindParam(1, $this->getUsername()); $statement->execute(); // close the PDO connection if ($connection != null) { try { $connection->close(); } catch (\Exception $e) { $application->getNamingDirectory()->search(NamingDirectoryKeys::SYSTEM_LOGGER)->error($e->__toString()); } } // query whether or not we've a password found or not if ($row = $statement->fetch(\PDO::FETCH_NUM)) { return new String($row[0]); } else { throw new LoginException('No matching username found in principals'); } }