- Gii – Generating Module
- Gii – Generating Controller
- Gii – Creating a Model
- Yii - Gii
- Yii - Localization
- Yii - Authorization
- Yii - Authentication
- Yii - Error Handling
- Yii - Logging
- Yii - Aliases
- Yii - Fragment Caching
- Yii - Caching
- Yii - Testing
- Yii - Fields
- Yii - RESTful APIs in Action
- Yii - RESTful APIs
- Yii - Theming
- Yii - Database Migration
- Yii - Active Record
- Yii - Query Builder
- Yii - Data Access Objects
- Yii - Database Access
- Yii - Dependency Injection
- Yii - Configurations
- Yii - Creating a Behavior
- Yii - Behaviors
- Yii - Creating Event
- Yii - Events
- Yii - GridView Widget
- Yii - ListView Widget
- Yii - Data Widgets
- Yii - Data Providers
- Yii - Properties
- Yii - Sorting
- Yii - Pagination
- Yii - Formatting
- Yii - Files Upload
- Yii - Using Cookies
- Yii - Cookies
- Yii - Using Flash Data
- Yii - Sessions
- Yii - AJAX Validation
- Yii - Ad Hoc Validation
- Yii - Validation
- Yii - HTML Forms
- Yii - Rules of URL
- Yii - URL Routing
- Yii - URL Formats
- Yii - Responses
- Yii - HTTP Requests
- Yii - Creating Extensions
- Yii - Extensions
- Yii - Asset Conversion
- Yii - Assets
- Yii - Layouts
- Yii - Views
- Yii - Modules
- Yii - Widgets
- Yii - Models
- Yii - Using Actions
- Yii - Using Controllers
- Yii - Controllers
- Yii - Entry Scripts
- Yii - Application Structure
- Yii - Create Page
- Yii - Installation
- Yii - Overview
- Yii - Home
Yii Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Yii - Authorization
The process of verifying that a user has enough permission to do something is called authorization. Yii provides an ACF (Access Control Filter), an authorization method implemented as yiifiltersAccessControl. Modify the behaviors() function of the SiteController −
pubpc function behaviors() { return [ access => [ class => AccessControl::className(), only => [ about , contact ], rules => [ [ allow => true, actions => [ about ], roles => [ ? ], ], [ allow => true, actions => [ contact , about ], roles => [ @ ], ], ], ], ]; }
In the above code, ACF is attached as a behavior. The only property specifies that the ACF should be appped only to the about and contact actions. All other actions are not subjected to the access control. The rules property psts the access rules. All guests (with the “?” role) will be allowed to access the about action. All authenticated users(with the “@” role) will be allowed to access the contact and about actions.
If you go to the URL http://localhost:8080/index.php?r=site/about, you will see the page, but if you open the URL http://localhost:8080/index.php?r=site/contact, you will be redirected to the login page because only authenticated users can access the contact action.
Access rules support many options −
allow − Defines whether this is an "allow" or "deny" rule.
actions − Defines which actions this rule matches.
controllers − Defines which controllers this rule matches.
roles − Defines user roles that this rule matches. Two special roles are recognized −
? − matches a guest user.
@ − matches an authenticated user.
ips − Defines IP addresses this rule matches.
verbs − Defines which request method (POST, GET, PUT, etc.) this rule matches.
matchCallback − Defines a PHP callable function that should be called to check if this rule should be appped.
denyCallback − Defines a PHP callable function that should be called when this rule will deny the access.
Passwords
Step 1 − Yii provides the following handy methods for working with passwords.
pubpc function actionAuth() { $password = "asd%#G3"; //generates password hasg $hash = Yii::$app->getSecurity()->generatePasswordHash($password); var_dump($hash); //vapdates password hash if (Yii::$app->getSecurity()->vapdatePassword($password, $hash)) { echo "correct password"; } else { echo "incorrect password"; } //generate a token $key = Yii::$app->getSecurity()->generateRandomString(); var_dump($key); //encrypt data with a secret key $encryptedData = Yii::$app->getSecurity()->encryptByPassword("mydata", $key); var_dump($encryptedData); //decrypt data with a secret key $data = Yii::$app->getSecurity()->decryptByPassword($encryptedData, $key); var_dump($data); //hash data with a secret key $data = Yii::$app->getSecurity()->hashData("mygenuinedata", $key); var_dump($data); //vapdate data with a secret key $data = Yii::$app->getSecurity()->vapdateData($data, $key); var_dump($data); }
Step 2 − Enter the URL http://localhost:8080/index.php?r=site/auth, you will see the following.
Advertisements