Search code examples
laravelcartalyst-sentry

Overriding the login() method of Sentry 2 package in Laravel 4


My basic aim is to extend the package class and override a method in it.

I have used Fnatte's ans as a reference : How to extend laravel 4 core?

Sentry2 is a package that i am using for authentication in larval 4.

A user can be logged in using Sentry::login($credentials)

I want to override the login method of the Sentry package and remove the check for activating the user(i have commented it in the code below)

public function login(UserInterface $user, $remember = false)
    {
        #prevent throwing error if not activated !
        // if ( ! $user->isActivated())
        // {
        //  $login = $user->getLogin();
        //  throw new UserNotActivatedException("Cannot login user [$login] as they are not activated.");
        // }

        $this->user = $user;

        // Create an array of data to persist to the session and / or cookie
        $toPersist = array($user->getId(), $user->getPersistCode());

        // Set sessions
        $this->session->put($toPersist);

        if ($remember)
        {
            $this->cookie->forever($toPersist);
        }

        // The user model can attach any handlers
        // to the "recordLogin" event.
        $user->recordLogin();
    }

Steps i have done till now :

1. Created a app/lib folder and added my extension class CustomSentry in it.

2. Added the app/lib folder to composer.json class map

app/lib/CustomSentry.php :

    use Cartalyst\Sentry\Sentry;
      use Cartalyst\Sentry\Users\UserInterface;

        class CustomSentry extends Sentry{

        public function login(UserInterface $user, $remember = false){

             $this->user = $user;

            // Create an array of data to persist to the session and / or cookie
            $toPersist = array($user->getId(), $user->getPersistCode());

            // Set sessions
            $this->session->put($toPersist);

            if ($remember)
            {
                $this->cookie->forever($toPersist);
            }

            // The user model can attach any handlers
            // to the "recordLogin" event.
            $user->recordLogin();   



  }

}

4. Created a service provider app/lib/CustomSentryServiceProvider

 use Cartalyst\Sentry\SentryServiceProvider

    class CustomSentryServiceProvider extends SentryServiceProvider{

       //What should i put it here?
    }

5. Register the service provider in app/config/app.php

'CustomSentryServiceProvider'

6. Use it as :

Sentry::login($credentials);

Solution

  • Well i figured out the answer to the question.

    The better way to extend the class would be to use the same name. Since i am already using namespacing it would help reduce the confusion.

    Assuming my apps name is MyApp i will only need to replace:

    Cartalyst\Sentry 
    

    by:

    MyApp\Cartalyst\Sentry
    

    the rest of the SentryService provider can be copied as it is.

    Then i can call the login method the normal way

    Sentry::login($credentials)