• 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

sentinel

Sentinel::Register() Vs Sentinel::RegisterAndActivate()

January 17, 2017 by ghazalitajuddin Leave a Comment

Sentinel::RegisterAndActivate()

       //Register the $request user and activate
	if(Sentinel::registerAndActivate($request->All())){

			//Search the new registered user
			$user = User::whereEmail($request->get('email'))->firstOrFail();

			//Bind the user to Activation Table
			$activation=Activation::create($userSentinel);

			//Assign A Profile To The New Registered User 
			$userProfile = new Profile();
			$userProfile->user_id = $user->id;
			$userProfile->smoked = $request->smoked;
			$userProfile->lat = 0.0;
			$userProfile->lng = 0.0;

			$user->profile()->save($userProfile);
			
			//Find role
			$role=Sentinel::findRoleBySlug('member')->firstOrFail();

			//Attach the user to the Role
			$role->users()->attach($user);


			return redirect()->back()->with('status','Registration Successful !');
		}else{
			return redirect()->back()->with('error','Registration Failed !');
			//return back()->withErrors($validator);
		}

Sentinel::Register()

                if($userSentinel = Sentinel::register($request->All())){

			//Search the new registered user
			$user = User::whereEmail($request->get('email'))->firstOrFail();

			//Bind the user to Activation Table
			$activation=Activation::create($userSentinel);

			//Assign A Profile To The New Registered User 
			$userProfile = new Profile();
			$userProfile->user_id = $user->id;
			$userProfile->smoked = $request->smoked;
			$userProfile->lat = 0.0;
			$userProfile->lng = 0.0;

			$user->profile()->save($userProfile);
			
			//Find role
			$role=Sentinel::findRoleBySlug('member')->firstOrFail();

			//Attach the user to the Role
			$role->users()->attach($user);

			return redirect()->back()->with('status','Registration Successful !');
		}else{
			return redirect()->back()->with('error','Registration Failed !');
			//return back()->withErrors($validator);
		}

 

Filed Under: General Tagged With: Register, RegisterAndActivate, sentinel

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 Handling Exceptions

January 17, 2017 by ghazalitajuddin Leave a Comment

This is examples how Laravels handle exceptions.

try{
..
.. Business Logic ..
..
}
catch(ThrottlingException $e){
$delay = $e->getDelay();
return redirect()->response->withErrors("You are banned for $delay seconds.");
}
catch(NotActivatedException $e){
return redirect()->response->withErrors("You account is not activated.");
}

Not to forget at the top of the class

use Cartalyst\Sentinel\Checkpoints\ThrottlingException;
use Cartalyst\Sentinel\Checkpoints\NotActivatedException;

Filed Under: General Tagged With: exceptions, laravel, NotActivatedException, sentinel, ThrottlingException

Laravel Logout Using Post Method

January 13, 2017 by ghazalitajuddin Leave a Comment

In terms of security, it is not wise to use GET method to logout from your applications.

To make more secure, use POST method.

<li> 
<a href="{{ url('/logout') }}" 
onclick="event.preventDefault(); 
document.getElementById('logout-form').submit();"> 

Logout 

</a> 

<form id="logout-form" action="{{ url('/logout') }}" 
method="POST" style="display: none;"> 

{{ csrf_field() }} 
</form> 

</li>

In your controller

public function logout()
{
if(Sentinel::logout()){
return redirect('/');
}
}

 

Filed Under: Kuantan Web Developer, Technology Tagged With: laravel, logout, method, post logout, secure, sentinel

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

Meraikan hari lahir di Loteng

Lunch di Nasi Kukus Alom

Kuih Keria Viral di Kuantan???

Day Trip Kuantan – Kuala Gandah – Temerloh – Kuantan

Daging Bakar Daun Pisang Pekan

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. •