• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

GhazaliTajuddin.com

Another Kuantan Blogger

  • Home
  • Blog
    • Kuantan
    • Foods
    • Technology
    • Health
  • Stock Photography
    • Senarai Microstock Agency
    • Membuka akaun contributor Shutterstock.com
    • Tips untuk 10 keping gambar pertama Shutterstock.com
    • Mengapa Shutterstock.com reject gambar / submission
    • Model Release
    • Bagaimana withdraw earning daripada Fotolia
    • Bagaimana untuk mengisi keyword kepada imej dengan menggunakan Adobe Photoshop

Authentication

Laravel Middleware

January 17, 2017 by ghazalitajuddin Leave a Comment

Middleware is a class that have special function in Laravel.

Middleware placed in between the User and  Apps. 

Before you can access the Apps, Middleware will do his job first.

We can use Middleware to do several thing

  • Do you authenticated?
  • What is your role?
  • Do you authorized?
  • Can you perform this action?
  • etc
class MemberMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        //1. User must be authenticated
        //2. User must should be a "member"
        

        if(Sentinel::check() && Sentinel::getUser()->roles()->first()->slug == 'member')

            return $next($request);

        else

            return redirect('/login')->withErrors('Please login to access this area.');
        

        
    }
}

Another example

class ProfileMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        1. Get Profile Id
        2. Get Profile User Id
        3. User must own the Profile

        //$ProfileId = $request->segments()[1]; //boleh pakai
        //$profileId= Profile::find($this->route()->parameter('profileShow'));
        
        $ProfileId = $request->route()->parameter('id'); //boleh pakai
        $profile = Profile::findOrFail($ProfileId);

        if ($profile->user_id !== Sentinel::getUser()->id) {
            
           // dd($request->user()->id);
           // abort(403, 'Unauthorized action.');
              // return redirect('/profile')->withError('Permission Denied');
            return redirect()->back()->withErrors('Permission Denied!');
            }

        return $next($request);
        
    }
}

Another finest!

class ProfileMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        //get User Id
        $UserId=$request->id;

        //get User Profile
        $profile=Profile::whereUserId($UserId)->first();

        if ($profile->user_id !== Sentinel::getUser()->id) {         
            return redirect()->back()->withErrors('Permission Denied!');
            }

        return $next($request);
        
    }
}

 

Filed Under: Technology Tagged With: Authentication, Authorization, laravel, middleware, sentinel

Laravel 5.3 Authentication

November 22, 2016 by ghazalitajuddin Leave a Comment

Antara perkara penting dalam membangunkan applikasi adalah sistem pengurusan akses maklumat atau pun authentication & authorization.

Laravel telah memudahkan kerja kita dengan menyediakan scaffolding authentication nya tersendiri.

Anda hanya perlu install sahaja.

Tetapi untuk install sistem authentication ini ia perlu dilaksanakan pada fresh setup SAHAJA.

Execute code berikut untuk install Auth

php artisan make:auth

untuk migrate database

php artisan migrate

Dah siap!

Sekarang anda boleh ke http://domainanda/register/.

Jika anda buka file route web.php. Anda akan lihat ada panggilan method Auth::routes(). Apakah kegunaan Auth:routes() ini sebenarnya? Apa fungsi terkandung dalamnya? Apa route yang telah diistiharkan?

Ia sebenarnya adalah helper yang telah disediakan oleh Laravel.

Routes berkenaan boleh didapati disini

/vendor/laravel/framework/src/Illuminate/Routing/Router.php

Ia kelihatan sebegini

public function auth()
{
// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');

// Registration Routes...
$this->get('register', 'Auth\RegisterController@showRegistrationForm');
$this->post('register', 'Auth\RegisterController@register');

// Password Reset Routes...
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm');
$this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm');
$this->post('password/reset', 'Auth\ResetPasswordController@reset');
}

 

 

 

 

Resource:

  • https://laracasts.com/discuss/channels/laravel/laravel-53-routing-as-authroutes?page=1
  • http://stackoverflow.com/questions/39196968/laravel-5-3-new-authroutes/39197278#39197278
  • https://github.com/laravel/framework/blob/5.3/src/Illuminate/Routing/Router.php

 

 

Filed Under: Technology Tagged With: 5.3, auth, Auth::routes(), Authentication, laravel

Understand Yii Authentication Intermediate

March 30, 2012 by ghazalitajuddin 6 Comments

Yii Framework
Yii Framework

In this tutorial we will discuss about setting up our Yii application

  • Our Web Apps will have user registration page
  • Our Web Apps will varified user registration trough emails activation

The first thing is we need to look into our User table or our User model. Im setting up my tbl_user as below. Im also insert sample record :

[php]

—
— Table structure for table `tbl_user`
—

CREATE TABLE IF NOT EXISTS `tbl_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`salt` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`joined` date NOT NULL,
`activationcode` int(11) NOT NULL,
`activationstatus` int(11) NOT NULL,
`profile` text COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=11 ;

—
— Dumping data for table `tbl_user`
—

INSERT INTO `tbl_user` (`id`, `username`, `password`, `salt`, `email`, `joined`, `activationcode`, `activationstatus`, `profile`) VALUES
(1, ‘demo’, ‘2e5c7db760a33498023813489cfadc0b’, ’28b206548469ce62182048fd9cf91760′, ‘webmaster@example.com’, ‘0000-00-00’, 0, 0, NULL);

[/php]

[Read more…] about Understand Yii Authentication Intermediate

Filed Under: General, Technology Tagged With: Authentication, Controller, Framework, Kuantan, kuantan programmer, kuantan software developer, kuantan webmaster, Login, model, MVC, OOP, PHP, RBAC, View, Widget, wordpress, yii, Yii Framework

Understanding Yii Basic User Authentication

March 26, 2012 by ghazalitajuddin 1 Comment

Yii Framework
Yii Framework

Basicly, Yii basic skeleton already come with aunthentication system which is very simple by checking username and password both admin or demo.

The default authentication files is

  1. UserIdentity.php
  2. LoginForm.php
  3. SiteController.php
  4. Login.php
[Read more…] about Understanding Yii Basic User Authentication

Filed Under: General, Technology Tagged With: Authentication, CakePHP, Controller, Framework, Kuantan, kuantan programmer, kuantan software developer, kuantan webmaster, Login, model, MVC, OOP, PHP, View, wordpress, yii, Yii Framework

Primary Sidebar

“Solat. Sabar. Syukur. Senyum. Sedekah.”

For Collaboration, Events & Review, kindly contact me at +6016[-]9212092 or click Whatsapp button on this page.

Sponsor

Recent Posts

BadMethodCallException Method Illuminate\Database\Eloquent\Collection::roles does not exist.

User Roles And Permissions Without Package Laravel 10

Laravel Many To Many Relationship

Makan malam bersama keluarga di Awangan Palace

Sarapan pagi di Warung Gulai Kawah

Recent Comments

  • helmi on Personal Tips Berhenti Merokok
  • ghazalitajuddin on Personal Tips Berhenti Merokok
  • helmi on Personal Tips Berhenti Merokok
  • ghazalitajuddin on Nasi Lemak Kukus Restoran Zaman. Otai masih berbisa.
  • ghazalitajuddin on Air tangki radiator Proton Exora cepat kering? Cuba tukar penutup radiator!
  • Mal on Nasi Lemak Kukus Restoran Zaman. Otai masih berbisa.
  • Firdaus on Air tangki radiator Proton Exora cepat kering? Cuba tukar penutup radiator!

My Link

  • Takaful Insurance Web

JJCM

Minuman Diet

Coke Coffee Caramel ~ Not recommended!

Kuih Keria Viral di Kuantan???

Lain macam Nasi Kerabu Mekla ni

7 Lokasi Nasi Dagang Terbaik di Kuantan

Tags

bebas rokok berhenti merokok breakfast Controller Framework Gezzeg Photography & Design health jalan-jalan cari makan jalan-jalan cari makan kuantan jjcm jjcm kuantan Jurufoto Kuantan Kuantan Kuantan Photographer kuantan programmer kuantan web developer kuantan webmaster laravel merokok merbahayakan kesihatan model MVC nikmat rokok OOP Pahang Pahangtourism pahang tourism Photo Manipulation PHP rajalanun retired smoking revisit pahang 2018 shutterstock stop smoking stop smoking tips stop smoking withdrawal symptom tips tips berhenti merokok View visit malaysia 2020 visit pahang visitpahang white wordpress yii Yii Framework

Recent Posts

  • BadMethodCallException Method Illuminate\Database\Eloquent\Collection::roles does not exist.
  • User Roles And Permissions Without Package Laravel 10
  • Laravel Many To Many Relationship
  • Makan malam bersama keluarga di Awangan Palace
  • Sarapan pagi di Warung Gulai Kawah

Copyright © 2025 — Ghazali Tajuddin • All rights reserved. •