Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create role permissions #68

Merged
merged 6 commits into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions app/Http/Controllers/Admin/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ class LoginController extends Controller
*/
public function showLoginForm()
{
if (auth()->guard('admin')->check()) {
if (auth()->guard('employee')->check()) {
return redirect()->route('admin.dashboard');
}

return view('auth.admin.login');
}

/**
* Login the admin
* Login the employee
*
* @param LoginRequest $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
* @throws \Illuminate\Validation\ValidationException
*/
public function login(LoginRequest $request)
{
Expand All @@ -53,7 +55,7 @@ public function login(LoginRequest $request)

$details = $request->only('email', 'password');
$details['status'] = 1;
if (auth()->guard('admin')->attempt($details)) {
if (auth()->guard('employee')->attempt($details)) {
return $this->sendLoginResponse($request);
}

Expand Down
36 changes: 36 additions & 0 deletions app/Http/Controllers/Admin/Permissions/PermissionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Http\Controllers\Admin\Permissions;

use App\Http\Controllers\Controller;
use App\Shop\Permissions\Repositories\PermissionRepository;

class PermissionController extends Controller
{
/**
* @var PermissionRepository
*/
private $permRepo;

/**
* PermissionController constructor.
*
* @param PermissionRepository $permissionRepository
*/
public function __construct(PermissionRepository $permissionRepository)
{
$this->permRepo = $permissionRepository;
}

/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
$list = $this->permRepo->listPermissions();

$permissions = $this->permRepo->paginateArrayResults($list->all());

return view('admin.permissions.list', compact('permissions'));
}
}
9 changes: 8 additions & 1 deletion app/Http/Controllers/Admin/Products/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;

class ProductController extends Controller
Expand Down Expand Up @@ -50,6 +49,9 @@ class ProductController extends Controller
*/
private $productAttribute;

/**
* @var BrandRepositoryInterface
*/
private $brandRepo;

/**
Expand All @@ -76,6 +78,11 @@ public function __construct(
$this->attributeValueRepository = $attributeValueRepository;
$this->productAttribute = $productAttribute;
$this->brandRepo = $brandRepository;

$this->middleware(['permission:create-product, guard:employee'], ['only' => ['create', 'store']]);
$this->middleware(['permission:update-product, guard:employee'], ['only' => ['edit', 'update']]);
$this->middleware(['permission:delete-product, guard:employee'], ['only' => ['destroy']]);
$this->middleware(['permission:view-product, guard:employee'], ['only' => [ 'index', 'show']]);
}

/**
Expand Down
113 changes: 113 additions & 0 deletions app/Http/Controllers/Admin/Roles/RoleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace App\Http\Controllers\Admin\Roles;

use App\Http\Controllers\Controller;
use App\Shop\Permissions\Repositories\Interfaces\PermissionRepositoryInterface;
use App\Shop\Roles\Repositories\RoleRepository;
use App\Shop\Roles\Repositories\RoleRepositoryInterface;
use App\Shop\Roles\Requests\CreateRoleRequest;
use App\Shop\Roles\Requests\UpdateRoleRequest;

class RoleController extends Controller
{
/**
* @var RoleRepositoryInterface
*/
private $roleRepo;

/**
* @var PermissionRepositoryInterface
*/
private $permissionRepository;

/**
* RoleController constructor.
*
* @param RoleRepositoryInterface $roleRepository
* @param PermissionRepositoryInterface $permissionRepository
*/
public function __construct(
RoleRepositoryInterface $roleRepository,
PermissionRepositoryInterface $permissionRepository
)
{
$this->roleRepo = $roleRepository;
$this->permissionRepository = $permissionRepository;
}

/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
$list = $this->roleRepo->listRoles('name', 'asc')->all();

$roles = $this->roleRepo->paginateArrayResults($list);

return view('admin.roles.list', compact('roles'));

}

/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function create()
{
return view('admin.roles.create');
}

/**
* @param CreateRoleRequest $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(CreateRoleRequest $request)
{
$this->roleRepo->createRole($request->except('_method', '_token'));

return redirect()->route('admin.roles.index')
->with('message', 'Create role successful!');
}

/**
* @param $id
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit($id)
{
$role = $this->roleRepo->findRoleById($id);

$roleRepo = new RoleRepository($role);
$attachedPermissionsArrayIds = $roleRepo->listPermissions()->pluck('id')->all();
$permissions = $this->permissionRepository->listPermissions(['*'], 'name', 'asc');

return view('admin.roles.edit', compact(
'role',
'permissions',
'attachedPermissionsArrayIds'
));
}

/**
* @param UpdateRoleRequest $request
* @param $id
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function update(UpdateRoleRequest $request, $id)
{
$role = $this->roleRepo->findRoleById($id);

if ($request->has('roles')) {
$roleRepo = new RoleRepository($role);
$roleRepo->syncPermissions($request->input('roles'));
}

$this->roleRepo->updateRole($request->except('_method', '_token'), $id);

return redirect()->route('admin.roles.edit', $id)
->with('message', 'Update role successful!');
}
}
4 changes: 2 additions & 2 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Kernel extends HttpKernel
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
'checkout' => \App\Http\Middleware\RedirectIfNotAdmin::class,
'employee' => \App\Http\Middleware\RedirectIfNotEmployee::class,
'checkout' => \App\Http\Middleware\RedirectIfNotCustomer::class,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfNotAdmin
class RedirectIfNotCustomer
{
/**
* Handle an incoming request.
Expand All @@ -15,7 +14,7 @@ class RedirectIfNotAdmin
* @param string $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = 'admin')
public function handle($request, Closure $next, $guard = 'checkout')
{
if (!auth()->guard($guard)->check()) {
$request->session()->flash('error', 'You must be an employee to see this page');
Expand Down
26 changes: 26 additions & 0 deletions app/Http/Middleware/RedirectIfNotEmployee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Middleware;

use Closure;

class RedirectIfNotEmployee
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = 'employee')
{
if (!auth()->guard($guard)->check()) {
$request->session()->flash('error', 'You must be an employee to see this page');
return redirect(route('admin.login'));
}

return $next($request);
}
}
9 changes: 6 additions & 3 deletions app/Providers/GlobalTemplateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ class GlobalTemplateServiceProvider extends ServiceProvider
*/
public function boot()
{
view()->composer('layouts.admin.app', function ($view) {
$view->with('user', Auth::guard('admin')->user());
$view->with('admin', $this->isAdmin(Auth::guard('admin')->user()));
view()->composer([
'layouts.admin.app',
'layouts.admin.sidebar',
'admin.shared.products'
], function ($view) {
$view->with('admin', Auth::guard('employee')->user());
});

view()->composer(['layouts.front.app', 'front.categories.sidebar-category'], function ($view) {
Expand Down
9 changes: 9 additions & 0 deletions app/Providers/RepositoryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
use App\Shop\Orders\Repositories\OrderRepository;
use App\Shop\OrderStatuses\Repositories\Interfaces\OrderStatusRepositoryInterface;
use App\Shop\OrderStatuses\Repositories\OrderStatusRepository;
use App\Shop\Permissions\Repositories\PermissionRepository;
use App\Shop\Permissions\Repositories\Interfaces\PermissionRepositoryInterface;
use App\Shop\PaymentMethods\Repositories\PaymentMethodRepository;
use App\Shop\PaymentMethods\Repositories\Interfaces\PaymentMethodRepositoryInterface;
use App\Shop\ProductAttributes\Repositories\ProductAttributeRepository;
use App\Shop\ProductAttributes\Repositories\ProductAttributeRepositoryInterface;
use App\Shop\Products\Repositories\Interfaces\ProductRepositoryInterface;
Expand Down Expand Up @@ -126,5 +130,10 @@ public function register()
RoleRepositoryInterface::class,
RoleRepository::class
);

$this->app->bind(
PermissionRepositoryInterface::class,
PermissionRepository::class
);
}
}
9 changes: 8 additions & 1 deletion app/Shop/Employees/Repositories/EmployeeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class EmployeeRepository extends BaseRepository implements EmployeeRepositoryInt
{
/**
* EmployeeRepository constructor.
*
* @param Employee $employee
*/
public function __construct(Employee $employee)
Expand All @@ -28,6 +29,7 @@ public function __construct(Employee $employee)
*
* @param string $order
* @param string $sort
*
* @return array
*/
public function listEmployees(string $order = 'id', string $sort = 'desc'): Collection
Expand All @@ -39,6 +41,7 @@ public function listEmployees(string $order = 'id', string $sort = 'desc'): Coll
* Create the employee
*
* @param array $data
*
* @return Employee
*/
public function createEmployee(array $data): Employee
Expand All @@ -51,6 +54,7 @@ public function createEmployee(array $data): Employee
* Find the employee by id
*
* @param int $id
*
* @return Employee
*/
public function findEmployeeById(int $id): Employee
Expand All @@ -66,6 +70,7 @@ public function findEmployeeById(int $id): Employee
* Update employee
*
* @param array $params
*
* @return bool
*/
public function updateEmployee(array $params): bool
Expand All @@ -91,6 +96,7 @@ public function listRoles(): Collection

/**
* @param string $roleName
*
* @return bool
*/
public function hasRole(string $roleName): bool
Expand All @@ -100,12 +106,13 @@ public function hasRole(string $roleName): bool

/**
* @param Employee $employee
*
* @return bool
*/
public function isAuthUser(Employee $employee): bool
{
$isAuthUser = false;
if (Auth::guard('admin')->user()->id == $employee->id) {
if (Auth::guard('employee')->user()->id == $employee->id) {
$isAuthUser = true;
}
return $isAuthUser;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Shop\Permissions\Exceptions;

class CreatePermissionErrorException extends \Exception
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Shop\Permissions\Exceptions;

class DeletePermissionErrorException extends \Exception
{

}
Loading