Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- custom implementation of users where users login on different site,
- however, I do have access to the DB.
- Stores user token in session instead of DB.
- Not sure if this will work.
- */
- //===========================================
- // In CustomUserProvider.php
- //===========================================
- use Illuminate\Contracts\Auth\UserProvider as IlluminateUserProvider;
- use Illuminate\Contracts\Auth\Authenticatable as Authenticatable;
- use Illuminate\Support\Facades\DB;
- class CustomUserProvider implements IlluminateUserProvider
- {
- /**
- * @param mixed $identifier
- * @return \Illuminate\Contracts\Auth\Authenticatable|null
- */
- public function retrieveById($identifier)
- {
- // Get and return a user by their unique identifier
- return new CustomUser($identifier);
- }
- /**
- * @param mixed $identifier
- * @param string $token
- * @return \Illuminate\Contracts\Auth\Authenticatable|null
- */
- public function retrieveByToken($identifier, $token)
- {
- // Get and return a user by their unique identifier and "remember me" token
- if (session('custom_user_token_'.$user->acct_id) == $token)
- {
- return new CustomUser($identifier);
- }
- else
- {
- return null;
- }
- }
- /**
- * @param \Illuminate\Contracts\Auth\Authenticatable $user
- * @param string $token
- * @return void
- */
- function updateRememberToken(Authenticatable $user, $token)
- {
- // Save the given "remember me" token for the given user
- session(['custom_user_token_'.$user->acct_id => $token]);
- }
- /**
- * Retrieve a user by the given credentials.
- *
- * @param array $credentials
- * @return \Illuminate\Contracts\Auth\Authenticatable|null
- */
- public function retrieveByCredentials(array $credentials)
- {
- // Get and return a user by looking up the given credentials
- // Return null, since user should be logging in using WAYF
- return null;
- }
- /**
- * Validate a user against the given credentials.
- *
- * @param \Illuminate\Contracts\Auth\Authenticatable $user
- * @param array $credentials
- * @return bool
- */
- public function validateCredentials(Authenticatable $user, array $credentials)
- {
- // Check that given credentials belong to the given user
- // Return false, since user should be logging in using WAYF
- return false;
- }
- public function isDeferred()
- {
- return false;
- }
- }
- //===========================================
- // in CustomUser.php
- //===========================================
- use Illuminate\Contracts\Auth\Authenticatable;
- use Illuminate\Support\Facades\DB;
- class CustomUser
- implements Authenticatable
- {
- /*
- * Construct with these fields:
- * acct_id
- */
- function __construct($acct_id)
- {
- // user should be created by ciac object, don't have to create new users here
- $results = DB::select("SELECT acct_id, username, email FROM my_user_table WHERE acct_id = :acct_id",[':acct_id' =>$acct_id]);
- $userRow = (array) reset($results);
- if ($userRow == null)
- {
- throw new Exception("Can't find user with id {$acct_id}");
- }
- foreach ($userRow as $property => $value)
- {
- $this->$property = $value;
- }
- }
- /**
- * @return string
- */
- public function getAuthIdentifierName()
- {
- // Return the name of unique identifier for the user (e.g. "id")
- return 'acct_id';
- }
- /**
- * @return mixed
- */
- public function getAuthIdentifier()
- {
- // Return the unique identifier for the user (e.g. their ID, 123)
- return $this->acct_id;
- }
- /**
- * @return string
- */
- public function getAuthPassword()
- {
- // Returns the (hashed) password for the user
- // Laravel doesn't need to know the password (?)
- return null;
- }
- /**
- * @return string
- */
- public function getRememberToken()
- {
- // Return the token used for the "remember me" functionality
- // no need to store in DB, is only relevant for this session
- return session('custom_user_token_obj_'.$this->acct_id);
- }
- /**
- * @param string $value
- * @return void
- */
- public function setRememberToken($value)
- {
- // Store a new token user for the "remember me" functionality
- // no need to store in DB, is only relevant for this session
- session(['custom_user_token_obj_'.$this->acct_id => $value]);
- }
- /**
- * @return string
- */
- public function getRememberTokenName()
- {
- // Return the name of the column / attribute used to store the "remember me" token
- return 'token';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement