Skip to content

Commit

Permalink
Merge branch 'release/v0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
themodernpk committed Mar 25, 2020
2 parents bb079c3 + d6f937b commit 362e9f1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 37 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["laravel", "cms"],
"homepage": "https://www.webreinvent.com",
"license": "MIT",
"version": "0.3.9",
"version": "0.4.0",
"authors": [
{
"name": "WebReinvent",
Expand Down
52 changes: 44 additions & 8 deletions src/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,18 @@ public function isAdmin()
public function hasPermission($permission_slug)
{


if ($this->isAdmin()) {

$response['status'] = 'success';
if(env('APP_DEBUG'))
{
$response['data']['permission'] = 'Permission slug: '.$permission_slug;
$response['hint'][] = 'Admin has all permission by default.';
}
return $response;
}

//check if permission exist or not
$permission = Permission::where('slug', $permission_slug)
->first();
Expand All @@ -899,26 +911,50 @@ public function hasPermission($permission_slug)
{
$response['status'] = 'failed';
$response['errors'][] = 'No Permission exist with slug: '.$permission_slug;
return response()->json($response);
}

if ($this->isAdmin()) {
return true;
if(env('APP_DEBUG'))
{
$response['hint'][] = 'Check the migrations & seeds are properly run.';
}

return $response;
}

if ($permission->is_active != 1) {
return false;
$response['status'] = 'failed';
$response['errors'][] = $permission_slug.' is inactive';
if(env('APP_DEBUG'))
{
$response['hint'][] = 'Enable the permission status to active from backend/admin control panel.';
}
return $response;
}

foreach ($this->permissions() as $permission)
{
if ($permission['slug'] == $permission_slug && $permission['is_active'] == 1)
if ($permission['slug'] == $permission_slug
&& $permission['is_active'] == 1
&& $permission['pivot']['is_active'] == 1
)
{
return true;
$response['status'] = 'success';
if(env('APP_DEBUG'))
{
$response['hint'][] = 'Permission slug: '.$permission_slug.' is active for '.\Auth::user()->email;
}
return $response;
break;
}
}

return false;
$response['status'] = 'failed';
$response['errors'][] = trans("vaahcms::messages.permission_denied");
if(env('APP_DEBUG'))
{
$response['hint'][] = 'Permission slug: '.$permission_slug.' is not active for '.\Auth::user()->email;
}
return $response;

}

//-------------------------------------------------
Expand Down
60 changes: 32 additions & 28 deletions src/Http/Middleware/HasAdminAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,38 @@ class HasAdminAccess
public function handle(Request $request, Closure $next)
{

//check user is logged in
if (Auth::guest())
{
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {

$url = url()->full();

session(['accessed_url' => $url]);

return redirect()->guest(route('vh.admin.login'))
->withErrors([trans("vaahcms::messages.login_required")]);
}
}

if(Auth::user()->is_active != 1)
{
return redirect()->guest(route('vh.admin.login'))
->withErrors([trans("vaahcms::messages.inactive_account")]);
}

//check user have permission to back login
if(!Auth::user()->hasPermission("vaahcms#admin#access"))
{
return redirect()->guest(route('vh.admin.login'))
->withErrors([trans("vaahcms::messages.permission_denied")]);
}
//check user is logged in
if (Auth::guest())
{
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {

$url = url()->full();

session(['accessed_url' => $url]);

return \Redirect::back()
->withErrors([trans("vaahcms::messages.login_required")]);
}
}

if(Auth::user()->is_active != 1)
{
return \Redirect::back()
->withErrors([trans("vaahcms::messages.inactive_account")]);
}


$check_permission = Auth::user()->hasPermission("can-access-admin-section");

//check user have permission to back login
if($check_permission['status'] == 'failed')
{

return \Redirect::back()
->withErrors($check_permission['errors']);
}


return $next($request);
Expand Down

0 comments on commit 362e9f1

Please sign in to comment.