Skip to content

Instantly share code, notes, and snippets.

@marbuser
Created March 27, 2020 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marbuser/c3e4d16609a579bb0808f333b691cd5e to your computer and use it in GitHub Desktop.
Save marbuser/c3e4d16609a579bb0808f333b691cd5e to your computer and use it in GitHub Desktop.
<?php
namespace App\Providers\Xenforo\Contracts;
interface UserInterface
{
public function getUserById($id);
public function getUserByNameOrEmail($indentifier);
}
<?php namespace App\Providers\Xenforo\Contracts;
interface VisitorInterface
{
public function getCurrentVisitor();
public function setCurrentVisitor($user);
public function isBanned();
public function isAdmin();
public function isSuperAdmin();
public function isLoggedIn();
public function hasPermission($group, $permission);
public function getUserId();
}
<?php
namespace App\Providers\Xenforo\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @method static mixed getApp()
* @method static \App\Providers\Xenforo\Contracts\VisitorInterface retrieveVisitor()
* @method static void setVisitor(\App\Providers\Xenforo\Contracts\VisitorInterface $visitor)
* @method static mixed getVisitor()
* @method static bool isBanned()
* @method static bool isAdmin()
* @method static bool isSuperAdmin()
* @method static bool isLoggedIn()
* @method static int getVisitorUserId()
* @method static mixed hasPermission($group, $permission)
* @method static \App\Providers\Xenforo\Contracts\UserInterface retrieveUser()
* @method static void setUser(\App\Providers\Xenforo\Contracts\UserInterface $user)
* @method static mixed getUser()
* @method static mixed getUserById(int $id)
*
* @see \App\Providers\Xenforo\Xenforo
*/
class Xenforo extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return \App\Providers\Xenforo\Xenforo::class;
}
}
<?php
namespace App\Providers\Xenforo\User;
use App\Providers\Xenforo\Contracts\UserInterface;
use App\Providers\Xenforo\Facades\Xenforo;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Auth\Authenticatable;
class User implements UserInterface, Authenticatable
{
protected $user;
/**
* User constructor.
*
*/
public function __construct()
{
$this->setUser(Xenforo::getApp()->repository('XF:User'));
}
/**
* Set User Model
*
* @param $user
*/
public function setUser($user)
{
$this->user = $user;
}
/**
* Get User Model
*
* @return mixed
*/
public function getUser()
{
return $this->user;
}
/**
* Get User by Id - returns null if user does not exist
*
* @param $id
* @return mixed
*/
public function getUserById($id)
{
return $this->user->getVisitor($id) ?: null;
}
/**
* Get User by Email - returns null if user does not exist
*
* @param $identifier
* @return mixed
*/
public function getUserByNameOrEmail($identifier)
{
return $this->user->getUserByNameOrEmail($identifier) ?: null;
}
public function getAuthIdentifierName()
{
return 'id';
}
public function getAuthIdentifier()
{
return $this->getUserId();
}
public function getAuthPassword()
{
throw new AuthenticationException('get Auth Password not implemented by '.get_class($this));
}
public function getRememberToken()
{
throw new AuthenticationException('Remember Tokens not implemented by '.get_class($this));
}
public function setRememberToken($value)
{
throw new AuthenticationException('Remember Tokens not implemented by '.get_class($this));
}
public function getRememberTokenName()
{
throw new AuthenticationException('Remember Tokens not implemented by '.get_class($this));
}
}
<?php
namespace App\Providers\Xenforo\Visitor;
use App\Providers\Xenforo\Contracts\VisitorInterface;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Auth\Authenticatable;
use XF;
class Visitor implements VisitorInterface, Authenticatable
{
protected $user;
public function getCurrentVisitor()
{
return $this->user ?? XF::visitor();
}
public function setCurrentVisitor($user)
{
$this->user = $user;
}
public function isBanned()
{
return (bool) $this->getCurrentVisitor()->toArray()['is_banned'];
}
public function isAdmin()
{
return (bool) $this->getCurrentVisitor()->toArray()['is_admin'];
}
public function isSuperAdmin()
{
return (bool) $this->getCurrentVisitor()->isSuperAdmin();
}
public function isLoggedIn()
{
return (bool) $this->getUserId();
}
public function hasPermission($group, $permission)
{
return $this->getCurrentVisitor()->hasPermission($group, $permission);
}
public function getUserId()
{
return (int) $this->getCurrentVisitor()->toArray()['user_id'];
}
public function getAuthIdentifierName()
{
return 'id';
}
public function getAuthIdentifier()
{
return $this->getUserId();
}
public function getAuthPassword()
{
throw new AuthenticationException('get Auth Password not implemented by '.get_class($this));
}
public function getRememberToken()
{
throw new AuthenticationException('Remember Tokens not implemented by '.get_class($this));
}
public function setRememberToken($value)
{
throw new AuthenticationException('Remember Tokens not implemented by '.get_class($this));
}
public function getRememberTokenName()
{
throw new AuthenticationException('Remember Tokens not implemented by '.get_class($this));
}
public function getName()
{
$user = $this->getCurrentVisitor()->toArray();
if ( isset($user['username']) ) {
return $user['username'];
}
return null;
}
public function __get($key)
{
if ( !$key ) {
return;
}
if ( method_exists(self::class,
'get'.\Illuminate\Support\Str::studly($key)) ) {
return $this->{'get'.\Illuminate\Support\Str::studly($key)}();
}
$user = $this->getCurrentVisitor()->toArray();
if ( isset($user[$key]) ) {
return $user[$key];
}
}
}
<?php
namespace App\Providers\Xenforo;
/**
* Required XenForo Classes
*/
use App\Providers\Xenforo\Contracts\UserInterface;
use App\Providers\Xenforo\Contracts\VisitorInterface;
use App\Providers\Xenforo\User\User;
use App\Providers\Xenforo\Visitor\Visitor;
use XF;
class Xenforo
{
/**
* The Xenforo App instance.
*/
protected $app;
/**
* Absolute Path to Xenforo Directory
* (ex. /home/username/www/forums/ )
*
* @var string
*/
protected $xenforoDirectoryPath;
/**
* @var VisitorInterface
*/
protected $visitor;
/**
* @var UserInterface
*/
protected $user;
/**
* Bootstrap Xenforo
*
* @param $xenforoDirectoryPath
* @param null|string $xenforoBaseUrl
*/
public function __construct($xenforoDirectoryPath, $xenforoBaseUrl = null)
{
$this->xenforoDirectoryPath = $xenforoDirectoryPath;
//Bootstrap Xenforo App
$this->bootstrapXenforo($this->xenforoDirectoryPath);
}
/**
* Bootstrap Xenforo Application and Start a Public Session
*
* @param string $directoryPath
*/
protected function bootstrapXenforo($directoryPath)
{
include_once($directoryPath.'/src/XF.php');
XF::start(null);
$this->app = XF::setupApp('XF\Pub\App');
$this->app->start($directoryPath);
}
/**
* Returns the Xenforo app instance.
*
* @return mixed
*/
public function getApp()
{
return $this->app;
}
/**
* Retrieve Visitor Class
*
* @return VisitorInterface
*/
public function retrieveVisitor()
{
if ( !$this->visitor instanceof VisitorInterface ) {
$this->setVisitor(new Visitor);
}
return $this->visitor;
}
/**
* Set a new Visitor implementation
*
* @param VisitorInterface $visitor
*/
public function setVisitor(VisitorInterface $visitor)
{
$this->visitor = $visitor;
}
/**
* Gets singleton instance of Xenforo_Visitor
*
* @return mixed
*/
public function getVisitor()
{
return $this->retrieveVisitor()->getCurrentVisitor();
}
/**
* Checks if current visitor is banned
*
* @return boolean
*/
public function isBanned()
{
return (bool) $this->retrieveVisitor()->isBanned();
}
/**
* Checks if current visitor is an Admin
*
* @return boolean
*/
public function isAdmin()
{
return $this->retrieveVisitor()->isAdmin();
}
/**
* Checks if visitor is a Super Admin
*
* @return boolean
*/
public function isSuperAdmin()
{
return $this->retrieveVisitor()->isSuperAdmin();
}
/**
* Checks if visitor is currently logged in
*
* @return boolean
*/
public function isLoggedIn()
{
return $this->retrieveVisitor()->isLoggedIn();
}
/**
* Retrieve the current Visitors User id
*
* @return mixed
*/
public function getVisitorUserId()
{
return $this->retrieveVisitor()->getUserId();
}
/**
* Checks if visitor has a particular permission
*
* @param $group - permission group
* @param $permission - permission
* @return mixed
*/
public function hasPermission($group, $permission)
{
return $this->retrieveVisitor()->hasPermission($group, $permission);
}
/**
* Return current implementation or set Default User
*
* @return UserInterface
*/
public function retrieveUser()
{
if ( !$this->user instanceof UserInterface ) {
$this->setUser(new User);
}
return $this->user;
}
/**
* Set current implementation of User
*
* @param UserInterface $user
*/
public function setUser(UserInterface $user)
{
$this->user = $user;
}
/**
* Return current User implementation
*
* @return UserInterface
*/
public function getUser()
{
return $this->retrieveUser();
}
/**
* Find User by Id
*
* @param $id
* @return array
*/
public function getUserById($id)
{
return $this->getUser()->getUserById($id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment