diff --git a/app/Authentication/Classes/CustomProfile/Repository/CustomProfileRepository.php b/app/Authentication/Classes/CustomProfile/Repository/CustomProfileRepository.php old mode 100755 new mode 100644 index f0dea58..f6d27e8 --- a/app/Authentication/Classes/CustomProfile/Repository/CustomProfileRepository.php +++ b/app/Authentication/Classes/CustomProfile/Repository/CustomProfileRepository.php @@ -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 @@ -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; } @@ -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; } @@ -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 @@ -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(); } @@ -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(); } diff --git a/app/Authentication/Controllers/PermissionController.php b/app/Authentication/Controllers/PermissionController.php old mode 100755 new mode 100644 index 228d43c..7e824a6 --- a/app/Authentication/Controllers/PermissionController.php +++ b/app/Authentication/Controllers/PermissionController.php @@ -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]); diff --git a/app/Authentication/Controllers/UserController.php b/app/Authentication/Controllers/UserController.php old mode 100755 new mode 100644 index c61e276..10e3da2 --- a/app/Authentication/Controllers/UserController.php +++ b/app/Authentication/Controllers/UserController.php @@ -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; @@ -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); diff --git a/app/Authentication/Presenters/Traits/PermissionTrait.php b/app/Authentication/Presenters/Traits/PermissionTrait.php old mode 100755 new mode 100644 index 84a2f60..226a8d3 --- a/app/Authentication/Presenters/Traits/PermissionTrait.php +++ b/app/Authentication/Presenters/Traits/PermissionTrait.php @@ -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) @@ -24,4 +24,13 @@ public function permissions_obj($model = null) } return $objs; } -} \ No newline at end of file + + public function getPermissionModel(){ + $config = config('cartalyst.sentry'); + if (isset($config['permission']) && isset($config['permission']['model'])) { + return new $config['permission']['model']; + } + + return new Permission; + } +} \ No newline at end of file diff --git a/app/Authentication/Repository/EloquentPermissionRepository.php b/app/Authentication/Repository/EloquentPermissionRepository.php old mode 100755 new mode 100644 index 48fd08c..96f8706 --- a/app/Authentication/Repository/EloquentPermissionRepository.php +++ b/app/Authentication/Repository/EloquentPermissionRepository.php @@ -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'); @@ -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); } /** diff --git a/app/Authentication/Repository/EloquentUserProfileRepository.php b/app/Authentication/Repository/EloquentUserProfileRepository.php old mode 100755 new mode 100644 index 17ee94a..91638a6 --- a/app/Authentication/Repository/EloquentUserProfileRepository.php +++ b/app/Authentication/Repository/EloquentUserProfileRepository.php @@ -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 @@ -17,6 +16,10 @@ */ class EloquentUserProfileRepository extends EloquentBaseRepository implements UserProfileRepositoryInterface { + protected $userprofile = 'LaravelAcl\Authentication\Models\UserProfile'; + + protected $user_repo; + use ImageHelperTrait; /** @@ -24,14 +27,20 @@ class EloquentUserProfileRepository extends EloquentBaseRepository implements Us */ 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; } diff --git a/app/Authentication/Repository/SentryGroupRepository.php b/app/Authentication/Repository/SentryGroupRepository.php old mode 100755 new mode 100644 index c32d6a1..7f8c1b1 --- a/app/Authentication/Repository/SentryGroupRepository.php +++ b/app/Authentication/Repository/SentryGroupRepository.php @@ -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 @@ -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 * @@ -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'); diff --git a/app/Authentication/Repository/SentryUserRepository.php b/app/Authentication/Repository/SentryUserRepository.php old mode 100755 new mode 100644 index 8e937b2..1f94af8 --- a/app/Authentication/Repository/SentryUserRepository.php +++ b/app/Authentication/Repository/SentryUserRepository.php @@ -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; @@ -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 ); } /** @@ -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) { @@ -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) { diff --git a/app/Authentication/Repository/UserRepositorySearchFilter.php b/app/Authentication/Repository/UserRepositorySearchFilter.php old mode 100755 new mode 100644 index ed9dcfd..061bb2b --- a/app/Authentication/Repository/UserRepositorySearchFilter.php +++ b/app/Authentication/Repository/UserRepositorySearchFilter.php @@ -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']; + } } /** diff --git a/app/Authentication/Validators/GroupValidator.php b/app/Authentication/Validators/GroupValidator.php old mode 100755 new mode 100644 index 0bc71e9..1ba3a93 --- a/app/Authentication/Validators/GroupValidator.php +++ b/app/Authentication/Validators/GroupValidator.php @@ -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']}"; }); } } \ No newline at end of file diff --git a/app/Authentication/Validators/PermissionValidator.php b/app/Authentication/Validators/PermissionValidator.php old mode 100755 new mode 100644 index 9f782ad..82e07ed --- a/app/Authentication/Validators/PermissionValidator.php +++ b/app/Authentication/Validators/PermissionValidator.php @@ -5,6 +5,8 @@ class PermissionValidator extends AbstractValidator { + protected static $table_name = 'permission'; + protected static $rules = array( "description" => ["required", "max:255"], "permission" => ["required", "max:255"], @@ -12,9 +14,10 @@ class PermissionValidator extends AbstractValidator 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']}"; }); } } \ No newline at end of file diff --git a/app/Authentication/Validators/UserValidator.php b/app/Authentication/Validators/UserValidator.php old mode 100755 new mode 100644 index b55aacf..57b66d6 --- a/app/Authentication/Validators/UserValidator.php +++ b/app/Authentication/Validators/UserValidator.php @@ -5,6 +5,8 @@ class UserValidator extends AbstractValidator { + protected static $table_name = 'users'; + protected static $rules = [ "email" => ["required", "email"], "password" => ["confirmed"] @@ -12,20 +14,16 @@ class UserValidator extends AbstractValidator public function __construct() { - Event::listen('validating', function($input) - { + Event::listen('validating', function ($input) { // check if the input comes form the correct form - if(!isset($input['form_name']) || $input['form_name']!='user') + if (!isset($input['form_name']) || $input['form_name'] != 'user') return true; - - if(empty($input["id"])) - { + $table_name = isset($input['_table_name']) ? $input['_table_name'] : $this::$table_name; + if (empty($input["id"])) { static::$rules["password"][] = "required"; - static::$rules["email"][] = "unique:users,email"; - } - else - { - static::$rules["email"][] = "unique:users,email,{$input['id']}"; + static::$rules["email"][] = "unique:{$table_name},email"; + } else { + static::$rules["email"][] = "unique:{$table_name},email,{$input['id']}"; } }); @@ -40,8 +38,8 @@ public function __construct() public static function resetStatic() { static::$rules = [ - "email" => ["required", "email"], - "password" => ["confirmed"] + "email" => ["required", "email"], + "password" => ["confirmed"] ]; } } \ No newline at end of file diff --git a/app/Library/Form/FormModel.php b/app/Library/Form/FormModel.php old mode 100755 new mode 100644 index 11c0623..06258b6 --- a/app/Library/Form/FormModel.php +++ b/app/Library/Form/FormModel.php @@ -50,6 +50,19 @@ public function __construct(ValidatorInterface $validator, $repository) */ public function process(array $input) { + + if (method_exists($this->r, 'getModel')) { + $model = $this->r->getModel(); + if ($model instanceof \Illuminate\Database\Eloquent\Model) { + $table_name = (new $model)->getTable(); + if ($table_name) { + $input['_table_name'] = $table_name; + } + } + } + + + if($this->v->validate($input)) { Event::fire("form.processing", array($input)); diff --git a/composer.json b/composer.json old mode 100755 new mode 100644 diff --git a/config/acl_sentry.php b/config/acl_sentry.php old mode 100755 new mode 100644 index 8fb8d87..cdd64c8 --- a/config/acl_sentry.php +++ b/config/acl_sentry.php @@ -137,6 +137,70 @@ ), + 'users_profile' => array( + + /* + |-------------------------------------------------------------------------- + | Model + |-------------------------------------------------------------------------- + | + | When using the "eloquent" driver, we need to know which + | Eloquent models should be used throughout Sentry. + | + */ + + 'model' => 'LaravelAcl\Authentication\Models\UserProfile', + + ), + + + 'profile_field' => array( + + /* + |-------------------------------------------------------------------------- + | Model + |-------------------------------------------------------------------------- + | + | When using the "eloquent" driver, we need to know which + | Eloquent models should be used throughout Sentry. + | + */ + + 'model' => 'LaravelAcl\Authentication\Models\ProfileField', + + ), + 'profile_field_type' => array( + + /* + |-------------------------------------------------------------------------- + | Model + |-------------------------------------------------------------------------- + | + | When using the "eloquent" driver, we need to know which + | Eloquent models should be used throughout Sentry. + | + */ + + 'model' => 'LaravelAcl\Authentication\Models\ProfileFieldType', + + ), + + 'permission' => array( + + /* + |-------------------------------------------------------------------------- + | Model + |-------------------------------------------------------------------------- + | + | When using the "eloquent" driver, we need to know which + | Eloquent models should be used throughout Sentry. + | + */ + + 'model' => 'LaravelAcl\Authentication\Models\Permission', + + ), + /* |-------------------------------------------------------------------------- | User Groups Pivot Table