Laravel Custom Auth - php

Here i do the Login Validation
$LoginData = Input::except(array('_token')) ;
if(Auth::attempt($LoginData))
{
return 'success';
}
My Table is different so, here i change the table name in auth.php
'table' => 'administrators'
But I have the dropdown to choose for the usertype.
So how can i choose the tables for Authentication according to the usertypeinput.
i.e., Table may be administrators or parents or employees

I don't know whether Laravel supports the change of auth table name on fly or not. I can suggest you a quick solution.
According to generalization theory of database design, you should store same type of information to one entity set. And according to specialization theory, if entity set can have various types of information for each entity, break them down into sub entity sets.
Suggestion:
Create user_types table with column id & name (store user type names here)
Create table users with 4 columns id, email/username, password & user_type_id (user_type_id is foreign key references id of user_types)
Create 3 separate tables named administrators, parents or employees. All 3 tables should have one column user_id which is a foreign key references users table.
Create relationship in model
After a user login, you can determine which type of user he/she is from the user<->user_type relation
You can get rid of the usertype dropdown from login page view now (You were disclosing some important information (3 user types) about your application to the whole planet, isn't it harmful for your application?)
More info about generalization & specialization:
Generalization, Specialization and Aggregation

Related

I have question regarding SQL and phpmyadmin queries

I have two table in my database first is shop_details and second one is shopOwner_login
in shop_details table there is column named
ShopOwner_email
Shop_Pass
and same in shopOwner_login table there is
ShopOwner_Id,
ShopOwner_email,
Shop_Pass
Now i want to make a form in which if user enter data in shop details table like shopowner Email and its password it will store on those two table automatically goes
Can i do this if yes please help me for it i also tried foreign key i am so confused with foreign key because i am new to php and MySql please help me.
Each of your table should have Primary key. Primary Key is the unique id (could be number or string or even uuid. Ex: ShopOwner_Id) which could be used to fetch any row in the table. This primary key is used to establish relationships between tables.
Also for relationships there could be 3 types of major relationships between two tables.
One to One
One to Many or Many to One
Many to Many
For example,
lets say you have two tables UserLogin and UserProfile. UserLogin contains -> id, email, password, and verified. Whereas UserProfile contains -> id name, address, mobile, dob, etc. Here Each UserLogin will have single UserProfile whereas each UserProfile will have only one UserLogin. So they have One to One relationship. In this case, You add the foreign key to the both tables. You will add profile_id as foreign key in UserLogin whereas login_id in UserProfile.
Lets say, you have two tables Shop and User. Where each Shop will belong to a single User (in your case shopowner) But a User can have multiple Shops. In this case, Shop and User have Many to One relationship (or User and Shop have One to Many relationship). In this case we add a foreign key of user_id (which is primary key of User table) to Shop table.
In your case I will suggest to only keep email and password in shopOwner_login table and add its foreign key to shop_details table. This way your data will be normalize and you will not have to make sure to maintain same data in multiple tables.
So,d query need to fetch the data looks like - SELECT a.ShopOwner_email Shop_Pass as EmailId,a.Shop_Pass as Password FROM shop_details as a left join shopOwner_login as b on b.ShopOwner_Id = a.ShopOwner_Id

how to DB design in mysql for multi user but diffrent data

how to DB design in mysql for multi user with different each user data.
For Ex. I have a 4 type of user with different data required for insert user profile, including user name and password.
Approach 1 :
In the table containing user profiles, have a user-type field as well.
Approach 2 :
Have a usertypes master table, with a usertypeid. And have a usertypeid field in the users table to indicate which type a user is. If a user can belong to multiple types, then you may either
(a) need to have a separate table to link userids and usertypeids,
or
(b)have the usertypeid field have a comma separated list of values of usertypeids to which a user belows.

implementing user login MySQL

i have three tables to store details of different types of uses: customers, suppliers, staff
here are the structures of them,
customer(id,f_name,....)
supplier(id, name, address....)
employee(id, name, job_title....)
now i need those to log-in to the system , the log-in details stored in separate table,
user(user_id, password, role, ref_id)
how i planed to work this is, when registering some one, firstly insert the record to customer, supplier or employee according to the person, then insert a record in to the USER table in which the "ref_id" is the id of the previous table. the user is provided the "user_id" which can not be changed and they can change their password themselves.
when log-in, check the user_id, password combination, if ok then takes the ref_id and type, the appropriate table can be determined by the type which may be customer, supplier or employee....
the reason i done this in above way is,
customer, supplier and employee table has many different attributes except few like id, name...so can not maintain all the data in one table. in this situation if we use ids of customer, supplier and employee..would provide duplicate ids because they are separate tables!
so i need to know,
Is it correct the way i have implemented the authentication ?
if it isn't what is the correct way? (please mentioned that the details of the three parties should be handled separately)
i need to define relationship between supplier, customer, employee --> with user table. so is it ok to define three relationship as follows or another solution, how if the user table keep alone without relationship? is it violate the relational database concept?
customer (id-pk) ---->user (ref_id-fk)
supplier (id-pk) ---->user (ref_id-fk)
employee (id-pk) ---->user (ref_id-fk)

Mysql design. Two types of users, two different profiles

I want to design a DB which will be connected to PHP Application. In the app there are two types of users: company and person. Some functionality like adding articles will be done by both so in other tables there are author_id columns. So firstly I decided to create user column.
That's easy: id, username, password, role, active, created where role defines whether user is person or company.
Now I want to add profile table or profile tables depends on what you'd suggest (joined with the previous table by adding profile_id column there).
Both roles have different fields, which are required during registration.
The easiest thing would be to create one table with all required fields for both roles, allow them NULL values and in the PHP app (made in Yii Framework in this case) define requirements for each role in models.
The nicest thing would be to create separate tables for both roles BUT the questions is how to connect these two tables to one table using Foreign Key? Is it even possible. I know I may omit foreign key creation then based on role choose table, and from that table choose profile_id.
Or maybe you have another solution to my problem.
Thanks in advance for replies.
Adrian
You need an intermediary between the page and the database to assign the user to a group that has specific privileges. It's usually accomplished with a user-group-role design.
You can have a table for users system info (username , pass ...), and another for users profile (firstname , birthday ...), and another for groups(superuser , ...).
where user table can have multiple groups: user:one->group:many
user can have one profile user:one->profile:one
I think this is a decent solution.

teachers students database design

Let's say you have students
and you have teachers
Students log in to the site using a different interface than teachers
Students can do the following on the site
- Look up grades
- Email teachers
Teachers can do the following on the site
- Lookup up students
- Input grades
How would you design the database table to allow teachers and students to log in? This question confuses me because I was thinking of doing the following
of having a teacher table
of having a student table
two separate tables
so
create table teacher
(
name varchar(255),
email varchar(100),
password varchar(100)
)
create table student
(
name varchar(255),
email varchar(100),
password varchar(100)
)
Is that how it is usually done? When you have two different entities logging in to two different login interfaces?
--- EDIT ---
also what if the two entities have different fields? Would you still create one Users table with a "Role" in that case.
For example, what if you have the following:
(notice how teacher and student have a few fields different. Would you still create one table to put them both??)
create table teacher
(
name varchar(255),
email varchar(100),
password varchar(100)
num_of_students int,
)
create table student
(
name varchar(255),
email varchar(100),
password varchar(100),
gpa decimal(10,2)
)
I would consider just making it one table and add a TypeID, where TypeID would classify the type of person be it a student or a teacher.
Take a look at the design of both of those tables, they are exactly the same, this should be a signal to you that you may be overdoing things.
By keeping it in 2 separate tables, it is more UI work for you and more administrative work for the person entering the data.
By combining it into one table and introducing a classification or type field you eliminate all that extra work.
CREATE TABLE Users ( ID int, Type int, name varchar(255), email varchar(100))
I also would not store the password as plain text, as it appears you may be hinting at with password varchar(100). Type in this case could be an int or even a bit datatype if the only two types of personnel will be Students and Teachers. The field could be IsTeacher with 1 being a teacher and 0 being a student. But in most cases, you end up realizing you needed a different type of User, hence the int datatype.
Designs in most other answers here lack when a user have to be assigned with two "types" or more. In fact, the question is very obviously about users associated with roles which are thenselves associated with rights. And therefore a typical table layout for role-based security is the best in my opinion.
Entities and Relationship
User: John Q. Public, ...
Role: Teacher, Student, ...
Right: Look up grades, Input grades, Look up students, ...
You've N:M relationship between each:
User<->Role
Role<->Right (optional)
User<->Right (optional)
Maybe checking for roles is enough and you can drop anything with Rights here.
Solution
Just have a User table. (Table: User)
Teacher and Student are Roles which Users can be associated with. (Tables: Role and User_Role)
Another Role you typically have in every application is the Role Admin which for instance can delete other users, reset passwords, create new users ...
When you've complex rights then you can add Right, too. Right can be both associated with User as well as with Role.
I would have a single login table of an ID, email, password as you have, but tack on a flag for TYPE of login... Admin (Site wide), Teacher, Student, whatever.
Then, from the login, you'll have that "type" as a flag to show/hide elments within the site or offer other redirection as needed.
I would make one table - person and add field type to it. Type can be STUDENT or TEACHER for example.
What I would do is have a single table called lets say 'users', then a column called 'role' for example, I would then create a script in each interface that will only allow students to login the student interface and teachers in the teachers interface.
To be honest I would actually only have a single permission based user interface, but that is what I would do if I were to use 2 interfaces.
I hope this helps you.

Categories