Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable classes for ACL objects and models #148

Open
wants to merge 1 commit into
base: 1.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
remove hard using of model
  • Loading branch information
Nikita Teryaev committed Nov 15, 2016
commit 73723fd370d5cc93d8929d65248878205a0306c4
31 changes: 23 additions & 8 deletions app/Authentication/Classes/CustomProfile/Repository/CustomProfileRepository.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use LaravelAcl\Authentication\Models\ProfileField;
use LaravelAcl\Authentication\Models\ProfileFieldType;

/**
* Class CustomProfileRepository
Expand All @@ -15,21 +13,38 @@ class CustomProfileRepository
{
protected $profile_id;

protected static $profile_field = 'LaravelAcl\Authentication\Models\ProfileField';
protected static $profile_field_type = 'LaravelAcl\Authentication\Models\ProfileFieldType';

protected static $profile_field_model = NULL;
protected static $profile_field_type_model = NULL;


public function __construct($profile_id)
{
$config = config('cartalyst.sentry');
if (isset($config['profile_field']) && isset($config['profile_field']['model'])) {
self::$profile_field = $config['profile_field']['model'];
}
if (isset($config['profile_field_type']) && isset($config['profile_field_type']['model'])) {
self::$profile_field_type = $config['profile_field_type']['model'];
}
self::$profile_field_model = new self::$profile_field;
self::$profile_field_type_model = new self::$profile_field_type;

$this->profile_id = is_array($profile_id) ? array_shift($profile_id) : $profile_id;
}

public static function getAllTypes()
{
return ProfileFieldType::all();
return self::$profile_field_type_model->all();
}

public static function addNewType($description)
{
// firing event so it can get catched for permission handling
Event::fire('customprofile.creating');
$profile_field_type = ProfileFieldType::create(["description" => $description]);
$profile_field_type = self::$profile_field_type_model->create(["description" => $description]);

return $profile_field_type;
}
Expand All @@ -38,7 +53,7 @@ public static function deleteType($id)
{
// firing event so it can get catched for permission handling
Event::fire('customprofile.deleting');
$success = ProfileFieldType::findOrFail($id)->delete();
$success = self::$profile_field_type_model->findOrFail($id)->delete();

return $success;
}
Expand All @@ -64,7 +79,7 @@ public function setField($profile_type_field_id, $field_value)
*/
protected function createNewField($profile_type_field_id, $field_value)
{
return ProfileField::create([
return self::$profile_field_model->create([
"profile_id" => $this->profile_id,
"profile_field_type_id" => $profile_type_field_id,
"value" => $field_value
Expand All @@ -90,7 +105,7 @@ public function getAllTypesWithValues()

public function getAllFields()
{
return ProfileField::where('profile_id','=',$this->profile_id)
return self::$profile_field_model->where('profile_id','=',$this->profile_id)
->get();
}

Expand All @@ -101,7 +116,7 @@ public function getAllFields()
*/
public function findField($profile_type_field_id)
{
return ProfileField::where('profile_id', '=', $this->profile_id)
return self::$profile_field_model->where('profile_id', '=', $this->profile_id)
->where('profile_field_type_id', '=', $profile_type_field_id)
->firstOrFail();
}
Expand Down
2 changes: 1 addition & 1 deletion app/Authentication/Controllers/PermissionController.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function editPermission(Request $request)
}
catch(JacopoExceptionsInterface $e)
{
$obj = new Permission;
$obj = $this->r->getModel();
}

return View::make('laravel-authentication-acl::admin.permission.edit')->with(["permission" => $obj]);
Expand Down
3 changes: 1 addition & 2 deletions app/Authentication/Controllers/UserController.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use LaravelAcl\Authentication\Services\UserProfileService;
use LaravelAcl\Authentication\Validators\UserProfileAvatarValidator;
use LaravelAcl\Library\Exceptions\NotFoundException;
use LaravelAcl\Authentication\Models\User;
use LaravelAcl\Authentication\Helpers\FormHelper;
use LaravelAcl\Authentication\Exceptions\UserNotFoundException;
use LaravelAcl\Authentication\Validators\UserValidator;
Expand Down Expand Up @@ -71,7 +70,7 @@ public function editUser(Request $request)
$user = $this->user_repository->find($request->get('id'));
} catch(JacopoExceptionsInterface $e)
{
$user = new User;
$user = $this->user_repository->getModel();
}
$presenter = new UserPresenter($user);

Expand Down
13 changes: 11 additions & 2 deletions app/Authentication/Presenters/Traits/PermissionTrait.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait PermissionTrait
*/
public function permissions_obj($model = null)
{
$model = $model ? $model : new Permission;
$model = $model ? $model : $this->getPermissionModel();
$objs = [];
$permissions = $this->resource->permissions;
if(! empty($permissions) ) foreach ($permissions as $permission => $status)
Expand All @@ -24,4 +24,13 @@ public function permissions_obj($model = null)
}
return $objs;
}
}

public function getPermissionModel(){
$config = config('cartalyst.sentry');
if (isset($config['permission']) && isset($config['permission']['model'])) {
return new $config['permission']['model'];
}

return new Permission;
}
}
8 changes: 7 additions & 1 deletion app/Authentication/Repository/EloquentPermissionRepository.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class EloquentPermissionRepository extends EloquentBaseRepository
protected $group_repo;
protected $user_repo;

protected $permissions_model = 'LaravelAcl\Authentication\Models\Permission';

public function __construct()
{
$this->group_repo = App::make('group_repository');
Expand All @@ -22,7 +24,11 @@ public function __construct()
Event::listen(['repository.deleting','repository.updating'], '\LaravelAcl\Authentication\Repository\EloquentPermissionRepository@checkIsNotAssociatedToAnyUser');
Event::listen(['repository.deleting','repository.updating'], '\LaravelAcl\Authentication\Repository\EloquentPermissionRepository@checkIsNotAssociatedToAnyGroup');

return parent::__construct(new Permission);
$config = config('cartalyst.sentry');
if (isset($config['permission']) && isset($config['permission']['model'])) {
$this->permissions_model = $config['permission']['model'];
}
return parent::__construct(new $this->permissions_model);
}

/**
Expand Down
17 changes: 13 additions & 4 deletions app/Authentication/Repository/EloquentUserProfileRepository.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
use LaravelAcl\Authentication\Classes\Images\ImageHelperTrait;
use LaravelAcl\Authentication\Exceptions\UserNotFoundException;
use LaravelAcl\Authentication\Exceptions\ProfileNotFoundException;
use LaravelAcl\Authentication\Models\User;
use LaravelAcl\Authentication\Models\UserProfile;
use LaravelAcl\Authentication\Repository\Interfaces\UserProfileRepositoryInterface;
use LaravelAcl\Library\Repository\EloquentBaseRepository;
use LaravelAcl\Library\Repository\Interfaces\BaseRepositoryInterface;
use App;

/**
* Class EloquentUserProfileRepository
Expand All @@ -17,21 +16,31 @@
*/
class EloquentUserProfileRepository extends EloquentBaseRepository implements UserProfileRepositoryInterface
{
protected $userprofile = 'LaravelAcl\Authentication\Models\UserProfile';

protected $user_repo;

use ImageHelperTrait;

/**
* We use the user profile as a model
*/
public function __construct()
{
return parent::__construct(new UserProfile);
$this->user_repo = App::make('user_repository');

$config = config('cartalyst.sentry');
if (isset($config['users_profile']) && isset($config['users_profile']['model'])) {
$this->userprofile = $config['users_profile']['model'];
}
return parent::__construct(new $this->userprofile);
}

public function getFromUserId($user_id)
{
// checks if the user exists
try {
User::findOrFail($user_id);
$this->user_repo->getModel()->findOrFail($user_id);
} catch (ModelNotFoundException $e) {
throw new UserNotFoundException;
}
Expand Down
14 changes: 11 additions & 3 deletions app/Authentication/Repository/SentryGroupRepository.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
use LaravelAcl\Authentication\Exceptions\UserNotFoundException as NotFoundException;
use App, Event;
use Cartalyst\Sentry\Groups\GroupNotFoundException;
use LaravelAcl\Library\Repository\EloquentBaseRepository;

class SentryGroupRepository implements BaseRepositoryInterface
class SentryGroupRepository extends EloquentBaseRepository implements BaseRepositoryInterface
{
/**
* Sentry instance
Expand All @@ -21,12 +22,19 @@ class SentryGroupRepository implements BaseRepositoryInterface

protected $config_reader;

protected $groupModel = \LaravelAcl\Authentication\Models\Group::class;

public function __construct($config_reader = null)
{
$this->sentry = App::make('sentry');
$this->config_reader = $config_reader ? $config_reader : App::make('config');
}

if (method_exists($this->sentry, 'getGroupProvider')) {
$this->groupModel = get_class( $this->sentry->getGroupProvider()->createModel());
}

return parent::__construct( new $this->groupModel );
}
/**
* Create a new object
*
Expand Down Expand Up @@ -95,7 +103,7 @@ public function find($id)
*/
public function all(array $search_filters = [])
{
$q = new Group;
$q = $this->sentry->getGroupProvider()->createModel();
$q = $this->applySearchFilters($search_filters, $q);

$results_per_page = $this->config_reader->get('acl_base.groups_per_page');
Expand Down
27 changes: 20 additions & 7 deletions app/Authentication/Repository/SentryUserRepository.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use Illuminate\Support\Facades\Config;
use LaravelAcl\Authentication\Exceptions\UserExistsException;
use LaravelAcl\Authentication\Exceptions\UserNotFoundException as NotFoundException;
use LaravelAcl\Authentication\Models\Group;
use LaravelAcl\Authentication\Models\User;
use LaravelAcl\Authentication\Repository\Interfaces\UserRepositoryInterface;
use LaravelAcl\Library\Repository\EloquentBaseRepository;

Expand All @@ -29,10 +27,22 @@ class SentryUserRepository extends EloquentBaseRepository implements UserReposit
*/
protected $sentry;

protected $groupModel = \LaravelAcl\Authentication\Models\Group::class;
protected $userModel = \LaravelAcl\Authentication\Models\User::class;

public function __construct()
{
$this->sentry = App::make('sentry');
return parent::__construct(new User);

if (method_exists($this->sentry, 'getGroupProvider')) {
$this->groupModel = get_class( $this->sentry->getGroupProvider()->createModel());
}

if (method_exists($this->sentry, 'getUserProvider')) {
$this->userModel = get_class ($this->sentry->getUserProvider()->createModel());
}

return parent::__construct( new $this->userModel );
}

/**
Expand Down Expand Up @@ -110,8 +120,10 @@ public function addGroup($user_id, $group_id)
{
try
{
$group = Group::findOrFail($group_id);
$user = User::findOrFail($user_id);
$group = new $this->groupModel;
$group = $group->findOrFail($group_id);
$user = $this->find($user_id);

$user->addGroup($group);
} catch(ModelNotFoundException $e)
{
Expand All @@ -129,8 +141,9 @@ public function removeGroup($user_id, $group_id)
{
try
{
$group = Group::findOrFail($group_id);
$user = User::findOrFail($user_id);
$group = new $this->groupModel;
$group = $group->findOrFail($group_id);
$user = $this->find($user_id);
$user->removeGroup($group);
} catch(ModelNotFoundException $e)
{
Expand Down
16 changes: 16 additions & 0 deletions app/Authentication/Repository/UserRepositorySearchFilter.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ class UserRepositorySearchFilter
public function __construct($per_page = 5)
{
$this->per_page = $per_page;
$config = config('cartalyst.sentry');
if (isset($config['users']) && isset($config['users']['model'])) {
$this->user_table_name = (new $config['users']['model'])->getTable();
}

if (isset($config['users_profile']) && isset($config['users_profile']['model'])) {
$this->profile_table_name = (new $config['users_profile']['model'])->getTable();
}

if (isset($config['groups']) && isset($config['groups']['model'])) {
$this->groups_table_name = (new $config['groups']['model'])->getTable();
}

if (isset($config['user_groups_pivot_table'])) {
$this->user_groups_table_name = $config['user_groups_pivot_table'];
}
}

/**
Expand Down
10 changes: 6 additions & 4 deletions app/Authentication/Validators/GroupValidator.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
use Event;
use LaravelAcl\Library\Validators\AbstractValidator;

class GroupValidator extends AbstractValidator
class GroupValidator extends AbstractValidator
{
protected static $table_name = 'groups';

protected static $rules = array(
"name" => ["required"],
);

public function __construct()
{
Event::listen('validating', function($input)
{
static::$rules["name"][] = "unique:groups,name,{$input['id']}";
Event::listen('validating', function ($input) {
$table_name = isset($input['_table_name']) ? $input['_table_name'] : $this::$table_name;
static::$rules["name"][] = "unique:{$table_name},name,{$input['id']}";
});
}
}
9 changes: 6 additions & 3 deletions app/Authentication/Validators/PermissionValidator.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@

class PermissionValidator extends AbstractValidator
{
protected static $table_name = 'permission';

protected static $rules = array(
"description" => ["required", "max:255"],
"permission" => ["required", "max:255"],
);

public function __construct()
{
Event::listen('validating', function($input)
{
static::$rules["permission"][] = "unique:permission,permission,{$input['id']}";
Event::listen('validating', function ($input) {
$table_name = isset($input['_table_name']) ? $input['_table_name'] : $this::$table_name;

static::$rules["permission"][] = "unique:{$table_name},permission,{$input['id']}";
});
}
}
Loading