Skip to content

Commit a877edd

Browse files
First commit
0 parents  commit a877edd

File tree

8 files changed

+363
-0
lines changed

8 files changed

+363
-0
lines changed

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Laravel User types - Admin/User
2+
3+
This package create user types like Simple user, Admin and Super Admin.
4+
5+
## Installation
6+
7+
You can install the package via composer:
8+
9+
``` bash
10+
composer require mohit/usertype
11+
php artisan vendor:publish --provider="mohit\usertype\UsertypeProvider"
12+
php artisan migrate
13+
```
14+
## Create Admin User
15+
16+
``` bash
17+
php artisan create:admin
18+
```
19+
It will ask you to enter name, email and password. Then you can login with entered email and password.
20+
21+
## Create Super Admin User
22+
23+
Super Admin user have roles of accessing admin pages as well as user pages.
24+
25+
``` bash
26+
php artisan create:super
27+
```
28+
It will ask you to enter name, email and password. Then you can login with entered email and password.
29+
30+
## Usage
31+
This package creates and registeres new middleware which you can use in routes file. You can assign middleware to either group of routes or individual routes.
32+
33+
For admin,
34+
35+
```php
36+
Route::group(['middleware' => 'authorize:admin'], function(){
37+
//
38+
});
39+
```
40+
For user,
41+
42+
```php
43+
Route::group(['middleware' => 'authorize:user'], function(){
44+
//
45+
});
46+
```
47+
For super admin,
48+
49+
```php
50+
Route::group(['middleware' => 'authorize:admin|user'], function(){
51+
//
52+
});
53+
```
54+
55+
## Using In Blades
56+
57+
```php
58+
@if(auth()->user()->type == 'admin')
59+
//
60+
@endif
61+
```
62+
63+
## Styling Unauthorized page
64+
65+
When any user try to access page for which he is not authorized then it will redirect him to unauthorized page. This page follow bootstrap structure. You can style this page by creating new css file under 'public/css/unauthorized.css'.

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "mohit/usertype",
3+
"description": "Laravel User types - Admin/User",
4+
"keywords": [
5+
"mohit",
6+
"laravel",
7+
"type",
8+
"acl",
9+
"permission"
10+
],
11+
"authors": [
12+
{
13+
"name": "Mohit Mehta",
14+
"email": "mehtamohit80@gmail.com"
15+
}
16+
],
17+
"require": {
18+
"php": "^7.0",
19+
"illuminate/database": "5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*",
20+
"illuminate/support": "5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "^6.4"
24+
},
25+
"minimum-stability": "dev",
26+
"extra": {
27+
"laravel": {
28+
"providers": [
29+
"mohit\\usertype\\UsertypeProvider"
30+
]
31+
}
32+
},
33+
"autoload": {
34+
"psr-4": {
35+
"mohit\\usertype\\": "src/"
36+
}
37+
}
38+
}

src/Commands/CreateAdmin.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Mohit\Usertype\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use App\User;
7+
use Illuminate\Support\Facades\Hash;
8+
9+
10+
class CreateAdmin extends Command
11+
{
12+
protected $signature = 'create:admin';
13+
14+
protected $description = 'Create admin user';
15+
16+
public function handle()
17+
{
18+
$name = $this->ask('Name');
19+
$email = $this->ask('Email');
20+
$password = $this->secret('Password');
21+
22+
$check = User::where('email',$email)->first();
23+
if($check){
24+
$this->error('Email already exist !!');
25+
}else{
26+
$user = new User;
27+
$user->name = $name;
28+
$user->email = $email;
29+
$user->password = Hash::make($password);
30+
$user->type = 'admin';
31+
$user->save();
32+
$this->info('Admin user successfully created !!');
33+
}
34+
}
35+
}

src/Commands/CreateSuperAdmin.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Mohit\Usertype\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use App\User;
7+
use Illuminate\Support\Facades\Hash;
8+
9+
class CreateSuperAdmin extends Command
10+
{
11+
protected $signature = 'create:super';
12+
13+
protected $description = 'Create super admin user';
14+
15+
public function handle()
16+
{
17+
$name = $this->ask('Name');
18+
$email = $this->ask('Email');
19+
$password = $this->secret('Password');
20+
21+
$check = User::where('email',$email)->first();
22+
if($check){
23+
$this->error('Email already exist !!');
24+
}else{
25+
$user = new User;
26+
$user->name = $name;
27+
$user->email = $email;
28+
$user->password = Hash::make($password);
29+
$user->type = 'admin|user';
30+
$user->save();
31+
$this->info('Super admin user successfully created !!');
32+
}
33+
}
34+
}

src/Middlewares/CheckPermission.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Mohit\Usertype\Middlewares;
4+
5+
use Closure;
6+
use Illuminate\Http\Response;
7+
use Illuminate\Support\Facades\Auth;
8+
9+
class CheckPermission
10+
{
11+
/**
12+
* Handle an incoming request.
13+
*
14+
* @param \Illuminate\Http\Request $request
15+
* @param \Closure $next
16+
* @return mixed
17+
*/
18+
public function handle($request, Closure $next, $permission)
19+
{
20+
if (!$request->user()){
21+
return redirect('login');
22+
}
23+
$permissions = explode('|', $permission);
24+
$roles = explode(',',Auth::user()->type);
25+
$check = false;
26+
27+
foreach ($permissions as $key => $value) {
28+
if (in_array($value, $roles)){
29+
$check = true;
30+
}
31+
}
32+
33+
if($check){
34+
return $next($request);
35+
}else{
36+
return new Response(view('usertype::unauthorized')->with('role', $roles[0]));
37+
}
38+
}
39+
}

src/UsertypeProvider.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Mohit\Usertype;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class UsertypeProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap the application services.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
$router = $this->app['router'];
17+
$this->app['router']->aliasMiddleware('authorize', 'mohit\usertype\Middlewares\CheckPermission');
18+
$this->loadMigrationsFrom(__DIR__.'/migrations');
19+
20+
$this->loadViewsFrom(__DIR__.'/views', 'usertype');
21+
$this->publishes([
22+
__DIR__.'/views' => base_path('resources/views/mohit/usertype'),
23+
]);
24+
25+
if ($this->app->runningInConsole()) {
26+
$this->commands([
27+
Commands\CreateAdmin::class,
28+
Commands\CreateSuperAdmin::class
29+
]);
30+
}
31+
}
32+
33+
/**
34+
* Register the application services.
35+
*
36+
* @return void
37+
*/
38+
public function register()
39+
{
40+
//$this->app->make('Hashcrypt\Dbexception\DbexceptionController');
41+
}
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddTypeToUsers extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('users', function($table) {
17+
$table->string('type')->default('user');
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('users', function($table) {
29+
$table->dropColumn('type');
30+
});
31+
}
32+
}

src/views/unauthorized.blade.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<!DOCTYPE html>
2+
<html lang="{{ app()->getLocale() }}">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
8+
<meta name="csrf-token" content="{{ csrf_token() }}">
9+
10+
<title>{{ config('app.name', 'Unauthorized') }}</title>
11+
12+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
13+
<link rel="stylesheet" href="{{ URL::to('css/unauthorized.css') }}">
14+
</head>
15+
<body>
16+
<div id="app" class="unauthorized-page">
17+
<nav class="navbar navbar-default navbar-static-top">
18+
<div class="container">
19+
<div class="navbar-header">
20+
21+
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse" aria-expanded="false">
22+
<span class="sr-only">Toggle Navigation</span>
23+
<span class="icon-bar"></span>
24+
<span class="icon-bar"></span>
25+
<span class="icon-bar"></span>
26+
</button>
27+
28+
<a class="navbar-brand" href="{{ url('/') }}">
29+
{{ config('app.name', 'Laravel') }}
30+
</a>
31+
</div>
32+
33+
<div class="collapse navbar-collapse" id="app-navbar-collapse">
34+
<ul class="nav navbar-nav">&nbsp;</ul>
35+
<ul class="nav navbar-nav navbar-right">
36+
<li class="dropdown">
37+
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true" v-pre>
38+
{{ Auth::user()->name }} <span class="caret"></span>
39+
</a>
40+
<ul class="dropdown-menu">
41+
<li>
42+
<a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
43+
Logout
44+
</a>
45+
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
46+
{{ csrf_field() }}
47+
</form>
48+
</li>
49+
</ul>
50+
</li>
51+
</ul>
52+
</div>
53+
</div>
54+
</nav>
55+
<div class="container">
56+
<div class="row">
57+
<div class="col-md-10 col-md-offset-1">
58+
<div class="panel panel-default">
59+
<div class="panel-heading">Error</div>
60+
<div class="panel-body">
61+
<strong>{{$role}}</strong> cannot access this page!
62+
</div>
63+
</div>
64+
</div>
65+
</div>
66+
</div>
67+
</div>
68+
69+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
70+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
71+
</body>
72+
</html>
73+
74+
75+
76+
77+
78+

0 commit comments

Comments
 (0)