Laravel authentication and security

Source: Internet
Author: User

Creating the user model
First of all, we need to define the model that is going to be used to represent
Users of our application. laravel already provides you with sensible defaults inside
APP/config/auth. php, where you change the model or table that is used to store
Your user accounts.

It also comes with an existing user model inside APP/models/user. php. For
Purposes of this application, we are going to simplify it slightly, remove certain
Class variables, and add new methods so that it can interact with the cat model:

use Illuminate\Auth\UserInterface;class User extends Eloquent implements UserInterface {public function getAuthIdentifier() {return $this->getKey();}public function getAuthPassword() {return $this->password;}public function cats(){return $this->hasMany(‘Cat‘);}public function owns(Cat $cat){return $this->id == $cat->owner;}public function canEdit(Cat $cat){return $this->is_admin or $this->owns($cat);}}

Remember that an interface does not give any implementation details. It is nothing
More than a contract that specifies the names of the methods that a class shocould
Define when it implements the interface, in this case, getauthidentifier ()
And getauthpassword (). These methods are used internally by laravel when
Authenticating a user. The next method, cats (), simply defines the has alias
Relationship with the cat model. The last two methods will be used to check
Whether a given cat instance is owned or editable by the current user instance.

 

Creating the necessary database schema
Now that we have defined a user model, we need to create the database schema
For it and alter the existing cats table to add information about the owner. Start
Creating a new migration:

$ PHP artisan migrate: Make create_users
And then define the UP method with the necessary database columns:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.