Laravel 5.2 custom Middleware Levels on RESTful controllers - laravel-routing

So from not using any framework at all, I finally forced myself to use LARAVEL 5.2 because i got tired of re-writing my own "framework" over and over again.
Anyway!
I think I'm pretty familiar with the Laravel framework and its dependencies now.
But now I need guidance on how to do this the smartest way.
I want to create a Middleware based on the companys DC.
Now every user is AUTHED by php's envget("username"); with a re-written "auth" middleware. So far, so good. (The server is not in the DMZ btw).
The thing is, our team thought that we should populate a database-table with all the users and give them a "Privilege-level", lets say 1 through 3 where 1 is "read only", 2 is "read+modify" and 3 is "read+create+modify" with our restful controllers. But.
since we use restful controllers i cannot really give them individual Middlewares? do I manually have to change every RESTful resource in my routes.php to make this happend? or can i do this another way? I would like to keep it as simple as possible.
Any suggestions?
Thanks in advance
(reserved for typsos )

#Tarre Tan
You can achieve this by adding your middleware in a parent controller's constructor.
All your controllers that require permissions will inherit from that controller.
You have one place to adjust your permissions if you want.
Hope this helps

Related

What does replace cakephp2 datasource in cakephp4?

I'm trying to make API calls to my web hosting (ovh) in order to create email alias and email accounts.
Some years ago I did that in Cakephp2 using Datasource, but now I'm using cakephp4 and I'm not sure how to proceed...
How can I create a Model without a database Table ?
My advice would be to just not go down that route, IMHO you'll be much better off if you create a simple custom service around the API instead, there's no need to make all datasources look like CakePHP database/ORM "repositories" (I put that in quotes because CakePHP's repositories should not be confused with what repositories are in the context of the repository pattern).
If there was really, really, really a need for it, I'd say first go look into muffin/webservice, which shoulders some of the work required for you. And if you wanted to know how to do it completely on your own, well you'd have to implement \Cake\Datasource\RepositoryInterface accordingly, which comes with lots of baggage and is anything but straightforward.
If you want to see how that could look like, I'd again refer to muffin/webservice, check for example its Endpoint repository and the surrounding classes, that should give you an idea of the "how to" and the complexity involved.

Net 5 Domain restricted routes

I'm using Net 5 and I want to set up a new domain on our application.
What I'd like to happen is to be able to create the following routes:
www.domaina.com/aboutus
www.domainb.com/aboutus
I'd like these 2 routes to go to different views.
I'd also like to be able to restrict certain pages e.g.
www.domaina.com/pageA wouldn't be accessible from www.domainb.com/pageA and vice versa.
I've tried to decorate my methods and controllers with the new "Host" attribute but that doesn't seem to retrict them.
I've also tried methods from here to no avail: Domain-based routing in ASP.NET Core 2.0
I should also point out that the plan is to have these behind Azure Front Door, so I need to pull from the header "X-Forwarded-Host". I have logic to do this already in place, but would need something I can customise.
I'm using attribute based routing at the moment.
Any help would be greatly appreciated.
Thanks,
David

Access yii extension controller from another controller

I am working on building API in YII controller, we have a comment module which is an extension . I want to access comment extension's controller so that I can use the same functionality to be accessed for my API instead of creating one.
Can anyone tell me how can i do it?
Actually I am working on making API for my website. Where I have created a controller for API and accessing other controllers for functionality already exists. There's a functionality that exist in comment-module in extensions. And i need to access that Controller in comment-module. Hope this cleared my question asked above. And I need to access this function via Ajax as well, if possible. But should be accessed via API controller.
Specifying in case any confusion. My requirements would be:
accessing methods of comment-module controller in only 1 method in API Controller. In order to make my functionality centeralized.
comment-module should itself keep working.
and work with API controller as well.
Note: Please explain your solutions as well, I am not a genius in yii like you.
Move common functions into treat or inherit controller from extension.

ASP.NET MVC - check Facebook login status

This is more of a design question.
I figured out how to use the facebook login via the Facebook SDK c#.
QUESTION: What is the best way to check whether the user is logged into FB or not each time the user goes to a different page?
Back on ASP.NET webforms, I could simply put in code to check FB login status in the code behind of a master page. This was good ... once and done. But I don't understand how to implement something similar in Asp.Net MVC 4.0.
Given that the _Layout.cshtml file (which acts like a master page) is only a view (hence, no code behind), what is the best way to code a way to check if the user is logged into FB each time a user goes to a different web page? Because I would think, adding this bit of code to each controller can't be the optimal design solution.
The only solution that I can think of involves using Javascript on the client side to do a WebApi call ... I guess the script will be bundled with all the other scripts so that it runs on each page. But I was hoping to find a solution on the server side ...
I'm pretty new to MVC, learning things as I go along ... tips appreciated ... thanks!
I can think of a couple of points that might help you devise a solution.
You can put code in your _Layout, but I agree that you want to be careful about doing so. You could create a helper or partial view and have your _Layout call it so that it's executed for every action. Your helper/partial would need to execute the required logic and then return something. The problem that I have with this is it's a lot of overhead every request.
You could do an AJAX call after the page is loaded (as you suggested). This means that the page still loads quickly. The problem I have with this is that you're now dependant on Javascript. It's also potentially a little hacky(?)
What about storing the user's status (logged on/off) in a session/cookie and also providing a 5 minute expiry. You can use the Helper/Partial method from before or have some logic fire in OnActionExecuting (or similar). Your logic should check to see if the status has expired and then connect to the Facebook API to update the status. This has the advantage of low overhead (i.e. not checking again until 5 minutes has passed).
I don't know of your exact situation so I can't say what method, if any, is best.

Rails 3: routing to customer areas

I'm a Rails 3 beginner, but have experiences with other MVC web frameworks and need a starting hint about how to setup my routing in Rails. The application should allow users to register and after that the users data should be available at URLs like:
http://domainname/username/xyz
The common and user independent part should be available at
http://domainname/abc
To distinguis between both routes, I would force usernames to have at least 6 characters and all "abc"-routes will have 5 or less. Until this point I would be manage the routing by myself, but for the "xyz" part of the user area I would like to use the existing REST full features of rails. Any hint how to do that?
Have you already read http://edgeguides.rubyonrails.org/routing.html?
It's really a good resource for this sort of question. I believe some of the scheme you describe falls into the "Non-Resourceful" routing category.