Skip to content

Commit

Permalink
Merge pull request #6 from UnderRatedBrilliance/development
Browse files Browse the repository at this point in the history
Merge Xenforo Rewrite
  • Loading branch information
gabarba committed Aug 19, 2015
2 parents f4feb30 + 87aef05 commit 2044a60
Show file tree
Hide file tree
Showing 10 changed files with 603 additions and 79 deletions.
65 changes: 59 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
XenforoBridge
=============

Simple to use XenForo bridge libary. The goal of this package is to allow developer to easily integrate their existing/new application with XenForo Forum Platfrom. This package is still heavily underdevelopment so use with caution. I have also included a ServiceProvider to use within a Laravel application.
Simple to use XenForo bridge library. The goal of this package is to allow developer to easily integrate their existing/new application with XenForo Forum Platfrom. This package is still heavily underdevelopment so use with caution. I have also included a ServiceProvider to use within a Laravel application.

Installation
------------

Install the XenforoBridge package with Composer.
Install the XenforoBridge package with Composer by adding the folowing to your composer.json file.

```json
{
"require": {
"urb/xenforobridge": "dev-master"
"urb/xenforobridge": "dev-development"
}
}
```
Or by using the composer require command

To install XenforoBridge into Laravel 4 simple add the following service provider to your 'app/config/app.php' in the 'providers' array:
```
composer require urb/xenforobridge:dev-development
```

To install XenforoBridge into Laravel 5 simple add the following service provider to your 'config/app.php' in the 'providers' array:

```php
'providers' => array(
'XenforoBridge\XenforoBridgeServiceProvider',
'XenforoBridge\XenforoBridgeServiceProvider::class',
)

```
Then publish the config file with 'php artisan config:publish urb/xenforobridge'. This will add the file 'app/config/packages/urb/xenforobridge/config.php'. This is where you will place the needed configurations to use the Xenforo Bridge.

Then publish the config file with

```
php artisan vendor:publish
```

This will add the file 'config/xenforobridge.php'. This is where you will place the needed configurations to use the Xenforo Bridge.

Within this config file you will need to supply the full directory path to your XenForo installation and the base url path like the example below

Expand All @@ -35,6 +47,47 @@ return array(
);
```

Installing Middleware
---------------------
To install Middleware you wil need to open up the app\Http\Kernel.php and the following middleware to either global middleware array
or the routeMiddleware array.

Here is an example adding to the routeMiddleware array

```php
protected $routeMiddleware = [
'xen.auth' => 'XenforoBridge\Middleware\XenAuthMiddleware',
'xen.auth.admin' => 'XenforoBridge\Middleware\XenAuthAdminMiddleware',
];

```

You can then use them in your routes like so

```php
Route::get('/example', ['middleware' => 'xen.auth',function(){
//Do stuff
}]);
```

or you can use them in your controllers themselves

```php
class SampleController extends Controller {


function __construct()
{

$this->middleware('xen.auth');
}

}

```

For more information on Middleware development an installation check out [Laravel Docs - Middleware](http://laravel.com/docs/5.1/middleware)

Credits
-------

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "urb/xenforobridge",
"description": "Xenforo Bridge - Easy to use extendable bridge to use outside of your XenForo application all containted within a simple to use composer package",
"keywords": ["laravel", "laravel4", "xenforo", "xenforo forum", "forum"],
"description": "Xenforo Bridge - Easy to use extendable bridge to use outside of your XenForo application all contained within a simple to use composer package",
"keywords": ["laravel", "laravel5", "xenforo", "xenforo forum", "forum"],
"type": "library",
"authors": [
{
Expand All @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">=5.3.0"
"php": ">=5.5.0"
},
"autoload": {
"psr-0": {
Expand Down
3 changes: 2 additions & 1 deletion src/XenforoBridge/Contracts/TemplateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

interface TemplateInterface
{


public function renderTemplate($name, $content, $params);
}
11 changes: 11 additions & 0 deletions src/XenforoBridge/Contracts/UserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php namespace XenforoBridge\Contracts;

interface UserInterface
{

public function getUserById($id);

public function getUserByUsername($name);

public function getUserByEmail($email);
}
13 changes: 12 additions & 1 deletion src/XenforoBridge/Contracts/VisitorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,16 @@

interface VisitorInterface
{


public function getCurrentVisitor();

public function isBanned();

public function isAdmin();

public function isSuperAdmin();

public function isLoggedIn();

public function hasPermission($group, $permission);
}
4 changes: 3 additions & 1 deletion src/XenforoBridge/Exceptions/XenforoAutoloaderException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php namespace XenforoBridge\Exceptions;

class XenforoAutoloaderException.php extends Exception
use Exception;

class XenforoAutoloaderException extends Exception
{

}
2 changes: 1 addition & 1 deletion src/XenforoBridge/Middleware/XenAuthAdminMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function handle($request, Closure $next)
{
$xenBaseUrl = config('xenforobridge.xenforo_base_url_path');

if(!$this->xenforo->isSuperAdmin() AND ! $this->xenforo->isBanned())
if(!$this->xenforo->isAdmin() AND ! $this->xenforo->isBanned())
{
Session::put('loginRedirect', $request->url());
return Redirect::to($xenBaseUrl.'login');
Expand Down
163 changes: 152 additions & 11 deletions src/XenforoBridge/User/User.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,170 @@
<?php namespace XenforoBridge\User;

use XenforoBridge\Contracts\VisitorInterface;
use XenForo_Model_User as XenUser;
use XenForo_Authentication_Abstract;
use XenForo_DataWriter;
use XenForo_Model_Ip;
use XenForo_Phrase;
use XenforoBridge\Contracts\UserInterface;
use XenForo_Model_User as XenforoUser;
use XenforoBridge\XenforoBridge;


class User implements VisitorInterface
class User implements UserInterface
{
/**
* Stores Xenforo User Model
*
* @var XenforoUser | \XenForo_Model_User
*/
protected $user;

public function getCurrentVisitor()
/**
* Construct XenforoBridge User Class
*/
public function __construct()
{
return XenForo_Visitor::getInstance();
$this->setUser(new XenforoUser);
}

public function isBanned()
/**
* Set User Model
*
* @param XenforoUser $user
*/
public function setUser(XenforoUser $user)
{
return $this->getCurrentVisitor()->toArray()['is_banned'];
$this->user = $user;
}

public function isSuperAdmin()
/**
* Get User Model
*
* @return void|XenforoUser
*/
public function getUser()
{
return $this->getCurrentVisitor()->isSuperAdmin();
return $this->user;
}

public function isLoggedIn()
/**
* Get User by Id - returns empty array if user does not exist
*
* @param $id
* @return array
*/
public function getUserById($id)
{
return $this->getCurrentVisitor()->getUserId();
return $this->user->getUserById($id)?:[];
}

/**
* Get User by Email - returns empty array if user does not exist
*
* @param $email
* @return array
*/
public function getUserByEmail($email)
{
return $this->user->getUserByEmail($email)?: [];
}

/**
* Get User by Username - returns empty array if user does not exist
*
* @param $name
* @return array
*/
public function getUserByUsername($name)
{
return $this->user->getUserByName($name)?:[];
}

/**
* Validates Passwords and Returns Hashed value
*
* @todo refactor this method clean up and break down into smaller methods also rename to better illustrate it's purpose
* @param $password
* @param bool|false $passwordConfirm
* @param XenForo_Authentication_Abstract|null $auth
* @param bool|false $requirePassword
* @return array|XenForo_Phrase
*/
public function setPassword($password, $passwordConfirm = false, XenForo_Authentication_Abstract $auth = null, $requirePassword = false)
{
if ($requirePassword && $password === '')
{
return new XenForo_Phrase('please_enter_valid_password');
}
if ($passwordConfirm !== false && $password !== $passwordConfirm)
{
return new XenForo_Phrase('passwords_did_not_match');
}
if (!$auth)
{
$auth = XenForo_Authentication_Abstract::createDefault();
}
$authData = $auth->generate($password);
if (!$authData)
{
return new XenForo_Phrase('please_enter_valid_password');
}
return array('scheme_class' => $auth->getClassName(), 'data' => $authData);
}

/**
* Add User to Xenforo
*
* @todo refactor this method clean up and break down into smaller methods
* @param $email
* @param $username
* @param $password
* @param array $additional
* @param int $languageId
* @return array|XenForo_Phrase
* @throws \Exception
* @throws \XenForo_Exception
*/
public function addUser($email,$username,$password,array $additional = [], $languageId = XenforoBridge::XENFOROBRIDGE_DEFAULT_LANGUAGE_ID)
{
// Verify Password
$userPassword = $this->setPassword($password);
if(is_object($userPassword) && get_class($userPassword) == 'XenForo_Phrase') {
return $userPassword;
}

/**
* @var $writer \XenForo_DataWriter_User
*/
$writer = XenForo_DataWriter::create('XenForo_DataWriter_User');

$info = array_merge($additional, array(
'username' => $username,
'email' => $email,
'user_group_id' => XenforoUser::$defaultRegisteredGroupId,
'language_id' => $languageId,
));

$writer->advanceRegistrationUserState();

$writer->bulkSet($info);

// Set user password
$writer->set('scheme_class', $userPassword['scheme_class']);
$writer->set('data', $userPassword['data'], 'xf_user_authenticate');

// Save user
$writer->save();
$user = $writer->getMergedData();

if(!$user['user_id']) {
return new XenForo_Phrase('user_was_not_created');
}
// log the ip of the user registering
XenForo_Model_Ip::log($user['user_id'], 'user', $user['user_id'], 'register');

/*if ($user['user_state'] == 'email_confirm') {
XenForo_Model::create('XenForo_Model_UserConfirmation')->sendEmailConfirmation($user);
}*/
return $user['user_id'];
}
}
16 changes: 13 additions & 3 deletions src/XenforoBridge/Visitor/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,26 @@ public function getCurrentVisitor()

public function isBanned()
{
return $this->getCurrentVisitor()->toArray()['is_banned'];
return (bool)$this->getCurrentVisitor()->toArray()['is_banned'];
}

public function isAdmin()
{
return (bool)$this->getCurrentVisitor()->toArray()['is_admin'];
}

public function isSuperAdmin()
{
return $this->getCurrentVisitor()->isSuperAdmin();
return (bool)$this->getCurrentVisitor()->isSuperAdmin();
}

public function isLoggedIn()
{
return $this->getCurrentVisitor()->getUserId();
return (bool)$this->getCurrentVisitor()->getUserId();
}

public function hasPermission($group,$permission)
{
return $this->getCurrentVisitor()->hasPermission($group,$permission);
}
}
Loading

0 comments on commit 2044a60

Please sign in to comment.