Created
September 30, 2015 17:23
-
-
Save mockiemockiz/cee24023c1d4bdbc442e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mockie | |
* Date: 13/05/15 | |
* Time: 4:20 | |
*/ | |
namespace Port\AdminControlPanel\Controller; | |
use Barryvdh\Debugbar\Controllers\BaseController; | |
use Chiara\AdminControlPanel\Repository\Interfaces\AcpRoleRepository; | |
use Chiara\AdminControlPanel\Repository\Interfaces\AcpUserRoleRepositoryInterface; | |
use Chiara\AdminControlPanel\Repository\RoleTree; | |
use Chiara\AdminControlPanel\Validators\Interfaces\AcpUserRoleValidatorInterface; | |
use Illuminate\Auth\AuthManager; | |
use Illuminate\Routing\Redirector; | |
use Illuminate\Support\Facades\Input; | |
use Illuminate\Support\Facades\Redirect; | |
use Illuminate\Support\Facades\View; | |
use Port\AdminControlPanel\Helpers\RoleHtmlOptionTree; | |
use Port\AdminControlPanel\Helpers\RoleHtmlUlTree; | |
class UserRoleController extends BaseController { | |
private $acpUserRoleRepository; | |
private $acpRoleRepository; | |
protected $authManager; | |
protected $acpUserRoleValidator; | |
protected $redirect; | |
protected $roleHtmlUlTree; | |
protected $roleTree; | |
public function __construct( | |
AcpUserRoleRepositoryInterface $acpUserRoleRepository, | |
AcpRoleRepository $acpRoleRepository, | |
AcpUserRoleValidatorInterface $acpUserRoleValidator, | |
Redirector $redirector, | |
AuthManager $authManager, | |
RoleHtmlOptionTree $roleHtmlOptionTree, | |
RoleHtmlUlTree $roleHtmlUlTree, | |
RoleTree $roleTree | |
) | |
{ | |
$this->acpUserRoleRepository = $acpUserRoleRepository; | |
$this->acpRoleRepository = $acpRoleRepository; | |
$this->authManager = $authManager; | |
$this->redirect = $redirector; | |
$this->roleHtmlOptionTree = $roleHtmlOptionTree; | |
$this->acpUserRoleValidator = $acpUserRoleValidator; | |
$this->roleHtmlUlTree = $roleHtmlUlTree; | |
$this->roleTree = $roleTree; | |
$this->acpUserRoleValidator->setValidatorFactory(); | |
} | |
protected function getRoleList($checked = []) | |
{ | |
$userId = $this->authManager->id(); | |
$roles = $this->acpUserRoleRepository->getRolesByUserId($userId, ['role_id'], []); | |
$roleIds = array_column($roles, 'role_id'); | |
$roles = array_merge($roleIds, $this->acpRoleRepository->findTheChildren($roleIds)); | |
$this->roleTree->setEnabledRoles($roles); | |
$this->roleTree->setCheckedRoles($checked); | |
return $this->roleHtmlUlTree->build($this->roleTree->getRoleTree()); | |
} | |
protected function getOption($selectedOption = 0) | |
{ | |
$this->roleTree->setEnabledAll(); | |
$this->roleTree->setSelected($selectedOption); | |
$selectOption = '<option value="0">-- select role --</option>' | |
. $this->roleHtmlOptionTree->build($this->roleTree->getRoleTree()); | |
return $selectOption; | |
} | |
public function index() | |
{ | |
$page = Input::get('page', 1); | |
$search = Input::get('search', []); | |
$searchRole = isset($search['role']) ? $search['role'] : []; | |
$data = [ | |
'users' => $this->acpUserRoleRepository->pagination($search, $page), | |
'roleTree' => $this->getOption($searchRole), | |
]; | |
return View::make('acp.user-roles.index', $data); | |
} | |
/** | |
* Page for adding user role | |
* | |
* @return mixed | |
*/ | |
public function create() | |
{ | |
$data = [ | |
'roleTree' => $this->getRoleList(), | |
]; | |
return View::make('acp.user-roles.create', $data); | |
} | |
/** | |
* Store the new user role | |
*/ | |
public function store() | |
{ | |
$data = Input::except('_method', '_token'); | |
try { | |
$this->acpUserRoleValidator->with($data)->passes(); | |
} catch (\Exception $e) { | |
return Redirect::back()->withErrors($this->acpUserRoleValidator->errors())->withInput(); | |
} | |
$this->acpUserRoleValidator->isUserNonExistedOrFail($data['user_id']); | |
$this->acpUserRoleRepository->create($data); | |
return Redirect::route('v2.acp.user.roles'); | |
} | |
/** | |
* Page for editing user role | |
* | |
* @param $userId | |
* @return mixed | |
*/ | |
public function edit($userId) | |
{ | |
$userRole = $this->acpUserRoleRepository->getRolesByUserId($userId, ['role_id'], []); | |
// if user role doesn't exist, redirect to "create" page | |
if (!$userRole) { | |
return $this->redirect->route('v2.acp.user.roles.create'); | |
} | |
$this->acpUserRoleValidator->userRolesDiffAndFail($userId, $this->authManager->id()); | |
$data = [ | |
'userRole' => $userRole[0], | |
'roleTree' => $this->getRoleList(array_column($userRole, 'role_id')), | |
]; | |
return View::make('acp.user-roles.edit', $data); | |
} | |
/** | |
* update user role | |
* | |
* @param $userId | |
* @return mixed | |
*/ | |
public function update($userId) | |
{ | |
$data = Input::except('_method', '_token'); | |
try { | |
$this->acpUserRoleValidator->with($data)->passes(); | |
} catch (\Exception $e) { | |
return Redirect::back()->withErrors($this->acpUserRoleValidator->errors())->withInput(); | |
} | |
$this->acpUserRoleRepository->update($data['role_id'], $userId); | |
return Redirect::route('v2.acp.user.roles.edit', ['userId' => $userId]); | |
} | |
/** | |
* Remove a user and his entire roles. | |
* | |
* @return mixed | |
*/ | |
public function destroy() | |
{ | |
$userId = (int) Input::get('user_id', 0); | |
$this->acpUserRoleRepository->delete($userId, 'user_id'); | |
return Redirect::route('v2.acp.user.roles'); | |
} | |
/** | |
* Remove a role of user's roles. | |
* | |
* @return mixed | |
*/ | |
public function destroyRole() | |
{ | |
$userId = (int) Input::get('user_id', 0); | |
$roleId = (int) Input::get('role_id', 0); | |
$this->acpUserRoleRepository->deleteRole($userId, [$roleId]); | |
return Redirect::route('v2.acp.user.roles'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment