Commit e2a85279 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

LoginHandler refactoring: identity passed directly to onIdentityDataReceived function

parent 15057653
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ namespace RAP;
class FacebookLogin extends LoginHandler {

    public function __construct(Locator $locator) {
        parent::__construct($locator, Identity::FACEBOOK);
        parent::__construct($locator);
    }

    public function login(): string {
@@ -65,13 +65,13 @@ class FacebookLogin extends LoginHandler {

        $fbUser = $response->getGraphUser();

        $typedId = $fbUser["id"];

        return $this->onIdentityDataReceived($typedId, function($identity) use($fbUser) {
        $identity = new Identity(Identity::FACEBOOK);
        $identity->typedId = $fbUser["id"];
        $identity->email = $fbUser["email"];
        $identity->name = $fbUser["first_name"];
        $identity->surname = $fbUser["last_name"];
                });

        return $this->onIdentityDataReceived($identity);
    }

}
+7 −6
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ namespace RAP;
class GoogleLogin extends LoginHandler {

    public function __construct(Locator $locator) {
        parent::__construct($locator, Identity::GOOGLE);
        parent::__construct($locator);
    }

    public function login() {
@@ -56,11 +56,12 @@ class GoogleLogin extends LoginHandler {

            $typedId = explode('/', $res->getResourceName())[1];

            return $this->onIdentityDataReceived($typedId, function($identity) use($emailAddresses, $name, $surname) {
            $identity = new Identity(Identity::GOOGLE);
            $identity->typedId = $typedId;
            $identity->email = $emailAddresses[0];
            $identity->name = $name;
            $identity->surname = $surname;
                    });
            return $this->onIdentityDataReceived($identity);
        } else {
            // Redirect to Google authorization URL for obtaining an access token
            $authUrl = $client->createAuthUrl();
+8 −6
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ namespace RAP;
class LinkedInLogin extends LoginHandler {

    public function __construct(Locator $locator) {
        parent::__construct($locator, Identity::LINKEDIN);
        parent::__construct($locator);
    }

    public function login(): string {
@@ -128,11 +128,13 @@ class LinkedInLogin extends LoginHandler {
                die($errorMessage);
            }

            return $this->onIdentityDataReceived($typedId, function($identity) use($data, $data2) {
            $identity = new Identity(Identity::LINKEDIN);
            $identity->typedId = $typedId;
            $identity->email = $data2['elements'][0]['handle~']['emailAddress'];
            $identity->name = $data['localizedFirstName'];
            $identity->surname = $data['localizedLastName'];
                    });

            return $this->onIdentityDataReceived($identity);
        } else {
            //show information regarding the error
            $errorMessage = "Error: LinkedIn server response code: " . $info2['http_code'] . " - ";
+34 −24
Original line number Diff line number Diff line
@@ -5,22 +5,26 @@ namespace RAP;
class LoginHandler {

    protected $locator;
    private $identityType;

    public function __construct(Locator $locator, string $identityType) {
    public function __construct(Locator $locator) {
        $this->locator = $locator;
        $this->identityType = $identityType;
        $this->locator->getSession()->setLoginIdentityType($identityType);
    }

    public function onIdentityDataReceived(string $typedId, \Closure $fillIdentityData): string {
    /**
     * Returns redirect URL.
     */
    public function onIdentityDataReceived(Identity $identity): string {
        
        $this->locator->getSession()->setLoginIdentityType($identity->type);

        $userDao = $this->locator->getUserDAO();
        
        $user = $this->locator->getUserDAO()->findUserByIdentity($this->identityType, $typedId);
        $user = $userDao->findUserByIdentity($identity->type, $identity->typedId);
        
        if ($user === null) {
            return $this->handleNewIdentity($typedId, $fillIdentityData);
            return $this->handleNewIdentity($identity);
        } else {
            $this->updateUser($user, $typedId, $fillIdentityData);
            $this->updateUser($user, $identity);
        }

        $session = $this->locator->getSession();
@@ -32,15 +36,15 @@ class LoginHandler {
        return $this->getAfterLoginRedirect($user);
    }

    protected function handleNewIdentity(string $typedId, \Closure $fillIdentityData): string {
    protected function handleNewIdentity(Identity $identity): string {

        $session = $this->locator->getSession();

        if ($session->getUser() !== null && $session->getAction() === 'join') {
            $userToJoin = $this->getNewUser($typedId, $fillIdentityData);
            $userToJoin = $this->getNewUser($identity);
            return $this->showConfirmJoin($userToJoin);
        } else {
            return $this->redirectToTOUCheck($typedId, $fillIdentityData);
            return $this->redirectToTOUCheck($identity);
        }
    }

@@ -52,21 +56,18 @@ class LoginHandler {
    /**
     * Stores the data into session and Redirect to Term of Use acceptance page.
     */
    private function redirectToTOUCheck(string $typedId, \Closure $fillIdentityData): string {
    private function redirectToTOUCheck(Identity $identity): string {

        // Create new user
        $user = $this->getNewUser($typedId, $fillIdentityData);
        $user = $this->getNewUser($identity);

        $this->locator->getSession()->setUser($user);

        return $this->locator->getBasePath() . '/tou-check';
    }

    private function getNewUser(string $typedId, \Closure $fillIdentityData): User {
    private function getNewUser(Identity $identity): User {
        $user = new User();
        $identity = new Identity($this->identityType);
        $identity->typedId = $typedId;
        $fillIdentityData($identity);
        $user->addIdentity($identity);
        return $user;
    }
@@ -74,16 +75,25 @@ class LoginHandler {
    /**
     * Update user with fresh information received by IdP. Useful for keeping email address always updated.
     */
    private function updateUser(User $user, string $typedId, \Closure $fillIdentityData): void {
        $identity = $user->getIdentityByTypedId($typedId);
        $fillIdentityData($identity);
        $this->locator->getUserDAO()->updateIdentity($identity);
    private function updateUser(User $user, Identity $identity): void {
        $savedIdentity = $user->getIdentityByTypedId($identity->typedId);
        $savedIdentity->email = $identity->email;
        if($identity->name !== null) {
            $savedIdentity->name = $identity->name;
        }
        if($identity->surname !== null) {
            $savedIdentity->surname = $identity->surname;
        }
        if($identity->institution !== null) {
            $savedIdentity->institution = $identity->institution;
        }
        $this->locator->getUserDAO()->updateIdentity($savedIdentity);
    }

    public function getAfterLoginRedirect(User $user): string {

        $session = $this->locator->getSession();
        $this->locator->getAuditLogger()->info("LOGIN," . $this->identityType . "," . $user->id);
        $this->locator->getAuditLogger()->info("LOGIN," . $session->getLoginIdentityType() . "," . $user->id);

        if ($session->getOAuth2RequestData() !== null) {
            $session->setUser($user);
+12 −10
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ namespace RAP;
class OrcidLogin extends LoginHandler {

    public function __construct(Locator $locator) {
        parent::__construct($locator, Identity::ORCID);
        parent::__construct($locator);
    }

    public function login(): string {
@@ -39,7 +39,8 @@ class OrcidLogin extends LoginHandler {
            throw new \Exception("ORCID didn't return the email");
        }

        return $this->onIdentityDataReceived($orcid_id, function($identity) use($orcid_data) {
        $identity = new Identity(Identity::ORCID);
        $identity->typedId = $orcid_id;
        $identity->email = $email = $orcid_data['person']['emails']['email'][0]['email'];
        $identity->name = $orcid_data['person']['name']['given-names']['value'];
        $identity->surname = $orcid_data['person']['name']['family-name']['value'];
@@ -47,7 +48,8 @@ class OrcidLogin extends LoginHandler {
        if (count($employmentSummary) > 0) {
            $identity->institution = $employmentSummary[0]['organization']['name'];
        }
                });

        return $this->onIdentityDataReceived($identity);
    }

    private function getAccessTokenFromCode($code): array {
Loading