Laravel Repository/Service Pattern: How to handle Auth and GatesProper Repository Pattern Design in PHP?Managing relationships in Laravel, adhering to the repository patternLaravel - Using the Repository PatternRepository pattern implementation with LaravelLaravel - Repository Pattern issuesWhat is a good approach to save relations in repository pattern?Dependency Injection/Repository Pattern on user model with laravelLaravel real repository patternLaravel Repsitory Pattern for all modelWhere put eloquent relationship with Repository Pattern in Laravel
How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?
Two-sided logarithm inequality
How will losing mobility of one hand affect my career as a programmer?
What is the difference between "Do you interest" and "...interested in" something?
Diode in opposite direction?
Could the E-bike drivetrain wear down till needing replacement after 400 km?
Is it improper etiquette to ask your opponent what his/her rating is before the game?
Divine apple island
Have I saved too much for retirement so far?
Can I use my Chinese passport to enter China after I acquired another citizenship?
THT: What is a squared annular “ring”?
Do Legal Documents Require Signing In Standard Pen Colors?
Filling the middle of a torus in Tikz
Can I sign legal documents with a smiley face?
MAXDOP Settings for SQL Server 2014
Is XSS in canonical link possible?
Should spaces be used when writing foreign names in katakana?
My friend sent me a screenshot of a transaction hash, but when I search for it I find divergent data. What happened?
Why we can't differentiate a polynomial equation as many times as we wish?
Drawing ramified coverings with tikz
Can the Supreme Court overturn an impeachment?
What linear sensor for a keyboard?
When quoting, must I also copy hyphens used to divide words that continue on the next line?
How does the reference system of the Majjhima Nikaya work?
Laravel Repository/Service Pattern: How to handle Auth and Gates
Proper Repository Pattern Design in PHP?Managing relationships in Laravel, adhering to the repository patternLaravel - Using the Repository PatternRepository pattern implementation with LaravelLaravel - Repository Pattern issuesWhat is a good approach to save relations in repository pattern?Dependency Injection/Repository Pattern on user model with laravelLaravel real repository patternLaravel Repsitory Pattern for all modelWhere put eloquent relationship with Repository Pattern in Laravel
I'm starting to use repository and service pattern for my laravel project.
Just a quick background, I use repository as the data mapper and service to help keeping up controllers do their main job which is, receiving requests and sending out responses, and thus be thinner.
Following code sample is to give you some idea about above explanation:
// AppRepositoriesEloquentUsersRepository
class UsersRepository implements UsersRepoInterface
protected $model;
public function __construct (Model $model)
$this->model = $model;
public function all() : Collection
//
public function find($id) : Model
//
// and all other methods ...
// AppServicesUserService
class UserService
protected $repo;
public function __construct (UsersRepoInterface $user_repo)
$this->repo = $user;
/**
* @param array $data
* @return User
* @throws Exception
*/
public function create(array $data) : User
// All business logics to create a user data
return $user;
// and all other methods ...
// AppHttpControllersUserController
class UserController extends Controller
private $user;
public function __construct (UserService $user)
$this->user = $user;
public function create (Request $request) : Response
$user = $this->user->create($request->all());
return response(['user' => $user]);
Also, my project is an internal API app, thus an authentication is needed to make most of the requests. I use OAuth2.0 from Laravel Passport.
The Question
Before I switched over to repository/service or what-sort-you-called pattern, I used Gates and Policies before the requests are passed to the controller methods. And obviously User Auth is passed to every policy method which I don't feel right as the Auth (which is from Passport), I believe use User Model directly instead which is then a violation for repository pattern, isn't it?
Therefore, I really need some advice how am I supposed to manage resolving such problem ?
Any suggestion and obviously help are really appreciated!
Thanks in advance!
php laravel repository-pattern
|
show 1 more comment
I'm starting to use repository and service pattern for my laravel project.
Just a quick background, I use repository as the data mapper and service to help keeping up controllers do their main job which is, receiving requests and sending out responses, and thus be thinner.
Following code sample is to give you some idea about above explanation:
// AppRepositoriesEloquentUsersRepository
class UsersRepository implements UsersRepoInterface
protected $model;
public function __construct (Model $model)
$this->model = $model;
public function all() : Collection
//
public function find($id) : Model
//
// and all other methods ...
// AppServicesUserService
class UserService
protected $repo;
public function __construct (UsersRepoInterface $user_repo)
$this->repo = $user;
/**
* @param array $data
* @return User
* @throws Exception
*/
public function create(array $data) : User
// All business logics to create a user data
return $user;
// and all other methods ...
// AppHttpControllersUserController
class UserController extends Controller
private $user;
public function __construct (UserService $user)
$this->user = $user;
public function create (Request $request) : Response
$user = $this->user->create($request->all());
return response(['user' => $user]);
Also, my project is an internal API app, thus an authentication is needed to make most of the requests. I use OAuth2.0 from Laravel Passport.
The Question
Before I switched over to repository/service or what-sort-you-called pattern, I used Gates and Policies before the requests are passed to the controller methods. And obviously User Auth is passed to every policy method which I don't feel right as the Auth (which is from Passport), I believe use User Model directly instead which is then a violation for repository pattern, isn't it?
Therefore, I really need some advice how am I supposed to manage resolving such problem ?
Any suggestion and obviously help are really appreciated!
Thanks in advance!
php laravel repository-pattern
Policies require user authentication but delegate that authentication to the gate. In your case the gate would be passport so there's no extra layer there. I don't see how the repository pattern changes any of this since that's just an additional layer over the Laravel models which are a different level of abstraction from authentication/authorisation.
– apokryfos
Mar 21 at 14:03
Sorry but maybe I misunderstood what you are saying. Are you saying policies don't require user authentication? If so, yes you are right. Sorry if my sentence up there from the question confused you. But anyway, I'm saying all my policies require user auth to check whether given user has the permission to do given request. Laravel automatically injected User model to the callback function of each policy method which I don't feel that's a right thing for repository pattern, don't you think so? I don't know how do I change the parameter to use UserService which I feel more appropriate @apokryfos
– Rifqi
Mar 21 at 14:22
User policies do require authentication but do not perform authentication. That is done by the guard which in your case is passport. That would not change with this code. The user injection comes from the guard and whatever method the guard uses to get the user. Passport probably just gets it from the bearer token
– apokryfos
Mar 21 at 14:24
Okay I got it. Then my question is, am I supposed to change the authentication layer to use the repo instead? In that case, I need to change the passport, right? Because I believe passport use User model instead to authenticate which I think is a violation to repo pattern. Or do you have any suggestion to keep using passport in repo pattern @apokryfos ?
– Rifqi
Mar 21 at 14:30
1
Passport (all guards really) use aUserProviderimplementation that you can specify in the configuration so you would need to write your own user provider which uses the repository rather than the default Laravel one which uses activerecord. the docs have more details on how
– apokryfos
Mar 21 at 14:34
|
show 1 more comment
I'm starting to use repository and service pattern for my laravel project.
Just a quick background, I use repository as the data mapper and service to help keeping up controllers do their main job which is, receiving requests and sending out responses, and thus be thinner.
Following code sample is to give you some idea about above explanation:
// AppRepositoriesEloquentUsersRepository
class UsersRepository implements UsersRepoInterface
protected $model;
public function __construct (Model $model)
$this->model = $model;
public function all() : Collection
//
public function find($id) : Model
//
// and all other methods ...
// AppServicesUserService
class UserService
protected $repo;
public function __construct (UsersRepoInterface $user_repo)
$this->repo = $user;
/**
* @param array $data
* @return User
* @throws Exception
*/
public function create(array $data) : User
// All business logics to create a user data
return $user;
// and all other methods ...
// AppHttpControllersUserController
class UserController extends Controller
private $user;
public function __construct (UserService $user)
$this->user = $user;
public function create (Request $request) : Response
$user = $this->user->create($request->all());
return response(['user' => $user]);
Also, my project is an internal API app, thus an authentication is needed to make most of the requests. I use OAuth2.0 from Laravel Passport.
The Question
Before I switched over to repository/service or what-sort-you-called pattern, I used Gates and Policies before the requests are passed to the controller methods. And obviously User Auth is passed to every policy method which I don't feel right as the Auth (which is from Passport), I believe use User Model directly instead which is then a violation for repository pattern, isn't it?
Therefore, I really need some advice how am I supposed to manage resolving such problem ?
Any suggestion and obviously help are really appreciated!
Thanks in advance!
php laravel repository-pattern
I'm starting to use repository and service pattern for my laravel project.
Just a quick background, I use repository as the data mapper and service to help keeping up controllers do their main job which is, receiving requests and sending out responses, and thus be thinner.
Following code sample is to give you some idea about above explanation:
// AppRepositoriesEloquentUsersRepository
class UsersRepository implements UsersRepoInterface
protected $model;
public function __construct (Model $model)
$this->model = $model;
public function all() : Collection
//
public function find($id) : Model
//
// and all other methods ...
// AppServicesUserService
class UserService
protected $repo;
public function __construct (UsersRepoInterface $user_repo)
$this->repo = $user;
/**
* @param array $data
* @return User
* @throws Exception
*/
public function create(array $data) : User
// All business logics to create a user data
return $user;
// and all other methods ...
// AppHttpControllersUserController
class UserController extends Controller
private $user;
public function __construct (UserService $user)
$this->user = $user;
public function create (Request $request) : Response
$user = $this->user->create($request->all());
return response(['user' => $user]);
Also, my project is an internal API app, thus an authentication is needed to make most of the requests. I use OAuth2.0 from Laravel Passport.
The Question
Before I switched over to repository/service or what-sort-you-called pattern, I used Gates and Policies before the requests are passed to the controller methods. And obviously User Auth is passed to every policy method which I don't feel right as the Auth (which is from Passport), I believe use User Model directly instead which is then a violation for repository pattern, isn't it?
Therefore, I really need some advice how am I supposed to manage resolving such problem ?
Any suggestion and obviously help are really appreciated!
Thanks in advance!
php laravel repository-pattern
php laravel repository-pattern
edited Mar 21 at 14:34
Rifqi
asked Mar 21 at 13:50
RifqiRifqi
365
365
Policies require user authentication but delegate that authentication to the gate. In your case the gate would be passport so there's no extra layer there. I don't see how the repository pattern changes any of this since that's just an additional layer over the Laravel models which are a different level of abstraction from authentication/authorisation.
– apokryfos
Mar 21 at 14:03
Sorry but maybe I misunderstood what you are saying. Are you saying policies don't require user authentication? If so, yes you are right. Sorry if my sentence up there from the question confused you. But anyway, I'm saying all my policies require user auth to check whether given user has the permission to do given request. Laravel automatically injected User model to the callback function of each policy method which I don't feel that's a right thing for repository pattern, don't you think so? I don't know how do I change the parameter to use UserService which I feel more appropriate @apokryfos
– Rifqi
Mar 21 at 14:22
User policies do require authentication but do not perform authentication. That is done by the guard which in your case is passport. That would not change with this code. The user injection comes from the guard and whatever method the guard uses to get the user. Passport probably just gets it from the bearer token
– apokryfos
Mar 21 at 14:24
Okay I got it. Then my question is, am I supposed to change the authentication layer to use the repo instead? In that case, I need to change the passport, right? Because I believe passport use User model instead to authenticate which I think is a violation to repo pattern. Or do you have any suggestion to keep using passport in repo pattern @apokryfos ?
– Rifqi
Mar 21 at 14:30
1
Passport (all guards really) use aUserProviderimplementation that you can specify in the configuration so you would need to write your own user provider which uses the repository rather than the default Laravel one which uses activerecord. the docs have more details on how
– apokryfos
Mar 21 at 14:34
|
show 1 more comment
Policies require user authentication but delegate that authentication to the gate. In your case the gate would be passport so there's no extra layer there. I don't see how the repository pattern changes any of this since that's just an additional layer over the Laravel models which are a different level of abstraction from authentication/authorisation.
– apokryfos
Mar 21 at 14:03
Sorry but maybe I misunderstood what you are saying. Are you saying policies don't require user authentication? If so, yes you are right. Sorry if my sentence up there from the question confused you. But anyway, I'm saying all my policies require user auth to check whether given user has the permission to do given request. Laravel automatically injected User model to the callback function of each policy method which I don't feel that's a right thing for repository pattern, don't you think so? I don't know how do I change the parameter to use UserService which I feel more appropriate @apokryfos
– Rifqi
Mar 21 at 14:22
User policies do require authentication but do not perform authentication. That is done by the guard which in your case is passport. That would not change with this code. The user injection comes from the guard and whatever method the guard uses to get the user. Passport probably just gets it from the bearer token
– apokryfos
Mar 21 at 14:24
Okay I got it. Then my question is, am I supposed to change the authentication layer to use the repo instead? In that case, I need to change the passport, right? Because I believe passport use User model instead to authenticate which I think is a violation to repo pattern. Or do you have any suggestion to keep using passport in repo pattern @apokryfos ?
– Rifqi
Mar 21 at 14:30
1
Passport (all guards really) use aUserProviderimplementation that you can specify in the configuration so you would need to write your own user provider which uses the repository rather than the default Laravel one which uses activerecord. the docs have more details on how
– apokryfos
Mar 21 at 14:34
Policies require user authentication but delegate that authentication to the gate. In your case the gate would be passport so there's no extra layer there. I don't see how the repository pattern changes any of this since that's just an additional layer over the Laravel models which are a different level of abstraction from authentication/authorisation.
– apokryfos
Mar 21 at 14:03
Policies require user authentication but delegate that authentication to the gate. In your case the gate would be passport so there's no extra layer there. I don't see how the repository pattern changes any of this since that's just an additional layer over the Laravel models which are a different level of abstraction from authentication/authorisation.
– apokryfos
Mar 21 at 14:03
Sorry but maybe I misunderstood what you are saying. Are you saying policies don't require user authentication? If so, yes you are right. Sorry if my sentence up there from the question confused you. But anyway, I'm saying all my policies require user auth to check whether given user has the permission to do given request. Laravel automatically injected User model to the callback function of each policy method which I don't feel that's a right thing for repository pattern, don't you think so? I don't know how do I change the parameter to use UserService which I feel more appropriate @apokryfos
– Rifqi
Mar 21 at 14:22
Sorry but maybe I misunderstood what you are saying. Are you saying policies don't require user authentication? If so, yes you are right. Sorry if my sentence up there from the question confused you. But anyway, I'm saying all my policies require user auth to check whether given user has the permission to do given request. Laravel automatically injected User model to the callback function of each policy method which I don't feel that's a right thing for repository pattern, don't you think so? I don't know how do I change the parameter to use UserService which I feel more appropriate @apokryfos
– Rifqi
Mar 21 at 14:22
User policies do require authentication but do not perform authentication. That is done by the guard which in your case is passport. That would not change with this code. The user injection comes from the guard and whatever method the guard uses to get the user. Passport probably just gets it from the bearer token
– apokryfos
Mar 21 at 14:24
User policies do require authentication but do not perform authentication. That is done by the guard which in your case is passport. That would not change with this code. The user injection comes from the guard and whatever method the guard uses to get the user. Passport probably just gets it from the bearer token
– apokryfos
Mar 21 at 14:24
Okay I got it. Then my question is, am I supposed to change the authentication layer to use the repo instead? In that case, I need to change the passport, right? Because I believe passport use User model instead to authenticate which I think is a violation to repo pattern. Or do you have any suggestion to keep using passport in repo pattern @apokryfos ?
– Rifqi
Mar 21 at 14:30
Okay I got it. Then my question is, am I supposed to change the authentication layer to use the repo instead? In that case, I need to change the passport, right? Because I believe passport use User model instead to authenticate which I think is a violation to repo pattern. Or do you have any suggestion to keep using passport in repo pattern @apokryfos ?
– Rifqi
Mar 21 at 14:30
1
1
Passport (all guards really) use a
UserProvider implementation that you can specify in the configuration so you would need to write your own user provider which uses the repository rather than the default Laravel one which uses activerecord. the docs have more details on how– apokryfos
Mar 21 at 14:34
Passport (all guards really) use a
UserProvider implementation that you can specify in the configuration so you would need to write your own user provider which uses the repository rather than the default Laravel one which uses activerecord. the docs have more details on how– apokryfos
Mar 21 at 14:34
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281972%2flaravel-repository-service-pattern-how-to-handle-auth-and-gates%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281972%2flaravel-repository-service-pattern-how-to-handle-auth-and-gates%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Policies require user authentication but delegate that authentication to the gate. In your case the gate would be passport so there's no extra layer there. I don't see how the repository pattern changes any of this since that's just an additional layer over the Laravel models which are a different level of abstraction from authentication/authorisation.
– apokryfos
Mar 21 at 14:03
Sorry but maybe I misunderstood what you are saying. Are you saying policies don't require user authentication? If so, yes you are right. Sorry if my sentence up there from the question confused you. But anyway, I'm saying all my policies require user auth to check whether given user has the permission to do given request. Laravel automatically injected User model to the callback function of each policy method which I don't feel that's a right thing for repository pattern, don't you think so? I don't know how do I change the parameter to use UserService which I feel more appropriate @apokryfos
– Rifqi
Mar 21 at 14:22
User policies do require authentication but do not perform authentication. That is done by the guard which in your case is passport. That would not change with this code. The user injection comes from the guard and whatever method the guard uses to get the user. Passport probably just gets it from the bearer token
– apokryfos
Mar 21 at 14:24
Okay I got it. Then my question is, am I supposed to change the authentication layer to use the repo instead? In that case, I need to change the passport, right? Because I believe passport use User model instead to authenticate which I think is a violation to repo pattern. Or do you have any suggestion to keep using passport in repo pattern @apokryfos ?
– Rifqi
Mar 21 at 14:30
1
Passport (all guards really) use a
UserProviderimplementation that you can specify in the configuration so you would need to write your own user provider which uses the repository rather than the default Laravel one which uses activerecord. the docs have more details on how– apokryfos
Mar 21 at 14:34