Skip to content

Commit 73723fd

Browse files
author
Nikita Teryaev
committed
remove hard using of model
1 parent 9379e05 commit 73723fd

15 files changed

+203
-48
lines changed

app/Authentication/Classes/CustomProfile/Repository/CustomProfileRepository.php

100755100644
Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
use Illuminate\Database\Eloquent\ModelNotFoundException;
44
use Illuminate\Support\Facades\DB;
55
use Illuminate\Support\Facades\Event;
6-
use LaravelAcl\Authentication\Models\ProfileField;
7-
use LaravelAcl\Authentication\Models\ProfileFieldType;
86

97
/**
108
* Class CustomProfileRepository
@@ -15,21 +13,38 @@ class CustomProfileRepository
1513
{
1614
protected $profile_id;
1715

16+
protected static $profile_field = 'LaravelAcl\Authentication\Models\ProfileField';
17+
protected static $profile_field_type = 'LaravelAcl\Authentication\Models\ProfileFieldType';
18+
19+
protected static $profile_field_model = NULL;
20+
protected static $profile_field_type_model = NULL;
21+
22+
1823
public function __construct($profile_id)
1924
{
25+
$config = config('cartalyst.sentry');
26+
if (isset($config['profile_field']) && isset($config['profile_field']['model'])) {
27+
self::$profile_field = $config['profile_field']['model'];
28+
}
29+
if (isset($config['profile_field_type']) && isset($config['profile_field_type']['model'])) {
30+
self::$profile_field_type = $config['profile_field_type']['model'];
31+
}
32+
self::$profile_field_model = new self::$profile_field;
33+
self::$profile_field_type_model = new self::$profile_field_type;
34+
2035
$this->profile_id = is_array($profile_id) ? array_shift($profile_id) : $profile_id;
2136
}
2237

2338
public static function getAllTypes()
2439
{
25-
return ProfileFieldType::all();
40+
return self::$profile_field_type_model->all();
2641
}
2742

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

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

4358
return $success;
4459
}
@@ -64,7 +79,7 @@ public function setField($profile_type_field_id, $field_value)
6479
*/
6580
protected function createNewField($profile_type_field_id, $field_value)
6681
{
67-
return ProfileField::create([
82+
return self::$profile_field_model->create([
6883
"profile_id" => $this->profile_id,
6984
"profile_field_type_id" => $profile_type_field_id,
7085
"value" => $field_value
@@ -90,7 +105,7 @@ public function getAllTypesWithValues()
90105

91106
public function getAllFields()
92107
{
93-
return ProfileField::where('profile_id','=',$this->profile_id)
108+
return self::$profile_field_model->where('profile_id','=',$this->profile_id)
94109
->get();
95110
}
96111

@@ -101,7 +116,7 @@ public function getAllFields()
101116
*/
102117
public function findField($profile_type_field_id)
103118
{
104-
return ProfileField::where('profile_id', '=', $this->profile_id)
119+
return self::$profile_field_model->where('profile_id', '=', $this->profile_id)
105120
->where('profile_field_type_id', '=', $profile_type_field_id)
106121
->firstOrFail();
107122
}

app/Authentication/Controllers/PermissionController.php

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function editPermission(Request $request)
4444
}
4545
catch(JacopoExceptionsInterface $e)
4646
{
47-
$obj = new Permission;
47+
$obj = $this->r->getModel();
4848
}
4949

5050
return View::make('laravel-authentication-acl::admin.permission.edit')->with(["permission" => $obj]);

app/Authentication/Controllers/UserController.php

100755100644
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use LaravelAcl\Authentication\Services\UserProfileService;
1616
use LaravelAcl\Authentication\Validators\UserProfileAvatarValidator;
1717
use LaravelAcl\Library\Exceptions\NotFoundException;
18-
use LaravelAcl\Authentication\Models\User;
1918
use LaravelAcl\Authentication\Helpers\FormHelper;
2019
use LaravelAcl\Authentication\Exceptions\UserNotFoundException;
2120
use LaravelAcl\Authentication\Validators\UserValidator;
@@ -71,7 +70,7 @@ public function editUser(Request $request)
7170
$user = $this->user_repository->find($request->get('id'));
7271
} catch(JacopoExceptionsInterface $e)
7372
{
74-
$user = new User;
73+
$user = $this->user_repository->getModel();
7574
}
7675
$presenter = new UserPresenter($user);
7776

app/Authentication/Presenters/Traits/PermissionTrait.php

100755100644
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait PermissionTrait
1515
*/
1616
public function permissions_obj($model = null)
1717
{
18-
$model = $model ? $model : new Permission;
18+
$model = $model ? $model : $this->getPermissionModel();
1919
$objs = [];
2020
$permissions = $this->resource->permissions;
2121
if(! empty($permissions) ) foreach ($permissions as $permission => $status)
@@ -24,4 +24,13 @@ public function permissions_obj($model = null)
2424
}
2525
return $objs;
2626
}
27-
}
27+
28+
public function getPermissionModel(){
29+
$config = config('cartalyst.sentry');
30+
if (isset($config['permission']) && isset($config['permission']['model'])) {
31+
return new $config['permission']['model'];
32+
}
33+
34+
return new Permission;
35+
}
36+
}

app/Authentication/Repository/EloquentPermissionRepository.php

100755100644
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class EloquentPermissionRepository extends EloquentBaseRepository
1414
protected $group_repo;
1515
protected $user_repo;
1616

17+
protected $permissions_model = 'LaravelAcl\Authentication\Models\Permission';
18+
1719
public function __construct()
1820
{
1921
$this->group_repo = App::make('group_repository');
@@ -22,7 +24,11 @@ public function __construct()
2224
Event::listen(['repository.deleting','repository.updating'], '\LaravelAcl\Authentication\Repository\EloquentPermissionRepository@checkIsNotAssociatedToAnyUser');
2325
Event::listen(['repository.deleting','repository.updating'], '\LaravelAcl\Authentication\Repository\EloquentPermissionRepository@checkIsNotAssociatedToAnyGroup');
2426

25-
return parent::__construct(new Permission);
27+
$config = config('cartalyst.sentry');
28+
if (isset($config['permission']) && isset($config['permission']['model'])) {
29+
$this->permissions_model = $config['permission']['model'];
30+
}
31+
return parent::__construct(new $this->permissions_model);
2632
}
2733

2834
/**

app/Authentication/Repository/EloquentUserProfileRepository.php

100755100644
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
use LaravelAcl\Authentication\Classes\Images\ImageHelperTrait;
55
use LaravelAcl\Authentication\Exceptions\UserNotFoundException;
66
use LaravelAcl\Authentication\Exceptions\ProfileNotFoundException;
7-
use LaravelAcl\Authentication\Models\User;
8-
use LaravelAcl\Authentication\Models\UserProfile;
97
use LaravelAcl\Authentication\Repository\Interfaces\UserProfileRepositoryInterface;
108
use LaravelAcl\Library\Repository\EloquentBaseRepository;
119
use LaravelAcl\Library\Repository\Interfaces\BaseRepositoryInterface;
10+
use App;
1211

1312
/**
1413
* Class EloquentUserProfileRepository
@@ -17,21 +16,31 @@
1716
*/
1817
class EloquentUserProfileRepository extends EloquentBaseRepository implements UserProfileRepositoryInterface
1918
{
19+
protected $userprofile = 'LaravelAcl\Authentication\Models\UserProfile';
20+
21+
protected $user_repo;
22+
2023
use ImageHelperTrait;
2124

2225
/**
2326
* We use the user profile as a model
2427
*/
2528
public function __construct()
2629
{
27-
return parent::__construct(new UserProfile);
30+
$this->user_repo = App::make('user_repository');
31+
32+
$config = config('cartalyst.sentry');
33+
if (isset($config['users_profile']) && isset($config['users_profile']['model'])) {
34+
$this->userprofile = $config['users_profile']['model'];
35+
}
36+
return parent::__construct(new $this->userprofile);
2837
}
2938

3039
public function getFromUserId($user_id)
3140
{
3241
// checks if the user exists
3342
try {
34-
User::findOrFail($user_id);
43+
$this->user_repo->getModel()->findOrFail($user_id);
3544
} catch (ModelNotFoundException $e) {
3645
throw new UserNotFoundException;
3746
}

app/Authentication/Repository/SentryGroupRepository.php

100755100644
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
use LaravelAcl\Authentication\Exceptions\UserNotFoundException as NotFoundException;
1111
use App, Event;
1212
use Cartalyst\Sentry\Groups\GroupNotFoundException;
13+
use LaravelAcl\Library\Repository\EloquentBaseRepository;
1314

14-
class SentryGroupRepository implements BaseRepositoryInterface
15+
class SentryGroupRepository extends EloquentBaseRepository implements BaseRepositoryInterface
1516
{
1617
/**
1718
* Sentry instance
@@ -21,12 +22,19 @@ class SentryGroupRepository implements BaseRepositoryInterface
2122

2223
protected $config_reader;
2324

25+
protected $groupModel = \LaravelAcl\Authentication\Models\Group::class;
26+
2427
public function __construct($config_reader = null)
2528
{
2629
$this->sentry = App::make('sentry');
2730
$this->config_reader = $config_reader ? $config_reader : App::make('config');
28-
}
2931

32+
if (method_exists($this->sentry, 'getGroupProvider')) {
33+
$this->groupModel = get_class( $this->sentry->getGroupProvider()->createModel());
34+
}
35+
36+
return parent::__construct( new $this->groupModel );
37+
}
3038
/**
3139
* Create a new object
3240
*
@@ -95,7 +103,7 @@ public function find($id)
95103
*/
96104
public function all(array $search_filters = [])
97105
{
98-
$q = new Group;
106+
$q = $this->sentry->getGroupProvider()->createModel();
99107
$q = $this->applySearchFilters($search_filters, $q);
100108

101109
$results_per_page = $this->config_reader->get('acl_base.groups_per_page');

app/Authentication/Repository/SentryUserRepository.php

100755100644
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use Illuminate\Support\Facades\Config;
1616
use LaravelAcl\Authentication\Exceptions\UserExistsException;
1717
use LaravelAcl\Authentication\Exceptions\UserNotFoundException as NotFoundException;
18-
use LaravelAcl\Authentication\Models\Group;
19-
use LaravelAcl\Authentication\Models\User;
2018
use LaravelAcl\Authentication\Repository\Interfaces\UserRepositoryInterface;
2119
use LaravelAcl\Library\Repository\EloquentBaseRepository;
2220

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

30+
protected $groupModel = \LaravelAcl\Authentication\Models\Group::class;
31+
protected $userModel = \LaravelAcl\Authentication\Models\User::class;
32+
3233
public function __construct()
3334
{
3435
$this->sentry = App::make('sentry');
35-
return parent::__construct(new User);
36+
37+
if (method_exists($this->sentry, 'getGroupProvider')) {
38+
$this->groupModel = get_class( $this->sentry->getGroupProvider()->createModel());
39+
}
40+
41+
if (method_exists($this->sentry, 'getUserProvider')) {
42+
$this->userModel = get_class ($this->sentry->getUserProvider()->createModel());
43+
}
44+
45+
return parent::__construct( new $this->userModel );
3646
}
3747

3848
/**
@@ -110,8 +120,10 @@ public function addGroup($user_id, $group_id)
110120
{
111121
try
112122
{
113-
$group = Group::findOrFail($group_id);
114-
$user = User::findOrFail($user_id);
123+
$group = new $this->groupModel;
124+
$group = $group->findOrFail($group_id);
125+
$user = $this->find($user_id);
126+
115127
$user->addGroup($group);
116128
} catch(ModelNotFoundException $e)
117129
{
@@ -129,8 +141,9 @@ public function removeGroup($user_id, $group_id)
129141
{
130142
try
131143
{
132-
$group = Group::findOrFail($group_id);
133-
$user = User::findOrFail($user_id);
144+
$group = new $this->groupModel;
145+
$group = $group->findOrFail($group_id);
146+
$user = $this->find($user_id);
134147
$user->removeGroup($group);
135148
} catch(ModelNotFoundException $e)
136149
{

app/Authentication/Repository/UserRepositorySearchFilter.php

100755100644
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ class UserRepositorySearchFilter
2323
public function __construct($per_page = 5)
2424
{
2525
$this->per_page = $per_page;
26+
$config = config('cartalyst.sentry');
27+
if (isset($config['users']) && isset($config['users']['model'])) {
28+
$this->user_table_name = (new $config['users']['model'])->getTable();
29+
}
30+
31+
if (isset($config['users_profile']) && isset($config['users_profile']['model'])) {
32+
$this->profile_table_name = (new $config['users_profile']['model'])->getTable();
33+
}
34+
35+
if (isset($config['groups']) && isset($config['groups']['model'])) {
36+
$this->groups_table_name = (new $config['groups']['model'])->getTable();
37+
}
38+
39+
if (isset($config['user_groups_pivot_table'])) {
40+
$this->user_groups_table_name = $config['user_groups_pivot_table'];
41+
}
2642
}
2743

2844
/**

app/Authentication/Validators/GroupValidator.php

100755100644
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
use Event;
44
use LaravelAcl\Library\Validators\AbstractValidator;
55

6-
class GroupValidator extends AbstractValidator
6+
class GroupValidator extends AbstractValidator
77
{
8+
protected static $table_name = 'groups';
9+
810
protected static $rules = array(
911
"name" => ["required"],
1012
);
1113

1214
public function __construct()
1315
{
14-
Event::listen('validating', function($input)
15-
{
16-
static::$rules["name"][] = "unique:groups,name,{$input['id']}";
16+
Event::listen('validating', function ($input) {
17+
$table_name = isset($input['_table_name']) ? $input['_table_name'] : $this::$table_name;
18+
static::$rules["name"][] = "unique:{$table_name},name,{$input['id']}";
1719
});
1820
}
1921
}

app/Authentication/Validators/PermissionValidator.php

100755100644
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55

66
class PermissionValidator extends AbstractValidator
77
{
8+
protected static $table_name = 'permission';
9+
810
protected static $rules = array(
911
"description" => ["required", "max:255"],
1012
"permission" => ["required", "max:255"],
1113
);
1214

1315
public function __construct()
1416
{
15-
Event::listen('validating', function($input)
16-
{
17-
static::$rules["permission"][] = "unique:permission,permission,{$input['id']}";
17+
Event::listen('validating', function ($input) {
18+
$table_name = isset($input['_table_name']) ? $input['_table_name'] : $this::$table_name;
19+
20+
static::$rules["permission"][] = "unique:{$table_name},permission,{$input['id']}";
1821
});
1922
}
2023
}

0 commit comments

Comments
 (0)