Skip to content

Commit 45a8d4c

Browse files
author
root
committed
Added Dingo Api, Plus Implement a Working Complete Auth Api, With Roles and Abilities Middleware, and Docs
1 parent a214368 commit 45a8d4c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+3218
-660
lines changed

.env.docker

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
APP_ENV=local
2+
APP_NAME="Laravel Dev"
3+
APP_KEY=base64:ExExqfImrGk+wFJg/IrePYV0yrES5f84ojB7dBvvczE=
4+
APP_DEBUG=true
5+
APP_LOG_LEVEL=debug
6+
APP_URL=http://laravel.dev
7+
APP_DOMAIN=laravel.dev
8+
9+
API_DOMAIN=api.laravel.dev
10+
API_DEBUG=true
11+
API_STANDARDS_TREE=vnd
12+
API_SUBTYPE=apisubtype
13+
API_VERSION=v1
14+
API_NAME="Laravel Dev Api"
15+
SIGN_UP_RELEASE_TOKEN=true
16+
PASSWORD_RESET_RELEASE_TOKEN=true
17+
JWT_SECRET=aYLINRYJugcmNYoZ8xDVJUh2EcibPduu
18+
19+
DB_CONNECTION=mysql
20+
DB_HOST=mysql
21+
DB_PORT=3306
22+
DB_DATABASE=api
23+
DB_USERNAME=root
24+
DB_PASSWORD=root
25+
26+
REDIS_HOST=redis
27+
REDIS_PASSWORD=null
28+
REDIS_PORT=6379
29+
30+
BROADCAST_DRIVER=redis
31+
CACHE_DRIVER=file
32+
33+
SESSION_DRIVER=file
34+
SESSION_DOMAIN=.laravel.dev
35+
36+
QUEUE_HOST=beanstalkd
37+
QUEUE_DRIVER=beanstalkd
38+
39+
MAIL_DRIVER=smtp
40+
MAIL_HOST=smtp.mailtrap.io
41+
MAIL_PORT=2525
42+
MAIL_USERNAME=ccbcdcb6f224ce
43+
MAIL_PASSWORD=7ad210d0196bb5
44+

.vscode/settings.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Place your settings in this file to overwrite the default settings
2+
{
3+
"terminal.integrated.fontFamily": "DejaVuSansMonoForPowerline NF",
4+
"terminal.integrated.fontSize": 16,
5+
"window.zoomLevel": 1,
6+
"editor.fontFamily": "DejaVuSansMonoForPowerline NF",
7+
"editor.fontSize": 19,
8+
"editor.fontLigatures": true,
9+
// Fix the bug on Blade Not Highlighting pair html tag
10+
"files.associations": {
11+
"*.vue": "vue",
12+
"*.blade.php": "html"
13+
},
14+
"emmet.syntaxProfiles": {
15+
// I disable Blade Since im Using Vue Template
16+
"blade": "html",
17+
"vue-html": "html"
18+
},
19+
// Add the Following In Your Packages.json to use this
20+
// "babel-eslint"
21+
// "eslint":
22+
// "eslint-config-standard"
23+
// "eslint-plugin-html"
24+
25+
"eslint.validate": [ "javascript", "javascriptreact", { "language": "html", "autoFix": true } ],
26+
"workbench.iconTheme": null,
27+
// Auto rename HTML tags
28+
"auto-rename-tag.activationOnLanguage": [
29+
"html",
30+
"xml",
31+
"php",
32+
"javascript",
33+
"blade"
34+
],
35+
36+
37+
38+
// Exclude Vendor Folders During Search and Replace
39+
"files.watcherExclude": {
40+
"**/vendor/**": true
41+
},
42+
"search.exclude": {
43+
"**/vendor/**": true
44+
},
45+
"stylelint.enable": true,
46+
"css.validate": false,
47+
"eslint.autoFixOnSave": true
48+
49+
}

Docs/ACL.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
2+
## ACL
3+
4+
#### Uses JWT as Default Driver for Auth User
5+
```
6+
App\Http\Controllers\Auth
7+
8+
- LoginController
9+
10+
```
11+
12+
Note: Auth User is Using JWT by Default... users auth:api Middleware
13+
14+
15+
#### Roles Abilities Middlewares
16+
Add Roles to User
17+
```
18+
Bouncer::assign('admin')->to($user);
19+
or
20+
$user->assign('admin')
21+
```
22+
23+
Add Abilities to User
24+
```
25+
\Bouncer::allow($user)->to('view-dashboard');
26+
or
27+
$user->allow('view-dashboard');
28+
or
29+
\Bouncer::allow('owner')->to('*', $post);
30+
```
31+
32+
Abilities and Roles Middleware (Default is Non Strict)
33+
34+
```
35+
$api = app('Dingo\Api\Routing\Router');
36+
$api->version('v1', function ($api) {
37+
$api->group(['middleware' => ['roles'],
38+
'roles' => ['admin','manager'],
39+
'rolesStrict' => true,
40+
'prefix' => 'admin'
41+
]
42+
, function ($api) {
43+
$api->get('/', function(){
44+
return 'Im a user with Admin and Manager Role';});
45+
});
46+
$api->group(['middleware' => ['jwt.auth', 'abilities'],
47+
'abilities' => ['view-dashboard', 'login'],
48+
'abilitiesStrict' => true,
49+
'prefix' => 'abilities'
50+
]
51+
, function ($api) {
52+
$api->get('/', function(){
53+
return 'I can view Dashboard and Login!';});
54+
});
55+
});
56+
```
57+
58+
#### Using Authorize Middleware
59+
Can be used with a simple ability:
60+
```
61+
Route::group(['middleware' => 'can:access-dashboard'], function () {
62+
// Dashboard routes...
63+
});
64+
```
65+
66+
Can be used on a single model
67+
It will pull the $user model from the route.
68+
```
69+
Route::get('users/{user}', 'UserController@show')->middleware('can:view,user');
70+
```
71+
it can be used on a model type
72+
```
73+
Route::get('users', 'UserController@index')->middleware('can:view,'.User::class);
74+
```
75+
A new AuthorizesResources trait
76+
```
77+
class UsersController extends Controller
78+
{
79+
public function __construct()
80+
{
81+
$this->authorizeResource('user', User::class);
82+
}
83+
}
84+
```
85+
#### Retracting Roles From Set of Users
86+
87+
```
88+
Bouncer::retract('admin')->from(User::pluck('id')->all());
89+
Or much Efficient Way
90+
Bouncer::retract('admin')->from(User::whereIs('admin')->pluck('id')->all());
91+
```
92+
93+
94+
#### Retracting All Roles From a User
95+
96+
```
97+
$user->roles->each(function ($role) {
98+
$user->retract($role);
99+
});
100+
```
101+
102+
#### WildCards
103+
```
104+
// This Will Give You All Abililities No restriction in your Account
105+
Bouncer::allow($user)->everything();
106+
// This Will Allow You to Manage Specific Post
107+
Bouncer::allow($user)->toManage($post);
108+
// Allows You to Manage the Whole Post Class
109+
Bouncer::allow($user)->toManage(Post::class);
110+
// Allows User to Give Specific Abilities to Any Models
111+
Bouncer::allow($user)->toAlways('view');
112+
```
113+
114+
#### Assigning Specific Model to User
115+
```
116+
// All Permissions
117+
Bouncer::allow($user)->toOwn(User::class);
118+
// With Specific Permission
119+
\Bouncer::allow($user)->toOwn(User::class,['view','update']);
120+
```

Docs/API.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
### Using Dingo Api
2+
3+
#### using api.domain.com
4+
if you are using this. you need to uncomment at VerifyCsrfToken.php
5+
6+
Meaning you set you API_DOMAIN=api.domain.dev at your .env
7+
```
8+
protected $except = [
9+
// 'api/*'
10+
];
11+
```
12+
13+
#### using prefix /api
14+
if you are using this you need this at VerifyCsrfToken.php
15+
16+
```
17+
protected $except = [
18+
// 'api/*'
19+
];
20+
```
21+
22+
#### Using Transformers
23+
24+
#### Configuring Config/api.php
25+
26+
27+
#### Using Postman
28+
- Open [Postman](https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en)
29+
30+
- add to header ,where API_SUBTYPE is define in our .env (Use for Accessing Specific Api Version)
31+
32+
```
33+
Accept: application/vnd.YOUR_SUBTYPE.v1+json
34+
```
35+
36+
37+
- Make Post Request to login
38+
39+
```
40+
http://api.laravel.dev/auth/login
41+
or
42+
http://laravel.dev/auth/login
43+
```
44+
45+
- Add to Body
46+
```
47+
email = admin@laravel.dev
48+
password = password
49+
```
50+
51+
- You will see Response something like this
52+
```
53+
{
54+
"status": "ok",
55+
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cL2FwaS5sYXJhdmVsLmRldlwvYXV0aFwvbG9naW4iLCJpYXQiOjE0ODkzMzI1OTcsImV4cCI6MTQ4OTM5NzM5NywibmJmIjoxNDg5MzMyNTk3LCJqdGkiOiJjODMxNTMzZjkzMGFiOTkzMGExMzhkMGNkOTI5NGI3ZCJ9.3v-cGtXA-ySmL67pp4kZ4U4Mf3v7ge_CzUEdWIRKSeM"
56+
}
57+
```
58+
If .env
59+
```
60+
API_STRICT = true
61+
```
62+
Header
63+
64+
Key:
65+
Accept
66+
67+
Value:
68+
application/x.apisubtype.v1+json
69+

0 commit comments

Comments
 (0)