Getting the Current User - laravel

I am sorry for the dumb question, but I am new at this programming thing and having fun. How do I get the current user in a base Laravel Spark project? I know what the Laravel Spark documentation says, but I can't seem to get the information even with that. I guess I just need an example. Any help would be appreciated. :)

you can access the current authorized user by using the Auth facade. $user = Auth::user(); Make sure to add use Auth; at the top of your file!
Adding on top of this, if you just need something small like the logged-in users's ID, you can say Auth::id()

Related

Laravel 5.8 Manual user approval?

I am new to laravel and before asking this question. I have read various other topics regarding this question sadly none of them helped me.
I have laravel 5.8 and use auth which I installed with:
php artisan make:auth
Now I read that I either need to make a middleware or edit the logincontroller. But what I want is the user to be redirected to a custom page that I already made saying they need to wait to be approved.
Could someone point me in the right direction and tell me which files I need to edit to achieve this?
I have already added a BIT column in the database table called is_approved which is auto set to 0.
In your
App/Htpp/Controllers/Auth
you'll find all the logic of authentication and registration of the user. Inside the controllers, you'll see this:
/**
* Where to redirect users after verification.
*
* #var string
*/
protected $redirectTo = '/home';
Last line is what you're looking for, it's pointing the view/route where your user will be redirected after the respective action.
Also, all your generated views when you run the make:auth command will be in
resources/views/auth
if you feel the need to modify them.
Go here. HomeController. Under return view('home'); change it to
if (Auth::user()->is_approved === "1"){
return view('home');
} else {
return view('welcome');
}
Where 'welcome' you can change to your custom page and '1' assumes the user has been approved. Remember to add use Illuminate\Support\Facades\Auth; to the top of that controller. I had that problem and came to read your post. Thought to drop what I did. It works for me
You need to build a user approval system. A common way is store an active state on the users table as a boolean MySQL column. Then check that column if a user tries to login. As for registration you need to write to the users table with an active state of 0 or false. It would be also great to trigger a notification to you the admin that "hey, you need to go approve a new user". It could be handled through Laravel Nova dashboard where ideally you only need to click a button.
It's not built into the Laravel framework but definitely seems like a good use case!

Laravel limiting access to route

I am trying to implement a basic image fetch system for my website. Already created a route that returns me the image.
what concerns me is that i want that route to be only accessible by certain controllers.
Tried to search it and found out passport might be viable option but it's pretty complex for this app. Are there any possible options ?
EDIT:
Sorry for providing insufficient information. I want the route to be accessible only by CONTROLLERS, not by anyone who enters the route url to address bar. Like using it as an api maybe.
There several ways to achieve that, you can use middleware, you can consider using packages like entrust which also require you to have some knowledge about using middleware. or use laravel Auth
create a table add all the routes in that table and then check the allowed route in AppService provider.
$routename = Request::route()->getName();
$allowed_route = AllowedRoutes::where("route","=",$routename)->count();
if($allowed_route == 0)
exit();

URL Routing: Laravel

I have been working on laravel and have been doing some routing. I was just wondering on what is the difference between writing the route as:
route::get('roles/{id}/edit',rolesController#edit);
versus
route::get('roles/edit/{id}',rolesController#edit);
One difference is clearly visible and that is the placement of the id variable. Can't figure out any other reason. Please provide an explanation on this.
Other than the actual look of the URL, there's no real difference as far as the framework is concerned.
I suppose it's the matter of preference when using any of this. Maybe, for example, if you are giving options of editing the user profile and posts, this might come handy as both are different routes, technically
No difference. It depends on you how you would like to build your routes. But try to user best practices which laravel creator recommend (https://laravel.com/docs/5.7/controllers#resource-controllers).
And also i want take your attention on how you called your controller. You should use CamelCase for naming your files (https://github.com/alexeymezenin/laravel-best-practices/blob/master/README.md).
There's no difference, but you might want to look in reosource routes and controller. Basically, laravel framework automatically creates routes and methods for controllers that you might need in your project. For example:
If you create a contoller like this:
php artisan make:controller RolesController --resource
and create a resource route like this:
Route::resource('/roles', 'RolesController ');
framework automatically crates this routes for you:
Verb Path Action Route Name
GET /roles index roles.index
GET /roles/create create roles.create
POST /roles store roles.store
GET /roles/{roles} show roles.show
GET /roles/{roles}/edit edit roles.edit
PUT|PATCH /roles/{roles} update roles.update
DELETE /roles/{roles} destroy roles.destroy
So you don't have to make your own routes and ask yourself if they are correct or not. Look into laravel official documentation for more info about this.

Is it possible to set content related permissions on cartalyst/sentinel?

i'm new to laravel and cartalyst/sentinel, but for this project i'm facing out an authorization problem:
I have to set User CRUD permissions for the single content, and i'm facing out how to do id with cartalyst/sentinel.
(a lil' example: if i have a blog, i wanna set User CRUD permissions separately for each article).
Can anyone help me to find some documentation about something like this?
I have already implemented this kind of permissions with CakePHP, but is the first time i'm using laravel.
Thank You
The way I think you'd best go about this is by setting the users permission by using the article ID. You could give a user permissions like this: "articles.14.read":1,"articles.24.*":1. 14 & 24 being IDs of articles I made up.
At the last seems noone never has been capable to solve this problem, and i cant test the solution proposed. So i've falled back to the ACL stock support of Laravel 5.0.
Baybe i'll retry when i need the multisession system or other things from sentinel.

Laravel 4 How to use Authority package

I have created model User and table users to store user information
I also have executed the migration script.
I have no idea how to log user in and make authority recognize the user.
I really need tutorial or sample application or anything that could help.
Thanks for reading.
Your help are much appreciated
Unfortunately there are a couple of steps to many to describe here, but just have a look at http://net.tutsplus.com/tutorials/php/authentication-with-laravel-4/.

Resources