Access to Auth or Request in Laravel Model - laravel

I have a shop-store project. This project starts with basic functionality and grow up on each software life-cycle. Recently tendency to deploy some customization for user and analytics grow. I wanted to add favorite capability to project. The project has guest mode which unauthenticated user can see products.
Each user may choose to add a product to his/her favorites(after Auth). so far so good. I decide to append favorite attribute to products.
Trouble comes when i try to return products from controller as response. If i use Auth middleware unauthenticated users redirected to login. If i use custom code, every API with product should be changed accordingly. If i try to append filed in product model, i don't have access to Auth class nor requestin product model.
What's the way to handle such a situation? I appreciate your help.
UPDATE:
There are many models in my project. Order, OrderProduct, Product, User, Payment, Category, Delivery, ....
I want to have a new table named Bookmark which has two column: user_id and product_id.
Product == One Many == Bookmark
User == One Many == Bookmark

You can use the blade #auth directive to check for user authentication.
Then for templating
{{auth()-> user -> bookmark -> all()}}
This should work if you have established a one to may relationship between user and bookmark in your model.

Related

Add invitation functionality to Laravels authentication controller

I am using Laravels normal authentication functionality with RegisterController and the default views that are included. I have modified the RegisterController#create method to create a related model called Home to the User. Although a Home can have several User relations.
After signup, the User automatically have a Home related to them. The user should now be able to invite new users to join their Home. All the logic around the invites are solved, but I was wondering what the best solution for registering the invited user is.
Currently I have created a InviteController with a acceptInvite method that registers the new User related to the existing Home. But I really want to reuse the RegisterController#create method instead of having to maintain two registration processes.
Any ideas?
Your question is not clear... The user you want to invite to HOME are they registered users? if yes
All you need is a many to many relationship using a pivot table.
If the user you want to add to HOME is a new user and not already register on your app, just send them a mail with link to your register page, and let then use the RegisterController#create
Hope this help:)

Multiple sites link to one back end, how to track which site traffic came from in laravel?

I have this web service that I'm trying to offer to multiple businesses of the same genre. They are all individual businesses not affiliated with each other but they can all use one database for submitting info. I basically want to put a link on each separate website that these businesses already have that will point a user to a generic login form that will dump their info into my database and track which company they came from. So if a user goes to Company A and clicks "sign up", i want it to take them to the sign up form but on the back end I'll know to put Company A's credentials with the user affiliation. Likewise if they went to Company B, i'd associate them with Company B. I don't know how to go about doing this in Laravel. Can i generate a custom URL for each company? if so, how do i do that so that i can track the company info? Side note: every company who uses this would also have a company profile with all their info already in my system. Could i create a database column for "company URL" and throw an if statement saying "if url==company A, do 'this'"? I just need a good direction to start in. thanks in advance.
You can add a header to the request going to Laravel says which company the request came from
Company model has many users and User model belongs to company change your routes of Login and Register Controllers to add company on your requests
https://laravel.com/docs/5.6/eloquent-relationships#one-to-many

Laravel Multi-auth (multiple user tables) from same login

I plan to develop a system that has three user types (admin, business, personal). I want to have each user types information stored in a separate table for easy access and to reduce the number of blank fields (if they were all in one table).
Having looked at multiple Multi-auth packages available for Laravel, they all appear to be insisting on an approach with URLs like the following:
/admin/login
/business/login
/personal/login
Ideally, I would like to take an approach where the standard Laravel Auth /login can be used meaning that all users log in from the same page.
My knowledge of Laravel is limited so all and any help is appreciated.
The usual approach is to have one users table which has the ID/email/username (as primary key), login credentials and user types for all the users which will be logging into the system. The information however can be stored in separate tables for each type, referencing the foreign key.
After authenticating the user, then you can decide what to do with the user based on the user type.
If you store login credentials in multiple tables, that's data redundancy. Unless you want the same email/username to have more than one user type, but then during login, the user has to decide which user type they want to log into (maybe by selecting a dropdown option).
Update: about user roles
If you need to simply redirect users after logging in, use $redirectTo. Or if you need to decide what to do with the users depending on the roles after logging, you can make use of authenticated() method (add this method if it's not already there, it will overwrite the AuthenticatesUsers trait) inside your AuthController/LoginController.
Throughout your application, I'd suggest assigning middleware to route groups to restrict routes based on user roles. And yes, in your views and controllers you can use something like if(Auth::user()->hasRole('business')) when needed, but first you'll need to create the hasRole() method in your User model. If you feel it's getting complicated, you may try packages like laravel-permission and Entrust. I haven't tried them though :)

Where to put logic for auto-login and creating members

Im new to Umbraco development, but im plenty familiar with ASP.Net & MVC etc. So Im getting to grips with the object model and terminology used, but Im not sure where to start. I need to use windows authentication on my Umbraco site, which will be for internal use only.
What I envision:
- When a domain user hits any area of the website, grab the user identity
- Lookup to see if matching user(or member) exists and if not create it
- Login this user to Umbraco
- By default all new visitors, if their user identity doesnt match a current member, then create that member and log them in.
Sounds like I need to create my own controller that overrides the base controller (RenderMvcController ?) and check the user identity on each and every request? Maybe do this by overriding the Index action method? Or could I do this with a macro - or as ive seen mentioned, are macros loosing favor with the new version of Umbraco?
Also, Im not sure how to deal with members vs users? As I understand it, members are who have access to the front part of the website, whereas users are those that have access to the back office area and can create/manage content.
Are all users also members?
There will be some that I want to give access to create/manage content, so when Im auto-creating users, its actually members that I need to create, not users?
[ update ]
Actually, I think I will need to create my own membership provider if I want every request routed through the check for a valid domain user? In my research, I keep coming across this example http://thegrayzone.co.uk/blog/2012/07/combined-authentication-with-umbraco/
I have overridden the default RenderMvcController in numerous projects with success, you could of course use the built in Umbraco auth to redirect to an authentication page for users that do not have a valid Umbraco Auth token and set it only only on that page based on their windows identity.
RE: Are users also members?
No. Users & Members are entirely independent of one another; users being back office users & members being front end users. You will need to create 2 accounts.

Varied registration form for different Joomla user types

I have two user groups: customers and vendors. At the time of basic registration I have a radio button which allows me to put a new user into the respective user groups.
Once the user (customer or vendor) logs in I need one additional registration form for each of them so that (s)he can give specific details such as a customer will have to give vehicle details, while a vendor will have to give his/her bank details.
So how do I get/create two different additional registration forms after login? I am using Joomla 2.5.
Unless you're going to use a community component of some kind like CB or JomSocial, I believe you'll have to write a custom profile plugin to make the distinction yourself.
The Joomla Extension Directory for Communities has a long list of possibilities that you can review to see which best suits your needs.

Resources